Skip to content

Commit

Permalink
Allow bounding box updates via VehicleProcessModel
Browse files Browse the repository at this point in the history
Merged-by: Stefan Walter <[email protected]>
  • Loading branch information
martingr authored and swltr committed Aug 22, 2024
1 parent e8476ed commit 6f9e8d2
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public VehicleProcessModelTO createTransferableProcessModel() {
.setPrecisePosition(getProcessModel().getPrecisePosition())
.setPosition(getProcessModel().getPosition())
.setState(getProcessModel().getState())
.setLength(getProcessModel().getLength());
.setBoundingBox(getProcessModel().getBoundingBox());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import java.util.Objects;
import java.util.Queue;
import org.opentcs.data.TCSObjectReference;
import org.opentcs.data.model.BoundingBox;
import org.opentcs.data.model.Triple;
import org.opentcs.data.model.Vehicle;
import org.opentcs.data.notification.UserNotification;
import org.opentcs.util.annotations.ScheduledApiChange;

/**
* An observable model of a vehicle's and its comm adapter's attributes.
Expand Down Expand Up @@ -94,9 +96,9 @@ public class VehicleProcessModel {
*/
private Vehicle.State state = Vehicle.State.UNKNOWN;
/**
* The vehicle's current length.
* The vehicle's current bounding box.
*/
private int length;
private BoundingBox boundingBox;

/**
* Creates a new instance.
Expand All @@ -109,7 +111,7 @@ public VehicleProcessModel(
) {
this.vehicle = requireNonNull(attachedVehicle, "attachedVehicle");
this.vehicleReference = vehicle.getReference();
this.length = (int) vehicle.getBoundingBox().getLength();
this.boundingBox = vehicle.getBoundingBox();
}

/**
Expand Down Expand Up @@ -471,24 +473,59 @@ else if (oldState == Vehicle.State.ERROR && newState != Vehicle.State.ERROR) {
* Returns the vehicle's current length.
*
* @return The vehicle's current length.
* @deprecated Use {@link #getBoundingBox()} instead.
*/
@Deprecated
@ScheduledApiChange(when = "7.0", details = "Will be removed.")
public int getLength() {
return length;
return (int) boundingBox.getLength();
}

/**
* Sets the vehicle's current length.
*
* @param length The new length.
* @deprecated Use {@link #setBoundingBox(BoundingBox)} instead.
*/
@Deprecated
@ScheduledApiChange(when = "7.0", details = "Will be removed.")
public void setLength(int length) {
int oldValue = this.length;
this.length = length;
setBoundingBox(getBoundingBox().withLength(length));
}

/**
* Returns the vehicle's current bounding box.
*
* @return The vehicle's current bounding box.
*/
@Nonnull
public BoundingBox getBoundingBox() {
return boundingBox;
}

/**
* Sets the vehicle's current bounding box.
*
* @param boundingBox The new bounding box.
*/
public void setBoundingBox(
@Nonnull
BoundingBox boundingBox
) {
requireNonNull(boundingBox, "boundingBox");

BoundingBox oldValue = this.boundingBox;
this.boundingBox = boundingBox;

getPropertyChangeSupport().firePropertyChange(
Attribute.LENGTH.name(),
Attribute.BOUNDING_BOX.name(),
oldValue,
length
boundingBox
);
getPropertyChangeSupport().firePropertyChange(
Attribute.LENGTH.name(),
oldValue.getLength(),
boundingBox.getLength()
);
}

Expand Down Expand Up @@ -752,7 +789,13 @@ public enum Attribute {
/**
* Indicates a change of the vehicle's length.
*/
@Deprecated
@ScheduledApiChange(when = "7.0", details = "Will be removed.")
LENGTH,
/**
* Indicates a change of the vehicle's bounding box.
*/
BOUNDING_BOX,
/**
* Indicates a new user notification was published.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import org.opentcs.data.model.BoundingBox;
import org.opentcs.data.model.Triple;
import org.opentcs.data.model.Vehicle;
import org.opentcs.data.notification.UserNotification;
import org.opentcs.drivers.vehicle.LoadHandlingDevice;
import org.opentcs.drivers.vehicle.VehicleProcessModel;
import org.opentcs.util.annotations.ScheduledApiChange;

/**
* A serializable representation of a {@link VehicleProcessModel}.
Expand All @@ -43,7 +45,7 @@ public class VehicleProcessModelTO
private int energyLevel;
private List<LoadHandlingDevice> loadHandlingDevices = new ArrayList<>();
private Vehicle.State state = Vehicle.State.UNKNOWN;
private int length;
private BoundingBox boundingBox = new BoundingBox(1000, 1000, 1000);

/**
* Creates a new instance.
Expand Down Expand Up @@ -164,12 +166,28 @@ public VehicleProcessModelTO setState(
return this;
}

@Deprecated
@ScheduledApiChange(when = "7.0", details = "Will be removed.")
public int getLength() {
return length;
return (int) boundingBox.getLength();
}

@Deprecated
@ScheduledApiChange(when = "7.0", details = "Will be removed.")
public VehicleProcessModelTO setLength(int length) {
this.length = length;
setBoundingBox(getBoundingBox().withLength(length));
return this;
}

public BoundingBox getBoundingBox() {
return boundingBox;
}

public VehicleProcessModelTO setBoundingBox(
@Nonnull
BoundingBox boundingBox
) {
this.boundingBox = requireNonNull(boundingBox, "boundingBox");
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,15 @@ public void propertyChange(PropertyChangeEvent evt) {
if (!getProcessModel().getLoadHandlingDevices().isEmpty()
&& getProcessModel().getLoadHandlingDevices().get(0).isFull()) {
loadState = LoadState.FULL;
getProcessModel().setLength(configuration.vehicleLengthLoaded());
getProcessModel().setBoundingBox(
getProcessModel().getBoundingBox().withLength(configuration.vehicleLengthLoaded())
);
}
else {
loadState = LoadState.EMPTY;
getProcessModel().setLength(configuration.vehicleLengthUnloaded());
getProcessModel().setBoundingBox(
getProcessModel().getBoundingBox().withLength(configuration.vehicleLengthUnloaded())
);
}
}
if (Objects.equals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.opentcs.data.ObjectUnknownException;
import org.opentcs.data.TCSObjectEvent;
import org.opentcs.data.TCSObjectReference;
import org.opentcs.data.model.BoundingBox;
import org.opentcs.data.model.Location;
import org.opentcs.data.model.Path;
import org.opentcs.data.model.Point;
Expand Down Expand Up @@ -285,7 +286,7 @@ public void initialize() {
commAdapter.getProcessModel().getLoadHandlingDevices()
);
updateVehicleState(commAdapter.getProcessModel().getState());
updateVehicleLength(commAdapter.getProcessModel().getLength());
updateVehicleBoundingBox(commAdapter.getProcessModel().getBoundingBox());

claimedResources.clear();
allocatedResources.clear();
Expand Down Expand Up @@ -985,8 +986,11 @@ else if (Objects.equals(
else if (Objects.equals(evt.getPropertyName(), VehicleProcessModel.Attribute.STATE.name())) {
updateVehicleState((Vehicle.State) evt.getNewValue());
}
else if (Objects.equals(evt.getPropertyName(), VehicleProcessModel.Attribute.LENGTH.name())) {
updateVehicleLength((int) evt.getNewValue());
else if (Objects.equals(
evt.getPropertyName(),
VehicleProcessModel.Attribute.BOUNDING_BOX.name()
)) {
updateVehicleBoundingBox((BoundingBox) evt.getNewValue());
}
else if (Objects.equals(
evt.getPropertyName(),
Expand Down Expand Up @@ -1170,7 +1174,7 @@ private void commandExecuted(MovementCommand executedCommand) {
= ResourceMath.freeableResourceSetCount(
SplitResources.from(allocatedResources, Set.of(currentVehiclePosition))
.getResourcesPassed(),
commAdapter.getProcessModel().getLength()
commAdapter.getProcessModel().getBoundingBox().getLength()
);
for (int i = 0; i < freeableResourceSetCount; i++) {
Set<TCSResource<?>> oldResources = allocatedResources.poll();
Expand Down Expand Up @@ -1244,13 +1248,9 @@ private void updateVehicleState(Vehicle.State newState) {
vehicleService.updateVehicleState(vehicle.getReference(), newState);
}

private void updateVehicleLength(int newLength) {
vehicleService.updateVehicleBoundingBox(
vehicle.getReference(),
vehicleService.fetchObject(Vehicle.class, vehicle.getReference())
.getBoundingBox()
.withLength(newLength)
);
private void updateVehicleBoundingBox(BoundingBox newBoundingBox) {
requireNonNull(newBoundingBox, "newBoundingBox");
vehicleService.updateVehicleBoundingBox(vehicle.getReference(), newBoundingBox);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private ResourceMath() {
public static int freeableResourceSetCount(
@Nonnull
List<Set<TCSResource<?>>> resourcesPassed,
int vehicleLength
long vehicleLength
) {
requireNonNull(resourcesPassed, "resourcesPassed");
checkArgument(vehicleLength > 0, "vehicleLength <= 0");
Expand All @@ -52,7 +52,7 @@ public static int freeableResourceSetCount(
List<Set<TCSResource<?>>> reversedPassedResources = new ArrayList<>(resourcesPassed);
Collections.reverse(reversedPassedResources);

int remainingRequiredLength = vehicleLength;
long remainingRequiredLength = vehicleLength;
int result = 0;
for (Set<TCSResource<?>> curSet : reversedPassedResources) {
if (remainingRequiredLength > 0) {
Expand All @@ -66,11 +66,10 @@ public static int freeableResourceSetCount(
return result;
}

private static int requiredLength(Set<TCSResource<?>> resources) {
private static long requiredLength(Set<TCSResource<?>> resources) {
return resources.stream()
.filter(resource -> resource instanceof Path)
.mapToLong(resource -> ((Path) resource).getLength())
.mapToInt(length -> (int) length)
.sum();
}

Expand Down

0 comments on commit 6f9e8d2

Please sign in to comment.