MCU
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#ifdef ENABLE_DEBUG_PRINTS
2#define DEBUG_PRINTS true
3#else
4#define DEBUG_PRINTS false
5#endif
6
7/* Include files */
8/* System Includes*/
9#include <Arduino.h>
10/* Libraries */
11#include "FlexCAN_T4.h"
12#include "HyTech_CAN.h"
13#include "MCU_rev15_defs.h"
14#include "pb.h"
15// /* Interfaces */
17#include "ProtobufMsgInterface.h"
18#include "HytechCANInterface.h"
19#include "ThermistorInterface.h"
20#include "Teensy_ADC.h"
21#include "MCP_ADC.h"
22#include "ORBIS_BR10.h"
23#include "MCUInterface.h"
24#include "AMSInterface.h"
25#include "WatchdogInterface.h"
26#include "DashboardInterface.h"
27#include "InverterInterface.h"
28#include "TelemetryInterface.h"
29#include "SABInterface.h"
30#include "VectornavInterface.h"
31#include "LoadCellInterface.h"
32
33/* Systems */
34#include "SysClock.h"
35#include "Buzzer.h"
36#include "SafetySystem.h"
37#include "DrivetrainSystem.h"
38#include "PedalsSystem.h"
40#include "TorqueControllerMux.h"
41#include "TorqueControllers.h"
42#include "CASESystem.h"
43
44// /* State machine */
45#include "MCUStateMachine.h"
46#include "HT08_CASE.h"
47
48#include "InterfaceParams.h"
49#include "PrintLogger.h"
50
51#include "hytech_msgs.pb.h"
52/*
53 PARAMETER STRUCTS
54*/
55using namespace qindesign::network;
56
59 .accel2_channel = MCU15_ACCEL2_CHANNEL,
60 .brake1_channel = MCU15_BRAKE1_CHANNEL,
61 .brake2_channel = MCU15_BRAKE2_CHANNEL,
62 .pots_fl_channel = MCU15_FL_POTS_CHANNEL,
63 .pots_fr_channel = MCU15_FR_POTS_CHANNEL,
64 .loadcell_fl_channel = MCU15_FL_LOADCELL_CHANNEL,
65 .loadcell_fr_channel = MCU15_FR_LOADCELL_CHANNEL,
66 .analog_steering_channel = MCU15_STEERING_CHANNEL,
67 .current_channel = MCU15_CUR_POS_SENSE_CHANNEL,
68 .current_ref_channel = MCU15_CUR_NEG_SENSE_CHANNEL,
69 .glv_sense_channel = MCU15_GLV_SENSE_CHANNEL,
70 .therm_fl_channel = MCU15_THERM_FL_CHANNEL,
71 .therm_fr_channel = MCU15_THERM_FR_CHANNEL};
72
75 .min_pedal_2 = ACCEL1_PEDAL_MIN,
76 .max_pedal_1 = ACCEL1_PEDAL_MAX,
77 .max_pedal_2 = ACCEL1_PEDAL_MAX,
78 .min_sensor_pedal_1 = ACCEL1_PEDAL_OOR_MIN,
79 .min_sensor_pedal_2 = ACCEL1_PEDAL_OOR_MIN,
80 .max_sensor_pedal_1 = ACCEL1_PEDAL_OOR_MAX,
81 .max_sensor_pedal_2 = ACCEL1_PEDAL_OOR_MAX,
82 .activation_percentage = APPS_ACTIVATION_PERCENTAGE,
83 .deadzone_margin = DEFAULT_PEDAL_DEADZONE,
84 .implausibility_margin = DEFAULT_PEDAL_IMPLAUSIBILITY_MARGIN,
85 .mechanical_activation_percentage = APPS_ACTIVATION_PERCENTAGE};
86
89 .min_pedal_2 = ACCEL2_PEDAL_MIN,
90 .max_pedal_1 = ACCEL2_PEDAL_MAX,
91 .max_pedal_2 = ACCEL2_PEDAL_MAX,
92 .min_sensor_pedal_1 = ACCEL2_PEDAL_OOR_MIN,
93 .min_sensor_pedal_2 = ACCEL2_PEDAL_OOR_MIN,
94 .max_sensor_pedal_1 = ACCEL2_PEDAL_OOR_MAX,
95 .max_sensor_pedal_2 = ACCEL2_PEDAL_OOR_MAX,
96 .activation_percentage = APPS_ACTIVATION_PERCENTAGE,
97 .deadzone_margin = DEFAULT_PEDAL_DEADZONE,
98 .implausibility_margin = DEFAULT_PEDAL_IMPLAUSIBILITY_MARGIN,
99 .mechanical_activation_percentage = APPS_ACTIVATION_PERCENTAGE};
100
103 .min_pedal_2 = ACCEL2_PEDAL_MIN,
104 .max_pedal_1 = ACCEL1_PEDAL_MAX,
105 .max_pedal_2 = ACCEL2_PEDAL_MAX,
106 .min_sensor_pedal_1 = ACCEL1_PEDAL_OOR_MIN,
107 .min_sensor_pedal_2 = ACCEL2_PEDAL_OOR_MIN,
108 .max_sensor_pedal_1 = ACCEL1_PEDAL_OOR_MAX,
109 .max_sensor_pedal_2 = ACCEL2_PEDAL_OOR_MAX,
110 .activation_percentage = APPS_ACTIVATION_PERCENTAGE,
111 .deadzone_margin = DEFAULT_PEDAL_DEADZONE,
112 .implausibility_margin = DEFAULT_PEDAL_IMPLAUSIBILITY_MARGIN,
113 .mechanical_activation_percentage = APPS_ACTIVATION_PERCENTAGE};
114
117 .min_pedal_2 = BRAKE1_PEDAL_MIN,
118 .max_pedal_1 = BRAKE1_PEDAL_MAX,
119 .max_pedal_2 = BRAKE1_PEDAL_MAX,
120 .min_sensor_pedal_1 = BRAKE1_PEDAL_OOR_MIN,
121 .min_sensor_pedal_2 = BRAKE1_PEDAL_OOR_MIN,
122 .max_sensor_pedal_1 = BRAKE1_PEDAL_OOR_MAX,
123 .max_sensor_pedal_2 = BRAKE1_PEDAL_OOR_MAX,
124 .activation_percentage = BRAKE_ACTIVATION_PERCENTAGE,
125 .deadzone_margin = DEFAULT_PEDAL_DEADZONE,
126 .implausibility_margin = DEFAULT_PEDAL_IMPLAUSIBILITY_MARGIN,
127 .mechanical_activation_percentage = BRAKE_MECH_THRESH,
128};
129
132 .min_pedal_2 = BRAKE2_PEDAL_MIN,
133 .max_pedal_1 = BRAKE2_PEDAL_MAX,
134 .max_pedal_2 = BRAKE2_PEDAL_MAX,
135 .min_sensor_pedal_1 = BRAKE2_PEDAL_OOR_MIN,
136 .min_sensor_pedal_2 = BRAKE2_PEDAL_OOR_MIN,
137 .max_sensor_pedal_1 = BRAKE2_PEDAL_OOR_MAX,
138 .max_sensor_pedal_2 = BRAKE2_PEDAL_OOR_MAX,
139 .activation_percentage = BRAKE_ACTIVATION_PERCENTAGE,
140 .deadzone_margin = DEFAULT_PEDAL_DEADZONE,
141 .implausibility_margin = DEFAULT_PEDAL_IMPLAUSIBILITY_MARGIN,
142 .mechanical_activation_percentage = BRAKE_MECH_THRESH,
143};
144
147 .min_pedal_2 = BRAKE2_PEDAL_MIN,
148 .max_pedal_1 = BRAKE1_PEDAL_MAX,
149 .max_pedal_2 = BRAKE2_PEDAL_MAX,
150 .min_sensor_pedal_1 = BRAKE1_PEDAL_OOR_MIN,
151 .min_sensor_pedal_2 = BRAKE2_PEDAL_OOR_MIN,
152 .max_sensor_pedal_1 = BRAKE1_PEDAL_OOR_MAX,
153 .max_sensor_pedal_2 = BRAKE2_PEDAL_OOR_MAX,
154 .activation_percentage = BRAKE_ACTIVATION_PERCENTAGE,
155 .deadzone_margin = DEFAULT_PEDAL_DEADZONE,
156 .implausibility_margin = DEFAULT_PEDAL_IMPLAUSIBILITY_MARGIN,
157 .mechanical_activation_percentage = BRAKE_MECH_THRESH,
158};
159
160/*
161 DATA SOURCES
162*/
163
166
167/* Two CAN lines on Main ECU rev15 */
168FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> INV_CAN; // Inverter CAN (now both are on same line)
169FlexCAN_T4<CAN3, RX_SIZE_256, TX_SIZE_16> TELEM_CAN; // telemetry CAN (basically everything except inverters)
170
171/* Set up CAN circular buffer */
172// using CircularBufferType = Circular_Buffer<uint8_t, (uint32_t)32, sizeof(CAN_message_t)>;
174
175/* Sensors */
176MCP_ADC<8> a1 = MCP_ADC<8>(ADC1_CS);
177MCP_ADC<4> a2 = MCP_ADC<4>(ADC2_CS, 1000000); // 1M baud needed for 04s
178MCP_ADC<4> a3 = MCP_ADC<4>(ADC3_CS, 1000000);
179Teensy_ADC<2> mcu_adc = Teensy_ADC<2>(DEFAULT_ANALOG_PINS);
180OrbisBR10 steering1(&Serial5);
181
182// /*
183// INTERFACES
184// */
190MCUInterface main_ecu(&CAN3_txBuffer);
192ThermistorInterface front_thermistors_interface(&CAN3_txBuffer);
194 LOADCELL_RL_SCALE, // RL Scale
195 LOADCELL_RL_OFFSET, // RL Offset (Migos)
196 LOADCELL_RR_SCALE, // RR Scale
197 LOADCELL_RR_OFFSET // RR Offset
198);
200
201// /* Inverter Interface Type */
204{
205 InvInt_t fl = InvInt_t(&CAN2_txBuffer, ID_MC1_SETPOINTS_COMMAND);
206 InvInt_t fr = InvInt_t(&CAN2_txBuffer, ID_MC2_SETPOINTS_COMMAND);
207 InvInt_t rl = InvInt_t(&CAN2_txBuffer, ID_MC3_SETPOINTS_COMMAND);
208 InvInt_t rr = InvInt_t(&CAN2_txBuffer, ID_MC4_SETPOINTS_COMMAND);
210
211// /*
212// SYSTEMS
213// */
214
215SysClock sys_clock;
216SteeringSystem steering_system(&steering1, &telem_interface, STEERING_IIR_ALPHA);
218
220// SafetySystem safety_system(&ams_interface, &wd_interface, &dashboard);
224// TODO ensure that case uses max regen torque, right now its not
226 // Following used for generated code
227 .AbsoluteTorqueLimit = 21.42, // N-m, Torque limit used for yaw pid torque split overflow
228 .yaw_pid_p = 1.15,
229 .yaw_pid_i = 0.0,
230 .yaw_pid_d = 0.0,
231 .tcs_pid_p_lowerBound_front = 55.0, // if tcs_pid_p_lowerBound_front > tcs_pid_p_upperBound_front, inverse relationship, no error
232 .tcs_pid_p_upperBound_front = 42.0,
233 .tcs_pid_p_lowerBound_rear = 32.0,
234 .tcs_pid_p_upperBound_rear = 42.0,
235 .tcs_pid_i = 0.0,
236 .tcs_pid_d = 0.0,
237 .useLaunch = false,
238 .usePIDTV = true,
239 .useTCSLimitedYawPID = true,
240 .useNormalForce = true,
241 .useTractionControl = true,
242 .usePowerLimit = true,
243 .usePIDPowerLimit = false,
244 .useDecoupledYawBrakes = true,
245 .useDiscontinuousYawPIDBrakes = true,
246 .launchDeadZone = 20.0, // N-m
247 .launchVelThreshold = 0.15, // m/s
248 .tcsVelThreshold = 1.5, // m/s
249 .yawPIDMaxDifferential = 10.0, // N-m
250 .yawPIDErrorThreshold = 0.1, // rad/s
251 .yawPIDVelThreshold = 0.35, // m/s
252 .yawPIDCoastThreshold = 2.5, // m/s
253 .yaw_pid_brakes_p = 0.1,
254 .yaw_pid_brakes_i = 0.0,
255 .yaw_pid_brakes_d = 0.0,
256 .decoupledYawPIDBrakesMaxDIfference = 2, // N-m
257 .discontinuousBrakesPercentThreshold = 0.7,
258 .TorqueMode = 21.42, // N-m
259 .RegenLimit = -15.0, // N-m
260 .useNoRegen5kph = true,
261 .useTorqueBias = true,
262 .DriveTorquePercentFront = 0.5, // DON'T TOUCH UNTIL LOAD CELL ADHERES TO DRIVE BIAS
263 .BrakeTorquePercentFront = 0.6,
264 .MechPowerMaxkW = 51.5, // kW
265 .launchLeftRightMaxDiff = 2.0, // N-m
266 .tcs_pid_lower_rpm_front = 0.0, // RPM
267 .tcs_pid_upper_rpm_front = 5000.0, // RPM
268 .tcs_pid_lower_rpm_rear = 0.0, // RPM
269 .tcs_pid_upper_rpm_rear = 5000.0, // RPM
270 .maxNormalLoadBrakeScalingFront = 1.15,
271 .tcs_saturation_front = 20,
272 .tcs_saturation_rear = 20,
273 .TCSGenLeftRightDiffLowerBound = 2, // N-m
274 .TCSGenLeftRightDiffUpperBound = 20, // N-m
275 .TCSWheelSteerLowerBound = 2, // Deg
276 .TCSWheelSteerUpperBound = 25, // Deg
277 .useRPM_TCS_GainSchedule = true, // If both are false, then P values defaults to lower bound per axle
278 .useNL_TCS_GainSchedule = false,
279 .TCS_NL_startBoundPerc_FrontAxle = 0.5,
280 .TCS_NL_endBoundPerc_FrontAxle = 0.4,
281 .TCS_NL_startBoundPerc_RearAxle = 0.5,
282 .TCS_NL_endBoundPerc_RearAxle = 0.6,
283 .useNL_TCS_SlipSchedule = true,
284 .launchSL_startBound_Front = 0.25,
285 .launchSL_endBound_Front = 0.25,
286 .launchSL_startBound_Rear = 0.3,
287 .launchSL_endBound_Rear = 0.4,
288 .TCS_SL_startBound_Front = 0.25,
289 .TCS_SL_endBound_Front = 0.15,
290 .TCS_SL_startBound_Rear = 0.3,
291 .TCS_SL_endBound_Rear = 0.4,
292 .TCS_SL_NLPerc_startBound_Front = 0.5,
293 .TCS_SL_NLPerc_endBound_Front = 0.4,
294 .TCS_SL_NLPerc_startBound_Rear = 0.5,
295 .TCS_SL_NLPerc_endBound_Rear = 0.6,
296
297 // Following used for calculate_torque_request in CASESystem.tpp
298 .max_rpm = 20000,
299 .max_regen_torque = 21.42,
300 .max_torque = 21.42,
301};
305// mode 0
307// mode 1
309// mode 2
311
312// mode 3
314// mode 4
316
318 static_cast<Controller *>(&tc_vec),
319 static_cast<Controller *>(&case_wrapper),
320 static_cast<Controller *>(&simple_launch),
321 static_cast<Controller *>(&db_controller)},
322 {false, false, true, false, true});
323
324/* Declare state machine */
326
327// /*
328// GROUPING STRUCTS (To limit parameter count in utilizing functions)
329// */
330
333
334/*
335 FUNCTION DEFINITIONS
336*/
337
338/* Initialize CAN communication */
340/* Tick interfaces */
341void tick_all_interfaces(const SysTick_s &current_system_tick);
342/* Tick all systems */
343void tick_all_systems(const SysTick_s &current_system_tick);
344/* Reset inverters */
346
347void handle_ethernet_interface_comms(const SysTick_s &systick, const hytech_msgs_MCUOutputData &out_msg);
348
349/*
350 SETUP
351*/
352
353void setup()
354{
355 // initialize CAN communication
357
361
362 SPI.begin();
363 a1.init();
364 a2.init();
365 a3.init();
366 mcu_adc.init();
367
368 a1.setChannelScale(MCU15_ACCEL1_CHANNEL, (1.0 / (float)(ACCEL1_PEDAL_MAX - ACCEL1_PEDAL_MIN)));
369 a1.setChannelScale(MCU15_ACCEL2_CHANNEL, (1.0 / (float)(ACCEL2_PEDAL_MAX - ACCEL2_PEDAL_MIN)));
370 a1.setChannelScale(MCU15_BRAKE1_CHANNEL, (1.0 / (float)(BRAKE1_PEDAL_MAX - BRAKE1_PEDAL_MIN)));
371 a1.setChannelScale(MCU15_BRAKE2_CHANNEL, (1.0 / (float)(BRAKE2_PEDAL_MAX - BRAKE2_PEDAL_MIN)));
372 a1.setChannelOffset(MCU15_ACCEL1_CHANNEL, -ACCEL1_PEDAL_MIN);
373 a1.setChannelOffset(MCU15_ACCEL2_CHANNEL, -ACCEL2_PEDAL_MIN);
374 a1.setChannelOffset(MCU15_BRAKE1_CHANNEL, -BRAKE1_PEDAL_MIN);
375 a1.setChannelOffset(MCU15_BRAKE2_CHANNEL, -BRAKE2_PEDAL_MIN);
378 a1.setChannelClamp(MCU15_STEERING_CHANNEL, -STEERING_RANGE_DEGREES / 0.5 * 1.15, STEERING_RANGE_DEGREES / 0.5 * 1.15); // 15% tolerance on each end of the steering sensor
380 a2.setChannelScale(MCU15_FL_LOADCELL_CHANNEL, LOADCELL_FL_SCALE /*Todo*/);
381 a3.setChannelScale(MCU15_FR_LOADCELL_CHANNEL, LOADCELL_FR_SCALE /*Todo*/);
382
383 a2.setChannelOffset(MCU15_FL_LOADCELL_CHANNEL, LOADCELL_FL_OFFSET /*Todo*/);
384 a3.setChannelOffset(MCU15_FR_LOADCELL_CHANNEL, LOADCELL_FR_OFFSET /*Todo*/);
385
386 mcu_adc.setAlphas(MCU15_THERM_FL, 0.95);
387 mcu_adc.setAlphas(MCU15_THERM_FR, 0.95);
388 // get latest tick from sys clock
389 SysTick_s curr_tick = sys_clock.tick(micros());
390
391 /*
392 Init Interfaces
393 */
394
395 main_ecu.init(); // initial shutdown circuit readings,
396 wd_interface.init(curr_tick.millis); // initialize wd kick time
397 ams_interface.init(curr_tick); // initialize last heartbeat time
398 steering1.init();
400
401 Serial.begin(115200);
402
403 /*
404 Init Systems
405 */
406 safety_system.init();
407
408 // Drivetrain set all inverters disabled
409 drivetrain.disable(); // write inv_en and inv_24V_en low: writing high in previous code though, should double check
410 // would an error list be good for debugging? i.e. which inverter has error
411
412 // ControllerMux set max torque to 20 NM, torque mode to whatever makes most sense
413 // Preventing drivers from forgetting to toggle torque mode and end up self-derating at comp
414 // Not strictly necessary at the moment, just don't forget
415}
416
417void loop()
418{
419 // get latest tick from sys clock
420 SysTick_s curr_tick = sys_clock.tick(micros());
421
422 // process received CAN messages
425
426 // tick interfaces
427 tick_all_interfaces(curr_tick);
428
429 // tick systems
430
431 // single source of truth for the state of the car.
432 // no systems or interfaces should write directly to this.
433 SharedCarState_s car_state_inst(curr_tick,
434 steering_system.getSteeringSystemData(),
439 vn_interface.get_vn_struct(),
443
445 hytech_msgs_MCUOutputData out_eth_msg = db_eth_interface.make_db_msg(car_state_inst);
446
447 handle_ethernet_interface_comms(curr_tick, out_eth_msg);
448
449 tick_all_systems(curr_tick);
450
451 // inverter procedure before entering state machine
452 // reset inverters
453 if (dashboard.inverterResetButtonPressed() && drivetrain.drivetrain_error_occured())
454 {
456 }
457 // tick state machine
458
459 fsm.tick_state_machine(curr_tick.millis, car_state_inst);
460
461 // tick safety system
462 safety_system.software_shutdown(curr_tick);
463
464 // send CAN
467
468 // Basic debug prints
469 // if (curr_tick.triggers.trigger5)
470 if (DEBUG_PRINTS)
471 {
472 Serial.print("Steering system reported angle (deg): ");
473 Serial.println(steering_system.getSteeringSystemData().angle);
474 Serial.print("Steering system status: ");
475 Serial.println(static_cast<uint8_t>(steering_system.getSteeringSystemData().status));
476 Serial.print("Primary sensor angle: ");
477 Serial.println(steering1.convert().angle);
478 Serial.print("Secondary sensor angle: ");
479 Serial.print(a1.get().conversions[MCU15_STEERING_CHANNEL].conversion);
480 Serial.print(" raw: ");
481 Serial.println(a1.get().conversions[MCU15_STEERING_CHANNEL].raw);
482 Serial.print("Sensor divergence: ");
483 Serial.println(steering1.convert().angle - a1.get().conversions[MCU15_STEERING_CHANNEL].conversion);
484 Serial.println();
485 Serial.println("Pedal outputs:");
486 Serial.print("Accel 1 raw: ");
487 Serial.println(a1.get().conversions[MCU15_ACCEL1_CHANNEL].raw);
488 Serial.print("Accel 2 raw: ");
489 Serial.println(a1.get().conversions[MCU15_ACCEL2_CHANNEL].raw);
490 Serial.print("Accel percent: ");
492 Serial.print("Brake 1 raw: ");
493 Serial.println(a1.get().conversions[MCU15_BRAKE1_CHANNEL].raw);
494 Serial.print("Brake 2 raw: ");
495 Serial.println(a1.get().conversions[MCU15_BRAKE2_CHANNEL].raw);
496 Serial.print("Brake percent: ");
498 Serial.println();
499 Serial.print("Derating factor: ");
500 Serial.println(ams_interface.get_acc_derate_factor());
501 Serial.print("Filtered min cell voltage: ");
502 Serial.println(ams_interface.get_filtered_min_cell_voltage());
503 Serial.print("Filtered max cell temp: ");
504 Serial.println(ams_interface.get_filtered_max_cell_temp());
505 Serial.print("Current TC index: ");
506 Serial.println(static_cast<int>(torque_controller_mux.get_tc_mux_status().active_controller_mode));
507 Serial.print("Current TC error: ");
508 Serial.println(static_cast<int>(torque_controller_mux.get_tc_mux_status().active_error));
509 Serial.println();
510 Serial.print("dial state: ");
511 Serial.println(static_cast<int>(dashboard.getDialMode()));
512 Serial.println();
513 Serial.println();
514 }
515}
516
517/*
518 Initialize CAN comm.
519*/
520
522{
523 // Inverter CAN line
524 INV_CAN.begin();
525 INV_CAN.setBaudRate(INV_CAN_BAUDRATE);
526 INV_CAN.setMaxMB(16);
527 INV_CAN.enableFIFO();
528 INV_CAN.enableFIFOInterrupt();
529 INV_CAN.onReceive(on_can2_receive);
530 INV_CAN.mailboxStatus();
531
532 // Telemetry CAN line
533 TELEM_CAN.begin();
534 TELEM_CAN.setBaudRate(TELEM_CAN_BAUDRATE);
535 TELEM_CAN.setMaxMB(16);
536 TELEM_CAN.enableFIFO();
537 TELEM_CAN.enableFIFOInterrupt();
538 TELEM_CAN.onReceive(on_can3_receive);
539 TELEM_CAN.mailboxStatus();
540}
541
542/*
543 TICK INTERFACES
544*/
545
546void tick_all_interfaces(const SysTick_s &current_system_tick)
547{
548
549 TriggerBits_s t = current_system_tick.triggers;
550 if (t.trigger10) // 10Hz
551 {
552 dashboard.tick10(
553 &main_ecu,
554 int(fsm.get_state()),
558 ams_interface.get_filtered_min_cell_voltage(),
559 a1.get().conversions[MCU15_GLV_SENSE_CHANNEL],
561 dashboard.getDialMode());
562
563 main_ecu.tick(
564 static_cast<int>(fsm.get_state()),
566 safety_system.get_software_is_ok(),
570 ams_interface.pack_charge_is_critical(),
571 dashboard.launchControlButtonPressed());
572
574 front_thermistors_interface.tick(mcu_adc.get().conversions[MCU15_THERM_FL_CHANNEL], mcu_adc.get().conversions[MCU15_THERM_FR_CHANNEL]);
575 // TODO pass in the shared state instead
576 telem_interface.tick(
577 a1.get(),
578 a2.get(),
579 a3.get(),
580 steering1.convert(),
581 &inv.fl,
582 &inv.fr,
583 &inv.rl,
584 &inv.rr,
585 data2.accelImplausible,
586 data2.brakeImplausible,
587 data2.accelPercent,
588 data2.brakePercent,
589 a1.get().conversions[MCU15_ACCEL1_CHANNEL],
590 a1.get().conversions[MCU15_ACCEL2_CHANNEL],
591 a1.get().conversions[MCU15_BRAKE1_CHANNEL],
592 a1.get().conversions[MCU15_BRAKE2_CHANNEL],
595
596 ams_interface.tick(current_system_tick);
597
598 }
599
600 if (t.trigger50) // 50Hz
601 {
602 steering1.sample();
603 }
604
605 if (t.trigger100) // 100Hz
606 {
607
608 a1.tick();
609 a2.tick();
610 a3.tick();
611 mcu_adc.tick();
614 .FLConversion = a2.get().conversions[MCU15_FL_LOADCELL_CHANNEL],
615 .FRConversion = a3.get().conversions[MCU15_FR_LOADCELL_CHANNEL],
616 .RLConversion = sab_interface.rlLoadCell.convert(),
617 .RRConversion = sab_interface.rrLoadCell.convert()});
618 }
620 .FLConversion = a2.get().conversions[MCU15_FL_LOADCELL_CHANNEL],
621 .FRConversion = a3.get().conversions[MCU15_FR_LOADCELL_CHANNEL],
622 .RLConversion = sab_interface.rlLoadCell.convert(),
623 .RRConversion = sab_interface.rrLoadCell.convert()});
624 // // Untriggered
625 main_ecu.read_mcu_status(); // should be executed at the same rate as state machine
626 // DO NOT call in main_ecu.tick()
627}
628
629// /*
630// TICK SYSTEMS
631// */
632
633void tick_all_systems(const SysTick_s &current_system_tick)
634{
635 // tick pedals system
636
638 current_system_tick,
639 a1.get().conversions[MCU15_ACCEL1_CHANNEL],
640 a1.get().conversions[MCU15_ACCEL2_CHANNEL],
641 a1.get().conversions[MCU15_BRAKE1_CHANNEL],
642 a1.get().conversions[MCU15_BRAKE2_CHANNEL]);
643
644 // tick steering system
645 steering_system.tick(
647 .tick = current_system_tick,
648 .secondaryConversion = a1.get().conversions[MCU15_STEERING_CHANNEL]});
649
650 // tick drivetrain system
651 drivetrain.tick(current_system_tick);
652 // // tick torque controller mux
653
654 auto __attribute__((unused)) case_status = case_system.evaluate(
655 current_system_tick,
656 vn_interface.get_vn_struct(),
657 steering_system.getSteeringSystemData(),
659 load_cell_interface.getLoadCellForces().loadCellConversions, // should CASE use filtered load cells?
661 0,
662 fsm.get_state(),
663 dashboard.startButtonPressed(),
664 vn_interface.get_vn_struct().vn_status);
665}
666
667void handle_ethernet_interface_comms(const SysTick_s &systick, const hytech_msgs_MCUOutputData &out_msg)
668{
669 // function that will handle receiving and distributing of all messages to all ethernet interfaces
670 // via the union message. this is a little bit cursed ngl.
671
672 std::function<void(unsigned long, const uint8_t *, size_t, DrivebrainETHInterface &, const pb_msgdesc_t *)> recv_boi = &recv_pb_stream_msg<hytech_msgs_MCUCommandData, DrivebrainETHInterface>;
673 handle_ethernet_socket_receive<1024, hytech_msgs_MCUCommandData>(systick, &protobuf_recv_socket, recv_boi, db_eth_interface, hytech_msgs_MCUCommandData_fields);
674
675 if (systick.triggers.trigger1000)
676 {
677 handle_ethernet_socket_send_pb<hytech_msgs_MCUOutputData, 1024>(EthParams::default_TCU_ip, EthParams::default_protobuf_send_port, &protobuf_send_socket, out_msg, hytech_msgs_MCUOutputData_fields);
678 }
679}
const int MCU15_BRAKE1_CHANNEL
const int MCU15_STEERING_CHANNEL
const float LOADCELL_FL_SCALE
const int MCU15_THERM_FR
const int MCU15_ACCEL2_CHANNEL
const int BRAKE2_PEDAL_OOR_MIN
const float LOADCELL_FL_OFFSET
const int WATCHDOG_INPUT
const int ACCEL1_PEDAL_MIN
const int BRAKE1_PEDAL_MAX
const int BRAKE2_PEDAL_MIN
const int MCU15_CUR_POS_SENSE_CHANNEL
const int ADC1_CS
Definition: MCU_rev15_defs.h:9
const float DEFAULT_PEDAL_IMPLAUSIBILITY_MARGIN
const int MCU15_GLV_SENSE_CHANNEL
const int BRAKE1_PEDAL_MIN
const int ACCEL1_PEDAL_MAX
const float BRAKE_ACTIVATION_PERCENTAGE
const unsigned long TELEM_CAN_BAUDRATE
const float GLV_SENSE_SCALE
const float LOADCELL_RL_SCALE
const float STEERING_RANGE_DEGREES
const int ACCEL2_PEDAL_OOR_MIN
const int MCU15_THERM_FL
const int ACCEL2_PEDAL_MIN
const int MCU15_FL_POTS_CHANNEL
const float STEERING_IIR_ALPHA
const int DEFAULT_ANALOG_PINS[MCU15_TEENSY_ADC_CHANNELS]
const int BRAKE1_PEDAL_OOR_MIN
const int ACCEL2_PEDAL_MAX
const int ACCEL1_PEDAL_OOR_MAX
const float DEFAULT_PEDAL_DEADZONE
const int SOFTWARE_OK
const float LOADCELL_FR_SCALE
const int MCU15_FR_LOADCELL_CHANNEL
const float APPS_ACTIVATION_PERCENTAGE
const int ACCEL2_PEDAL_OOR_MAX
const int ADC2_CS
const int MCU15_BRAKE2_CHANNEL
const int MCU15_FL_LOADCELL_CHANNEL
const float LOADCELL_RL_OFFSET
const int SECONDARY_STEERING_SENSE_RIGHTMOST_BOUND
const int MCU15_ACCEL1_CHANNEL
const int ACCEL1_PEDAL_OOR_MIN
const float PRIMARY_STEERING_SENSE_OFFSET
const unsigned long INV_CAN_BAUDRATE
const int MCU15_CUR_NEG_SENSE_CHANNEL
const unsigned long BUZZER_ON_INTERVAL
const float BRAKE_MECH_THRESH
const float LOADCELL_RR_OFFSET
const float LOADCELL_RR_SCALE
const int BRAKE2_PEDAL_MAX
const int BRAKE2_PEDAL_OOR_MAX
const float LOADCELL_FR_OFFSET
const int BRAKE1_PEDAL_OOR_MAX
const int MCU15_FR_POTS_CHANNEL
const unsigned long INVERTER_ENABLING_TIMEOUT_INTERVAL
const int SECONDARY_STEERING_SENSE_LEFTMOST_BOUND
const int SECONDARY_STEERING_SENSE_CENTER
const int ADC3_CS
Circular_Buffer< uint8_t,(uint32_t) 128, sizeof(CAN_message_t)> CANBufferType
const int MCU15_THERM_FR_CHANNEL
const int MCU15_THERM_FL_CHANNEL
this class is for interfacing with the AMS (accumulator management system)
Definition: AMSInterface.h:27
AnalogConversion_s convert()
Calculate sensor output and whether result is in sensor's defined bounds. DOES NOT SAMPLE.
bool buzzer_is_on()
Definition: Buzzer.h:16
this class with both take in sensor inputs as well as handle calculations for various derived states ...
Definition: CASESystem.h:97
Base class for all controllers, which define drivetrain command containing different variations of
Drivebrain Controller for fail-safe pass-through control of the car RESPONSIBILITIES AND DOCUMENTATIO...
bool get_timing_failure_status()
getter for the current status of whether or not the controller has had a timing failure during operat...
hytech_msgs_MCUOutputData make_db_msg(const SharedCarState_s &shared_state)
DrivebrainData_s get_latest_data()
bool drivetrain_error_occured()
void tick(const SysTick_s &tick)
DrivetrainDynamicReport_s get_dynamic_data()
void reset_drivetrain()
LoadCellInterfaceOutput_s getLoadCellForces()
void tick(const LoadCellInterfaceTick_s &intake)
LoadCellInterfaceRawOutput_s get_latest_raw_data()
void update_raw_data(const LoadCellInterfaceTick_s &intake)
PedalsSystemData_s getPedalsSystemDataCopy()
Definition: PedalsSystem.h:67
const PedalsSystemData_s & getPedalsSystemData()
Definition: PedalsSystem.h:62
void tick(const SysTick_s &tick, const AnalogConversion_s &accel1, const AnalogConversion_s &accel2, const AnalogConversion_s &brake)
overloaded tick function that runs the evaluation of the pedals system. evaluates brake using only mi...
float getMechBrakeActiveThreshold()
Definition: PedalsSystem.h:72
AnalogChannel rlLoadCell
Definition: SABInterface.h:12
AnalogChannel rrLoadCell
Definition: SABInterface.h:13
makes CASE system a part of Controller hierarchy for use in TC Mux
const TorqueControllerMuxStatus & get_tc_mux_status()
void init(unsigned long curr_millis)
void on_can2_receive(const CAN_message_t &msg)
Circular_Buffer< uint8_t,(uint32_t) 16, sizeof(CAN_message_t)> CAN3_rxBuffer
void process_ring_buffer(BufferType &rx_buffer, const InterfaceType &interfaces, unsigned long curr_millis)
void send_all_CAN_msgs(bufferType &buffer, FlexCAN_T4_Base *can_interface)
void on_can3_receive(const CAN_message_t &msg)
Circular_Buffer< uint8_t,(uint32_t) 16, sizeof(CAN_message_t)> CAN2_rxBuffer
CANBufferType CAN2_txBuffer
const uint16_t default_protobuf_send_port
uint8_t default_MCU_MAC_address[6]
const IPAddress default_TCU_ip(192, 168, 1, 69)
const uint16_t default_protobuf_recv_port
const IPAddress default_MCU_ip(192, 168, 1, 30)
LoadCellInterface load_cell_interface
Definition: main.cpp:199
SteeringSystem steering_system & steering1
Definition: main.cpp:216
PedalsSystem pedals_system(accel_params, brake_params)
TorqueControllerSimple tc_simple(1.0f, 1.0f)
InverterInterface< CircularBufferType > InvInt_t
Definition: main.cpp:202
SABInterface sab_interface(LOADCELL_RL_SCALE, LOADCELL_RL_OFFSET, LOADCELL_RR_SCALE, LOADCELL_RR_OFFSET)
DrivebrainController db_controller(210)
void drivetrain_reset()
OrbisBR10 steering1 & Serial5
Definition: main.cpp:180
FlexCAN_T4< CAN2, RX_SIZE_256, TX_SIZE_16 > INV_CAN
Definition: main.cpp:168
void setup()
Definition: main.cpp:353
const PedalsParams accel2_only_params
Definition: main.cpp:87
TCMuxType torque_controller_mux({static_cast< Controller * >(&tc_simple), static_cast< Controller * >(&tc_vec), static_cast< Controller * >(&case_wrapper), static_cast< Controller * >(&simple_launch), static_cast< Controller * >(&db_controller)}, {false, false, true, false, true})
TorqueControllerLoadCellVectoring tc_vec
Definition: main.cpp:308
const PedalsParams accel_params
Definition: main.cpp:101
#define DEBUG_PRINTS
Definition: main.cpp:4
void tick_all_interfaces(const SysTick_s &current_system_tick)
Definition: main.cpp:546
EthernetUDP protobuf_send_socket
Definition: main.cpp:164
Teensy_ADC< 2 > mcu_adc
Definition: main.cpp:179
DriveSys_t drivetrain
Definition: main.cpp:223
CANBufferType CircularBufferType
Definition: main.cpp:173
TorqueControllerCASEWrapper< CircularBufferType > case_wrapper & case_system
Definition: main.cpp:310
const PedalsParams brake1_only_params
Definition: main.cpp:115
const PedalsParams brake_params
Definition: main.cpp:145
const TelemetryInterfaceReadChannels telem_read_channels
Definition: main.cpp:57
MCP_ADC< 4 > a2
Definition: main.cpp:177
struct inverters inv
BuzzerController buzzer(BUZZER_ON_INTERVAL)
Definition: main.cpp:325
CANInterfaces< CircularBufferType > CAN_receive_interfaces
Definition: main.cpp:332
WatchdogInterface wd_interface(WATCHDOG_INPUT)
VNInterface< CircularBufferType > vn_interface & CAN3_txBuffer
Definition: main.cpp:186
ETHInterfaces ethernet_interfaces
Definition: main.cpp:185
MCP_ADC< 8 > a1
Definition: main.cpp:176
DrivebrainETHInterface db_eth_interface
Definition: main.cpp:331
FlexCAN_T4< CAN3, RX_SIZE_256, TX_SIZE_16 > TELEM_CAN
Definition: main.cpp:169
EthernetUDP protobuf_recv_socket
Definition: main.cpp:165
const PedalsParams brake2_only_params
Definition: main.cpp:130
void tick_all_systems(const SysTick_s &current_system_tick)
Definition: main.cpp:633
CASEConfiguration case_config
Definition: main.cpp:225
void handle_ethernet_interface_comms(const SysTick_s &systick, const hytech_msgs_MCUOutputData &out_msg)
Definition: main.cpp:667
SafetySystem safety_system & ams_interface
Definition: main.cpp:219
const PedalsParams accel1_only_params
Definition: main.cpp:73
SysClock sys_clock
Definition: main.cpp:215
void init_all_CAN_devices()
Definition: main.cpp:521
MCP_ADC< 4 > a3
Definition: main.cpp:178
RateLimitedLogger logger
Definition: main.cpp:302
DrivetrainSystem< InvInt_t > DriveSys_t
Definition: main.cpp:222
TorqueControllerSimpleLaunch simple_launch
Definition: main.cpp:313
void loop()
Definition: main.cpp:417
float AbsoluteTorqueLimit
Definition: CASESystem.h:15
veh_vec< AnalogConversion_s > loadCellConversions
system interface struct that contains the data from the pedal system
Definition: PedalsSystem.h:26
car state struct that contains state of everything about the car including
TorqueLimit_e active_torque_limit_enum
TorqueControllerMuxError active_error
ControllerMode_e active_controller_mode
InvInt_t rr
Definition: main.cpp:208
InvInt_t rl
Definition: main.cpp:207
InvInt_t fr
Definition: main.cpp:206
InvInt_t fl
Definition: main.cpp:205