MCU
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
DrivebrainController Class Reference

Drivebrain Controller for fail-safe pass-through control of the car RESPONSIBILITIES AND DOCUMENTATION. More...

#include <DrivebrainController.h>

Inheritance diagram for DrivebrainController:
Inheritance graph
Collaboration diagram for DrivebrainController:
Collaboration graph

Public Member Functions

 DrivebrainController (unsigned long allowed_latency, ControllerMode_e assigned_controller_mode=ControllerMode_e::MODE_4)
 constructor for the drivebrain controller class More...
 
TorqueControllerOutput_s evaluate (const SharedCarState_s &state)
 evaluate function for running the business logic More...
 
bool get_timing_failure_status ()
 getter for the current status of whether or not the controller has had a timing failure during operation More...
 
virtual TorqueControllerOutput_s evaluate (const SharedCarState_s &state)=0
 This mehod must be implemented by every controller in the Tc Muxer. This is called in the Muxer whenever the drivetrain command is obtained. TorqueControllerMux.cpp to see that in every tick of the system, the active controller must be ticked through this method. More...
 

Private Attributes

struct {
   unsigned long   allowed_latency
 
   ControllerMode_e   assigned_controller_mode
 
_params
 
unsigned long _last_worst_latency_timestamp
 
int64_t _worst_latency_so_far
 
bool _timing_failure = false
 
unsigned long _last_setpoint_millis = -1
 
TorqueControllerSimple _emergency_control
 

Detailed Description

Drivebrain Controller for fail-safe pass-through control of the car RESPONSIBILITIES AND DOCUMENTATION.

this class is the "controller" that allows for pass-through control as commanded by the drivebrain. It also calculates the latency of the most recent input and checks to see if the most recent input is still valid and has not expired. If the input has expired then it switches over to the fail-safe control mode (MODE0) to allow for safe failing even while the car is driving so that we dont lose hard-braking capabilities.

This class can clear it's own fault by switching off of this operating mode and then swapping back to this operating mode. The fault clears the first time this controller gets evaluated while switch from the swapped-to mode back to this pass through mode.

latency measurement:

config:

Definition at line 52 of file DrivebrainController.h.

Constructor & Destructor Documentation

◆ DrivebrainController()

DrivebrainController::DrivebrainController ( unsigned long  allowed_latency,
ControllerMode_e  assigned_controller_mode = ControllerMode_e::MODE_4 
)
inline

constructor for the drivebrain controller class

Parameters
allowed_latencythe allowed latency in milliseconds for which if the most recent packet has a timestamp older than this measure of time we fail safe
assigned_controller_modethe controller mode that the drivebrain controller is assigned to. is required for evaluating whether or not we are active or not

Definition at line 59 of file DrivebrainController.h.

61 : _emergency_control(1.0f, 1.0f)
62 {
66 }
struct DrivebrainController::@0 _params
unsigned long _last_worst_latency_timestamp
ControllerMode_e assigned_controller_mode
TorqueControllerSimple _emergency_control

Member Function Documentation

◆ evaluate()

TorqueControllerOutput_s DrivebrainController::evaluate ( const SharedCarState_s state)
virtual

evaluate function for running the business logic

Parameters
statethe current state of the car
Returns
torque controller output that gets passed through the TC MUX

Implements Controller.

Definition at line 3 of file DrivebrainController.cpp.

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}
unsigned long _last_setpoint_millis
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]
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

◆ get_timing_failure_status()

bool DrivebrainController::get_timing_failure_status ( )
inline

getter for the current status of whether or not the controller has had a timing failure during operation

Returns
bool of status

Definition at line 75 of file DrivebrainController.h.

75{ return _timing_failure; }

Member Data Documentation

◆ _emergency_control

TorqueControllerSimple DrivebrainController::_emergency_control
private

Definition at line 88 of file DrivebrainController.h.

◆ _last_setpoint_millis

unsigned long DrivebrainController::_last_setpoint_millis = -1
private

Definition at line 87 of file DrivebrainController.h.

◆ _last_worst_latency_timestamp

unsigned long DrivebrainController::_last_worst_latency_timestamp
private

Definition at line 84 of file DrivebrainController.h.

◆ 

struct { ... } DrivebrainController::_params

◆ _timing_failure

bool DrivebrainController::_timing_failure = false
private

Definition at line 86 of file DrivebrainController.h.

◆ _worst_latency_so_far

int64_t DrivebrainController::_worst_latency_so_far
private

Definition at line 85 of file DrivebrainController.h.

◆ allowed_latency

unsigned long DrivebrainController::allowed_latency

Definition at line 80 of file DrivebrainController.h.

◆ assigned_controller_mode

ControllerMode_e DrivebrainController::assigned_controller_mode

Definition at line 81 of file DrivebrainController.h.


The documentation for this class was generated from the following files: