MCU
Loading...
Searching...
No Matches
drivetrain_system_test.h
Go to the documentation of this file.
1#ifndef DRIVETRAIN_SYSTEM_TEST
2#define DRIVETRAIN_SYSTEM_TEST
3#include "DrivetrainSystem.h"
4
5// TODO @ben
6// TODO handle startup sequence stuff
8{
11};
13{
14public:
19
24
26 {
27
30 error_ = false;
31 system_ready_ = false;
32 dc_quit_on_ = false;
33 quit_inverter_on_ = false;
34 };
35
37 uint16_t voltage_;
38
39 // want to ensure that we are only sending requests once at a time
40 // stage 1
42 {
44 dc_quit_on_ = true;
45 };
46 // stage 2
48 {
49
51 quit_inverter_on_ = true;
52 };
53
54 // periodic commands
56 void command_reset() {
58 error_ = false; };
60 {
64 };
65 void disable() {
67 }
68
69 bool error() { return error_; };
71
72 uint16_t dc_bus_voltage() { return voltage_; };
73 bool dc_quit_on() { return dc_quit_on_; }
74 bool get_quit_dc_on() { return dc_quit_on_; }
76};
77
78TEST(DrivetrainSystemTesting, test_drivetrain_startup)
79{
80
81 InverterMock inv_fl, inv_fr, inv_rl, inv_rr;
82 MCUInterface mcu;
83 DrivetrainSystem<InverterMock> dt({&inv_fl, &inv_fr, &inv_rl, &inv_rr}, &mcu, 1000);
84 unsigned long sys_time = 1000;
85 // inverters are not ready so an inverter startup call shouldnt send any requests to the inverter
86 // to start the initialization process
87 EXPECT_FALSE(dt.handle_inverter_startup(sys_time));
88 EXPECT_EQ(inv_fl.request_enable_hv_count_, 0);
89 EXPECT_EQ(inv_fr.request_enable_hv_count_, 0);
90 EXPECT_EQ(inv_rl.request_enable_hv_count_, 0);
91 EXPECT_EQ(inv_rr.request_enable_hv_count_, 0);
92
93 inv_fl.system_ready_ = true;
94 inv_fr.system_ready_ = true;
95 inv_rl.system_ready_ = true;
96 inv_rr.system_ready_ = true;
97
98 // this should enable the drivetrain hv
99 EXPECT_FALSE(dt.handle_inverter_startup(sys_time));
100
101 EXPECT_TRUE(inv_fl.dc_quit_on_);
102 EXPECT_TRUE(inv_fr.dc_quit_on_);
103 EXPECT_TRUE(inv_rl.dc_quit_on_);
104 EXPECT_TRUE(inv_rr.dc_quit_on_);
105
106 // im just gonna test one from now on for this type of stuff
107 EXPECT_EQ(inv_rr.request_enable_hv_count_, 1);
108 EXPECT_EQ(inv_rr.request_enable_inverter_count_, 0); // this should enable the inverters
109 EXPECT_FALSE(dt.handle_inverter_startup(sys_time));
110
111 EXPECT_EQ(inv_rr.request_enable_hv_count_, 1);
112 EXPECT_EQ(inv_rr.request_enable_inverter_count_, 1);
113
114 EXPECT_TRUE(dt.handle_inverter_startup(sys_time));
115}
116
117TEST(DrivetrainSystemTesting, test_drivetrain_init_timeout)
118{
119 InverterMock inv_fl, inv_fr, inv_rl, inv_rr;
120 MCUInterface mcu;
121 DrivetrainSystem<InverterMock> dt({&inv_fl, &inv_fr, &inv_rl, &inv_rr}, &mcu, 1000);
122 unsigned long sys_time = 1000;
123 // test and make sure that normally the drivetrain doesnt timeout when changing the time
124 EXPECT_FALSE(dt.handle_inverter_startup(sys_time));
125 inv_fl.system_ready_ = true;
126 inv_fr.system_ready_ = true;
127 inv_rl.system_ready_ = true;
128 inv_rr.system_ready_ = true;
129 sys_time += 10;
130 // tick to request hv enable
131 EXPECT_FALSE(dt.handle_inverter_startup(sys_time));
132
133 sys_time += 10;
134 // tick to request enable inverters
135 EXPECT_FALSE(dt.handle_inverter_startup(sys_time));
136
137 sys_time += 10;
138 // tick to check inverters status
139 EXPECT_TRUE(dt.handle_inverter_startup(sys_time));
140 EXPECT_FALSE(dt.inverter_init_timeout(sys_time));
141
142 // test timeout
143 InverterMock inv_fl2, inv_fr2, inv_rl2, inv_rr2;
144 DrivetrainSystem<InverterMock> dt2({&inv_fl2, &inv_fr2, &inv_rl2, &inv_rr2}, &mcu,1000);
145 unsigned long sys_time2 = 1000;
146
147 EXPECT_FALSE(dt2.handle_inverter_startup(sys_time2));
148 // inv_fl.system_ready_ = true;
149 inv_fr2.system_ready_ = true;
150 inv_rl2.system_ready_ = true;
151 inv_rr2.system_ready_ = true;
152 sys_time2 += 1050;
153 EXPECT_TRUE(dt.inverter_init_timeout(sys_time2));
154}
155
156TEST(DrivetrainSystemTesting, test_drivetrain_inverter_comms)
157{
158
159 InverterMock inv_fl, inv_fr, inv_rl, inv_rr;
160 MCUInterface mcu;
161 DrivetrainSystem<InverterMock> dt({&inv_fl, &inv_fr, &inv_rl, &inv_rr}, &mcu, 1000);
162 dt.command_drivetrain({{1000.0, 1001.0, 1002.0, 1003.0}, {2000.0, 2001.0, 2002.0, 2003.0}});
163
164 // torque_setpoint_nm_
165 // speed_setpoint_rpm_
166 // ensure that without ticking the inverters dont get the data
167 // as of 3/13/24 this isnt being handled by the drivetrain rn
168 // EXPECT_EQ(inv_fl.speed_setpoint_rpm_, 0.0);
169 // EXPECT_EQ(inv_fl.torque_setpoint_nm_, 0);
170
171 // EXPECT_EQ(inv_fr.torque_setpoint_nm_, 0);
172 // EXPECT_EQ(inv_fr.speed_setpoint_rpm_, 0);
173
174 // EXPECT_EQ(inv_rl.torque_setpoint_nm_, 0);
175 // EXPECT_EQ(inv_rl.speed_setpoint_rpm_, 0);
176
177 // EXPECT_EQ(inv_rr.torque_setpoint_nm_, 0);
178 // EXPECT_EQ(inv_rr.speed_setpoint_rpm_, 0);
179 SysClock clock;
180 auto micros = 1000000;
181 dt.tick(clock.tick(micros));
182 dt.command_drivetrain({{1000.0, 1001.0, 1002.0, 1003.0}, {10.0, 11.0, 12.0, 13.0}});
183 EXPECT_EQ(inv_fl.speed_setpoint_rpm_, 1000.0);
184 EXPECT_EQ(inv_fl.torque_setpoint_nm_, 10.0);
185
186 EXPECT_EQ(inv_fr.torque_setpoint_nm_, 11.0);
187 EXPECT_EQ(inv_fr.speed_setpoint_rpm_, 1001.0);
188
189 EXPECT_EQ(inv_rl.torque_setpoint_nm_, 12.0);
190 EXPECT_EQ(inv_rl.speed_setpoint_rpm_, 1002.0);
191
192 EXPECT_EQ(inv_rr.torque_setpoint_nm_, 13.0);
193 EXPECT_EQ(inv_rr.speed_setpoint_rpm_, 1003.0);
194
195 // testing to ensure that these extra general commands dont get through the period filter
196 // as of 3/13/24, the inverter sending rate is being handled at the inverter interface
197 // TODO decide if we want to add this back into the drivetrain
198 // micros += (20 * 1000);
199 // dt.tick(clock.tick(micros));
200 // dt.command_drivetrain({{1000.0, 1001.0, 1002.0, 1003.0}, {2000.0, 2001.0, 2002.0, 2003.0}});
201 // dt.command_drivetrain({{1000.0, 1001.0, 1002.0, 1003.0}, {2000.0, 2001.0, 2002.0, 2003.0}});
202 // dt.command_drivetrain({{1000.0, 1001.0, 1002.0, 1003.0}, {2000.0, 2001.0, 2002.0, 2003.0}});
203 // EXPECT_EQ(inv_rl.cmd_general_count_, 1);
204 // micros += (20 * 1000);
205 // dt.tick(clock.tick(micros));
206 // dt.command_drivetrain({{1000.0, 1001.0, 1002.0, 1003.0}, {2000.0, 2001.0, 2002.0, 2003.0}});
207 // EXPECT_EQ(inv_rl.cmd_general_count_, 2);
208}
209// TODO test commanding of drivetrain to ensure that the data is getting accross correctly
210#endif /* DRIVETRAIN_SYSTEM_TEST */
void command_drivetrain(const DrivetrainCommand_s &data)
uint16_t dc_bus_voltage()
void handle_command(const InverterCommand &cmd)
TEST(DrivetrainSystemTesting, test_drivetrain_startup)