MCU
Loading...
Searching...
No Matches
Functions | Variables
Coulomb_Counting_Tests.h File Reference
#include <Arduino.h>
#include <unity.h>
#include <iostream>
#include "AMSInterface.h"
#include "SysClock.h"
#include "MessageQueueDefine.h"
Include dependency graph for Coulomb_Counting_Tests.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void test_initialize_charge ()
 
void test_calculate_SoC_em ()
 
void test_calculate_SoC_acu ()
 

Variables

CANBufferType CAN_BUFFER_CC_TESTS
 

Function Documentation

◆ test_calculate_SoC_acu()

void test_calculate_SoC_acu ( )

Definition at line 144 of file Coulomb_Counting_Tests.h.

145{
146 // Declaring initial conditions
147 unsigned long starting_millis = millis();
148 unsigned long starting_micros = micros();
149 SysTick_s starting_tick;
150 starting_tick.millis = starting_millis;
151 starting_tick.micros = starting_micros;
152
153 // Declaring & instantiating a new AMSInterface (to read from CAN messages and perform the SoC calculations)
154 AMSInterface interface(&CAN_BUFFER_CC_TESTS, 8);
155 interface.init(starting_tick); // Sets heartbeat and puts "uninitialized" value into bms_voltages_
156
157 interface.set_use_em_for_soc(false);
158
159 CAN_message_t acu_measurements_can, bms_voltages_can;
160
161 // Declaring CAN frames and feeding them into the AMSInterface.
162 // Initializes BMS_VOLTAGES to have a min (of 3.7 volts, which is 0x9088U).
163 bms_voltages_can = generate_can_msg_from_uint_16s(0x9088U, 37000U, 0x9858U, 0xFFFFU, false);
164 interface.retrieve_voltage_CAN(bms_voltages_can);
165
166 // 2372 analog value corresponds to 25A
167 acu_measurements_can = generate_can_msg_from_uint_16s(2372, HYTECH_pack_filtered_read_ro_toS(0.0), HYTECH_ts_out_filtered_read_ro_toS(0.0), 0, false);
168 interface.retrieve_current_shunt_CAN(acu_measurements_can); // Reads CAN message into the acu_shunt_measurements_ member variable
169
170
171
172 // TEST CASE ZERO - 5000ms to allow initialization to happen
173 SysTick_s tick_zero;
174 tick_zero.millis = starting_millis + 5000;
175 tick_zero.micros = starting_micros + 1000000;
176
177 // Call tick() once (with no delta t) so that initialize_charge() will be called
178 interface.tick(tick_zero);
179 // assert initial SoC is correct
180 TEST_ASSERT_EQUAL_FLOAT(75.0, interface.get_SoC());
181
182
183 // Running actual test cases
184
185 // TEST CASE ONE - 10ms - 25amps
186 SysTick_s tick_one;
187 tick_one.millis = tick_zero.millis + 10;
188 tick_one.micros = tick_zero.micros + 10000;
189
190 // 25 amps of current * 0.01sec = 0.25 coulombs of charge.
191 // Starting charge = 75%, so 36450 coulombs. After this,
192 // charge should be at 36449.75, or 74.9994855%
193 interface.tick(tick_one);
194 TEST_ASSERT_EQUAL_FLOAT(74.9994855, interface.get_SoC());
195
196
197
198 // TEST CASE TWO - 20ms - 50amps
199 SysTick_s tick_two;
200 tick_two.millis = tick_zero.millis + 10 + 20;
201 tick_two.micros = tick_zero.micros + 10000 + 20000;
202
203 acu_measurements_can = generate_can_msg_from_uint_16s(2458, HYTECH_pack_filtered_read_ro_toS(0.0), HYTECH_ts_out_filtered_read_ro_toS(0.0), 0, false);
204 interface.retrieve_current_shunt_CAN(acu_measurements_can); // Reads CAN message into the acu_shunt_measurements_ member variable
205
206 // 50 amps of current * 0.02sec = 1 coulomb of charge.
207 // Starting charge = 36449.75 coulombs. After this,
208 // charge should be at 36448.75, or 74.9974279f%
209 interface.tick(tick_two);
210 TEST_ASSERT_EQUAL_FLOAT(74.9974279f, interface.get_SoC());
211
212
213
214 // TEST CASE THREE - 10 full seconds, in 20ms intervals, at 50 amps
215
216 acu_measurements_can = generate_can_msg_from_uint_16s(2457, HYTECH_pack_filtered_read_ro_toS(0.0), HYTECH_ts_out_filtered_read_ro_toS(0.0), 0, false);
217 interface.retrieve_current_shunt_CAN(acu_measurements_can); // Reads CAN message into the acu_shunt_measurements_ member variable
218
219 for (int i = 0; i <= 10000000; i += 20000) {
220 SysTick_s curr_tick;
221 curr_tick.millis = tick_zero.millis + 10 + 20 + i/1000;
222 curr_tick.micros = tick_zero.micros + 10000 + 20000 + i;
223 interface.tick(curr_tick);
224 }
225
226 // 50 amps of current * 10sec = 500 coulombs of charge
227 // Starting charge = 36448.75 coulombs. After this, charge
228 // should be 35948.75 coulombs, which is 73.9686%
229 TEST_ASSERT_EQUAL_FLOAT(73.9686f, interface.get_SoC());
230
231}
CAN_message_t generate_can_msg_from_uint_16s(uint16_t first, uint16_t second, uint16_t third, uint16_t fourth, bool use_little_endian)
CANBufferType CAN_BUFFER_CC_TESTS
this class is for interfacing with the AMS (accumulator management system)
Definition: AMSInterface.h:27

◆ test_calculate_SoC_em()

void test_calculate_SoC_em ( )

Definition at line 55 of file Coulomb_Counting_Tests.h.

56{
57
58 // Declaring initial conditions
59 unsigned long starting_millis = millis();
60 unsigned long starting_micros = micros();
61 SysTick_s starting_tick;
62 starting_tick.millis = starting_millis;
63 starting_tick.micros = starting_micros;
64
65 // Declaring & instantiating a new AMSInterface (to read from CAN messages and perform the SoC calculations)
66 AMSInterface interface(&CAN_BUFFER_CC_TESTS, 8);
67 interface.init(starting_tick); // Sets heartbeat and puts "uninitialized" value into bms_voltages_
68
69 interface.set_use_em_for_soc(true);
70
71 CAN_message_t em_measurements_can, bms_voltages_can;
72
73 // Declaring CAN frames and feeding them into the AMSInterface.
74 // Initializes BMS_VOLTAGES to have a min (of 3.7 volts, which is 0x9088U).
75 bms_voltages_can = generate_can_msg_from_uint_16s(0x9088U, 37000U, 0x9858U, 0xFFFFU, false);
76 interface.retrieve_voltage_CAN(bms_voltages_can);
77
78 em_measurements_can = generate_can_msg_from_uint_32s(HYTECH_em_current_ro_toS(25.0f), HYTECH_em_voltage_ro_toS(530.5f), true);
79 interface.retrieve_em_measurement_CAN(em_measurements_can); // Reads CAN message into the acu_shunt_measurements_ member variable
80
81
82 // TEST CASE ZERO - 5000ms to allow initialization to happen
83 SysTick_s tick_zero;
84 tick_zero.millis = starting_millis + 5000;
85 tick_zero.micros = starting_micros + 1000000;
86
87 // Call tick() once (with no delta t) so that initialize_charge() will be called
88 interface.tick(tick_zero);
89
90 // assert initial SoC is correct
91 TEST_ASSERT_EQUAL_FLOAT(75.0, interface.get_SoC());
92
93
94 // Running actual test cases
95
96 // TEST CASE ONE - 10ms - 25amps
97 SysTick_s tick_one;
98 tick_one.millis = tick_zero.millis + 10;
99 tick_one.micros = tick_zero.micros + 10000;
100
101 // 25 amps of current * 0.01sec = 0.25 coulombs of charge.
102 // Starting charge = 75%, so 36450 coulombs. After this,
103 // charge should be at 36449.75, or 74.9994%
104 interface.tick(tick_one);
105 TEST_ASSERT_EQUAL_FLOAT(74.9994855, interface.get_SoC());
106
107
108
109 // TEST CASE TWO - 20ms - 50amps
110 SysTick_s tick_two;
111 tick_two.millis = tick_zero.millis + 10 + 20;
112 tick_two.micros = tick_zero.micros + 10000 + 20000;
113
114 em_measurements_can = generate_can_msg_from_uint_32s(HYTECH_em_current_ro_toS(50.0f), HYTECH_em_voltage_ro_toS(530.5f), true);
115 interface.retrieve_em_measurement_CAN(em_measurements_can); // Reads CAN message into the acu_shunt_measurements_ member variable
116
117 // 50 amps of current * 0.02sec = 1 coulomb of charge.
118 // Starting charge = 36449.75 coulombs. After this,
119 // charge should be at 36448.75, or 74.9974279%
120 interface.tick(tick_two);
121 TEST_ASSERT_EQUAL_FLOAT(74.9974279f, interface.get_SoC());
122
123
124
125 // TEST CASE THREE - 10 full seconds, in 20ms intervals, at 50 amps
126
127 em_measurements_can = generate_can_msg_from_uint_32s(HYTECH_em_current_ro_toS(50.0f), HYTECH_em_voltage_ro_toS(530.5f), true);
128 interface.retrieve_em_measurement_CAN(em_measurements_can); // Reads CAN message into the acu_shunt_measurements_ member variable
129
130 for (int i = 0; i <= 10000000; i += 20000) {
131 SysTick_s curr_tick;
132 curr_tick.millis = tick_zero.millis + 10 + 20 + i/1000;
133 curr_tick.micros = tick_zero.micros + 10000 + 20000 + i;
134 interface.tick(curr_tick);
135 }
136
137 // 50 amps of current * 10sec = 500 coulombs of charge
138 // Starting charge = 36448.75 coulombs. After this, charge
139 // should be 35948.75 coulombs, which is 73.9686%
140 TEST_ASSERT_EQUAL_FLOAT(73.9686f, interface.get_SoC());
141
142}
CAN_message_t generate_can_msg_from_uint_32s(uint32_t first, uint32_t second, bool use_little_endian)

◆ test_initialize_charge()

void test_initialize_charge ( )

Definition at line 10 of file Coulomb_Counting_Tests.h.

11{
12
13 // Declaring & instantiating new AMSInterface (to read from CAN message)
14 AMSInterface interface(&CAN_BUFFER_CC_TESTS, 8);
15
16
17
18 // -------------------------- Initializes bms_voltages CAN message -------------------------- //
19 CAN_message_t bms_voltages_can_msg = generate_can_msg_from_uint_16s(0x9088U, 0x88B8U, 0x9858U, 0xFFFFU, false);
20
21 interface.retrieve_voltage_CAN(bms_voltages_can_msg); // Reads CAN message into the acu_shunt_measurements_ member variable
22 BMS_VOLTAGES_t received_voltages = interface.get_bms_voltages();
23
24
25
26 // -------------------------- Tests the initialize_charge function -------------------------- //
27
28 // 3.7 avg 3.5 low 3.9 high
29 bms_voltages_can_msg = generate_can_msg_from_uint_16s(0x9088U, 0x88B8U, 0x9858U, 0xFFFFU, false); // Regular use case
30 interface.retrieve_voltage_CAN(bms_voltages_can_msg);
31 TEST_ASSERT_EQUAL_FLOAT(13122.0f, interface.initialize_charge());
32
33 // 3.7 avg 3.3 low 3.9 high
34 bms_voltages_can_msg = generate_can_msg_from_uint_16s(0x9088U, 0x80E8U, 0x9858U, 0xFFFFU, false); // Regular use case
35 interface.retrieve_voltage_CAN(bms_voltages_can_msg);
36 TEST_ASSERT_EQUAL_FLOAT(3402.0f, interface.initialize_charge());
37
38 // 3.8 avg 3.7 low 3.9 high
39 bms_voltages_can_msg = generate_can_msg_from_uint_16s(0x9470U, 0x9088U, 0x9858U, 0xFFFFU, false); // Regular use case
40 interface.retrieve_voltage_CAN(bms_voltages_can_msg);
41 TEST_ASSERT_EQUAL_FLOAT(36450.0f, interface.initialize_charge());
42
43 // 4.0 avg 4.0 low 4.0 high
44 bms_voltages_can_msg = generate_can_msg_from_uint_16s(0x9C40U, 0x9C40U, 0x9C40U, 0xFFFFU, false); // Maximum voltage (100% charge)
45 interface.retrieve_voltage_CAN(bms_voltages_can_msg);
46 TEST_ASSERT_EQUAL_FLOAT(48600.0f, interface.initialize_charge());
47
48 // 3.3 avg 2.9 low 3.7 high
49 bms_voltages_can_msg = generate_can_msg_from_uint_16s(0x80E8U, 0x7148U, 0x9088U, 0xFFFFU, false); // Minimum voltage (0% charge)
50 interface.retrieve_voltage_CAN(bms_voltages_can_msg);
51 TEST_ASSERT_EQUAL_FLOAT(0.0f, interface.initialize_charge());
52
53}

Variable Documentation

◆ CAN_BUFFER_CC_TESTS

CANBufferType CAN_BUFFER_CC_TESTS

Definition at line 8 of file Coulomb_Counting_Tests.h.