Skip to content

Commit

Permalink
Merge pull request #33 from hytech-racing/drivetrain_system_testing
Browse files Browse the repository at this point in the history
Drivetrain system testing
  • Loading branch information
RCMast3r committed Feb 8, 2024
2 parents 39ee0e8 + b87115d commit 0a744bf
Show file tree
Hide file tree
Showing 8 changed files with 198 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
15 changes: 9 additions & 6 deletions lib/systems/include/DrivetrainSystem.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ bool DrivetrainSystem<InverterType>::handle_inverter_startup(unsigned long curr_
{
enable_drivetrain_hv_(curr_time);
hv_en_requested_ = true;
return false;
}
else if (drivetrain_ready_() && check_drivetrain_quit_dc_on_() && !enable_requested_)
else if (drivetrain_ready_() && check_drivetrain_quit_dc_on_() && !enable_requested_ && hv_en_requested_)
{
request_enable_();
enable_requested_ = true;
return false;
}
bool all_ready = ( drivetrain_ready_() && check_drivetrain_quit_dc_on_() && drivetrain_enabled_() );
return all_ready;
Expand Down Expand Up @@ -78,11 +80,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
154 changes: 153 additions & 1 deletion test/drivetrain_system_test.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,159 @@
#ifndef DRIVETRAIN_SYSTEM_TEST
#define DRIVETRAIN_SYSTEM_TEST
#include "DrivetrainSystem.h"

// TODO @ben
// TODO handle startup sequence stuff
//
struct InverterCommand
{
float torque_setpoint_nm;
float speed_setpoint_rpm;
};
class InverterMock
{
public:
int request_enable_hv_count_, request_enable_inverter_count_;
float torque_setpoint_nm_;
float speed_setpoint_rpm_;
InverterMock()
{

request_enable_hv_count_ = 0;
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_; }
void handle_command(const InverterCommand &cmd)
{
torque_setpoint_nm_ = cmd.torque_setpoint_nm;
speed_setpoint_rpm_ = cmd.speed_setpoint_rpm;
};
};

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);
unsigned long sys_time = 1000;
// inverters are not ready so an inverter startup call shouldnt send any requests to the inverter
// to start the initialization process
EXPECT_FALSE(dt.handle_inverter_startup(sys_time));
EXPECT_EQ(inv_fl.request_enable_hv_count_, 0);
EXPECT_EQ(inv_fr.request_enable_hv_count_, 0);
EXPECT_EQ(inv_rl.request_enable_hv_count_, 0);
EXPECT_EQ(inv_rr.request_enable_hv_count_, 0);

inv_fl.system_ready_ = true;
inv_fr.system_ready_ = true;
inv_rl.system_ready_ = true;
inv_rr.system_ready_ = true;

// this should enable the drivetrain hv
EXPECT_FALSE(dt.handle_inverter_startup(sys_time));

EXPECT_TRUE(inv_fl.dc_quit_on_);
EXPECT_TRUE(inv_fr.dc_quit_on_);
EXPECT_TRUE(inv_rl.dc_quit_on_);
EXPECT_TRUE(inv_rr.dc_quit_on_);

// im just gonna test one from now on for this type of stuff
EXPECT_EQ(inv_rr.request_enable_hv_count_, 1);
EXPECT_EQ(inv_rr.request_enable_inverter_count_, 0); // this should enable the inverters
EXPECT_FALSE(dt.handle_inverter_startup(sys_time));

EXPECT_EQ(inv_rr.request_enable_hv_count_, 1);
EXPECT_EQ(inv_rr.request_enable_inverter_count_, 1);

EXPECT_TRUE(dt.handle_inverter_startup(sys_time));
}

TEST(DrivetrainSystemTesting, test_drivetrain_init_timeout)
{
InverterMock inv_fl, inv_fr, inv_rl, inv_rr;
DrivetrainSystem<InverterMock> dt({&inv_fl, &inv_fr, &inv_rl, &inv_rr}, 1000);
unsigned long sys_time = 1000;
// test and make sure that normally the drivetrain doesnt timeout when changing the time
EXPECT_FALSE(dt.handle_inverter_startup(sys_time));
inv_fl.system_ready_ = true;
inv_fr.system_ready_ = true;
inv_rl.system_ready_ = true;
inv_rr.system_ready_ = true;
sys_time += 10;
// tick to request hv enable
EXPECT_FALSE(dt.handle_inverter_startup(sys_time));

sys_time += 10;
// tick to request enable inverters
EXPECT_FALSE(dt.handle_inverter_startup(sys_time));

sys_time += 10;
// tick to check inverters status
EXPECT_TRUE(dt.handle_inverter_startup(sys_time));
EXPECT_FALSE(dt.inverter_init_timeout(sys_time));

// test timeout
InverterMock inv_fl2, inv_fr2, inv_rl2, inv_rr2;
DrivetrainSystem<InverterMock> dt2({&inv_fl2, &inv_fr2, &inv_rl2, &inv_rr2}, 1000);
unsigned long sys_time2 = 1000;

EXPECT_FALSE(dt2.handle_inverter_startup(sys_time2));
// inv_fl.system_ready_ = true;
inv_fr2.system_ready_ = true;
inv_rl2.system_ready_ = true;
inv_rr2.system_ready_ = true;
sys_time2 += 1050;
EXPECT_TRUE(dt.inverter_init_timeout(sys_time2));
}

TEST(DrivetrainSystemTesting, test_drivetrain_inverter_comms)
{

InverterMock inv_fl, inv_fr, inv_rl, inv_rr;
DrivetrainSystem<InverterMock> dt({&inv_fl, &inv_fr, &inv_rl, &inv_rr}, 1000);
dt.command_drivetrain({{1000.0, 1001.0, 1002.0, 1003.0}, {2000.0, 2001.0, 2002.0, 2003.0}});

// torque_setpoint_nm_
// speed_setpoint_rpm_

EXPECT_EQ(inv_fl.speed_setpoint_rpm_, 1000.0);
EXPECT_EQ(inv_fl.torque_setpoint_nm_, 2000.0);

EXPECT_EQ(inv_fr.torque_setpoint_nm_, 2001.0);
EXPECT_EQ(inv_fr.speed_setpoint_rpm_, 1001.0);

EXPECT_EQ(inv_rl.torque_setpoint_nm_, 2002.0);
EXPECT_EQ(inv_rl.speed_setpoint_rpm_, 1002.0);

EXPECT_EQ(inv_rr.torque_setpoint_nm_, 2003.0);
EXPECT_EQ(inv_rr.speed_setpoint_rpm_, 1003.0);
}
// TODO test commanding of drivetrain to ensure that the data is getting accross correctly
#endif /* DRIVETRAIN_SYSTEM_TEST */
1 change: 1 addition & 0 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "state_machine_test.h"
#include "pedals_system_test.h"
#include "drivetrain_system_test.h"
#include "torque_controller_mux_test.h"


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 0a744bf

Please sign in to comment.