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

: Contains functions to interface between SDEC terminal commands and SDR sensor APIs More...

#include <string.h>
#include <stdbool.h>
#include <math.h>
#include "sdr_pin_defines_A0002.h"
#include "main.h"
#include "imu.h"
#include "baro.h"
#include "timer.h"
#include "usb.h"
#include "sensor.h"
#include "math_sdr.h"
#include "mahony.h"
#include "error_sdr.h"
#include "debug_sdr.h"

Macros

#define SENSOR_MAHONY_KP   1.0f
#define SENSOR_MAHONY_KI   0.0f
#define BARO_PRESS_ALPHA   (0.7f)
#define BARO_TEMP_ALPHA   (0.7f)

Functions

SENSOR_STATUS sensor_cmd_execute (uint8_t subcommand)
 Executes a sensor subcommand and transmits the requested readings.
SENSOR_STATUS sensor_dump (SENSOR_DATA *sensor_data_ptr)
 Reads the available sensors and fills the sensor data structure.
SENSOR_STATUS sensor_init (PRESET_DATA *preset_data)
 Initializes sensor timing and resets velocity state.
void sensor_conv_imu (IMU_CONVERTED *imu_converted, IMU_RAW *imu_raw)
 Converts raw IMU readings into calibrated, remapped sensor data.
MOUNT_ORIENTATION sensor_get_mount_orientation (void)
 Get the mount orientation.
void sensor_set_mount_orientation (MOUNT_ORIENTATION orientation)
 Set the mount orientation.
SENSOR_STATUS sensor_body_state (const IMU_CONVERTED *imu_converted, STATE_ESTIMATION *state_estimate)
 Integrates gyro data to update the estimated body attitude and rate.
void sensor_axis_remap (float *x, float *y, float *z)
 Remaps sensor axes according to the flight-computer mounting orientation.
float sensor_acc_conv (int16_t readout)
 Converts a raw accelerometer reading to meters per second squared.
float sensor_gyro_conv (int16_t readout)
 Converts a raw gyro reading to degrees per second.
void sensor_imu_velo (const IMU_CONVERTED *imu_converted, STATE_ESTIMATION *state_estimate)
 Calculates velocity from converted accelerometer measurements.
void sensor_baro_alt (SENSOR_DATA *sen_data)
 Calculates altitude from the current barometric pressure and temperature.
void sensor_reset_velo (void)
 Resets accumulated velocity values to prevent drift.

Variables

GPS_DATA gps_data
IMU_OFFSET imu_offset
uint64_t imu_velo_tick = 0
float velo_x_prev = 0.0f
float velo_y_prev = 0.0f
float velo_z_prev = 0.0f
QUAT attitude = { 1.0f, 0.0f, 0.0f, 0.0f }

Detailed Description

: Contains functions to interface between SDEC terminal commands and SDR sensor APIs

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

Function Documentation

◆ sensor_acc_conv()

float sensor_acc_conv ( int16_t readout)

Converts a raw accelerometer reading to meters per second squared.

Converts accelerometer readouts to meters per second squared.

Parameters
readoutRaw accelerometer readout.
Returns
Acceleration in meters per second squared.

Converts a raw accelerometer reading to meters per second squared.

Parameters
readoutRaw accelerometer readout.
Returns
Acceleration in meters per second squared.

◆ sensor_axis_remap()

void sensor_axis_remap ( float * x,
float * y,
float * z )

Remaps sensor axes according to the flight-computer mounting orientation.

Remaps sensor axes for the flight configuration.

Parameters
xX-axis value to remap.
yY-axis value to remap.
zZ-axis value to remap.

Remaps sensor axes according to the flight-computer mounting orientation.

Parameters
xX-axis value to remap.
yY-axis value to remap.
zZ-axis value to remap.

◆ sensor_baro_alt()

void sensor_baro_alt ( SENSOR_DATA * sensor_data_ptr)

Calculates altitude from the current barometric pressure and temperature.

Calculates altitude from pressure readings.

Parameters
sen_dataSensor data structure to update.

Calculates altitude from the current barometric pressure and temperature.

Parameters
sensor_data_ptrSensor data structure to update.

◆ sensor_body_state()

SENSOR_STATUS sensor_body_state ( const IMU_CONVERTED * imu_converted,
STATE_ESTIMATION * state_estimate )

Integrates gyro data to update the estimated body attitude and rate.

Performs sensor fusion on converted IMU data to get body rate.

Parameters
imu_convertedConverted IMU data.
state_estimateState estimate to update.

Integrates gyro data to update the estimated body attitude and rate.

Parameters
imu_convertedConverted IMU data.
state_estimateState estimate to update.

◆ sensor_cmd_execute()

SENSOR_STATUS sensor_cmd_execute ( uint8_t subcommand)

Executes a sensor subcommand and transmits the requested readings.

Executes a sensor subcommand.

Parameters
subcommandSensor subcommand code.
Returns
Sensor operation status.

Executes a sensor subcommand and transmits the requested readings.

Parameters
subcommandSensor subcommand code.
Returns
Sensor operation status.

◆ sensor_conv_imu()

void sensor_conv_imu ( IMU_CONVERTED * imu_converted,
IMU_RAW * imu_raw )

Converts raw IMU readings into calibrated, remapped sensor data.

Converts raw IMU readouts into accelerometer, gyro, and magnetometer data.

Parameters
imu_convertedConverted IMU data to fill.
imu_rawRaw IMU readouts.

Converts raw IMU readings into calibrated, remapped sensor data.

Parameters
imu_convertedConverted IMU data to fill.
imu_rawRaw IMU readouts.

◆ sensor_dump()

SENSOR_STATUS sensor_dump ( SENSOR_DATA * sensor_data_ptr)

Reads the available sensors and fills the sensor data structure.

Reads all sensors and fills the sensor data structure.

Parameters
sensor_data_ptrPointer to the sensor data structure to fill.
Returns
Sensor operation status.

Reads the available sensors and fills the sensor data structure.

Parameters
sensor_data_ptrPointer to the sensor data structure to fill.
Returns
Sensor operation status.

◆ sensor_get_mount_orientation()

MOUNT_ORIENTATION sensor_get_mount_orientation ( void )

Get the mount orientation.

Gets the configured flight-computer mount orientation.

Returns
The configured mount orientation

Get the mount orientation.

Returns
The configured mount orientation.

◆ sensor_gyro_conv()

float sensor_gyro_conv ( int16_t readout)

Converts a raw gyro reading to degrees per second.

Converts gyro readouts to degrees per second.

Parameters
readoutRaw gyro readout.
Returns
Angular rate in degrees per second.

Converts a raw gyro reading to degrees per second.

Parameters
readoutRaw gyro readout.
Returns
Angular rate in degrees per second.

◆ sensor_imu_velo()

void sensor_imu_velo ( const IMU_CONVERTED * imu_converted,
STATE_ESTIMATION * state_estimate )

Calculates velocity from converted accelerometer measurements.

Calculates velocity from acceleration.

Parameters
imu_convertedConverted IMU data.
state_estimateState estimate to update.

Calculates velocity from converted accelerometer measurements.

Parameters
imu_convertedConverted IMU data.
state_estimateState estimate to update.

◆ sensor_init()

SENSOR_STATUS sensor_init ( PRESET_DATA * preset_data)

Initializes sensor timing and resets velocity state.

Initializes sensor timing, velocity, and attitude state.

Parameters
preset_dataPointer to the preset calibration data.

Initializes sensor timing and resets velocity state.

Parameters
preset_dataPointer to the preset calibration data.

◆ sensor_reset_velo()

void sensor_reset_velo ( void )

Resets accumulated velocity values to prevent drift.

Resets velocity values to prevent accumulation of drift.

Resets accumulated velocity values to prevent drift.

◆ sensor_set_mount_orientation()

void sensor_set_mount_orientation ( MOUNT_ORIENTATION orientation)

Set the mount orientation.

Sets the flight-computer mount orientation.

Parameters
orientationThe new mount orientation

Set the mount orientation.

Parameters
orientationMount orientation to use for axis remapping.