MCU
Loading...
Searching...
No Matches
TorqueControllerMux.h
Go to the documentation of this file.
1#ifndef TORQUECONTROLLERMUX
2#define TORQUECONTROLLERMUX
3
4#include <unordered_map>
5#include <array>
6#include "SharedDataTypes.h"
7#include "BaseController.h"
8
9// notes:
10// 21 torque limit should be first
11
12// tc mux needs to handle these things:
13// 1 swapping between controller outputs
14// 2 turning on and off running of controllers
15// 3 application of safeties and limits to controller outputs
16// 4 torque limit changing (torque mode) -->
17// TODO the torque limit value changing should be handled in the dashboard interface
18
19
20// TODOs
21// - [x] make the controllers inherit from the base controller class
22// - [x] port TorqueControllerSimple
23// - [x] port TorqueControllerLoadCellVectoring
24// - [x] port BaseLaunchController
25// - [x] port TorqueControllerSimpleLaunch
26// - [x] port slip launch
27// - [x] port TorqueControllerLookupLaunch
28// - [x] port CASE
29// - [x] add the torque limit evaluation logic into dashboard interface
30// - [x] integrate into state machine
31// - [x] pass through the car state
32// - [x] get dial and torque mode from the dashboard
33// - [x] create car_state in main and pass into state machine
34// - [x] add 3 bit status to a telemetry message for the TC mux status to HT_CAN
35// - [x] pass state of tc mux into telem interface and add the CAN signal
36// - [x] remove the old tc mux
37// - [ ] add back checking of the ready flag of the controllers and if the controller isnt ready it defaults
38// - [ ] add test for this
39// - [ ] make folder for the controllers
40// - [ ] write integration tests for the real controllers
41// - [x] test construction with real controllers
42// - [ ] ensure that sane outputs occur on first tick of each controller
43// - [x] update the state machine unit test with integration test of new tc mux
44
45// ON CAR testing
46// - [ ] test the change of the torque mode from the dashboard interface
47// - [ ] write testing code for this in separate environment
48
49
53{
54 constexpr const float MAX_SPEED_FOR_MODE_CHANGE = 5.0; // m/s
55 constexpr const float MAX_TORQUE_DELTA_FOR_MODE_CHANGE = 0.5; // Nm
56 constexpr const float MAX_POWER_LIMIT = 63000.0; // watts of mechanical power
57};
58
59template <std::size_t num_controllers>
61{
62 static_assert(num_controllers > 0, "Must create TC mux with at least 1 controller");
63
64
65
66private:
67 std::array<Controller *, num_controllers> controller_pointers_;
68
69 std::array<bool, num_controllers> mux_bypass_limits_;
70
71 std::unordered_map<TorqueLimit_e, float> torque_limit_map_ = {
72 {TorqueLimit_e::TCMUX_FULL_TORQUE, PhysicalParameters::AMK_MAX_TORQUE},
73 {TorqueLimit_e::TCMUX_MID_TORQUE, 15.0f},
74 {TorqueLimit_e::TCMUX_LOW_TORQUE, 10.0f}};
79 DrivetrainCommand_s previous_controller_command,
80 DrivetrainCommand_s desired_controller_out);
81
86
92
98 DrivetrainCommand_s apply_power_limit_(const DrivetrainCommand_s &command, const DrivetrainDynamicReport_s &drivetrain, float power_limit, float max_torque);
99
105
106public:
107
116 explicit TorqueControllerMux(std::array<Controller *, num_controllers> controller_pointers,
117 std::array<bool, num_controllers> mux_bypass_limits,
119 float max_torque_pos_change_delta = TC_MUX_DEFAULT_PARAMS::MAX_TORQUE_DELTA_FOR_MODE_CHANGE,
120 float max_power_limit = TC_MUX_DEFAULT_PARAMS::MAX_POWER_LIMIT) : controller_pointers_(controller_pointers),
121 mux_bypass_limits_(mux_bypass_limits),
122 max_change_speed_(max_change_speed),
123 max_torque_pos_change_delta_(max_torque_pos_change_delta),
124 max_power_limit_(max_power_limit)
125
126
127 {
128 static_assert(num_controllers > 0, "Must create TC mux with at least 1 controller");
129
130 }
131
133
140 TorqueLimit_e controller_command_torque_limit,
141 const SharedCarState_s &input_state);
142};
143// }
144
147
148#include "TorqueControllerMux.tpp"
149#endif // __TorqueControllerMux_H__
TorqueLimit_e
Defines modes of torque limit to be processed in torque limit map for exact values.
ControllerMode_e
TorqueControllerMuxError
Defines errors for TC Mux to use to maintain system safety.
const int number_of_controllers
DrivetrainCommand_s apply_torque_limit_(const DrivetrainCommand_s &command, float max_torque)
Ensure torque is at most at the specified limit. If exceeding, then limit it in the returned Drivetra...
DrivetrainCommand_s getDrivetrainCommand(ControllerMode_e requested_controller_type, TorqueLimit_e controller_command_torque_limit, const SharedCarState_s &input_state)
function that evaluates the mux, controllers and gets the active command
std::array< Controller *, num_controllers > controller_pointers_
DrivetrainCommand_s apply_regen_limit_(const DrivetrainCommand_s &command, const DrivetrainDynamicReport_s &drivetrain_data)
begin limiting regen at noRegenLimitKPH (hardcoded in func) and completely limit regen at fullRegenLi...
DrivetrainCommand_s prev_command_
DrivetrainCommand_s apply_power_limit_(const DrivetrainCommand_s &command, const DrivetrainDynamicReport_s &drivetrain, float power_limit, float max_torque)
Apply power limit (watts) such that the mechanical power of all wheels never exceeds the preset mecha...
DrivetrainCommand_s apply_positive_speed_limit_(const DrivetrainCommand_s &command)
Clamps negative rpms to 0f.
std::array< bool, num_controllers > mux_bypass_limits_
std::unordered_map< TorqueLimit_e, float > torque_limit_map_
TorqueControllerMuxError can_switch_controller_(DrivetrainDynamicReport_s active_drivetrain_data, DrivetrainCommand_s previous_controller_command, DrivetrainCommand_s desired_controller_out)
const TorqueControllerMuxStatus & get_tc_mux_status()
TorqueControllerMux(std::array< Controller *, num_controllers > controller_pointers, std::array< bool, num_controllers > mux_bypass_limits, float max_change_speed=TC_MUX_DEFAULT_PARAMS::MAX_SPEED_FOR_MODE_CHANGE, float max_torque_pos_change_delta=TC_MUX_DEFAULT_PARAMS::MAX_TORQUE_DELTA_FOR_MODE_CHANGE, float max_power_limit=TC_MUX_DEFAULT_PARAMS::MAX_POWER_LIMIT)
constructor for the TC mux
TorqueControllerMuxStatus active_status_
TorqueControllerMux()=delete
const float AMK_MAX_TORQUE
Contains a max speed for mode changes(5 m/s), a max torque delta for mode change(....
constexpr const float MAX_POWER_LIMIT
constexpr const float MAX_SPEED_FOR_MODE_CHANGE
constexpr const float MAX_TORQUE_DELTA_FOR_MODE_CHANGE
DriveSys_t drivetrain
Definition: main.cpp:223
Stores setpoints for a command to the Drivetrain, containing speed and torque setpoints for each moto...
car state struct that contains state of everything about the car including
packages TC Mux indicators: errors, mode, torque limit, bypass