Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion autonomy/lib/src/detector/network_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class NetworkDetector extends DetectorInterface {

@override
Future<bool> init() async {
collection.server.messages.onMessage(
collection.server.messages.listenFor(
name: AutonomyData().messageName,
constructor: AutonomyData.fromBuffer,
callback: _onDataReceived,
Expand Down
2 changes: 1 addition & 1 deletion autonomy/lib/src/detector/rover_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class RoverDetector extends DetectorInterface {

@override
Future<bool> init() async {
_subscription = collection.server.messages.onMessage(
_subscription = collection.server.messages.listenFor(
name: LidarPointCloud().messageName,
constructor: LidarPointCloud.fromBuffer,
callback: _handleLidarCloud,
Expand Down
4 changes: 2 additions & 2 deletions autonomy/lib/src/drive/drive_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ abstract class DriveInterface extends Service {
faceOrientationState(direction.orientation);

/// State to face the rover towards [orientation]
StateInterface faceOrientationState(Orientation orientation);
StateInterface faceOrientationState(Rotation3d orientation);

/// State to execute actions relating to the turning of an [AutonomyAStarState]
StateInterface turnStateState(AutonomyAStarState state) =>
Expand All @@ -83,7 +83,7 @@ abstract class DriveInterface extends Service {
Future<bool> driveForward(GpsCoordinates position);

/// Turn to face [orientation], returns whether or not it was able to turn
Future<bool> faceOrientation(Orientation orientation);
Future<bool> faceOrientation(Rotation3d orientation);

/// Turn to face the orientation of [direction], returns whether or not it was able to turn
Future<bool> faceDirection(CardinalDirection direction) =>
Expand Down
4 changes: 2 additions & 2 deletions autonomy/lib/src/drive/rover_drive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class RoverDrive extends DriveInterface {
Future<void> approachAruco() => sensorDrive.approachAruco();

@override
Future<bool> faceOrientation(Orientation orientation) async {
Future<bool> faceOrientation(Rotation3d orientation) async {
if (useImu) {
return sensorDrive.faceOrientation(orientation);
} else {
Expand Down Expand Up @@ -135,7 +135,7 @@ class RoverDrive extends DriveInterface {
}

@override
StateInterface faceOrientationState(Orientation orientation) {
StateInterface faceOrientationState(Rotation3d orientation) {
if (useImu) {
return sensorDrive.faceOrientationState(orientation);
} else {
Expand Down
6 changes: 3 additions & 3 deletions autonomy/lib/src/drive/sensor_drive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SensorDrive extends DriveInterface with RoverDriveCommands {
);

@override
StateInterface faceOrientationState(Orientation orientation) =>
StateInterface faceOrientationState(Rotation3d orientation) =>
SensorTurnState(
controller,
collection: collection,
Expand Down Expand Up @@ -115,15 +115,15 @@ class SensorDrive extends DriveInterface with RoverDriveCommands {
}

@override
Future<bool> faceOrientation(Orientation orientation) async {
Future<bool> faceOrientation(Rotation3d orientation) async {
collection.logger.info("Turning to face $orientation...");
setThrottle(config.turnThrottle);
final result = await runFeedback(() => _tryToFace(orientation));
await stop();
return result;
}

bool _tryToFace(Orientation orientation) {
bool _tryToFace(Rotation3d orientation) {
final current = collection.imu.heading;
final target = orientation.heading;
final error = (target - current).clampHalfAngle();
Expand Down
4 changes: 2 additions & 2 deletions autonomy/lib/src/drive/sim_drive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DriveSimulator extends DriveInterface {
);

@override
StateInterface faceOrientationState(Orientation orientation) =>
StateInterface faceOrientationState(Rotation3d orientation) =>
SimulationDriveTurn(
controller,
collection: collection,
Expand All @@ -68,7 +68,7 @@ class DriveSimulator extends DriveInterface {
}

@override
Future<bool> faceOrientation(Orientation orientation) async {
Future<bool> faceOrientation(Rotation3d orientation) async {
if (shouldDelay) {
await Future<void>.delayed(const Duration(milliseconds: 500));
}
Expand Down
4 changes: 2 additions & 2 deletions autonomy/lib/src/drive/timed_drive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class TimedDrive extends DriveInterface with RoverDriveCommands {
TimedDrive({required super.collection, super.config});

@override
StateInterface faceOrientationState(Orientation orientation) {
StateInterface faceOrientationState(Rotation3d orientation) {
throw UnsupportedError(
"Cannot face any arbitrary direction using TimedDrive",
);
Expand Down Expand Up @@ -206,7 +206,7 @@ class TimedDrive extends DriveInterface with RoverDriveCommands {
}

@override
Future<bool> faceOrientation(Orientation orientation) =>
Future<bool> faceOrientation(Rotation3d orientation) =>
throw UnsupportedError(
"Cannot face any arbitrary direction using TimedDrive",
);
Expand Down
2 changes: 1 addition & 1 deletion autonomy/lib/src/gps/rover_gps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class RoverGps extends GpsInterface {

@override
Future<bool> init() async {
collection.server.messages.onMessage(
collection.server.messages.listenFor(
name: RoverPosition().messageName,
constructor: RoverPosition.fromBuffer,
callback: _internalUpdate,
Expand Down
6 changes: 3 additions & 3 deletions autonomy/lib/src/imu/cardinal_direction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ enum CardinalDirection {
final double angle;
const CardinalDirection(this.angle);

Orientation get orientation => Orientation(z: angle);
Rotation3d get orientation => Rotation3d(yaw: angle);

static CardinalDirection nearest(Orientation orientation) {
static CardinalDirection nearest(Rotation3d orientation) {
var smallestDiff = double.infinity;
var closestOrientation = CardinalDirection.north;

for (final value in values) {
final diff = (value.angle - orientation.z).clampHalfAngle();
final diff = (value.angle - orientation.yaw).clampHalfAngle();
if (diff.abs() < smallestDiff) {
smallestDiff = diff.abs();
closestOrientation = value;
Expand Down
10 changes: 5 additions & 5 deletions autonomy/lib/src/imu/imu_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ abstract class ImuInterface extends Service with Receiver {
final AutonomyInterface collection;
ImuInterface({required this.collection});

double get heading => raw.z;
Orientation get raw;
double get heading => raw.yaw;
Rotation3d get raw;

CardinalDirection get nearest => CardinalDirection.nearest(raw);

void update(Orientation newValue);
void update(Rotation3d newValue);

@visibleForTesting
void forceUpdate(Orientation newValue) {}
void forceUpdate(Rotation3d newValue) {}

bool isNear(Orientation orientation, [double? tolerance]) =>
bool isNear(Rotation3d orientation, [double? tolerance]) =>
raw.isNear(orientation.heading, tolerance);

@override
Expand Down
26 changes: 13 additions & 13 deletions autonomy/lib/src/imu/rover_imu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RoverImu extends ImuInterface {

@override
Future<bool> init() async {
collection.server.messages.onMessage(
collection.server.messages.listenFor(
name: RoverPosition().messageName,
constructor: RoverPosition.fromBuffer,
callback: _internalUpdate,
Expand All @@ -23,32 +23,32 @@ class RoverImu extends ImuInterface {
}

@override
void update(Orientation newValue) {
void update(Rotation3d newValue) {
// Do nothing, since this should only be internally updated
}

@override
void forceUpdate(Orientation newValue) =>
void forceUpdate(Rotation3d newValue) =>
_internalUpdate(RoverPosition(orientation: newValue));

void _internalUpdate(RoverPosition newValue) {
if (!newValue.hasOrientation()) return;
// Angles are always between -180 and +180
if (newValue.orientation.x.abs() > 180 ||
newValue.orientation.y.abs() > 180 ||
newValue.orientation.z.abs() > 180) {
if (newValue.orientation.pitch.abs() > 180 ||
newValue.orientation.roll.abs() > 180 ||
newValue.orientation.yaw.abs() > 180) {
return;
}
_xCorrector.addValue(newValue.orientation.x);
_yCorrector.addValue(newValue.orientation.y);
_zCorrector.addValue(newValue.orientation.z);
_xCorrector.addValue(newValue.orientation.pitch);
_yCorrector.addValue(newValue.orientation.roll);
_zCorrector.addValue(newValue.orientation.yaw);
hasValue = true;
}

@override
Orientation get raw => Orientation(
x: _xCorrector.calibratedValue.clampHalfAngle(),
y: _yCorrector.calibratedValue.clampHalfAngle(),
z: _zCorrector.calibratedValue.clampHalfAngle(),
Rotation3d get raw => Rotation3d(
pitch: _xCorrector.calibratedValue.clampHalfAngle(),
roll: _yCorrector.calibratedValue.clampHalfAngle(),
yaw: _zCorrector.calibratedValue.clampHalfAngle(),
);
}
14 changes: 7 additions & 7 deletions autonomy/lib/src/imu/sim_imu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ class ImuSimulator extends ImuInterface with ValueReporter {
@override
RoverPosition getMessage() => RoverPosition(orientation: raw);

Orientation _orientation = Orientation();
Rotation3d _orientation = Rotation3d();

@override
Orientation get raw => Orientation(
x: _orientation.x + _error.value,
y: _orientation.y + _error.value,
z: _orientation.z + _error.value,
Rotation3d get raw => Rotation3d(
pitch: _orientation.pitch + _error.value,
roll: _orientation.roll + _error.value,
yaw: _orientation.yaw + _error.value,
);

@override
void update(Orientation newValue) => _orientation = newValue;
void update(Rotation3d newValue) => _orientation = newValue;

@override
Future<bool> init() async {
Expand All @@ -29,7 +29,7 @@ class ImuSimulator extends ImuInterface with ValueReporter {

@override
Future<void> dispose() async {
_orientation = Orientation();
_orientation = Rotation3d();
await super.dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract class OrchestratorInterface extends Service {

@override
Future<bool> init() async {
collection.server.messages.onMessage(
collection.server.messages.listenFor(
name: AutonomyCommand().messageName,
constructor: AutonomyCommand.fromBuffer,
callback: onCommand,
Expand Down
10 changes: 5 additions & 5 deletions autonomy/lib/src/orchestrator/rover_orchestrator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,15 @@ class RoverOrchestrator extends OrchestratorInterface with ValueReporter {
}
// Re-align to desired start orientation if angle is too far
if (state.instruction == DriveDirection.forward) {
Orientation targetOrientation;
Rotation3d targetOrientation;
// if it has RTK, point towards the next coordinate
if (collection.gps.coordinates.hasRTK) {
final difference =
state.position.toUTM() - collection.gps.coordinates.toUTM();

final angle = atan2(difference.y, difference.x) * 180 / pi;

targetOrientation = Orientation(z: angle);
targetOrientation = Rotation3d(yaw: angle);
} else {
targetOrientation = state.orientation.orientation;
}
Expand Down Expand Up @@ -309,8 +309,8 @@ class RoverOrchestrator extends OrchestratorInterface with ValueReporter {

collection.logger.info("Found aruco");
currentState = AutonomyState.APPROACHING;
final arucoOrientation = Orientation(
z: collection.imu.heading - detectedAruco.yaw,
final arucoOrientation = Rotation3d(
yaw: collection.imu.heading - detectedAruco.yaw,
);
await collection.drive.faceOrientation(arucoOrientation);
detectedAruco = await collection.video.waitForAruco(
Expand Down Expand Up @@ -409,7 +409,7 @@ class RoverOrchestrator extends OrchestratorInterface with ValueReporter {
if (detectedAruco != null) {
collection.logger.info("Rotating towards Aruco");
await collection.drive.faceOrientation(
Orientation(z: collection.imu.heading - detectedAruco!.yaw),
Rotation3d(yaw: collection.imu.heading - detectedAruco!.yaw),
);
} else {
collection.logger.warning("Could not find Aruco after following path");
Expand Down
4 changes: 2 additions & 2 deletions autonomy/lib/src/state_machine/rover_states/navigation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ class NavigationState extends RoverState {
/// If the rover is not facing the proper direction, a new state will be pushed
/// to re-correct the rover's orientation
bool checkOrientation(AutonomyAStarState state) {
Orientation targetOrientation;
Rotation3d targetOrientation;
// if it has RTK, point towards the next coordinate
if (collection.gps.coordinates.hasRTK) {
final difference =
state.position.toUTM() - collection.gps.coordinates.toUTM();

final angle = atan2(difference.y, difference.x) * 180 / pi;

targetOrientation = Orientation(z: angle);
targetOrientation = Rotation3d(yaw: angle);
} else {
targetOrientation = state.orientation.orientation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "package:autonomy/src/drive/drive_config.dart";

class SensorTurnState extends RoverState {
final AutonomyInterface collection;
final Orientation orientation;
final Rotation3d orientation;

final RoverDriveCommands drive;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import "package:autonomy/autonomy.dart";
class SimulationDriveTurn extends RoverState {
final SimulationMethod method;
final AutonomyInterface collection;
final Orientation goalOrientation;
final Rotation3d goalOrientation;

Duration get delay => collection.drive.config.turnDelay;

DateTime _startTime = DateTime(0);
Orientation _startOrientation = Orientation();
Rotation3d _startOrientation = Rotation3d();

SimulationDriveTurn(
super.controller, {
Expand Down Expand Up @@ -38,16 +38,16 @@ class SimulationDriveTurn extends RoverState {
}

if (method == SimulationMethod.intermediate) {
final deltaOrientation = (goalOrientation.z - _startOrientation.z)
final deltaOrientation = (goalOrientation.yaw - _startOrientation.yaw)
.clampHalfAngle();
final deltaTime = DateTime.now().difference(_startTime);
final timeFraction =
1.0 * deltaTime.inMicroseconds / delay.inMicroseconds;

final intermediateRotation =
_startOrientation.z + deltaOrientation * timeFraction;
_startOrientation.yaw + deltaOrientation * timeFraction;

collection.imu.update(Orientation(z: intermediateRotation));
collection.imu.update(Rotation3d(yaw: intermediateRotation));
}
}
}
14 changes: 7 additions & 7 deletions autonomy/lib/src/utils/imu_utils.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import "package:autonomy/autonomy.dart";

extension OrientationUtils on Orientation {
extension OrientationUtils on Rotation3d {
/// North orientation
static final north = Orientation(z: CardinalDirection.north.angle);
static final north = Rotation3d(yaw: CardinalDirection.north.angle);

/// East orientation
static final west = Orientation(z: CardinalDirection.west.angle);
static final west = Rotation3d(yaw: CardinalDirection.west.angle);

/// South Orientation
static final south = Orientation(z: CardinalDirection.south.angle);
static final south = Rotation3d(yaw: CardinalDirection.south.angle);

/// East orientation
static final east = Orientation(z: CardinalDirection.east.angle);
static final east = Rotation3d(yaw: CardinalDirection.east.angle);

/// The heading of the orientation, or the compass direction we are facing
double get heading => z;
double get heading => yaw;

/// Whether or not this orientation is within [epsilon] degrees of [value]
bool isNear(double value, [double? epsilon]) {
epsilon ??= Constants.turnEpsilon;
final error = (value - z).clampHalfAngle();
final error = (value - yaw).clampHalfAngle();

return error.abs() <= epsilon;
// if (value > 270 && z < 90) {
Expand Down
Loading
Loading