MCU
Loading...
Searching...
No Matches
DrivebrainController.cpp
Go to the documentation of this file.
2
4{
5
6 auto sys_tick = state.systick;
7 auto db_input = state.db_data;
8
9 // cases for timing_failure:
10 // 1 if the last_receive_time_millis < 0, then we have not received any messages from the db (initialized at -1 in constructor)
11 bool no_messages_received = db_input.last_receive_time_millis < 0;
12
13 // 2 if the DB_prev_MCU_recv_millis < 0, then the drivebrain has not received a time from the MCU
14 // (meaning that the MCU is not sending properly or the drivebrain is not receiving properly or it has
15 // yet to receive from the MCU yet)
16 bool drivebrain_has_not_received_time = (db_input.DB_prev_MCU_recv_millis < 0);
17 // Serial.println("uh");
18 // 3 if the time between the current MCU sys_tick.millis time and the last millis time that the drivebrain received is too high
19 bool message_too_latent = (::abs((int)(sys_tick.millis - db_input.DB_prev_MCU_recv_millis)) > (int)_params.allowed_latency);
20 if((sys_tick.millis - _last_worst_latency_timestamp) > 5000)
21 {
22 _last_worst_latency_timestamp = sys_tick.millis;
24 }
25
26 if( (sys_tick.millis - db_input.DB_prev_MCU_recv_millis) > _worst_latency_so_far)
27 {
28 _worst_latency_so_far = (sys_tick.millis - db_input.DB_prev_MCU_recv_millis);
29 }
30
31
32 bool timing_failure = (message_too_latent || no_messages_received || drivebrain_has_not_received_time);
33
34 // only in the case that our speed is low enough (<1 m/s) do we want to clear the fault
35
36 bool is_active_controller = state.tc_mux_status.active_controller_mode == _params.assigned_controller_mode;
37
38 if ((!is_active_controller) && (!timing_failure))
39 {
40 // timing failure should be false here
41 _timing_failure = false;
42 }
43
45 if (!timing_failure && (!_timing_failure))
46 {
47 _last_setpoint_millis = db_input.last_receive_time_millis;
48
49 db_input.speed_setpoints_rpm.copy_to_arr(output.command.speeds_rpm);
50 db_input.torque_limits_nm.copy_to_arr(output.command.inverter_torque_limit);
51 }
52 else
53 {
54 _timing_failure = true;
55 // output.command = {{0.0f}, {0.0f}};
56 output = _emergency_control.evaluate(state);
57 }
58 return output;
59}
struct DrivebrainController::@0 _params
unsigned long _last_worst_latency_timestamp
TorqueControllerOutput_s evaluate(const SharedCarState_s &state)
evaluate function for running the business logic
unsigned long _last_setpoint_millis
TorqueControllerSimple _emergency_control
TorqueControllerOutput_s evaluate(const SharedCarState_s &state) override
int64_t last_receive_time_millis
the latest time that the MCU received a message w.r.t the current tick's millis
Definition: DrivebrainData.h:9
float inverter_torque_limit[NUM_MOTORS]
float speeds_rpm[NUM_MOTORS]
car state struct that contains state of everything about the car including
DrivebrainData_s db_data
TorqueControllerMuxStatus tc_mux_status
ControllerMode_e active_controller_mode
Packages drivetrain command with ready boolean to give feedback on controller successfully evaluating...
DrivetrainCommand_s command