SBB Micro
Source code for the self-balancing-bike microcontroller unit (Teensy 4.1-based). 🚀️
Loading...
Searching...
No Matches
userfun.h
Go to the documentation of this file.
1
2#ifndef __FUNCTIONS_H__
3#define __FUNCTIONS_H__
4
11
12/*
13void set_buses(void) starts the IO buses
14*/
15void start_IObus(void) {
16 USED_SPI.begin(); //start sensor SPI
17 //start imu, mag, and dac
18 iostatus.imu = imu.begin() == ISM330DHCX_OK;
19 iostatus.mag = mag.begin() == LIS3MDL_STATUS_OK;
20 dac.begin(); iostatus.dac = true;
21
22 //start speed-sensor UART
23 UART_SPEEDSENS.begin(BAUD_SPEEDSENS); iostatus.speedsens = (bool) UART_SPEEDSENS;
25 //CCM_CSCDR1 = 105450240; //UART_CLK_SEL bit set to 0 - to use baudrate of serial above 6e6 Mbit/s, see https://forum.pjrc.com/threads/67150-Teensy4-1-MAX-baud-rate
26 //start gps UART
27 UART_GPS.begin(BAUD_GPS_DEF); iostatus.gps = (bool) UART_GPS;
29 /*delay(66);
30 gps.sendCommand(PMTK_SET_BAUD_57600), delay(66);
31 UART_GPS.end(), delay(66);
32 UART_GPS.begin(BAUD_GPS);*/
33 //start sbus
34#if EN_REMOTE_CTRL==1
35 iostatus.sbus = sbus.Begin(); //This inclues begin of the #UART_SBUS
36#endif
37}
38
39/*
40void set_IObus(void) sets the IO (sensors, ADC, DAC, etc.)
41*/
42void set_IObus(void) {
43 //enable sensors
44 iostatus.accen = imu.ACC_Enable() == ISM330DHCX_OK;
45 iostatus.gyroen = imu.GYRO_Enable() == ISM330DHCX_OK;
46 iostatus.magen = mag.Enable() == LIS3MDL_STATUS_OK;
47 gps.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
48
49 //set sensor rates
50 imu.GYRO_SetOutputDataRate(GYRO_ODR);
51 imu.ACC_SetOutputDataRate(ACC_ODR);
52 mag.SetODR(MAG_ODR);
53 gps.sendCommand(PMTK_SET_NMEA_UPDATE_10HZ);
54
55 //set adc
56 adc->adc0->setAveraging(ADC_AVERAGE); // set number of averages
57 adc->adc0->setResolution(ADC_RES); // set bits of resolution
58 adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed
59 adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // change the sampling speed
60 adc->adc1->setAveraging(ADC_AVERAGE); // set number of averages
61 adc->adc1->setResolution(ADC_RES); // set bits of resolution
62 adc->adc1->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed
63 adc->adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // change the sampling speed
64
65 //set pwm
66#if MTR_CTRL_MODE==0
67 analogWriteResolution(PWM_RES);
68 analogWriteFrequency(PWM_PIN, PWM_FREQ);
69 analogWrite(PWM_PIN, CONVERT_CURRENT_TO_PWM(0));
70#endif
71 //set dac
72 dac.analogWrite(DAC_MTR_CH, CONVERT_CURRENT_TO_DAC(0));
73 dac.analogWrite(DAC_THROTTLE_CH, 0);
74}
75
76/*
77void do_zeros(void) does the zero of sensor with offset (like torsiometer and gyro)
78*/
79void do_zeros(void) {
80 //inits
81 uint16_t c = 0; //counter
82 riderTorque_raw.riderTorque_offset = 0;
83 actCurr_raw.actCurr_offset = 0;
84 steer_raw.steerVel_offset = 0;
85 //cycle with SAMPLING_TIME
86 while (c < NUM_ZERO_SAMPLES) { //do NUM_ZERO_SAMPLES cycles
87 if ((micros() - sampling_timer) >= SAMPLING_TIME) {
88 sampling_timer = micros(); //update timer
89 //gyro
90 //...
91 //torsiometer
92 riderTorque_raw.riderTorque_offset += CONVERT_RIDERTRQ_TO_NM(float(analogRead(RIDER_TRQ)));
93 //actual motor current
94 actCurr_raw.actCurr_offset += CONVERT_ACTCURR_TO_A(float(analogRead(ACT_CURR)));
95 //motor speed
96 steer_raw.steerVel_offset += CONVERT_STEERVEL_TO_RADPS(float(analogRead(STEER_SPEED)));
97 //fork displacement
98 //forkDisp_raw.forkDisp_offset += CONVERT_FORKDISP_TO_MM(float(analogRead(FORK_DISP)));
99
100 //increase the counter
101 c++;
102
103 }
104 }
105 //compute averages
106 riderTorque_raw.riderTorque_offset = riderTorque_raw.riderTorque_offset / float(NUM_ZERO_SAMPLES);
107 actCurr_raw.actCurr_offset = actCurr_raw.actCurr_offset / float(NUM_ZERO_SAMPLES);
108 steer_raw.steerVel_offset = steer_raw.steerVel_offset / float(NUM_ZERO_SAMPLES);
109 voltage_raw.batVolt_offset = 0;
110 forkDisp_raw.forkDisp_offset = FULLEXT_FORKDISP; // forkDisp_raw.forkDisp_offset / float(NUM_ZERO_SAMPLES);
111 imuData_raw.gyros_offset[0] = GYROX_OFFSET;
112 imuData_raw.gyros_offset[1] = GYROY_OFFSET;
113 imuData_raw.gyros_offset[2] = GYROZ_OFFSET;
114}
115
116/*
117void set_GPIO(void) sets the GPIO pins
118*/
119void set_GPIO(void) {
120 pinMode(MTR_EN_PIN, OUTPUT);
121 pinMode(MTR_DIR_PIN, OUTPUT);
122 pinMode(RELAY_EN_PIN, OUTPUT);
123 pinMode(LED_BUILTIN, OUTPUT);
124 pinMode(ONOFF_STATE_PIN, INPUT_PULLUP);
125 pinMode(BR_SLEEP_PIN, OUTPUT);
126 digitalWriteFast(MTR_EN_PIN, !MTR_EN_STATE);
127 digitalWriteFast(MTR_DIR_PIN, CW);
128 digitalWriteFast(RELAY_EN_PIN, !RELAY_EN_STATE);
129 digitalWriteFast(BR_SLEEP_PIN, !BR_SLEEP_STATE);
130}
131
132/*
133void start_brake_stepper(void) starts the stepper motor
134*/
136 TS4::begin(); //begin the teensy stepper library
139}
140
141/*
142void get_sensors(void) gets the sensor readings
143*/
144void get_sensors(void) {
145 //imu
146 if (counters.imu == 0) {
147 imu.ACC_GetAxes(imuData_raw.accs_mg);
148 imu.GYRO_GetAxes(imuData_raw.gyros_mdps);
149 counters.imu = IMU_SAMPLING_FAC - 1;
150 }
151 else counters.imu--;
152
153 //mag
154 if (counters.mag == 0) {
155 mag.GetAxes(imuData_raw.mags_mG);
156 counters.mag = MAG_SAMPLING_FAC - 1;
157 }
158 else counters.mag--;
159
160 //speed
161 if (counters.speedSensor == 0) {
162 if (speedSensor.available()) speedSensor.rxObj(speed_raw);
163 counters.speedSensor = SPEED_SAMPLING_FAC - 1;
164 }
165 else counters.speedSensor--;
166
167 //gps
168 gps.read();
169 if (gps.newNMEAreceived()) {
170 gps.parse(gps.lastNMEA());
171 gpsData_raw.fix = gps.fix;
172 if (gpsData_raw.fix) {
173 gpsData_raw.lat = gps.latitudeDegrees;
174 gpsData_raw.lon = gps.longitudeDegrees;
175 gpsData_raw.speed = gps.speed*GPSSPEED_SCALE;
176 if (gps.lat == 'S') gpsData_raw.lat *= -1;
177 if (gps.lon == 'W') gpsData_raw.lon *= -1;
178 }
179 else {
180 gps.lat = 0;
181 gps.lon = 0;
182 gps.speed = 0;
183 }
184 }
185
186 //analogs
187 actCurr_raw.actCurr = analogRead(ACT_CURR);
188 riderTorque_raw.riderTorque = analogRead(RIDER_TRQ);
189 steer_raw.steerPos = analogRead(STEER_POS);
190 steer_raw.steerVel = analogRead(STEER_SPEED);
191 voltage_raw.batVolt = analogRead(VOLTAGE);
192 forkDisp_raw.forkDisp = analogRead(FORK_DISP);
193
194 //sbus
195#if EN_REMOTE_CTRL==1
196 if (sbus.Read()) {
197 if (check_sbus()) { //success!!!
198 sbus.ch(remote_raw.ch, NUM_CH_SBUS*sizeof(int16_t)); //read channels
199 remote_raw.missing_frame = 0; //reset missing frame
200 } else remote_raw.missing_frame++; //increment missing frame
201 } else remote_raw.missing_frame++; //increment missing frame
202 if (remote_raw.missing_frame >= MAX_MISSING_SBUS) { //set zero values to
203 remote_raw.missing_frame = MAX_MISSING_SBUS; //set to #MAX_MISSING_SBUS to avoid overflow
204 for (uint8_t i = 0; i < NUM_CH_SBUS; ++i) remote_raw.ch[i] = ZERO_SBUS; //set channel to zero
205 }
206#endif
207}
208
209/*
210bool check_sbus(void) check for SBUS errors
211*/
212bool check_sbus(void) {
213 if (sbus.lost_frame()) { return false; }
214 if (sbus.failsafe()) { return false; }
215 for (uint8_t i = 0; i < NUM_CH_SBUS; ++i) {
216 if ((sbus.ch(i) < MIN_SBUS) || (sbus.ch(i) > MAX_SBUS)) { return false; } //return false as soon as an unfeasible value is found
217 }
218 return true; //all checks ok
219}
220
221/*
222void set_ctrl_input(void) sets the control input structure
223*/
224void set_ctrl_input(void) {
225 //set speed
226 if (speed_raw.speed >= -1) ctrl.controlModel_U.speed = SPEED_SCALE*float(speed_raw.speed); // Avoid negative values
227 ctrl.controlModel_U.dist = DIST_SCALE * float(speed_raw.dist);
228 //set the threee components of accs, gyros, mags
229 for (int i = 0; i < 3; i++) {
230 ctrl.controlModel_U.accs[i] = float(imuData_raw.accs_mg[0] * imu_rotMat[i][0] +
231 imuData_raw.accs_mg[1] * imu_rotMat[i][1] +
232 imuData_raw.accs_mg[2] * imu_rotMat[i][2]) * ACC_SCALE;
233 ctrl.controlModel_U.gyros[i] = ((float(imuData_raw.gyros_mdps[0]) - imuData_raw.gyros_offset[0]) * imu_rotMat[i][0] +
234 (float(imuData_raw.gyros_mdps[1]) - imuData_raw.gyros_offset[1]) * imu_rotMat[i][1] +
235 (float(imuData_raw.gyros_mdps[2]) - imuData_raw.gyros_offset[2]) * imu_rotMat[i][2] ) * GYRO_SCALE; //may have also offset
236 ctrl.controlModel_U.mags[i] = float(imuData_raw.mags_mG[0] * mag_rotMat[i][0] +
237 imuData_raw.mags_mG[1] * mag_rotMat[i][1] +
238 imuData_raw.mags_mG[2] * mag_rotMat[i][2]) * MAG_SCALE;
239 }
240 //set gps
241 ctrl.controlModel_U.gps[0] = gpsData_raw.lat;
242 ctrl.controlModel_U.gps[1] = gpsData_raw.lon;
243 ctrl.controlModel_U.gps[2] = gpsData_raw.speed;
244 //set front fork displacement
245 ctrl.controlModel_U.forkdisp = CONVERT_FORKDISP_TO_MM(float(forkDisp_raw.forkDisp)) - forkDisp_raw.forkDisp_offset;
246 //set rider torque
247 ctrl.controlModel_U.torque = CONVERT_RIDERTRQ_TO_NM(float(riderTorque_raw.riderTorque)) - riderTorque_raw.riderTorque_offset;
248 //set steer position and velocity
249 ctrl.controlModel_U.steer[0] = CONVERT_STEERPOS_TO_RAD(float(steer_raw.steerPos));
250 ctrl.controlModel_U.steer[1] = CONVERT_STEERVEL_TO_RADPS(float(steer_raw.steerVel)) - steer_raw.steerVel_offset;
251 //set actual motor current
252 ctrl.controlModel_U.curr = CONVERT_ACTCURR_TO_A(float(actCurr_raw.actCurr)) - actCurr_raw.actCurr_offset;
253 //set battery voltage
254 ctrl.controlModel_U.voltage = CONVERT_VOLTAGE_TO_V(float(voltage_raw.batVolt)) - voltage_raw.batVolt_offset;
255 //CPU temp
256 ctrl.controlModel_U.CPUTemp = tempmonGetTemp();
257 //set remote control (2 channels) sbus
258#if EN_REMOTE_CTRL==1
259 ctrl.controlModel_U.ref_inputs[0] = CONVERT_CHANNEL_TO_FLOAT(remote_raw.ch[SBUS_ROLL_CH-1], MIN_REF_INPUT, MAX_REF_INPUT);
261#endif
262}
263
264/*
265void do_control(void) performs control update cycle
266*/
267void do_control(void) {
268
269 //tests
270 #ifdef IMP_TEST
272 if (enable_old != enable_new) {
273 if (enable_new) test_start = millis();
274 }
276 test_timer = millis() - test_start;
277 ctrl.controlModel_Y.curr_ref = 0;
278 if (enable_new != 0 && (test_timer <= IMP_DUR)) {
279 ctrl.controlModel_Y.curr_ref = 0.5F*IMP_AMP*(1.0F - cosf(2*PI*float(test_timer)/float(IMP_DUR)))*float(enable_new);
280 }
281 return;
282 #endif
283
284 #ifdef SWEEP_TEST
286 if (enable_old != enable_new) {
287 if (enable_new) test_start = millis();
288 }
290 test_timer = millis() - test_start;
291 if (enable_new) {
292 if (test_timer <= TEST_DURATION) {
293 float freq = 0.0F + float(test_timer)/float(TEST_DURATION) * (10.0F-0.0F);
294 float ampl = 2.0F;
295 ctrl.controlModel_Y.curr_ref = ampl*sinf(2.0F*PI*freq*float(test_timer)/1000.0F/2.0F);
296 } else ctrl.controlModel_Y.curr_ref = 0, remote_raw.ch[SBUS_EN_CH-1] = TRESHOLD_LOGIC_SBUS/2;
297 } else ctrl.controlModel_Y.curr_ref = 0, remote_raw.ch[SBUS_EN_CH-1] = TRESHOLD_LOGIC_SBUS/2;
298 return;
299 #endif
300
301 #ifdef SIN_TEST
303 if (enable_old != enable_new) {
304 if (enable_new) test_start = millis();
305 }
307 test_timer = millis() - test_start;
308 if (enable_new) {
309 const float freq = 0.5F;
310 if (test_timer <= (2*SIN_WAVE*freq*1000.0F)) ctrl.controlModel_Y.curr_ref = 4.0F*sinf(2.0F*PI*freq*float(test_timer)/1000.0F);
311 } else ctrl.controlModel_Y.curr_ref = 0;
312 return;
313 #endif
314
315 //update control
316 ctrl.update(); //this performs the control loop
317
318}
319
320void update_tet(uint32_t t0) {
321 timing.tet = micros() - t0; //duty cycle (i.e. time to do stuff), should be < SAMPLING_TIME, better < SAMPLING_TIME/2 to avoid large delay
322 if (timing.tet > timing.max_tet) timing.max_tet = timing.tet;
323 if (timing.tet < timing.min_tet) timing.min_tet = timing.tet;
324}
325
326void check_error(void) {
327 //check error is also performed in the control loop, so only low-level checks are performed in the code, such as....
328 //Instead, steering limits etc. are checked in the control loop (see simulink model)
329 //save error state in ctrl.controlModel_U to change error state inside the control loop
330}
331
332//get selector
333int8_t get_selector(void) {
335 return +1;
336 }
338 return -1;
339 }
340 return 0;
341}
342
343//get enable status
344bool get_enable(void) {
345 bool remote_enable = true;
346#if EN_REMOTE_CTRL==1
347 remote_enable = (remote_raw.ch[SBUS_EN_CH-1] >= TRESHOLD_LOGIC_SBUS);
348#endif
349 return (remote_enable && (ctrl.controlModel_Y.error_state_out < 1) && (ctrl.controlModel_U.error_state_in < 1)); //enable from remote control and check errors
350}
351
352//set drivers
353void set_driver(void) {
354 bool enable = get_enable();
355
356 // override control throttle_ref
357 //ctrl.controlModel_Y.throttle_ref = CONVERT_CHANNEL_TO_FLOAT(remote_raw.ch[SBUS_THR_OR-1], MIN_REF_INPUT, MAX_REF_INPUT);
358
359 //set motor driver
360#if MTR_CTRL_MODE==0
361 analogWrite(PWM_PIN, constrain(CONVERT_CURRENT_TO_PWM(ctrl.controlModel_Y.curr_ref), PWM_MIN*(powf(2, PWM_RES) - 1), PWM_MAX*(powf(2, PWM_RES) - 1)));
362#else
363 dac.analogWrite(DAC_MTR_CH, constrain(CONVERT_CURRENT_TO_DAC(ctrl.controlModel_Y.curr_ref), 0, pow(2, DAC_RES) - 1));
364#endif
365 digitalWriteFast(MTR_DIR_PIN, (ctrl.controlModel_Y.curr_ref >= 0) ? CW : !CW);
366 digitalWriteFast(MTR_EN_PIN, (enable) ? MTR_EN_STATE : !MTR_EN_STATE);
367
368#if EN_LONG_CTRL==1
369 //set throttle signal
370 dac.analogWrite(DAC_THROTTLE_CH, (ctrl.controlModel_Y.throttle_ref >= 0 && enable) ?
371 constrain(CONVERT_TRHOTTLE_TO_DAC(ctrl.controlModel_Y.throttle_ref), 0, pow(2, DAC_RES) - 1) :
373
374 //set brake stepper motor
375 int32_t brake_motor_position = (ctrl.controlModel_Y.throttle_ref <= 0) ?
376 constrain(CONVERT_TRHOTTLE_TO_STEPS(-ctrl.controlModel_Y.throttle_ref),0,CONVERT_BRLEV_TO_STEPS(MAX_BR_DISP)) :
378 if (remote_raw.ch[SBUS_BR_CH-1] >= TRESHOLD_LOGIC_SBUS) brake_motor_position = CONVERT_BRLEV_TO_STEPS(MAX_BR_DISP);
379 bool is_brake_motor_arrived = brakeMotor.getPosition() == brake_motor_position;
380 if (is_brake_motor_arrived) BR_DISABLE;
381 if (!brakeMotor.isMoving && !is_brake_motor_arrived){
382 BR_ENABLE;
383 brakeMotor.moveAbsAsync(brake_motor_position);
384 }
385#endif
386
387
388 //set other stuff below
389}
390
391//turn off callback
393 uint32_t timer_turnoff = millis();
394 LEDON;
395 log_closer();
396 while ((millis() - timer_turnoff) < 500) {}; //wait
397 LEDOFF;
398}
399
400//check battery voltag
401boolean check_voltage(void) {
402 voltage_raw.batVolt = analogRead(VOLTAGE);
403 float volts = CONVERT_VOLTAGE_TO_V(float(voltage_raw.batVolt)) - voltage_raw.batVolt_offset;
404 if ((volts > MAX_VOLTAGE) | (volts < MIN_VOLTAGE)) {
405 delay(1000);
406 POWER_OFF;
407 return false;
408 }
409 return true;
410}
411
412//return internal RTC time
413time_t getTime() {
414 return Teensy3Clock.get();
415}
416
417//do led stuff
418void do_led() {
419 if (counters.LED == 0) {
420 uint16_t ledfac = DEF_LED;
421 switch (LEDmode) {
422 case LedMode::LOG:
423 ledfac = LOG_LED;
424 break;
425 case LedMode::MTP:
426 ledfac = MTP_LED;
427 break;
428 case LedMode::ERR:
429 ledfac = ERR_LED;
430 break;
431 case LedMode::MTPWAIT:
432 ledfac = MTPWAIT_LED;
433 break;
434 default:
435 ledfac = DEF_LED;
436 break;
437 }
439 if (LEDstate) LEDON;
440 else LEDOFF;
441 //reset counter
442 counters.LED = ledfac - 1;
443 }
444 else counters.LED--;
445}
446
447void serial_flush(void) {
448 UART_SPEEDSENS.flush(); //speed sensor
449 UART_GPS.flush(); //gps
450}
451
452void set_ctrl_param(void) {
453
454}
455
456#endif
#define UART_SPEEDSENS
Used UART for speed sensor bus.
Definition config.h:42
#define BAUD_SPEEDSENS
Baudrate of the UART for speed sensor bus.
Definition config.h:43
#define DAC_MTR_CH
DAC channel for motor control.
Definition config.h:56
#define PWM_MAX
Maximum PWM value for motor current reference.
Definition config.h:63
#define USED_SPI
Used SPI for sensor IO bus.
Definition config.h:36
#define PWM_RES
PWM resolution in bit (max is 14).
Definition config.h:60
#define UART_GPS
Used UART for GPS sensor bus.
Definition config.h:44
#define DAC_THROTTLE_CH
DAC channel for throttle control.
Definition config.h:57
#define PWM_MIN
Minimum PWM value for motor current reference.
Definition config.h:62
#define PWM_FREQ
PWM frequency (max is 5kHz).
Definition config.h:61
#define ADC_AVERAGE
ADC averaging 1/2/8/16 (samples). The more the averaging, the less the noise, but the more the readin...
Definition config.h:52
#define BAUD_GPS_DEF
Default baudrate of GPS sensor (for init only).
Definition config.h:45
#define ADC_RES
ADC resolution (in bits).
Definition config.h:51
#define DAC_RES
DAC resolution (in bits).
Definition config.h:55
#define GYROX_OFFSET
Offset of gyro x.
Definition config.h:154
#define MAX_VOLTAGE
Maximum battery voltage (overvoltage) (V).
Definition config.h:180
#define MAX_SBUS
Maximum value from SBUS.
Definition config.h:160
#define ACC_SCALE
Acceleration scale.
Definition config.h:138
#define SBUS_THROTTLE_CH
Throttle channel in SBUS.
Definition config.h:169
#define GYROZ_OFFSET
Offset of gyro z.
Definition config.h:156
#define TRESHOLD_LOGIC_SBUS
Treshold for SBUS logic channels.
Definition config.h:163
#define ERR_LED
Sampling factor of led in error mode (expressed in units of SAMPLING_TIME).
Definition config.h:127
#define MAX_REF_INPUT
Maximum value for input reference.
Definition config.h:167
#define MIN_VOLTAGE
Minimum battery voltage (undervoltage) (V).
Definition config.h:179
#define IMU_SAMPLING_FAC
Sampling factor of IMU (expressed in units of SAMPLING_TIME).
Definition config.h:121
#define SBUS_BR_CH
brake channel in SBUS.
Definition config.h:172
#define GPSSPEED_SCALE
GPS speed scale.
Definition config.h:141
#define MIN_SBUS
Minimum value from SBUS.
Definition config.h:161
#define MTPWAIT_LED
Sampling factor of led in mtp wait mode (expressed in units of SAMPLING_TIME).
Definition config.h:128
#define DIST_SCALE
Distance scale.
Definition config.h:137
#define MTP_LED
Sampling factor of led in mtp mode (expressed in units of SAMPLING_TIME).
Definition config.h:126
#define GYROY_OFFSET
Offset of gyro y.
Definition config.h:155
#define SBUS_SEL_CH
3 selector channel in SBUS.
Definition config.h:174
#define ACC_ODR
Output data rate of accelerometer (sps).
Definition config.h:131
#define MIN_REF_INPUT
Minimum value for input reference.
Definition config.h:168
#define MAX_BR_DISP
Maximum brake lever displacement (mm).
Definition config.h:188
#define MAG_ODR
Output data rate of magnetometer (sps).
Definition config.h:133
#define SBUS_ROLL_CH
Roll channel in SBUS.
Definition config.h:170
#define TRESHOLD_NEG_SBUS
Treshold for negative value.
Definition config.h:165
#define LOG_LED
Sampling factor of led in log mode (expressed in units of SAMPLING_TIME).
Definition config.h:125
#define GYRO_SCALE
Gyrometer scale.
Definition config.h:139
#define NUM_CH_SBUS
Number channels from SBUS.
Definition config.h:159
#define GYRO_ODR
Output data rate of gyrometer (sps).
Definition config.h:132
#define SBUS_EN_CH
Enable channel in SBUS.
Definition config.h:171
#define DEF_LED
Sampling factor of led in default mode (expressed in units of SAMPLING_TIME).
Definition config.h:124
#define TRESHOLD_POS_SBUS
Treshold for positive value.
Definition config.h:164
#define SPEED_SAMPLING_FAC
Sampling factor of speed sensor (expressed in units of SAMPLING_TIME).
Definition config.h:120
#define MAX_BR_ACC
Maximum brake motor acceleration (mm/s2).
Definition config.h:185
#define MAX_BR_SPEED
Maximum brake motor speed (mm/s).
Definition config.h:184
#define SPEED_SCALE
Speed scale.
Definition config.h:136
#define MAG_SAMPLING_FAC
Sampling factor of magnetometer (expressed in units of SAMPLING_TIME).
Definition config.h:122
#define ZERO_SBUS
Zero-value for SBUS.
Definition config.h:162
#define MAX_MISSING_SBUS
Maximum missing packets for SBUS.
Definition config.h:166
#define MAG_SCALE
Magnetometer scale.
Definition config.h:140
#define SAMPLING_TIME
Sampling time of the control loop (us).
Definition config.h:119
void log_closer(void)
Close the log file.
Definition logger.h:126
#define CONVERT_CURRENT_TO_PWM(X)
Convert reference current to PWM value.
Definition config.h:245
#define CONVERT_FORKDISP_TO_MM(X)
Convert fork displacement to millimiters.
Definition config.h:281
#define BR_DISABLE
Macro for brake motor enable.
Definition config.h:318
#define CONVERT_VOLTAGE_TO_V(X)
Convert battery voltage to volts.
Definition config.h:272
#define CONVERT_RIDERTRQ_TO_NM(X)
Convert rider torque to Nm.
Definition config.h:209
#define CONVERT_CHANNEL_TO_FLOAT(X, XMIN, XMAX)
Convert SBUS channel to float.
Definition config.h:291
#define CONVERT_BRLEV_TO_STEPS(X)
Convert brake lever position to motor steps.
Definition config.h:299
#define MTR_EN_STATE
Enable state for steer motor.
Definition config.h:312
#define RELAY_EN_STATE
Enable state for relay.
Definition config.h:313
#define CONVERT_STEERPOS_TO_RAD(X)
Convert steer position to radiants.
Definition config.h:218
#define CONVERT_TRHOTTLE_TO_STEPS(X)
Convert throttle to motor steps.
Definition config.h:307
#define LEDON
Macro for LED on.
Definition config.h:319
#define BR_SLEEP_STATE
Enable state for brake motor.
Definition config.h:314
#define LEDOFF
Macro for LED off.
Definition config.h:320
#define CONVERT_CURRENT_TO_DAC(X)
Convert reference current to DAC value.
Definition config.h:254
#define POWER_OFF
Macro for turning off the microcontroller.
Definition config.h:323
#define BR_ENABLE
Macro for brake motor enable.
Definition config.h:317
#define CONVERT_TRHOTTLE_TO_DAC(X)
Convert reference throttle to DAC value.
Definition config.h:263
#define CONVERT_ACTCURR_TO_A(X)
Convert steer rate to rad/s.
Definition config.h:236
#define CONVERT_STEERVEL_TO_RADPS(X)
Convert steer rate to rad/s.
Definition config.h:227
#define RIDER_TRQ
Rider torque analog input pin.
Definition config.h:87
#define ACT_CURR
Actual current analog input pin.
Definition config.h:86
#define ONOFF_STATE_PIN
Digital in for on/off buttun state (diode between onoff and this).
Definition config.h:103
#define FORK_DISP
Front fork displacement analog input pin.
Definition config.h:89
#define BR_SLEEP_PIN
Digital out for brake motor sleep.
Definition config.h:100
#define STEER_SPEED
Steer speed analog input pin.
Definition config.h:85
#define MTR_DIR_PIN
Digital out for motor direction.
Definition config.h:96
#define VOLTAGE
Battery voltage analog input pin.
Definition config.h:88
#define STEER_POS
Steer position analog input pin.
Definition config.h:84
#define MTR_EN_PIN
Digital out for motor enable.
Definition config.h:95
#define RELAY_EN_PIN
Digital out for relay enable.
Definition config.h:97
#define PWM_PIN
PWM pin for motor current reference.
Definition config.h:92
bool get_enable(void)
Get the enable.
Definition userfun.h:344
void set_driver(void)
Set the drivers.
Definition userfun.h:353
time_t getTime()
Get internal RTC time.
Definition userfun.h:413
void check_error(void)
Check for errors.
Definition userfun.h:326
void do_zeros(void)
Perform the zero.
Definition userfun.h:79
void update_tet(uint32_t t0)
Update task execution time.
Definition userfun.h:320
void do_control(void)
Update the control loop.
Definition userfun.h:267
void turnoff_callback(void)
Callback for tuning off.
Definition userfun.h:392
void do_led()
Management of the status LED.
Definition userfun.h:418
int8_t get_selector(void)
Get the selector.
Definition userfun.h:333
boolean check_voltage(void)
Check the battery voltage.
Definition userfun.h:401
void serial_flush(void)
Flush the UARTs.
Definition userfun.h:447
bool check_sbus(void)
Check for SBUS.
Definition userfun.h:212
void start_brake_stepper(void)
Start the brake stepper motor.
Definition userfun.h:135
void set_IObus(void)
Start the IO buses.
Definition userfun.h:42
void get_sensors(void)
Get sensor readings.
Definition userfun.h:144
void set_ctrl_param(void)
Set custom control parameters.
Definition userfun.h:452
float tempmonGetTemp(void)
Get the CPU temperature.
void set_ctrl_input(void)
Set the control inputs.
Definition userfun.h:224
void set_GPIO(void)
Set the GPIO.
Definition userfun.h:119
void start_IObus(void)
Initialize the IO buses.
Definition userfun.h:15
#define FULLEXT_FORKDISP
Fork displacement at full extended suspension (in mm).
Definition config.h:342
const float imu_rotMat[3][3]
Rotation matrix from IMU frame to SAE body-fixed frame.
Definition config.h:345
#define IMP_DUR
Impulse duration (ms).
Definition config.h:356
#define IMP_AMP
Impulse amplitude (A).
Definition config.h:357
#define CW
Digital pin state for clockwise torque.
Definition config.h:339
#define NUM_ZERO_SAMPLES
Samples used to perform the zero of sensors.
Definition config.h:340
const float mag_rotMat[3][3]
Rotation matrix from MAG frame to SAE body-fixed frame.
Definition config.h:348
Timing timing
Timing of functions.
Definition objects.h:222
uint32_t sampling_timer
Timer for the main cycle.
Definition objects.h:217
Steer steer_raw
Raw data from the steering sensors.
Definition objects.h:229
SerialTransfer speedSensor
SerialTransfer object for speed sensor.
Definition objects.h:242
uint32_t test_timer
Definition objects.h:249
RemoteCtrl remote_raw
Channels from the remote controller.
Definition objects.h:232
ImuData imuData_raw
Raw data from IMU and magnetometer.
Definition objects.h:224
GPSData gpsData_raw
Raw data from the GPS.
Definition objects.h:226
TS4::Stepper brakeMotor(BR_STEP_PIN, BR_DIR_PIN)
Stepper object for the brake motor.
ControlClass ctrl
Control object.
Definition objects.h:241
int8_t LEDmode
LED mode variable.
Definition objects.h:220
uint8_t GPSserial_extra_buffer[GPS_EXTBUFSIZE]
Definition objects.h:218
IOstatus iostatus
IO status flags.
Definition objects.h:233
ActCurr actCurr_raw
Raw data from the motor current sensor.
Definition objects.h:228
Speed speed_raw
Raw data from speed sensor.
Definition objects.h:225
ForkDisp forkDisp_raw
Raw data from the fork-displacement sensor.
Definition objects.h:230
RiderTorque riderTorque_raw
Raw data from the torsiometer.
Definition objects.h:227
Voltage voltage_raw
Raw data from the battery voltage sensor.
Definition objects.h:231
Counter counters
Counters for lower sampling rate.
Definition objects.h:223
int8_t enable_new
Definition objects.h:248
boolean LEDstate
LED state.
Definition objects.h:221
ADC * adc
Pointer to ADC object.
Definition objects.h:238
uint32_t test_start
Definition objects.h:249
int8_t enable_old
Definition objects.h:247
constexpr int8_t MTPWAIT
MTP wait for USB connected.
Definition objects.h:35
constexpr int8_t MTP
MTP mode.
Definition objects.h:33
constexpr int8_t ERR
Error mode.
Definition objects.h:34
constexpr int8_t LOG
Default mode with signal-logging.
Definition objects.h:32