LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
drive_utils.hpp
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cmath>
4
10#include "pros/distance.hpp"
11#include "subsystems.hpp"
12
41inline void drive_until_distance(okapi::QLength target,
42 pros::Distance& sensor,
43 int speed = 127,
44 int timeout_ms = 10000) {
45 int target_mm = (int)(target.convert(okapi::inch) * light::units::MM_PER_IN);
46 // Drive forward or backward depending on speed sign.
47 // 5000 is an arbitrarily large distance — the sensor triggers the real stop.
48 int drive_dir = (speed >= 0) ? 5000 : -5000;
49 chassis.pid_drive_set(drive_dir, std::abs(speed));
50
51 int elapsed = 0;
52 while (elapsed < timeout_ms) {
53 int reading = sensor.get();
54 if (reading != PROS_ERR && reading <= target_mm) {
55 chassis.pid_drive_set(0_in, 0);
56 return;
57 }
58 pros::delay(10);
59 elapsed += 10;
60 }
62}
63
87 pros::Distance& sensor,
88 double offset_fwd,
89 double offset_side,
90 bool fix_x,
91 bool faces_positive = false,
92 double field_size_in = light::field::FIELD_SIZE_IN) {
93 int raw = sensor.get();
94 if (raw == PROS_ERR) return;
95
96 double heading_deg = chassis.drive_imu_get();
97 double heading = heading_deg * light::units::RAD_PER_DEG;
98 double dist_in = raw * light::units::IN_PER_MM;
99 Pose cur = light::getPose();
100
101 double projected_offset = fix_x
102 ? (offset_fwd * std::sin(heading) + offset_side * std::cos(heading))
103 : (offset_fwd * std::cos(heading) - offset_side * std::sin(heading));
104
105 double center_to_wall = dist_in + projected_offset;
106 double coord = faces_positive ? (field_size_in - center_to_wall)
107 : center_to_wall;
108
109 if (fix_x)
110 light::setPose(Pose(coord, cur.y, heading_deg));
111 else
112 light::setPose(Pose(cur.x, coord, heading_deg));
113}
114
127 double known_coord,
128 bool fix_x) {
129 double heading_deg = chassis.drive_imu_get();
130 Pose cur = light::getPose();
131
132 if (fix_x)
133 light::setPose(Pose(known_coord, cur.y, heading_deg));
134 else
135 light::setPose(Pose(cur.x, known_coord, heading_deg));
136}
137
155 pros::Distance& front_sensor,
156 pros::Distance& rear_sensor,
157 double separation) {
158 int front_raw = front_sensor.get();
159 int rear_raw = rear_sensor.get();
160 if (front_raw == PROS_ERR || rear_raw == PROS_ERR) return;
161
162 double angle_error_deg = std::atan2((rear_raw - front_raw) * light::units::IN_PER_MM, separation) * light::units::DEG_PER_RAD;
163 chassis.drive_imu_reset(chassis.drive_imu_get() - angle_error_deg);
164}
Pose, sensor specs, and the global odometry / pose-estimation API.
Tank-drive chassis class.
Definition drive.hpp:72
double drive_imu_get()
Returns the current imu rotation value in degrees.
void pid_drive_set(okapi::QLength p_target, int speed)
Sets the robot to move forward using PID with okapi units, only using slew if globally enabled.
void pid_wait_quick_chain()
Lock the code in a while loop until the robot has settled.
void drive_imu_reset(double new_heading=0)
Resets the current imu value.
Tank chassis class — the main user-facing API for autonomous and opcontrol drive motions.
void drive_distance_reset(light::Drive &chassis, double known_coord, bool fix_x)
Re-zero one odom axis from a known field coordinate (e.g.
void drive_until_distance(okapi::QLength target, pros::Distance &sensor, int speed=127, int timeout_ms=10000)
Drive forward (or reverse) until a distance sensor reads at or below target.
void angle_reset(light::Drive &chassis, pros::Distance &front_sensor, pros::Distance &rear_sensor, double separation)
Correct heading from front- and rear-mounted distance sensors looking at the same parallel wall.
void distance_reset(light::Drive &chassis, pros::Distance &sensor, double offset_fwd, double offset_side, bool fix_x, bool faces_positive=false, double field_size_in=light::field::FIELD_SIZE_IN)
Re-zero one odom axis (x or y) using a wall-facing distance sensor.
Fixed-map ray caster used by LightCast's sensor model.
constexpr float FIELD_SIZE_IN
Field side length, inches (12 ft).
Definition field_map.hpp:20
constexpr double MM_PER_IN
Millimeters per inch.
Definition util.hpp:160
constexpr double IN_PER_MM
Inches per millimeter.
Definition util.hpp:161
constexpr double DEG_PER_RAD
Degrees per radian.
Definition util.hpp:164
constexpr double RAD_PER_DEG
Radians per degree.
Definition util.hpp:165
@ raw
Use signed delta as-is (no wrap).
Definition util.hpp:98
Pose getPose(bool radians=false)
void setPose(Pose pose, bool radians=false)
Overwrite the current pose.
constexpr QLength inch
Definition QLength.hpp:22
Field-frame 2D pose.
Definition odometry.hpp:24
float x
X position, inches.
Definition odometry.hpp:25
float y
Y position, inches.
Definition odometry.hpp:26
Robot subsystem declarations: motors, sensors, and pistons.
Drive chassis
The drive chassis, configured in main.cpp and used everywhere else.
Shared types, enums, math helpers, and unit conversions used across LightLib.