LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
mathUtil.hpp
Go to the documentation of this file.
1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6#pragma once
7
9#include <algorithm>
10#include <cstdint>
11#include <math.h>
12#include <type_traits>
13
14namespace okapi {
18static constexpr double inchToMM = 25.4;
19
23static constexpr double mmToInch = 0.0393700787;
24
28static constexpr double degreeToRadian = 0.01745329252;
29
33static constexpr double radianToDegree = 57.2957795;
34
38static constexpr double imeTorqueTPR = 627.2;
39
43static constexpr std::int32_t imeSpeedTPR = 392;
44
48static constexpr double imeTurboTPR = 261.333;
49
53static constexpr double ime269TPR = 240.448;
54
58static constexpr std::int32_t imev5RedTPR = 1800;
59
63static constexpr std::int32_t imev5GreenTPR = 900;
64
68static constexpr std::int32_t imev5BlueTPR = 300;
69
73static constexpr std::int32_t quadEncoderTPR = 360;
74
78static constexpr double pi = 3.1415926535897932;
79
83static constexpr double pi2 = 1.5707963267948966;
84
88static constexpr double gravity = 9.80665;
89
93static constexpr auto OKAPI_PROS_ERR = INT32_MAX;
94
98static constexpr auto OKAPI_PROS_ERR_F = INFINITY;
99
103static constexpr double v5MotorMaxVoltage = 12000;
104
108static constexpr std::int8_t motorUpdateRate = 10;
109
113static constexpr std::int8_t adiUpdateRate = 10;
114
122constexpr double ipow(const double base, const int expo) {
123 return (expo == 0) ? 1
124 : expo == 1 ? base
125 : expo > 1 ? ((expo & 1) ? base * ipow(base, expo - 1)
126 : ipow(base, expo / 2) * ipow(base, expo / 2))
127 : 1 / ipow(base, -expo);
128}
129
139constexpr double cutRange(const double value, const double min, const double max) {
140 const double middle = max - ((max - min) / 2);
141
142 if (value > min && value < middle) {
143 return min;
144 } else if (value <= max && value >= middle) {
145 return max;
146 }
147
148 return value;
149}
150
160constexpr double deadband(const double value, const double min, const double max) {
161 return std::clamp(value, min, max) == value ? 0 : value;
162}
163
174constexpr double remapRange(const double value,
175 const double oldMin,
176 const double oldMax,
177 const double newMin,
178 const double newMax) {
179 return (value - oldMin) * ((newMax - newMin) / (oldMax - oldMin)) + newMin;
180}
181
188template <typename E> constexpr auto toUnderlyingType(const E e) noexcept {
189 return static_cast<std::underlying_type_t<E>>(e);
190}
191
198constexpr auto boolToSign(const bool b) noexcept {
199 return b ? 1 : -1;
200}
201
210constexpr long modulus(const long lhs, const long rhs) noexcept {
211 return ((lhs % rhs) + rhs) % rhs;
212}
213
220constexpr std::int32_t gearsetToTPR(const AbstractMotor::gearset igearset) noexcept {
221 switch (igearset) {
223 return imev5RedTPR;
225 return imev5GreenTPR;
228 default:
229 return imev5BlueTPR;
230 }
231}
232
246constexpr std::int8_t transformADIPort(const std::int8_t port) {
247 if (port >= 'a' && port <= 'h') {
248 return port - ('a' - 1);
249 } else if (port >= 'A' && port <= 'H') {
250 return port - ('A' - 1);
251 } else {
252 return port;
253 }
254}
255} // namespace okapi
gearset
Indicates the internal gear ratio of a motor.
@ blue
6:1, 600 RPM, Blue gear set
@ green
18:1, 200 RPM, Green gear set
@ red
36:1, 100 RPM, Red gear set
static constexpr auto OKAPI_PROS_ERR_F
Same as PROS_ERR_F.
Definition mathUtil.hpp:98
static constexpr double pi2
The value of pi divided by 2.
Definition mathUtil.hpp:83
static constexpr auto OKAPI_PROS_ERR
Same as PROS_ERR.
Definition mathUtil.hpp:93
constexpr long modulus(const long lhs, const long rhs) noexcept
Computes lhs mod rhs using Euclidean division.
Definition mathUtil.hpp:210
static constexpr double pi
The value of pi.
Definition mathUtil.hpp:78
static constexpr double gravity
The conventional value of gravity of Earth.
Definition mathUtil.hpp:88
static constexpr std::int32_t imev5RedTPR
The ticks per rotation of the V5 motor with a red gearset.
Definition mathUtil.hpp:58
constexpr double deadband(const double value, const double min, const double max)
Deadbands a range of the number.
Definition mathUtil.hpp:160
static constexpr double v5MotorMaxVoltage
The maximum voltage that can be sent to V5 motors.
Definition mathUtil.hpp:103
constexpr auto toUnderlyingType(const E e) noexcept
Converts an enum to its value type.
Definition mathUtil.hpp:188
static constexpr double inchToMM
Converts inches to millimeters.
Definition mathUtil.hpp:18
static constexpr std::int32_t imev5GreenTPR
The ticks per rotation of the V5 motor with a green gearset.
Definition mathUtil.hpp:63
static constexpr double ime269TPR
The ticks per rotation of the 269 IME.
Definition mathUtil.hpp:53
static constexpr double degreeToRadian
Converts degrees to radians.
Definition mathUtil.hpp:28
constexpr std::int32_t gearsetToTPR(const AbstractMotor::gearset igearset) noexcept
Converts a gearset to its TPR.
Definition mathUtil.hpp:220
static constexpr double imeTorqueTPR
The ticks per rotation of the 393 IME with torque gearing.
Definition mathUtil.hpp:38
constexpr auto boolToSign(const bool b) noexcept
Converts a bool to a sign.
Definition mathUtil.hpp:198
static constexpr std::int32_t imev5BlueTPR
The ticks per rotation of the V5 motor with a blue gearset.
Definition mathUtil.hpp:68
constexpr std::int8_t transformADIPort(const std::int8_t port)
Maps ADI port numbers/chars to numbers:
Definition mathUtil.hpp:246
constexpr double cutRange(const double value, const double min, const double max)
Cuts out a range from the number.
Definition mathUtil.hpp:139
static constexpr double radianToDegree
Converts radians to degrees.
Definition mathUtil.hpp:33
constexpr double remapRange(const double value, const double oldMin, const double oldMax, const double newMin, const double newMax)
Remap a value in the range [oldMin, oldMax] to the range [newMin, newMax].
Definition mathUtil.hpp:174
constexpr double ipow(const double base, const int expo)
Integer power function.
Definition mathUtil.hpp:122
static constexpr std::int8_t adiUpdateRate
The polling frequency of the ADI ports in milliseconds.
Definition mathUtil.hpp:113
static constexpr std::int32_t quadEncoderTPR
The ticks per rotation of the red quadrature encoders.
Definition mathUtil.hpp:73
static constexpr std::int32_t imeSpeedTPR
The ticks per rotation of the 393 IME with speed gearing.
Definition mathUtil.hpp:43
static constexpr std::int8_t motorUpdateRate
The polling frequency of V5 motors in milliseconds.
Definition mathUtil.hpp:108
static constexpr double imeTurboTPR
The ticks per rotation of the 393 IME with turbo gearing.
Definition mathUtil.hpp:48
static constexpr double mmToInch
Converts millimeters to inches.
Definition mathUtil.hpp:23