Loading...
Searching...
No Matches
debug_gyroscope.c
1/***********************************************************************************
2 * @file debug_gyroscope.c *
3 * @author Matt Ricci *
4 * @addtogroup Shell *
5 * *
6 * @{ *
7 ***********************************************************************************/
8
9#include "string.h"
10#include "stdio.h"
11
12#include "shell.h"
13#include "parser.h"
14#include "state.h"
15#include "devicelist.h"
16#include "gyroscope.h"
17
18static void Gyro_exec(UART_t *uart, char *);
19
20DEFINE_PROGRAM_HANDLE("gyro", Gyro_exec, NULL)
21
22/* =============================================================================== */
29static void Gyro_exec(UART_t *uart, char *flags) {
30
31 ArgParser parser = ArgParser_init();
32 int argPrintIdx = parser.addArg(
33 &parser, "print", 0, ARG_TYPE_BOOL, false
34 );
35 int argResetIdx = parser.addArg(
36 &parser, "reset", 0, ARG_TYPE_BOOL, false
37 );
38 int argRepeatIdx = parser.addArg(
39 &parser, "--repeat", 'r', ARG_TYPE_BOOL, false
40 );
41 int argVerboseIdx = parser.addArg(
42 &parser, "--verbose", 'v', ARG_TYPE_BOOL, false
43 );
44 int argBiasIdx = parser.addArg(
45 &parser, "bias", 0, ARG_TYPE_STRING, false
46 );
47
48 char *tokens[MAX_ARGS];
49 int numTokens = 0;
50
51 // Tokenize the input string
52 char *token = strtok(flags, " ");
53 while (token != NULL && numTokens < MAX_ARGS) {
54 tokens[numTokens++] = token;
55 token = strtok(NULL, " ");
56 }
57
58 // Parse input tokens
59 parser.parseArgs(&parser, numTokens, tokens);
60
61 // Early exit with error message
62 if (parser.error.status == PARSER_STATUS_ERROR) {
63 uart->println(uart, parser.error.msg);
64 return;
65 }
66
67 Gyro_t *gyro = DeviceList_getDeviceHandle(DEVICE_GYRO).device;
68
69 // Print requested state variable value
70 if (parser.args[argPrintIdx].provided) {
71 State *state = State_getState();
72
73 char *terminal = (parser.args[argRepeatIdx].provided) ? "\r" : "\r\n";
74
75 REPEAT:
76 if (parser.args[argVerboseIdx].provided) {
77 // Print timestamp
78 char timestamp[20];
79 snprintf(
80 timestamp, sizeof(timestamp), "[t=%fs] ",
81 state->flightTimeMs / 1000.0f
82 );
83 uart->print(uart, timestamp);
84 }
85
86 char str[50];
87 snprintf(
88 str, 50, "X: % -10.4f Y: % -10.4f Z: % -10.4f%s",
89 gyro->gyroData[0],
90 gyro->gyroData[1],
91 gyro->gyroData[2],
92 terminal
93 );
94 uart->print(uart, str);
95
96 // Continue until interrupted if specified
97 if (parser.args[argRepeatIdx].provided)
98 goto REPEAT;
99 }
100
101 // Reset bias parameters
102 else if (parser.args[argResetIdx].provided) {
103 gyro->bias[0] = 0;
104 gyro->bias[1] = 0;
105 gyro->bias[2] = 0;
106 gyro->gyroData[0] = 0;
107 gyro->gyroData[1] = 0;
108 gyro->gyroData[2] = 0;
109 gyro->rawGyroData[0] = 0;
110 gyro->rawGyroData[1] = 0;
111 gyro->rawGyroData[2] = 0;
112 gyro->rawGyroData[3] = 0;
113 gyro->rawGyroData[4] = 0;
114 gyro->rawGyroData[5] = 0;
115 uart->println(uart, "Reset gyroscope parameters.");
116 }
117
118 else {
119 uart->println(uart, parser.error.msg);
120 }
121}
122
float * gyroData
Processed angular rates array.
Definition gyroscope.h:67
uint8_t * rawGyroData
Raw angular rates array.
Definition gyroscope.h:66
float * bias
Bias offset array.
Definition gyroscope.h:68
DeviceHandle_t DeviceList_getDeviceHandle(DeviceKey)
Retrieve device handle from list by key.
Definition devicelist.c:36
State * State_getState()
Definition state.c:69
State variable struct.
Definition state.h:36
Struct definition for UART interface.
Definition uart.h:132
Defines the API for the Gyroscope sensor.