Mod Library
Sun Devil Rocketry's hardware agnostic utility library
Loading...
Searching...
No Matches
math_sdr.h File Reference

: Contains math and utility functions for SDR code. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  _QUAT
struct  _VECTOR_3F
 Three-dimensional floating-point vector. More...

Macros

#define GRAVITY   9.8f
#define IDENTITY_QUAT   ((QUAT) {1.0f, 0.0f, 0.0f, 0.0f })
#define util_set_bit(orig, idx)
 Sets a certain bit and returns the new value.
#define rad_to_deg(x)
 Convert a value in radians to degrees.
#define deg_to_rad(x)
 Convert a value in degrees to radians.
#define array_size(array)
 Returns the number of elements in an array where each element is a fixed size.

Typedefs

typedef struct _QUAT QUAT
typedef struct _VECTOR_3F VECTOR_3F
 Three-dimensional floating-point vector.

Functions

uint32_t crc32 (const uint8_t *data, size_t len)
 Computes a CRC-32 checksum over a block of data.
float clamp_float (float value, float minimum, float maximum)
 Clamps a floating-point value between minimum and maximum bounds.
QUAT eul_to_quat (float yaw, float pitch, float roll)
 Converts ZYX Euler angles to a quaternion.
QUAT quat_mult (QUAT a, QUAT b)
 Hamilton product of two quaternions.
float quat_dot (QUAT a, QUAT b)
 Provides the dot product of two quaternions.
QUAT quat_add (QUAT a, QUAT b)
 Adds two quaternions component-wise.
QUAT quat_scale (QUAT q, float s)
 Scales a quaternion by a scalar value.
QUAT quat_normalize (QUAT q)
 Normalizes a quaternion to unit length.
QUAT quat_conj (QUAT q)
 Computes the conjugate of a quaternion.
QUAT quat_rotate_world_to_body (QUAT attitude, QUAT vector_world)
 Rotates a vector from the world frame into the body frame.
QUAT quat_rotate_body_to_world (QUAT attitude, QUAT vector_body)
 Rotates a vector from the body frame into the world frame.
bool quat_is_finite (QUAT quaternion)
 Determines whether every quaternion component is finite.
bool vector_is_finite (VECTOR_3F vector)
 Determines whether every vector component is finite.
float vector_magnitude (VECTOR_3F vector)
 Calculates the magnitude of a three-dimensional vector.
bool vector_normalize (VECTOR_3F *vector)
 Normalizes a three-dimensional vector in place.
VECTOR_3F vector_cross (VECTOR_3F a, VECTOR_3F b)
 Calculates the cross product of two three-dimensional vectors.
VECTOR_3F vector_add (VECTOR_3F a, VECTOR_3F b)
 Adds two three-dimensional vectors.
VECTOR_3F vector_scale (VECTOR_3F vector, float scalar)
 Scales a three-dimensional vector by a scalar.

Detailed Description

: Contains math and utility functions for SDR code.

Copyright (c) 2025 Sun Devil Rocketry. All rights reserved.

This software is licensed under terms that can be found in the LICENSE file in the root directory of this software component. If no LICENSE file comes with this software, it is covered under the BSD-3-Clause.

https://opensource.org/license/bsd-3-clause

==============================================================================
                    ##### Math module features #####
==============================================================================
[..]
(+) Macros for common values, conversions, and utilities
(+) CRC-32 checksum of data
(+) Quaternion operations
******************************************************************************

Macro Definition Documentation

◆ array_size

#define array_size ( array)
Value:
( sizeof( array ) / sizeof( array[0] ) )

Returns the number of elements in an array where each element is a fixed size.

Note
An error or warning from this macro indicates that it can't be used in that context.
Parameters
arrayThe array with desired element count.
Returns
Number of elements in array.

◆ deg_to_rad

#define deg_to_rad ( x)
Value:
((x) * 0.01745329252f)

Convert a value in degrees to radians.

Parameters
xValue in degrees.
Returns
Equivalent value in radians.

◆ rad_to_deg

#define rad_to_deg ( x)
Value:
((x) * 57.29577951f)

Convert a value in radians to degrees.

Parameters
xValue in radians.
Returns
Equivalent value in degrees.

◆ util_set_bit

#define util_set_bit ( orig,
idx )
Value:
( orig | ( 1 << idx ) )

Sets a certain bit and returns the new value.

Parameters
origThe original value.
idxIndex of the bit to set.
Returns
orig with bit idx set.

Function Documentation

◆ clamp_float()

float clamp_float ( float value,
float minimum,
float maximum )

Clamps a floating-point value between minimum and maximum bounds.

Parameters
valueValue to clamp.
minimumMinimum value.
maximumMaximum value.

◆ crc32()

uint32_t crc32 ( const uint8_t * data,
size_t len )

Computes a CRC-32 checksum over a block of data.

Parameters
[in]dataPointer to the input data buffer.
[in]lenNumber of bytes in the buffer.
Returns
The 32-bit CRC checksum of the input data.

◆ eul_to_quat()

QUAT eul_to_quat ( float yaw,
float pitch,
float roll )

Converts ZYX Euler angles to a quaternion.

Note
Angles must be provided in radians.
The rotation order is ZYX (standard aerospace convention).
Todo
The rotation sequence has not been validated yet.
Parameters
yawRotation about the Z axis in radians.
pitchRotation about the Y axis in radians.
rollRotation about the X axis in radians.
Returns
The quaternion representing the rotation.

◆ quat_add()

QUAT quat_add ( QUAT a,
QUAT b )

Adds two quaternions component-wise.

Parameters
aThe first quaternion.
bThe second quaternion.
Returns
The quaternion sum a + b.

◆ quat_conj()

QUAT quat_conj ( QUAT q)

Computes the conjugate of a quaternion.

Parameters
qThe input quaternion.
Returns
The conjugate quaternion (w, -x, -y, -z).

◆ quat_dot()

float quat_dot ( QUAT a,
QUAT b )

Provides the dot product of two quaternions.

Note
Quaternion dot products are commutative.
Parameters
aThe left-hand quaternion.
bThe right-hand quaternion.
Returns
The quaternion scalar product a . b

◆ quat_mult()

QUAT quat_mult ( QUAT a,
QUAT b )

Hamilton product of two quaternions.

Note
Quaternion multiplication is NOT commutative.
Parameters
aThe left-hand quaternion.
bThe right-hand quaternion.
Returns
The quaternion product a * b.

◆ quat_normalize()

QUAT quat_normalize ( QUAT q)

Normalizes a quaternion to unit length.

Parameters
qThe quaternion to normalize.
Returns
The normalized unit quaternion.

Divides each component by the quaternion's norm. If the norm is zero (e.g. a zero-initialized quaternion), returns the identity quaternion (1, 0, 0, 0) to avoid a divide-by-zero. Values near but not equal to zero are divided normally and may exhibit floating-point roundoff.

◆ quat_rotate_body_to_world()

QUAT quat_rotate_body_to_world ( QUAT attitude,
QUAT vector_body )

Rotates a vector from the body frame into the world frame.

The attitude quaternion represents the body-to-world rotation:

vector_world = q * vector_body * conjugate(q)
Parameters
attitudeBody-to-world attitude quaternion.
vector_bodyPure quaternion containing the body-frame vector.
Returns
Pure quaternion containing the world-frame vector.

◆ quat_rotate_world_to_body()

QUAT quat_rotate_world_to_body ( QUAT attitude,
QUAT vector_world )

Rotates a vector from the world frame into the body frame.

The attitude quaternion represents the body-to-world rotation:

vector_body = conjugate(q) * vector_world * q
Parameters
attitudeBody-to-world attitude quaternion.
vector_worldPure quaternion containing the world-frame vector.
Returns
Pure quaternion containing the body-frame vector.

◆ quat_scale()

QUAT quat_scale ( QUAT q,
float s )

Scales a quaternion by a scalar value.

Parameters
qThe quaternion to scale.
sThe scalar factor.
Returns
The quaternion q scaled by s (q * s).