|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +--- |
| 4 | + |
| 5 | +# 2 - Odometry |
| 6 | + |
| 7 | +## Initialization |
| 8 | + |
| 9 | +import Tabs from '@theme/Tabs'; |
| 10 | +import TabItem from '@theme/TabItem'; |
| 11 | + |
| 12 | +<Tabs groupId="odometry-type"> |
| 13 | + |
| 14 | + <TabItem value="perpendicular" label="Perpendicular" default> |
| 15 | + ```cpp |
| 16 | + // Define rotation sensors for odometry wheels |
| 17 | + RotationSensor verticalOdomWheel = RotationSensor("VerticalOdomWheel", 10); |
| 18 | + RotationSensor horizontalOdomWheel = RotationSensor("HorizontalOdomWheel", 11); |
| 19 | + |
| 20 | + // Define the radius of the dead wheels (in inches) |
| 21 | + static constexpr double DEAD_WHEEL_RADIUS = 1.0; // in |
| 22 | + |
| 23 | + // Create a perpendicular sensor odometry object |
| 24 | + // (Since PerpendicularSensorOdometry is an AsyncTask, we must use a shared pointer) |
| 25 | + PerpendicularSensorOdometry odometry = PerpendicularSensorOdometry( |
| 26 | + verticalOdomWheel, |
| 27 | + horizontalOdomWheel, |
| 28 | + DEAD_WHEEL_RADIUS |
| 29 | + ); |
| 30 | + |
| 31 | + // Run odometry in a background task |
| 32 | + void initialize() { |
| 33 | + odometry.start(); |
| 34 | + } |
| 35 | + ``` |
| 36 | + </TabItem> |
| 37 | + |
| 38 | + <TabItem value="parallel" label="Parallel"> |
| 39 | + ```cpp |
| 40 | + // Define rotation sensors for odometry wheels |
| 41 | + RotationSensor leftOdomWheel = RotationSensor("LeftOdomWheel", 10); |
| 42 | + RotationSensor rightOdomWheel = RotationSensor("RightOdomWheel", 11); |
| 43 | + |
| 44 | + // Define the radius of the dead wheels (in inches) |
| 45 | + static constexpr double DEAD_WHEEL_RADIUS = 1.0; // in |
| 46 | + |
| 47 | + // Define the distance between the left and right odometry wheels (in inches) |
| 48 | + static constexpr double DEAD_WHEEL_BASE = 12.0; // in |
| 49 | + |
| 50 | + // Create a parallel sensor odometry object |
| 51 | + // (Since ParallelSensorOdometry is an AsyncTask, we must use a shared pointer) |
| 52 | + std::shared_ptr<ParallelSensorOdometry> odometry = std::make_shared<ParallelSensorOdometry>( |
| 53 | + leftOdomWheel, |
| 54 | + rightOdomWheel, |
| 55 | + DEAD_WHEEL_RADIUS, |
| 56 | + DEAD_WHEEL_BASE |
| 57 | + ); |
| 58 | + |
| 59 | + // Run odometry in a background task |
| 60 | + void initialize() { |
| 61 | + odometry.start(); |
| 62 | + } |
| 63 | + ``` |
| 64 | + </TabItem> |
| 65 | +</Tabs> |
| 66 | + |
| 67 | +## Usage |
| 68 | + |
| 69 | +Odometry provides the current heading and position of the robot on the field as a `Pose` object. |
| 70 | +The `Pose` provides `x` and `y` coordinates (in inches) representing the robot's position, as well as a `heading` (in degrees) representing the robot's orientation. |
| 71 | + |
| 72 | +```cpp |
| 73 | +void opcontrol() { |
| 74 | + while (true) { |
| 75 | + // Get the current pose of the robot |
| 76 | + Pose pose = odometry.getPose(); |
| 77 | + std::cout << "X: " << pose.x << " Y: " << pose.y << " Heading: " << pose.heading << std::endl; |
| 78 | + |
| 79 | + // Example: Resetting the robot's pose when a button is pressed |
| 80 | + bool resetPose = mainController.get_digital(DIGITAL_A); |
| 81 | + if (resetPose) |
| 82 | + odometry.setPose(Pose(0, 0, 0)); |
| 83 | + |
| 84 | + // ... |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## Configuration |
| 90 | + |
| 91 | +### Inertial Sensor |
| 92 | + |
| 93 | +An Inertial Sensor (IMU) can be used to improve the accuracy of odometry calculations by providing accurate orientation data that drifts significantly less over time. |
| 94 | +While optional, it is **highly recommended** to use an IMU for the best performance. |
| 95 | + |
| 96 | +```cpp |
| 97 | +// Define the Inertial Sensor |
| 98 | +InertialSensor imu = InertialSensor("IMU", 15); |
| 99 | + |
| 100 | +void initialize() { |
| 101 | + // Use the IMU for odometry calculations |
| 102 | + odometry.useIMU(&imu); |
| 103 | + |
| 104 | + // ... |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### Rotational Compensation |
| 109 | + |
| 110 | +<Tabs groupId="odometry-type"> |
| 111 | + |
| 112 | + <TabItem value="perpendicular" label="Perpendicular" default> |
| 113 | + When the robot rotates, the perpendicular orientation of the odometry wheels can inadvertently cause lateral movement readings. |
| 114 | + To correct for this, you can set the offsets of the odometry sensors relative to the robot's center of rotation. |
| 115 | + |
| 116 | + These are defined as `Vector2` objects, where the x-coordinate represents the horizontal offset (left/right) and the y-coordinate represents the vertical offset (forward/backward). |
| 117 | + |
| 118 | + :::note |
| 119 | + You may have to manually tune these values based on the position and grip of your odometry wheels. |
| 120 | + ::: |
| 121 | + |
| 122 | + ```cpp |
| 123 | + // Offset values for the odometry sensors, relative to the robot's center of rotation (in inches) |
| 124 | + Vector2 verticalSensorOffset = Vector2(-0.5, 0); // (Y position is disregarded) |
| 125 | + Vector2 horizontalSensorOffset = Vector2(0, 1); // (X position is disregarded) |
| 126 | + |
| 127 | + void initialize() { |
| 128 | + // Apply the offsets for odometry calculations |
| 129 | + odometry.setSensorOffsets(verticalSensorOffset, horizontalSensorOffset); |
| 130 | + |
| 131 | + // ... |
| 132 | + } |
| 133 | + |
| 134 | + ``` |
| 135 | + </TabItem> |
| 136 | +
|
| 137 | + <TabItem value="parallel" label="Parallel"> |
| 138 | + :::tip[You don't need this with Parallel Odom Wheels] |
| 139 | + Rotational compensation is not required for Parallel Sensor Odometry since the odometry wheels will not produce inadvertent lateral movement readings when the robot rotates. |
| 140 | + ::: |
| 141 | + </TabItem> |
| 142 | +</Tabs> |
0 commit comments