SBB Micro
Source code for the self-balancing-bike microcontroller unit (Teensy 4.1-based). 🚀️
Loading...
Searching...
No Matches
debug.h
Go to the documentation of this file.
1
2#ifndef __DEBUG_H_
3#define __DEBUG_H_
4
10
11//defines
12#define DODEBUG 1
13#define HEADER 0xFF812345
14#define TERMINATOR 0xFF8CABDE
15#define DEBUG_SAMPLING_FAC 10
16#define MAX_MISSING_READS 50
17//#define USE_HOSTPORT //!< Uncomment to use the HostPort library
18
19#if DODEBUG == 1
20
21//variables and objects
22#ifdef USE_HOSTPORT
23
24HostPort hostPort(&Serial, HEADER, TERMINATOR);
25
31struct Debug_rx {
32 float refin0;
33 float refin1;
34} rx_packet;
35
36uint32_t missing_reads = MAX_MISSING_READS;
37
38//functions
39/*
40void start_debug(void) waits for the usb serial for debugging
41*/
42void start_debug(void) {
43 //Serial.begin(12e6) //not necessary with teensy
44 //attach pointers (i.e. RAM address of first byte) of objects to transmit and corresponding size
45 hostPort.attachTx((uint8_t*) &ctrl.controlModel_U, sizeof(ControlClass::ExtU_controlModel_T)); //control input
46 hostPort.attachTx((uint8_t*) &ctrl.controlModel_Y, sizeof(ControlClass::ExtY_controlModel_T)); //control output
47
48 //attach pointers (i.e. RAM address of first byte) of objects to receive and corresponding size
49 hostPort.attachRx((uint8_t*) &rx_packet, sizeof(rx_packet)); //receive rx_packet
50
51 //init rx_packet with NaNs
52 rx_packet.refin0 = *((float*) &nanVal);
53 rx_packet.refin1 = *((float*) &nanVal);
54}
55
56/*
57void do_debug(void) performs debug stuff
58*/
59void do_debug(void) {
60
61 //this is done in the main loop
62 if (missing_reads<MAX_MISSING_READS) { //set the last receive packet until missing_reads<MAX_MISSING_READS and if not NaN
63 if (*((uint32_t*)&rx_packet.refin0) != nanVal) {
64 ctrl.controlModel_U.ref_inputs[0] = rx_packet.refin0;
65 //ctrl.controlModel_Y.curr_ref = rx_packet.curr_ref;
66 //remote_raw.ch[SBUS_EN_CH-1] = TRESHOLD_LOGIC_SBUS;
67 }
68 if (*((uint32_t*)&rx_packet.refin1) != nanVal) {
69 ctrl.controlModel_U.ref_inputs[1] = rx_packet.refin1;
70 //ctrl.controlModel_Y.throttle_ref = rx_packet.throttle_ref;
71 //remote_raw.ch[SBUS_EN_CH-1] = TRESHOLD_LOGIC_SBUS;
72 }
73 }
74
75 if (counters.debug == 0) {
76
77 /*ctrl.controlModel_Y.user_data[1] = missing_reads;
78 if (missing_reads<MAX_MISSING_READS) {
79 if (*((uint32_t*)&rx_packet.curr_ref) != nanVal) ctrl.controlModel_Y.user_data[2] = rx_packet.curr_ref;
80 else ctrl.controlModel_Y.user_data[2] = -1.0F;
81 if (*((uint32_t*)&rx_packet.throttle_ref) != nanVal) ctrl.controlModel_Y.user_data[3] = rx_packet.throttle_ref;
82 else ctrl.controlModel_Y.user_data[3] = -1.0F;
83 }
84 else {
85 ctrl.controlModel_Y.user_data[2] = -1.0F;
86 ctrl.controlModel_Y.user_data[3] = -1.0F;
87 }*/
88
89 //ctrl.controlModel_Y.user_data[1] = timing.dt_cycle;
90 //ctrl.controlModel_Y.user_data[3] = get_selector();
91
92 //write and read
93 uint32_t streamStartTime = micros();
94 hostPort.write(); //write data to debug port - send control input, output and additional user data
95 if (hostPort.read()) missing_reads = 0; //read from debug port - overwrite control output structure)
96 else { if (missing_reads<MAX_MISSING_READS) missing_reads++;} //increase missing reads (up to MAX_MISSING_READS);
97 timing.dt_debug = micros() - streamStartTime;
98
99 //reset counter
100 counters.debug = DEBUG_SAMPLING_FAC - 1;
101 }
102 else counters.debug--;
103}
104
105#else
106
107struct TxData {
108 ControlClass::ExtU_controlModel_T u;
109 ControlClass::ExtY_controlModel_T y;
112 TX_DATA = 0x01,
113 TX_TIME = 0x02,
115};
116const int NUMSIGS = 41;
117const char SPLIT_CHAR = '\0';
118String signames[NUMSIGS] = {
119 "Speed", "Distance",
120 "Acc x", "Acc y", "Acc z",
121 "Gyro x", "Gyro y", "Gyro z",
122 "Mag x", "Mag y", "Mag z",
123 "Latitude", "Longitude", "GPS Speed",
124 "Steer angle", "Steer rate",
125 "Fork disp",
126 "Curr act",
127 "Steer torque",
128 "CPU temp", "Voltage",
129 "Ref input 1", "Ref input 2",
130 "Err state in",
131 "SE switch", "SC switch",
132 "Curr ref", "Throttle",
133 "Roll est", "Yaw est",
134 "Pos est x", "Pos est y",
135 "Dist est", "Speed est",
136 "Steer angle est", "Steer rate est",
137 "Err state out",
138 "User data 1", "User data 2", "User data 3", "User data 4"
139};
140
141void start_debug(void) {
142 //nothing
143}
144
145void do_debug(void) {
146
147 if (USB_CONNECTED) {
148 while (Serial.available()) {
149 serialAction c = (serialAction) Serial.read();
150 switch (c)
151 {
153 //update vals
154 txdata.u = ctrl.controlModel_U;
155 txdata.y = ctrl.controlModel_Y;
156 //txdata.y.user_data[0] = timing.max_tet;
157 //txdata.y.user_data[1] = timing.dt_cycle;
158 //txdata.y.user_data[2] = timing.tet;
159 //txdata.y.user_data[3] = timing.dt_logger;
160 //write data
161 Serial.write((uint8_t*) &txdata, sizeof(txdata));
162 break;
164 {float current_time = ((float) micros()) / 1000000.0F;
165 Serial.write((uint8_t*) &current_time, sizeof(current_time));}
166 break;
168 for (int i = 0; i < NUMSIGS; ++i) {
169 Serial.print(signames[i]);
170 Serial.print(SPLIT_CHAR);
171 }
172 break;
173 default:
174 break;
175 }
176 }
177 }
178}
179
180#endif
181
182#else //empty funs if not debug
183void start_debug(void) {}
184void do_debug(void) {}
185
186//add more empty debug functions here if necessary
187
188#endif
189#endif
A class for communication with PC via USB/serial.
Definition HostPort.h:60
String signames[NUMSIGS]
Names of the signals.
Definition debug.h:118
const int NUMSIGS
Number of signals.
Definition debug.h:116
struct TxData txdata
Data to transmit via Serial.
const char SPLIT_CHAR
Char to split strings.
Definition debug.h:117
serialAction
Definition debug.h:111
@ TX_DATA
Definition debug.h:112
@ TX_SIGNAMES
Definition debug.h:114
@ TX_TIME
Definition debug.h:113
#define DEBUG_SAMPLING_FAC
Sampling factor of data streaming (expressed in units of SAMPLING_TIME).
Definition debug.h:15
#define MAX_MISSING_READS
Maximum consecutive missing readings.
Definition debug.h:16
void start_debug(void)
Initialize the debug.
Definition debug.h:141
#define TERMINATOR
4-bytes terminator of data packet.
Definition debug.h:14
#define HEADER
4-bytes header of data packet.
Definition debug.h:13
void do_debug(void)
Stream and receive signals.
Definition debug.h:145
#define USB_CONNECTED
Macro for checking the USB connection status-.
Definition config.h:311
Timing timing
Timing of functions.
Definition objects.h:223
ControlClass ctrl
Control object.
Definition objects.h:242
Counter counters
Counters for lower sampling rate.
Definition objects.h:224
uint32_t nanVal
NaN variable.
Definition objects.h:220
Definition debug.h:107
ControlClass::ExtU_controlModel_T u
Definition debug.h:108
ControlClass::ExtY_controlModel_T y
Definition debug.h:109