Skip to main content

Math

note

Math functions are built with templates. This means that you can use different types of parameters with these functions, such as int, float, double, etc. Templates allow these functions to operate with various data types, providing flexibility and reusability in your code.

Math functions

sign()

Sign function.

template <typename T> T sign(const T &num);

Parameters
numA number.

Returns: -1 if its negative or 1 if its positive.


Trigonometry

to_rad()

Translates degrees to radians.

template <typename T> T to_rad(const T &num)

Parameters
numAn angle in degrees.

Returns: Angle in radians.


to_degrees()

Translates radians to degrees.

template <typename T> T to_degrees(const T &num)

Parameters
numAn angle in radians.

Returns: Angle in degrees.


reduce_angle_0_to_360()

Puts angle in a 0-360 degrees range.

template <typename T> T reduce_angle_0_to_360(T degrees); 

Parameters
degreesAn angle in degrees.

Returns: The angle in degrees in range of 0-360 degrees.


reduce_angle_180_to_180()

Put angle in a -180 -180 degrees range.

template <typename T> T reduce_angle_180_to_180()
Parameters
degreesAn angle in degrees.

Returns: The angle in degrees in range of -180-180 degrees.


get_angle_btw_points()

Gets the angle in degrees between two points.

auto get_angle_btw_points(const std::vector<T1> &current,
const std::vector<T2> &target);

Parameters
currentCurrent point.
targetTarget point.

Returns: The angle in degrees that exist between current and target points.


distance_btw_points()

Gets the distance between two points using Pythagoras.

auto distance_btw_points(const std::vector<T1> &p1, const std::vector<T2> &p2);
Parameters
p1Current point.
p2Target point.

Returns: Distance between current and target points.


Velocity

note

The linear velocity is always in distance/seconds units.

rpm_to_linear()

Convert a RPM velocity to a linear velocity (distance/seconds).

auto rpm_to_linear(const T1 &rpm, const T2 &wheel_diameter);
Parameters
rpmRPM velocity.
wheel_diameterThe wheel size.

Returns: Velocity converted.


linear_to_rpm()

Convert a lineal velocity (distance/seconds) to a RPM.

warning

The distance units in the velocity and wheel diameter must to be the same.

auto linear_to_rpm(const T1 &lineal_velocity, const T2 &wheel_diameter);
Parameters
lineal_velocitylineal velocity (distance/seconds).
wheel_diameterThe wheel size .

Returns: Velocity converted.


Vector operations

swap_vector()

Swaps the first two elements of a vector.

template <typename T1> std::vector<T1> swap_vector(const std::vector<T1> &a);
Parameters
aThe input vector.

Returns: A vector with the first two elements swapped.


vector_add()

Adds two vectors element-wise.

std::vector<T1> vector_add(const std::vector<T1> &a, const std::vector<T1> &b);
Parameters
aThe first input vector.
bThe second input vector.

Returns: A vector with each element being the sum of the corresponding elements of a and b.


vector_sub()

Subtracts the second vector from the first vector element-wise.

std::vector<T1> vector_sub(const std::vector<T1> &a, const std::vector<T1> &b);
Parameters
aThe first input vector.
bThe second input vector.

Returns: A vector with each element being the difference of the corresponding elements of a and b.


vector_dot()

Computes the dot product of two vectors.

auto vector_dot(const std::vector<T1> &a, const std::vector<T1> &b);
Parameters
aThe first input vector.
bThe second input vector.

Returns: The dot product of the vectors.


vector_mult()

Multiplies each element of a vector by a scalar.

std::vector<T1> vector_mult(const std::vector<T1> &a, const T2 b);
Parameters
aThe input vector.
bThe scalar value.

Returns: A vector with each element multiplied by the scalar.