LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
util.hpp
Go to the documentation of this file.
1/*
2This Source Code Form is subject to the terms of the Mozilla Public
3License, v. 2.0. If a copy of the MPL was not distributed with this
4file, You can obtain one at http://mozilla.org/MPL/2.0/.
5*/
6
7#pragma once
8
16#include <algorithm>
17#include <atomic>
18#include <cmath>
19#include <cstdint>
20#include <cstdio>
21#include <cstring>
22#include <functional>
23#include <iomanip>
24#include <iostream>
25#include <memory>
26#include <sstream>
27#include <string>
28#include <vector>
29
30#include "LightLib/api.h"
31#include "light/units.hpp"
32
33using namespace okapi::literals;
34
36extern pros::Controller master;
37
38namespace light {
39
44
55void screen_print(std::string text, int line = 0);
56
63enum e_type { SINGLE = 0,
64 SPLIT = 1 };
65
69
77
79enum e_mode { DISABLE = 0,
80 SWING = 1,
81 TURN = 2,
83 DRIVE = 4,
86
96
109
111const double ANGLE_NOT_SET = 0.0000000000000000000001;
113const okapi::QAngle p_ANGLE_NOT_SET = 0.0000000000000000000001_deg;
114
116typedef struct pose {
117 double x;
118 double y;
120} pose;
121
123typedef struct united_pose {
124 okapi::QLength x;
125 okapi::QLength y;
126 okapi::QAngle theta = p_ANGLE_NOT_SET;
127} united_pose;
128
136
144
153std::string exit_to_string(exit_output input);
154
159namespace units {
160inline constexpr double MM_PER_IN = 25.4;
161inline constexpr double IN_PER_MM = 1.0 / 25.4;
162inline constexpr double M_PER_IN = 0.0254;
163inline constexpr double IN_PER_M = 1.0 / 0.0254;
164inline constexpr double DEG_PER_RAD = 180.0 / M_PI;
165inline constexpr double RAD_PER_DEG = M_PI / 180.0;
166} // namespace units
167
169namespace util {
170extern bool AUTON_RAN;
171
180int places_after_decimal(double input, int min = 0);
181
190std::string to_string_with_precision(double input, int n = 2);
191
198int sgn(double input);
199
206bool reversed_active(double input);
207
218double clamp(double input, double max, double min);
219
230double clamp(double input, double max);
231
233const bool SD_CARD_ACTIVE = pros::usd::is_installed();
234
236const int DELAY_TIME = 10;
237
244double to_deg(double input);
245
252double to_rad(double input);
253
262double absolute_angle_to_point(pose itarget, pose icurrent);
263
272double distance_to_point(pose itarget, pose icurrent);
273
280double wrap_angle(double theta);
281
291inline float wrap_rad(float a) {
292 return std::atan2(std::sin(a), std::cos(a));
293}
294
303pose vector_off_point(double added, pose icurrent);
304
315double turn_shortest(double target, double current, bool print = false);
316
327double turn_longest(double target, double current, bool print = false);
328
336
343std::vector<odom> united_odoms_to_odoms(std::vector<united_odom> inputs);
344
352
353} // namespace util
354} // namespace light
PROS API header provides high-level user functionality.
constexpr double IN_PER_M
Inches per meter.
Definition util.hpp:163
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
constexpr double M_PER_IN
Meters per inch.
Definition util.hpp:162
double turn_longest(double target, double current, bool print=false)
Returns the farthest away angle for the robot to turn to in order to get to target.
const int DELAY_TIME
Default loop period for LightLib background tasks, in milliseconds.
Definition util.hpp:236
int places_after_decimal(double input, int min=0)
Returns the amount of places after a decimal, maxing out at 6.
double to_rad(double input)
Converts degrees to radians.
int sgn(double input)
Returns 1 if input is positive and -1 if input is negative.
bool AUTON_RAN
Set true once any autonomous routine has run this match.
double distance_to_point(pose itarget, pose icurrent)
Returns the distance between two points.
const bool SD_CARD_ACTIVE
True if an SD card is currently inserted.
Definition util.hpp:233
pose vector_off_point(double added, pose icurrent)
Returns a new pose that is projected off of the current pose.
odom united_odom_to_odom(united_odom input)
Converts odom movement with united pose to an odom movement without united pose.
std::vector< odom > united_odoms_to_odoms(std::vector< united_odom > inputs)
Converts vector of poses with okapi units to a vector of poses without okapi units.
float wrap_rad(float a)
Wraps an angle in radians to the half-open interval (-pi, pi].
Definition util.hpp:291
double absolute_angle_to_point(pose itarget, pose icurrent)
Returns the angle between two points.
bool reversed_active(double input)
Returns true if the input is < 0.
pose united_pose_to_pose(united_pose input)
Converts pose with okapi units to a pose without okapi units.
double to_deg(double input)
Converts radians to degrees.
double wrap_angle(double theta)
Constrains an angle between 180 and -180.
double turn_shortest(double target, double current, bool print=false)
Returns the shortest angle for the robot to turn to in order to get to target.
double clamp(double input, double max, double min)
Returns input restricted to min-max threshold.
std::string to_string_with_precision(double input, int n=2)
Returns a string with a specific number of decimal points.
Public LightLib odometry / pose-estimation API.
Definition pid.hpp:22
e_type
Arcade joystick layout.
Definition util.hpp:63
@ SPLIT
Definition util.hpp:64
@ SINGLE
Single-stick arcade (one stick = throttle + turn).
Definition util.hpp:63
e_angle_behavior
How to choose a turn direction when reaching an absolute heading.
Definition util.hpp:98
@ ccw
Definition util.hpp:102
@ counterclockwise
Definition util.hpp:101
@ left_turn
Always turn counterclockwise.
Definition util.hpp:99
@ cw
Definition util.hpp:106
@ raw
Use signed delta as-is (no wrap).
Definition util.hpp:98
@ RIGHT_TURN
Definition util.hpp:104
@ clockwise
Definition util.hpp:105
@ LEFT_TURN
Definition util.hpp:100
@ right_turn
Always turn clockwise.
Definition util.hpp:103
@ shortest
Pick the smaller of CW/CCW.
Definition util.hpp:107
@ longest
Definition util.hpp:108
const okapi::QAngle p_ANGLE_NOT_SET
Sentinel value indicating "no angle specified" (united).
Definition util.hpp:113
void screen_print(std::string text, int line=0)
Prints to the brain screen in one string.
e_swing
Direction for swing-turn motions.
Definition util.hpp:67
@ RIGHT_SWING
Definition util.hpp:68
@ LEFT_SWING
Left side stationary, right side drives.
Definition util.hpp:67
std::string exit_to_string(exit_output input)
Returns a human-readable name for an exit_output code.
const double ANGLE_NOT_SET
Sentinel value indicating "no angle specified".
Definition util.hpp:111
exit_output
Result codes returned by PID::exit_condition.
Definition util.hpp:71
@ VELOCITY_EXIT
Stopped moving long enough.
Definition util.hpp:74
@ RUNNING
Still inside the motion.
Definition util.hpp:71
@ BIG_EXIT
Big-error timer expired.
Definition util.hpp:73
@ ERROR_NO_CONSTANTS
Definition util.hpp:76
@ SMALL_EXIT
Small-error timer expired.
Definition util.hpp:72
@ mA_EXIT
Motor current limit hit.
Definition util.hpp:75
e_mode
Active chassis motion mode.
Definition util.hpp:79
@ TURN
Turn-in-place to absolute heading.
Definition util.hpp:81
@ TURN_TO_POINT
Turn to face a field point.
Definition util.hpp:82
@ SWING
Swing turn.
Definition util.hpp:80
@ DISABLE
No motion active.
Definition util.hpp:79
@ DRIVE
Straight-line drive.
Definition util.hpp:83
@ POINT_TO_POINT
Drive to a field point.
Definition util.hpp:84
@ PURE_PURSUIT
Definition util.hpp:85
void ez_template_print()
Prints our branding all over your pros terminal.
drive_directions
Drive direction with multiple spelling aliases.
Definition util.hpp:88
@ FORWARD
Definition util.hpp:89
@ fwd
Definition util.hpp:90
@ FWD
Definition util.hpp:88
@ REVERSE
Definition util.hpp:93
@ REV
Definition util.hpp:92
@ forward
Definition util.hpp:91
@ rev
Definition util.hpp:94
@ reverse
Definition util.hpp:95
One waypoint of an odom-mode movement.
Definition util.hpp:130
pose target
Target pose.
Definition util.hpp:131
int max_xy_speed
Speed cap, 0..127.
Definition util.hpp:133
drive_directions drive_direction
Drive forward or reverse.
Definition util.hpp:132
e_angle_behavior turn_behavior
How to choose turn direction.
Definition util.hpp:134
2D pose: position (in) and heading (deg).
Definition util.hpp:116
double y
Y position, inches.
Definition util.hpp:118
double theta
Heading, degrees.
Definition util.hpp:119
double x
X position, inches.
Definition util.hpp:117
One waypoint of an odom-mode movement, with okapi units.
Definition util.hpp:138
e_angle_behavior turn_behavior
How to choose turn direction.
Definition util.hpp:142
int max_xy_speed
Speed cap, 0..127.
Definition util.hpp:141
united_pose target
Target pose with units.
Definition util.hpp:139
drive_directions drive_direction
Drive forward or reverse.
Definition util.hpp:140
2D pose with okapi units.
Definition util.hpp:123
okapi::QLength y
Y position with units.
Definition util.hpp:125
okapi::QLength x
X position with units.
Definition util.hpp:124
okapi::QAngle theta
Heading with units.
Definition util.hpp:126
pros::Controller master
Global V5 master controller, used by opcontrol throughout LightLib.