LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
odometry.hpp
Go to the documentation of this file.
1#pragma once
2#include <cmath>
3#include <vector>
4
6#include "light/distance.hpp"
7#include "light/gps.hpp"
8#include "light/imu.hpp"
10#include "light/rotation.hpp"
11
20struct Pose {
21 float x;
22 float y;
23 float theta;
24 Pose(float x = 0, float y = 0, float theta = 0) : x(x), y(y), theta(theta) {}
25 Pose operator*(float scalar) const { return {x * scalar, y * scalar, theta * scalar}; }
26 Pose operator+(const Pose& o) const { return {x + o.x, y + o.y, theta + o.theta}; }
27 Pose operator-(const Pose& o) const { return {x - o.x, y - o.y, theta - o.theta}; }
28};
29
35 public:
48 TrackingWheel(pros::Rotation* sensor, float wheelDiam, float offset, float tpr = 36000.0f)
49 : rotSensor(sensor), motorGroup(nullptr), wheelCircumference(wheelDiam * M_PI), offset(offset), tpr(tpr), powered(false) {}
50
63 TrackingWheel(pros::MotorGroup* motors, float wheelDiam, float offset, float tpr = 360.0f)
64 : rotSensor(nullptr), motorGroup(motors), wheelCircumference(wheelDiam * M_PI), offset(offset), tpr(tpr), powered(true) {}
65
67 float getDistanceTraveled() const {
68 if (rotSensor) return (rotSensor->get_position() / tpr) * wheelCircumference;
69 if (motorGroup) return (motorGroup->get_position() / tpr) * wheelCircumference;
70 return 0.0f;
71 }
72
74 float getOffset() const { return offset; }
75
77 bool isPowered() const { return powered; }
78
80 void reset() {
81 if (rotSensor) rotSensor->reset_position();
82 if (motorGroup) motorGroup->tare_position();
83 }
84
85 private:
86 pros::Rotation* rotSensor;
87 pros::MotorGroup* motorGroup;
88 float wheelCircumference;
89 float offset;
90 float tpr;
91 bool powered;
92};
93
100 pros::Distance* sensor;
101 float offsetX;
102 float offsetY;
103 float angleRad;
106 float (*raycastFn)(float, float, float, float) = nullptr;
107};
108
115 pros::Imu* imu;
116 pros::Imu* imu2;
117
119 pros::Gps* gps;
122
124 std::vector<DistanceSensorSpec> distanceSensors;
125
129 pros::Imu* imu, pros::Imu* imu2 = nullptr)
130 : vertical1(v1), vertical2(v2), horizontal1(h1), horizontal2(h2), imu(imu), imu2(imu2), gps(nullptr), gpsOffsetX(0.0f), gpsOffsetY(0.0f) {}
131
135 pros::Imu* imu, pros::Imu* imu2,
136 pros::Gps* gps, float gpsOffsetX, float gpsOffsetY,
137 std::vector<DistanceSensorSpec> distanceSensors)
139};
140
142namespace light {
143
145void reset();
146
155void init(OdomSensors sensors, MCLConfig cfg = {});
156
158void stop();
159
161void moveToPoint(float targetX, float targetY, int timeout, float maxSpeed, bool reversed);
162
164Pose getPose(bool radians = false);
165
167void setPose(Pose pose, bool radians = false);
168
170Pose getSpeed(bool radians = false);
171
173Pose getLocalSpeed(bool radians = false);
174
176Pose estimatePose(float time, bool radians = false);
177
179void update();
180
182inline Pose getPoseRad() { return getPose(true); }
183} // namespace light
Single tracking wheel — works with either a Rotation sensor (preferred) or a powered MotorGroup.
Definition odometry.hpp:34
float getDistanceTraveled() const
Definition odometry.hpp:67
void reset()
Zero the encoder.
Definition odometry.hpp:80
float getOffset() const
Definition odometry.hpp:74
TrackingWheel(pros::Rotation *sensor, float wheelDiam, float offset, float tpr=36000.0f)
Construct from an unpowered Rotation sensor.
Definition odometry.hpp:48
TrackingWheel(pros::MotorGroup *motors, float wheelDiam, float offset, float tpr=360.0f)
Construct from a powered MotorGroup (blue-cart default).
Definition odometry.hpp:63
bool isPowered() const
Definition odometry.hpp:77
Runtime tuning for LightCast (particle filter) and the EKF.
Initialize once via init(); poll getPose/setPose from any task.
Definition pid.hpp:22
void update()
Force one update (normally called by the background task).
Pose getSpeed(bool radians=false)
void init(OdomSensors sensors, MCLConfig cfg={})
Initialize the estimator.
Pose getPoseRad()
Radian-only accessor; trajectory code MUST use this to avoid deg/rad bugs.
Definition odometry.hpp:182
void stop()
Stop the background odometry task.
Pose estimatePose(float time, bool radians=false)
Linear extrapolation of pose time seconds ahead at current velocity.
Pose getLocalSpeed(bool radians=false)
void reset()
Reset all internal pose state to (0, 0, 0).
Pose getPose(bool radians=false)
void moveToPoint(float targetX, float targetY, int timeout, float maxSpeed, bool reversed)
Drive to a field-relative point (legacy).
void setPose(Pose pose, bool radians=false)
Overwrite the current pose.
STL namespace.
Distance sensor mount for LightCast.
Definition odometry.hpp:99
float offsetX
Mount X in robot frame, inches.
Definition odometry.hpp:101
float(* raycastFn)(float, float, float, float)
leave null for the generic perimiter raycast (works for any angle).
Definition odometry.hpp:106
pros::Distance * sensor
Backing PROS distance sensor (not owned).
Definition odometry.hpp:100
float offsetY
Mount Y in robot frame, inches.
Definition odometry.hpp:102
float angleRad
Ray direction in robot frame, radians.
Definition odometry.hpp:103
Tuning knobs for the LightCast particle filter and the EKF.
Bundle of all sensors used by the LightLib pose estimator.
Definition odometry.hpp:110
TrackingWheel * horizontal1
Primary horizontal (lateral) tracking wheel.
Definition odometry.hpp:113
TrackingWheel * vertical1
Primary vertical (forward-axis) tracking wheel.
Definition odometry.hpp:111
OdomSensors(TrackingWheel *v1, TrackingWheel *v2, TrackingWheel *h1, TrackingWheel *h2, pros::Imu *imu, pros::Imu *imu2=nullptr)
Wheel + IMU only constructor.
Definition odometry.hpp:127
pros::Gps * gps
nullptr → EKF skips the GPS update step.
Definition odometry.hpp:119
pros::Imu * imu2
Optional second IMU; nullptr if not used.
Definition odometry.hpp:116
TrackingWheel * horizontal2
Secondary horizontal tracking wheel (or nullptr).
Definition odometry.hpp:114
float gpsOffsetX
meters, robot frame
Definition odometry.hpp:120
std::vector< DistanceSensorSpec > distanceSensors
Empty → LightCast inactive; EKF-only mode.
Definition odometry.hpp:124
pros::Imu * imu
Primary IMU.
Definition odometry.hpp:115
TrackingWheel * vertical2
Secondary vertical tracking wheel (or nullptr).
Definition odometry.hpp:112
OdomSensors(TrackingWheel *v1, TrackingWheel *v2, TrackingWheel *h1, TrackingWheel *h2, pros::Imu *imu, pros::Imu *imu2, pros::Gps *gps, float gpsOffsetX, float gpsOffsetY, std::vector< DistanceSensorSpec > distanceSensors)
Full constructor with GPS and distance sensors.
Definition odometry.hpp:133
float gpsOffsetY
meters, robot frame
Definition odometry.hpp:121
Field-frame 2D pose.
Definition odometry.hpp:20
float theta
Heading.
Definition odometry.hpp:23
float x
X position, inches.
Definition odometry.hpp:21
Pose(float x=0, float y=0, float theta=0)
Definition odometry.hpp:24
float y
Y position, inches.
Definition odometry.hpp:22
Pose operator*(float scalar) const
Definition odometry.hpp:25
Pose operator-(const Pose &o) const
Definition odometry.hpp:27
Pose operator+(const Pose &o) const
Definition odometry.hpp:26
2D pose: position (in) and heading (deg).
Definition util.hpp:116