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//get enable status
222bool get_enable(void) {
223 bool remote_enable = true;
224#if EN_REMOTE_CTRL==1
225 remote_enable = (remote_raw.ch[SBUS_EN_CH-1] >= TRESHOLD_LOGIC_SBUS);
226#endif
227 return (remote_enable && (ctrl.controlModel_Y.error_state_out < 1) && (ctrl.controlModel_U.error_state_in < 1)); //enable from remote control and check errors
228}
229
230//get pb status
231bool get_pb(void) {
232 return (remote_raw.ch[SBUS_PB_CH-1] >= TRESHOLD_LOGIC_SBUS);
233}
234
235//get selector
236int8_t get_selector(void) {
237 // return +1 if SBUS_SEL_CH is UP, -1 if is DOWN, 0 otherwise
238 return (remote_raw.ch[SBUS_SEL_CH-1] >= TRESHOLD_POS_SBUS) ? +1 :
239 ( (remote_raw.ch[SBUS_SEL_CH-1] <= TRESHOLD_NEG_SBUS) ? -1 : 0);
240}
241
242/*
243void set_ctrl_input(void) sets the control input structure
244*/
245void set_ctrl_input(void) {
246 //set speed
247 if (speed_raw.speed >= -1) ctrl.controlModel_U.speed = SPEED_SCALE*float(speed_raw.speed); // Avoid negative values
248 ctrl.controlModel_U.dist = DIST_SCALE * float(speed_raw.dist);
249 //set the threee components of accs, gyros, mags
250 for (int i = 0; i < 3; i++) {
251 ctrl.controlModel_U.accs[i] = float(imuData_raw.accs_mg[0] * imu_rotMat[i][0] +
252 imuData_raw.accs_mg[1] * imu_rotMat[i][1] +
253 imuData_raw.accs_mg[2] * imu_rotMat[i][2]) * ACC_SCALE;
254 ctrl.controlModel_U.gyros[i] = ((float(imuData_raw.gyros_mdps[0]) - imuData_raw.gyros_offset[0]) * imu_rotMat[i][0] +
255 (float(imuData_raw.gyros_mdps[1]) - imuData_raw.gyros_offset[1]) * imu_rotMat[i][1] +
256 (float(imuData_raw.gyros_mdps[2]) - imuData_raw.gyros_offset[2]) * imu_rotMat[i][2] ) * GYRO_SCALE; //may have also offset
257 ctrl.controlModel_U.mags[i] = float(imuData_raw.mags_mG[0] * mag_rotMat[i][0] +
258 imuData_raw.mags_mG[1] * mag_rotMat[i][1] +
259 imuData_raw.mags_mG[2] * mag_rotMat[i][2]) * MAG_SCALE;
260 }
261 //set gps
262 ctrl.controlModel_U.gps[0] = gpsData_raw.lat;
263 ctrl.controlModel_U.gps[1] = gpsData_raw.lon;
264 ctrl.controlModel_U.gps[2] = gpsData_raw.speed;
265 //set front fork displacement
266 ctrl.controlModel_U.forkdisp = CONVERT_FORKDISP_TO_MM(float(forkDisp_raw.forkDisp)) - forkDisp_raw.forkDisp_offset;
267 //set rider torque
268 ctrl.controlModel_U.torque = CONVERT_RIDERTRQ_TO_NM(float(riderTorque_raw.riderTorque)) - riderTorque_raw.riderTorque_offset;
269 //set steer position and velocity
270 ctrl.controlModel_U.steer[0] = CONVERT_STEERPOS_TO_RAD(float(steer_raw.steerPos));
271 ctrl.controlModel_U.steer[1] = CONVERT_STEERVEL_TO_RADPS(float(steer_raw.steerVel)) - steer_raw.steerVel_offset;
272 //set actual motor current
273 ctrl.controlModel_U.curr = CONVERT_ACTCURR_TO_A(float(actCurr_raw.actCurr)) - actCurr_raw.actCurr_offset;
274 //set battery voltage
275 ctrl.controlModel_U.voltage = CONVERT_VOLTAGE_TO_V(float(voltage_raw.batVolt)) - voltage_raw.batVolt_offset;
276 //CPU temp
277 ctrl.controlModel_U.CPUTemp = tempmonGetTemp();
278 //set remote control sbus
279#if EN_REMOTE_CTRL==1
282 // SE and SC switches
283 ctrl.controlModel_U.se_switch = (float) get_pb(); //get push button state
284 ctrl.controlModel_U.sc_switch = (float) get_selector(); //get selector state
285#endif
286}
287
288/*
289void do_control(void) performs control update cycle
290*/
291void do_control(void) {
292
293 //tests
294#ifdef IMP_TEST
296 if (start_old != start_new) {
297 if (start_new) test_start = millis();
298 }
300 test_timer = millis() - test_start;
301 ctrl.controlModel_Y.curr_ref = 0;
302 if (start_new != 0 && (test_timer <= IMP_DUR)) {
303 ctrl.controlModel_Y.curr_ref = 0.5F*IMP_AMP*(1.0F - cosf(2*PI*float(test_timer)/float(IMP_DUR)))*float(start_new);
304 }
305 return;
306#endif
307
308#ifdef SWEEP_TEST
310 if (start_old != start_new) {
311 if (start_new) test_start = millis();
312
313 }
315 test_timer = millis() - test_start;
316 if (start_new) {
317 if (test_timer <= TEST_DURATION) {
318 float freq = 0.0F + float(test_timer)/float(TEST_DURATION) * (10.0F-0.0F);
319 float ampl = 2.0F;
320 ctrl.controlModel_Y.curr_ref = ampl*sinf(2.0F*PI*freq*float(test_timer)/1000.0F/2.0F);
321 } else ctrl.controlModel_Y.curr_ref = 0, remote_raw.ch[SBUS_EN_CH-1] = TRESHOLD_LOGIC_SBUS/2;
322 } else ctrl.controlModel_Y.curr_ref = 0, remote_raw.ch[SBUS_EN_CH-1] = TRESHOLD_LOGIC_SBUS/2;
323 return;
324#endif
325
326#ifdef SIN_TEST
328 if (start_old != start_new) {
329 if (start_new) test_start = millis();
330 }
332 test_timer = millis() - test_start;
333 if (start_new) {
334 const float freq = 0.5F;
335 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);
336 } else ctrl.controlModel_Y.curr_ref = 0;
337 return;
338#endif
339
340 //update control
341 ctrl.update(); //this performs the control loop
342
343}
344
345void update_tet(uint32_t t0) {
346 timing.tet = micros() - t0; //duty cycle (i.e. time to do stuff), should be < SAMPLING_TIME, better < SAMPLING_TIME/2 to avoid large delay
347 if (timing.tet > timing.max_tet) timing.max_tet = timing.tet;
348 if (timing.tet < timing.min_tet) timing.min_tet = timing.tet;
349}
350
351void check_error(void) {
352 //check error is also performed in the control loop, so only low-level checks are performed in the code, such as....
353 //Instead, steering limits etc. are checked in the control loop (see simulink model)
354 //save error state in ctrl.controlModel_U to change error state inside the control loop
355}
356
357//set drivers
358void set_driver(void) {
359 bool enable = get_enable();
360
361 //set motor driver
362#if MTR_CTRL_MODE==0
363 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)));
364#else
365 dac.analogWrite(DAC_MTR_CH, constrain(CONVERT_CURRENT_TO_DAC(ctrl.controlModel_Y.curr_ref), 0, pow(2, DAC_RES) - 1));
366#endif
367 digitalWriteFast(MTR_DIR_PIN, (ctrl.controlModel_Y.curr_ref >= 0) ? CW : !CW);
368 digitalWriteFast(MTR_EN_PIN, (enable) ? MTR_EN_STATE : !MTR_EN_STATE);
369
370#if EN_LONG_CTRL==1
371 //set throttle signal
372#if TEST_THROTTLE==1
373 ctrl.controlModel_Y.throttle_ref = ctrl.controlModel_U.ref_inputs[0];
374#endif
375 dac.analogWrite(DAC_THROTTLE_CH, (ctrl.controlModel_Y.throttle_ref >= 0 && enable) ?
376 constrain(CONVERT_TRHOTTLE_TO_DAC(ctrl.controlModel_Y.throttle_ref), 0, pow(2, DAC_RES) - 1) :
378#endif
379#if EN_BRAKE_CTRL==1
380 //set brake stepper motor
381 int32_t brake_motor_position = (ctrl.controlModel_Y.throttle_ref <= 0) ?
382 constrain(CONVERT_TRHOTTLE_TO_STEPS(-ctrl.controlModel_Y.throttle_ref),0,CONVERT_BRLEV_TO_STEPS(MAX_BR_DISP)) :
384 if (remote_raw.ch[SBUS_PB_CH-1] >= TRESHOLD_LOGIC_SBUS) brake_motor_position = CONVERT_BRLEV_TO_STEPS(MAX_BR_DISP);
385 bool is_brake_motor_arrived = brakeMotor.getPosition() == brake_motor_position;
386 if (is_brake_motor_arrived) BR_DISABLE;
387 if (!brakeMotor.isMoving && !is_brake_motor_arrived){
388 BR_ENABLE;
389 brakeMotor.moveAbsAsync(brake_motor_position);
390 }
391#endif
392
393
394 //set other stuff below
395}
396
397//turn off callback
399 uint32_t timer_turnoff = millis();
400 LEDON;
401 log_closer();
402 while ((millis() - timer_turnoff) < 500) {}; //wait
403 LEDOFF;
404}
405
406//check battery voltag
407boolean check_voltage(void) {
408 voltage_raw.batVolt = analogRead(VOLTAGE);
409 float volts = CONVERT_VOLTAGE_TO_V(float(voltage_raw.batVolt)) - voltage_raw.batVolt_offset;
410 if ((volts > MAX_VOLTAGE) | (volts < MIN_VOLTAGE)) {
411 delay(1000);
412 POWER_OFF;
413 return false;
414 }
415 return true;
416}
417
418//return internal RTC time
419time_t getTime() {
420 return Teensy3Clock.get();
421}
422
423//do led stuff
424void do_led() {
425 if (counters.LED == 0) {
426 uint16_t ledfac = DEF_LED;
427 switch (LEDmode) {
428 case LedMode::LOG:
429 ledfac = LOG_LED;
430 break;
431 case LedMode::MTP:
432 ledfac = MTP_LED;
433 break;
434 case LedMode::ERR:
435 ledfac = ERR_LED;
436 break;
437 case LedMode::MTPWAIT:
438 ledfac = MTPWAIT_LED;
439 break;
440 default:
441 ledfac = DEF_LED;
442 break;
443 }
445 if (LEDstate) LEDON;
446 else LEDOFF;
447 //reset counter
448 counters.LED = ledfac - 1;
449 }
450 else counters.LED--;
451}
452
453void serial_flush(void) {
454 UART_SPEEDSENS.flush(); //speed sensor
455 UART_GPS.flush(); //gps
456}
457
458void set_ctrl_param(void) {
459
460}
461
462#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:182
#define MIN_REF_INPUT2
Minimum value for input reference 2.
Definition config.h:170
#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:171
#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 MIN_VOLTAGE
Minimum battery voltage (undervoltage) (V).
Definition config.h:181
#define IMU_SAMPLING_FAC
Sampling factor of IMU (expressed in units of SAMPLING_TIME).
Definition config.h:121
#define GPSSPEED_SCALE
GPS speed scale.
Definition config.h:141
#define MIN_SBUS
Minimum value from SBUS.
Definition config.h:161
#define MAX_REF_INPUT2
Maximum value for input reference 2.
Definition config.h:169
#define MTPWAIT_LED
Sampling factor of led in mtp wait mode (expressed in units of SAMPLING_TIME).
Definition config.h:128
#define MIN_REF_INPUT1
Minimum value for input reference 1.
Definition config.h:168
#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:176
#define ACC_ODR
Output data rate of accelerometer (sps).
Definition config.h:131
#define MAX_BR_DISP
Maximum brake lever displacement (mm).
Definition config.h:190
#define MAG_ODR
Output data rate of magnetometer (sps).
Definition config.h:133
#define SBUS_ROLL_CH
Roll channel in SBUS.
Definition config.h:172
#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 SBUS_PB_CH
brake channel in SBUS.
Definition config.h:174
#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:173
#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:187
#define MAX_BR_SPEED
Maximum brake motor speed (mm/s).
Definition config.h:186
#define MAX_REF_INPUT1
Maximum value for input reference 1.
Definition config.h:167
#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:247
#define CONVERT_FORKDISP_TO_MM(X)
Convert fork displacement to millimiters.
Definition config.h:283
#define BR_DISABLE
Macro for brake motor enable.
Definition config.h:320
#define CONVERT_VOLTAGE_TO_V(X)
Convert battery voltage to volts.
Definition config.h:274
#define CONVERT_RIDERTRQ_TO_NM(X)
Convert rider torque to Nm.
Definition config.h:211
#define CONVERT_CHANNEL_TO_FLOAT(X, XMIN, XMAX)
Convert SBUS channel to float.
Definition config.h:293
#define CONVERT_BRLEV_TO_STEPS(X)
Convert brake lever position to motor steps.
Definition config.h:301
#define MTR_EN_STATE
Enable state for steer motor.
Definition config.h:314
#define RELAY_EN_STATE
Enable state for relay.
Definition config.h:315
#define CONVERT_STEERPOS_TO_RAD(X)
Convert steer position to radiants.
Definition config.h:220
#define CONVERT_TRHOTTLE_TO_STEPS(X)
Convert throttle to motor steps.
Definition config.h:309
#define LEDON
Macro for LED on.
Definition config.h:321
#define BR_SLEEP_STATE
Enable state for brake motor.
Definition config.h:316
#define LEDOFF
Macro for LED off.
Definition config.h:322
#define CONVERT_CURRENT_TO_DAC(X)
Convert reference current to DAC value.
Definition config.h:256
#define POWER_OFF
Macro for turning off the microcontroller.
Definition config.h:325
#define BR_ENABLE
Macro for brake motor enable.
Definition config.h:319
#define CONVERT_TRHOTTLE_TO_DAC(X)
Convert reference throttle to DAC value.
Definition config.h:265
#define CONVERT_ACTCURR_TO_A(X)
Convert steer rate to rad/s.
Definition config.h:238
#define CONVERT_STEERVEL_TO_RADPS(X)
Convert steer rate to rad/s.
Definition config.h:229
#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:222
void set_driver(void)
Set the drivers.
Definition userfun.h:358
time_t getTime()
Get internal RTC time.
Definition userfun.h:419
void check_error(void)
Check for errors.
Definition userfun.h:351
void do_zeros(void)
Perform the zero.
Definition userfun.h:79
void update_tet(uint32_t t0)
Update task execution time.
Definition userfun.h:345
void do_control(void)
Update the control loop.
Definition userfun.h:291
void turnoff_callback(void)
Callback for tuning off.
Definition userfun.h:398
void do_led()
Management of the status LED.
Definition userfun.h:424
int8_t get_selector(void)
Get the selector.
Definition userfun.h:236
boolean check_voltage(void)
Check the battery voltage.
Definition userfun.h:407
void serial_flush(void)
Flush the UARTs.
Definition userfun.h:453
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:458
float tempmonGetTemp(void)
Get the CPU temperature.
void set_ctrl_input(void)
Set the control inputs.
Definition userfun.h:245
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:346
const float imu_rotMat[3][3]
Rotation matrix from IMU frame to SAE body-fixed frame.
Definition config.h:349
#define CW
Digital pin state for clockwise torque.
Definition config.h:343
#define NUM_ZERO_SAMPLES
Samples used to perform the zero of sensors.
Definition config.h:344
const float mag_rotMat[3][3]
Rotation matrix from MAG frame to SAE body-fixed frame.
Definition config.h:352
Timing timing
Timing of functions.
Definition objects.h:223
uint32_t sampling_timer
Timer for the main cycle.
Definition objects.h:218
Steer steer_raw
Raw data from the steering sensors.
Definition objects.h:230
SerialTransfer speedSensor
SerialTransfer object for speed sensor.
Definition objects.h:243
uint32_t test_timer
Definition objects.h:252
RemoteCtrl remote_raw
Channels from the remote controller.
Definition objects.h:233
ImuData imuData_raw
Raw data from IMU and magnetometer.
Definition objects.h:225
GPSData gpsData_raw
Raw data from the GPS.
Definition objects.h:227
TS4::Stepper brakeMotor(BR_STEP_PIN, BR_DIR_PIN)
Stepper object for the brake motor.
ControlClass ctrl
Control object.
Definition objects.h:242
int8_t LEDmode
LED mode variable.
Definition objects.h:221
uint8_t GPSserial_extra_buffer[GPS_EXTBUFSIZE]
Definition objects.h:219
IOstatus iostatus
IO status flags.
Definition objects.h:234
ActCurr actCurr_raw
Raw data from the motor current sensor.
Definition objects.h:229
Speed speed_raw
Raw data from speed sensor.
Definition objects.h:226
ForkDisp forkDisp_raw
Raw data from the fork-displacement sensor.
Definition objects.h:231
RiderTorque riderTorque_raw
Raw data from the torsiometer.
Definition objects.h:228
Voltage voltage_raw
Raw data from the battery voltage sensor.
Definition objects.h:232
Counter counters
Counters for lower sampling rate.
Definition objects.h:224
boolean LEDstate
LED state.
Definition objects.h:222
ADC * adc
Pointer to ADC object.
Definition objects.h:239
uint32_t test_start
Definition objects.h:252
int8_t start_old
Definition objects.h:248
int8_t start_new
Definition objects.h:249
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
bool get_pb(void)
Definition userfun.h:231