Skip to content

Commit

Permalink
added todos for drivetrain system testing and mock inverter interface…
Browse files Browse the repository at this point in the history
… for use within
  • Loading branch information
RCMast3r committed Feb 8, 2024
1 parent 47f2fa2 commit 54eb7fe
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 62 deletions.
5 changes: 2 additions & 3 deletions lib/systems/include/DrivetrainSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
#include "stdint.h"
struct DrivetrainCommand_s
{
float speeds[NUM_MOTORS];
float posTorqueLimits[NUM_MOTORS];
float negTorqueLimits[NUM_MOTORS];
float speeds_rpm[NUM_MOTORS];
float torqueSetpoints[NUM_MOTORS];
};

struct DrivetrainDynamicReport_s
Expand Down
11 changes: 6 additions & 5 deletions lib/systems/include/DrivetrainSystem.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ void DrivetrainSystem<InverterType>::reset_drivetrain()
template <typename InverterType>
void DrivetrainSystem<InverterType>::command_drivetrain(const DrivetrainCommand_s &data)
{

// inverters_[0]->handle_command(data.left_front_inverter_cmd);
// inverters_[1]->handle_command(data.right_front_inverter_cmd);
// inverters_[2]->handle_command(data.left_rear_inverter_cmd);
// inverters_[3]->handle_command(data.right_rear_inverter_cmd);
int index = 0;
for (auto inv_pointer : inverters_)
{
inv_pointer->handle_command({data.torqueSetpoints[index], data.speeds_rpm[index]});
index++;
}
}

// feedback functions
Expand Down
5 changes: 2 additions & 3 deletions lib/systems/include/TorqueControllers.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ class TorqueControllerNone : public TorqueController<TC_NO_CONTROLLER>
{
private:
DrivetrainCommand_s data_ = {
.speeds = {0.0, 0.0, 0.0, 0.0},
.posTorqueLimits = {0.0, 0.0, 0.0, 0.0},
.negTorqueLimits = {0.0, 0.0, 0.0, 0.0}
.speeds_rpm = {0.0, 0.0, 0.0, 0.0},
.torqueSetpoints= {0.0, 0.0, 0.0, 0.0}
};
public:
TorqueControllerNone(DrivetrainCommand_s& writeout)
Expand Down
13 changes: 5 additions & 8 deletions lib/systems/src/TorqueControllerMux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,12 @@ void TorqueControllerMux::tick(
bool torqueDeltaPreventsModeChange = false;
for (int i = 0; i < NUM_MOTORS; i++)
{
float posTorqueDelta = abs(
controllerCommands_[static_cast<int>(muxMode_)].posTorqueLimits[i]
- controllerCommands_[static_cast<int>(dialModeMap_[dashboardDialMode])].posTorqueLimits[i]
float torqueDelta = abs(
controllerCommands_[static_cast<int>(muxMode_)].torqueSetpoints[i]
- controllerCommands_[static_cast<int>(dialModeMap_[dashboardDialMode])].torqueSetpoints[i]
);
float negTorqueDelta = abs(
controllerCommands_[static_cast<int>(muxMode_)].negTorqueLimits[i]
- controllerCommands_[static_cast<int>(dialModeMap_[dashboardDialMode])].negTorqueLimits[i]
);
if (posTorqueDelta > maxTorqueDeltaForModeChange || negTorqueDelta > maxTorqueDeltaForModeChange)

if (torqueDelta > maxTorqueDeltaForModeChange)
{
torqueDeltaPreventsModeChange = true;
break;
Expand Down
65 changes: 26 additions & 39 deletions lib/systems/src/TorqueControllers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static inline void TCPosTorqueLimit(DrivetrainCommand_s &command, float torqueLi
{
for (int i = 0; i < NUM_MOTORS; i++)
{
command.posTorqueLimits[i] = std::min(command.posTorqueLimits[i], torqueLimit);
command.torqueSetpoints[i] = std::min(command.torqueSetpoints[i], torqueLimit);
}
}

Expand All @@ -37,40 +37,31 @@ void TorqueControllerSimple::tick(const SysTick_s& tick, const PedalsSystemData_
if (accelRequest > 0.0)
{
// Positive torque request
data_.speeds[FL] = AMK_MAX_RPM;
data_.speeds[FR] = AMK_MAX_RPM;
data_.speeds[RL] = AMK_MAX_RPM;
data_.speeds[RR] = AMK_MAX_RPM;
data_.speeds_rpm[FL] = AMK_MAX_RPM;
data_.speeds_rpm[FR] = AMK_MAX_RPM;
data_.speeds_rpm[RL] = AMK_MAX_RPM;
data_.speeds_rpm[RR] = AMK_MAX_RPM;

data_.posTorqueLimits[FL] = torqueRequest * frontTorqueScale_;
data_.posTorqueLimits[FR] = torqueRequest * frontTorqueScale_;
data_.posTorqueLimits[RL] = torqueRequest * rearTorqueScale_;
data_.posTorqueLimits[RR] = torqueRequest * rearTorqueScale_;
data_.torqueSetpoints[FL] = torqueRequest * frontTorqueScale_;
data_.torqueSetpoints[FR] = torqueRequest * frontTorqueScale_;
data_.torqueSetpoints[RL] = torqueRequest * rearTorqueScale_;
data_.torqueSetpoints[RR] = torqueRequest * rearTorqueScale_;

data_.negTorqueLimits[FL] = 0.0;
data_.negTorqueLimits[FR] = 0.0;
data_.negTorqueLimits[RL] = 0.0;
data_.negTorqueLimits[RR] = 0.0;
}
else
{
// Negative torque request
torqueRequest = MAX_REGEN_TORQUE * torqueRequest * -1.0; // TODO: determine whether negative torque limits are signed
torqueRequest = MAX_REGEN_TORQUE * torqueRequest * -1.0;

data_.speeds[FL] = 0.0;
data_.speeds[FR] = 0.0;
data_.speeds[RL] = 0.0;
data_.speeds[RR] = 0.0;
data_.speeds_rpm[FL] = 0.0;
data_.speeds_rpm[FR] = 0.0;
data_.speeds_rpm[RL] = 0.0;
data_.speeds_rpm[RR] = 0.0;

data_.posTorqueLimits[FL] = 0.0;
data_.posTorqueLimits[FR] = 0.0;
data_.posTorqueLimits[RL] = 0.0;
data_.posTorqueLimits[RR] = 0.0;

data_.negTorqueLimits[FL] = torqueRequest;
data_.negTorqueLimits[FR] = torqueRequest;
data_.negTorqueLimits[RL] = torqueRequest;
data_.negTorqueLimits[RR] = torqueRequest;
data_.torqueSetpoints[FL] = torqueRequest;
data_.torqueSetpoints[FR] = torqueRequest;
data_.torqueSetpoints[RL] = torqueRequest;
data_.torqueSetpoints[RR] = torqueRequest;
}

// Apply the torque limit
Expand All @@ -80,20 +71,16 @@ void TorqueControllerSimple::tick(const SysTick_s& tick, const PedalsSystemData_
{
// Both pedals are pressed or an implausibility has been detected
// Zero out torque
data_.speeds[FL] = 0.0;
data_.speeds[FR] = 0.0;
data_.speeds[RL] = 0.0;
data_.speeds[RR] = 0.0;
data_.speeds_rpm[FL] = 0.0;
data_.speeds_rpm[FR] = 0.0;
data_.speeds_rpm[RL] = 0.0;
data_.speeds_rpm[RR] = 0.0;

data_.posTorqueLimits[FL] = 0.0;
data_.posTorqueLimits[FR] = 0.0;
data_.posTorqueLimits[RL] = 0.0;
data_.posTorqueLimits[RR] = 0.0;
data_.torqueSetpoints[FL] = 0.0;
data_.torqueSetpoints[FR] = 0.0;
data_.torqueSetpoints[RL] = 0.0;
data_.torqueSetpoints[RR] = 0.0;

data_.negTorqueLimits[FL] = 0.0;
data_.negTorqueLimits[FR] = 0.0;
data_.negTorqueLimits[RL] = 0.0;
data_.negTorqueLimits[RR] = 0.0;
}

writeout_ = data_;
Expand Down
53 changes: 52 additions & 1 deletion test/drivetrain_system_test.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,58 @@
#ifndef DRIVETRAIN_SYSTEM_TEST
#define DRIVETRAIN_SYSTEM_TEST
#include "DrivetrainSystem.h"

// TODO @ben
// TODO handle startup sequence stuff
//

class InverterMock
{
public:
int request_enable_hv_count_, request_enable_inverter_count_;

InverterMock()
{
int request_enable_hv_count_ = 0;
int request_enable_inverter_count_ = 0;
error_ = false;
system_ready_ = false;
dc_quit_on_ = false;
quit_inverter_on_ = false;
};

bool error_, system_ready_, dc_quit_on_, quit_inverter_on_;
uint16_t voltage_;

// want to ensure that we are only sending requests once at a time
// stage 1
void request_enable_hv()
{
request_enable_hv_count_++;
dc_quit_on_ = true;
};
// stage 2
void request_enable_inverter()
{
request_enable_inverter_count_++;
quit_inverter_on_ = true;
};

void command_no_torque(){};
bool error() { return error_; };
bool inverter_system_ready() { return system_ready_; };
void command_reset() { error_ = false; };
uint16_t dc_bus_voltage() { return voltage_; };
bool dc_quit_on() { return dc_quit_on_; }
bool quit_inverter_on() { return quit_inverter_on_; }
};

// TODO
TEST(DrivetrainSystemTesting, test_drivetrain_startup)
{
InverterMock inv_fl, inv_fr, inv_rl, inv_rr;
DrivetrainSystem<InverterMock> dt({&inv_fl, &inv_fr, &inv_rl, &inv_rr}, 1000);
}
// TODO test startup timeout

// TODO test commanding of drivetrain to ensure that the data is getting accross correctly
#endif /* DRIVETRAIN_SYSTEM_TEST */
2 changes: 1 addition & 1 deletion test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "state_machine_test.h"
#include "pedals_system_test.h"

#include "drivetrain_system_test.h"


int main(int argc, char **argv)
Expand Down
2 changes: 0 additions & 2 deletions test/pedals_system_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ rough draft
author: Lucas Plant
*/

// TODO @ben

#ifndef PEDALS_SYSTEM_TEST
#define PEDALS_SYSTEM_TEST
Expand Down Expand Up @@ -106,6 +105,5 @@ TEST(PedalsSystemTesting, test_implausibility_duration)

EXPECT_TRUE(data.implausibilityExceededMaxDuration);
}
// TODO test implausibility duration alert

#endif /* PEDALS_SYSTEM_TEST */

0 comments on commit 54eb7fe

Please sign in to comment.