Siphon Regulator 1.0
Nanosattelite attitude determination and control system.
Loading...
Searching...
No Matches
sensor_fcn.c
Go to the documentation of this file.
1/*
2 * sensor_fcn library
3 * (c) Antonin Putala 2026
4 *
5 * Developed using STM32CubeIDE
6 * Tested on BluePill board and STM32F103C8T6, 32 MHz.
7 */
8
9/* Includes -----------------------------------------------*/
10#include "sensor_fcn.h"
11#include "sensor_fcn_def.h"
12#include "main.h"
14#include <stdio.h>
15
16/* Global functions ---------------------------------------*/
17/*
18 * Function: sensor_init
19 * Purpose: It initiates communication with the sensor.
20 * Input(s): gyr_offset - gyroscope offset obtained
21 * repeated measurement
22 * Returns: none
23 */
24void sensor_init(float * gyr_offset)
25{
26 /* For SPI interface set MPU9250_INTERFACE_SPI.
27 * Address only for I2C, library requires it.
28 */
30 if (res != 0)
31 {
32 printf("Sensor error!\n");
33 }
34
35 /* Pause for stabilization */
36 HAL_Delay(200);
37
38 sensor_calibration(gyr_offset);
39
40}
41
42/*
43 * Function: sensor_get_value
44 * Purpose: It handles reading the value from the sensor.
45 * Input(s): gyr_offset - gyroscope offset obtained
46 * repeated measurement
47 * Returns: none
48 */
49void sensor_get_value(float * gyr_offset)
50{
51 static uint32_t tick = 0;
52
53 if (HAL_GetTick() > tick)
54 {
55 float acc[3];
56 float gyr[3];
57 float mag[3];
58
59 tick = HAL_GetTick() + MEAS_TIME;
60
61 if (mpu9250_basic_read(acc, gyr, mag) == 0)
62 {
63 for (uint8_t i = 0; i < 3; i++)
64 {
65 measured_data.acc[i] = acc[i];
66
67 /* The gyroscope data are corrected using the calibration constant. */
68 measured_data.gyr[i] = gyr[i] - gyr_offset[i];
69
70 measured_data.mag[i] = mag[i];
71
72 /* CAL_MUL enables manual calibration of orientation measurement. */
73 measured_data.pos[i] += (measured_data.gyr[i] * MEAS_TIME * CAL_MUL) / 1000;
74
75 /* Get format -180° ÷ 180°. */
77 }
78 }
79 }
80}
81
82/*
83 * Function: sensor_deg_limit
84 * Purpose: It normalizes the orientation value to the –180° to +180° range.
85 * Input(s): p_deg - gyroscope offset obtained repeated measurement
86 * Returns: none
87 */
88void sensor_deg_limit(float * p_deg)
89{
90 *p_deg += (*p_deg < -180) ? 360: 0;
91 *p_deg -= (*p_deg > 180) ? 360: 0;
92}
93
94/* Static functions ---------------------------------------*/
95/*
96 * Function: sensor_calibration
97 * Purpose: It performs the computation of the gyroscope offset.
98 * Input(s): gyr_offset - gyroscope offset obtained
99 * repeated measurement
100 * Returns: none
101 */
102static void sensor_calibration(float * gyr_offset)
103{
104 /* Temporary variables for calibration */
105 float acc[3];
106 float gyr[3];
107 float mag[3];
108
109 float offset[3] = {0};
110 uint8_t i;
111
112 /* A total of 100 measurements are taken. */
113 for (i = 0; i < CAL_CYC_NUM; i++)
114 {
115 if (mpu9250_basic_read(acc, gyr, mag) == 0)
116 {
117 for (uint8_t j = 0; j < 3; j++)
118 {
119 offset[j] += gyr[j];
120 }
121 }
122
123 HAL_Delay(CAL_TIME);
124 }
125
126 /* Only successful samples are included in the computation, i.e.,
127 * the sum is divided by the number of valid measurements. */
128 for (uint8_t j = 0; j < 3; j++)
129 {
130 gyr_offset[j] = (offset[j] / i);
131 }
132}
133
driver mpu9250 basic header file
sensor_data_t measured_data
Current sensor measured data.
Definition main.c:60
#define MEAS_TIME
void sensor_get_value(float *gyr_offset)
It handles reading the value from the sensor.
Definition sensor_fcn.c:49
void sensor_init(float *gyr_offset)
It initiates communication with the sensor.
Definition sensor_fcn.c:24
#define CAL_CYC_NUM
#define CAL_TIME
void sensor_deg_limit(float *p_deg)
It normalizes the orientation value to the –180° to +180° range.
Definition sensor_fcn.c:88
@ MPU9250_INTERFACE_SPI
uint8_t mpu9250_basic_init(mpu9250_interface_t interface, mpu9250_address_t addr_pin)
basic example init
uint8_t mpu9250_basic_read(float g[3], float dps[3], float ut[3])
basic example read
: Header for main.c file. This file contains the common defines of the application.
#define CAL_MUL
Definition main.h:204
static void sensor_calibration(float *gyr_offset)
Definition sensor_fcn.c:102
float pos[3]
Orientation X, Y, Z [deg]; related to initial position.
Definition main.h:82
float gyr[3]
Gyroscope X, Y, Z [dps].
Definition main.h:80
float mag[3]
Magnetometer X, Y, Z [uT].
Definition main.h:81
float acc[3]
Accelerometer X, Y, Z [g].
Definition main.h:79