Common function reference

common.h

Defines

ROCKETLIB_VERSION_MAJOR

Rocketlib major release number, shall be the design cycle year.

ROCKETLIB_VERSION_MINOR

Rocketlib minor release number, shall be the RocketCAN protocol version.

ROCKETLIB_VERSION_PATCH

Rocketlib patch release number, shall be the n-th revision of this library that does not involve RocketCAN protocol change.

STATIC_ASSERT(expr, msg)
w_assert(statement)

Assertion.

Works just like standard C assertion, except use can define custom assertion failure handler(see w_assert_fail), only active when W_DEBUG is defined

Typedefs

typedef float float32_t
typedef double float64_t

Enums

enum w_status_t

Standard error code.

Values:

enumerator W_SUCCESS

Success.

enumerator W_FAILURE

Failure, if not one of the more specific case below.

enumerator W_INVALID_PARAM

Illegal function call arguments/parameters.

enumerator W_IO_ERROR

IO error, for example no SD card was plugged in.

enumerator W_IO_TIMEOUT

IO timeout, for example SD card takes too long to respond.

enumerator W_MATH_ERROR

Math error, for example divide by zero.

enumerator W_OVERFLOW

Buffer overflow, for example trying to write/push to a full FIFO buffer.

enumerator W_UNDERFLOW

Buffer underflow, for example trying to read/pop from an empty FIFO buffer.

enumerator W_DATA_FORMAT_ERROR

Data format error, for example input is a malformed CAN packet.

enumerator W_NO_DATA

Succeed, but no data available.

Functions

void w_assert_fail(const char *file, int line, const char *statement)

Assertion fail handler.

Assertion fail handler, shall be implemented in the firmware, only used if W_DEBUG is defined

Parameters:
  • file – Path to C source file contains assertion failure

  • line – C source line number of assertion failure

  • statement – Statement caused assertion failure

crc8.h

Functions

uint8_t crc8_checksum(const uint8_t *pdata, size_t nbytes, uint8_t crc)

Compute CRC8.

Computes the CRC8 checksum of input data

Parameters:
  • pdata – Input data buffer

  • nbytes – Buffer size in bytes

  • crc – CRC8 checksum of previous CRC8 calculation if calculating checksum on multiple discontinuous buffers, otherwise 0

Returns:

Computed CRC8 checksum

electrical.h

Defines

RAIL_12V_MAX_mV
RAIL_12V_MIN_mV
INA180A3_GAIN

low_pass_filter.h

Low-pass filter implementation.

This module provides a low-pass filter implementation for smoothing sensor readings and reducing noise in analog signals. The filter uses an exponential moving average algorithm with configurable response time.

Functions

static inline double sample_freq(double time_diff_ms)

Calculate the sample frequency based on the time difference in milliseconds.

Parameters:

time_diff_ms – Time difference between samples in milliseconds

Returns:

double Sample frequency in Hz

static inline double low_pass_alpha(double TR, double time_diff_ms)

Calculate the alpha value for the low-pass filter based on the response time.

The alpha value determines the filter’s responsiveness. A higher alpha value results in faster response but less filtering, while a lower alpha value provides more filtering but slower response.

Parameters:
  • TR – Response time constant in milliseconds

  • time_diff_ms – Time difference between samples in milliseconds

Returns:

double Alpha value (0.0 to 1.0) for the low-pass filter

w_status_t low_pass_filter_init(double *alpha, double response_time)

Initializes the low-pass filter by calculating and storing the alpha value.

This function calculates the alpha value based on the given response time and stores it in the provided pointer. The alpha value is used in subsequent filter updates.

Parameters:
  • alpha – Pointer to store the calculated alpha value

  • response_time – Response time constant in milliseconds (must be > 0)

Returns:

w_status_t Returns W_SUCCESS on success, W_INVALID_PARAM if alpha is NULL or response_time is invalid

w_status_t update_low_pass(double alpha, uint16_t new_input_value, double *low_pass_value)

Updates the low-pass filter with a new value and returns the operation status.

This function applies the low-pass filter algorithm to the new input value using the provided alpha value. The filtered result is stored in low_pass_value and updated in-place for the next iteration.

Parameters:
  • alpha – Alpha value (0.0 to 1.0) for the low-pass filter

  • new_input_value – New input value to filter

  • low_pass_value – Pointer to the current filtered value (updated in-place)

Returns:

w_status_t Returns W_SUCCESS on success, W_INVALID_PARAM if low_pass_value is NULL or alpha is out of valid range (0.0 to 1.0)

mathops.h

Functions

static inline uint32_t value_clamp_uint32(uint32_t x, uint32_t low, uint32_t high)

Clamps a 32 bit unsigned integer between a lower and upper bound.

Parameters:
  • x – The value to clamp

  • low – The lower bound, inclusive

  • high – The upper bound, inclusive

Returns:

The clamped value

static inline int32_t value_clamp_int32(int32_t x, int32_t low, int32_t high)

Clamps a 32 bit signed integer between a lower and upper bound.

Parameters:
  • x – The value to clamp

  • low – The lower bound, inclusive

  • high – The upper bound, inclusive

Returns:

The clamped value

static inline float32_t value_clamp_float32(float32_t x, float32_t low, float32_t high)

Clamps a 32 bit float between a lower and upper bound.

Parameters:
  • x – The value to clamp

  • low – The lower bound, inclusive

  • high – The upper bound, inclusive

Returns:

The clamped value