Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
BMSTest.c
Go to the documentation of this file.
1#include "../../../Inc/Systems/External/BMS.h"
2#include "test.h"
3
4#include <assert.h>
5#include <math.h>
6#include <stdbool.h>
7
8// TODO: Put in bms file
9#define FLOAT_ERROR 1e-5
10bool floatEqual(float a, float b) {
11 return fabs(a - b) < FLOAT_ERROR;
12}
13
14bool equal(Bms* bms1, Bms* bms2) {
15 if (!floatEqual(bms1->packVoltage, bms2->packVoltage)) return false;
16 if (!floatEqual(bms1->packCurrent, bms2->packCurrent)) return false;
17 if (!floatEqual(bms1->stateOfCharge, bms2->stateOfCharge)) return false;
18 if (!floatEqual(bms1->cellVoltageMin, bms2->cellVoltageMin)) return false;
19 if (!floatEqual(bms1->cellVoltageMax, bms2->cellVoltageMax)) return false;
20 if (!floatEqual(bms1->cellTemperatureMin, bms2->cellTemperatureMin)) return false;
21 if (!floatEqual(bms1->cellTemperatureMax, bms2->cellTemperatureMax)) return false;
22 if (!floatEqual(bms1->totalPackCapacity, bms2->totalPackCapacity)) return false;
23 if (!floatEqual(bms1->remainingPackCapacity, bms2->remainingPackCapacity)) return false;
24 if (!floatEqual(bms1->packHealth, bms2->packHealth)) return false;
25 // TODO: Add charge status
26
27 return true;
28}
29
30#define BmsHz 500
31
32void bms_main() {
33 TEST(bms_init, {
34 Bms bms;
35 initBms(&bms, BmsHz, "Tests/Systems/External/Orion_CANBUS.dbc");
36 ASSERT(bms.chargeStatus == IDLE, "bms is idle", "bms is not idle");
37 ASSERT_EQ(bms.packVoltage, 0, "pack voltage", "expected pack voltage");
38 ASSERT_EQ(bms.packCurrent, 0, "pack current", "expected pack current");
39 })
40
41 Bms expectedBms;
42 initBms(&expectedBms, BmsHz, "Tests/Systems/External/Orion_CANBUS.dbc");
43 expectedBms.packVoltage = 600.0f;
44 expectedBms.packVoltage = 400.0f;
45 expectedBms.stateOfCharge = 80.0f;
46
47 TEST(bms_update, {
48 Bms bms;
49 initBms(&bms, BmsHz, "Tests/Systems/External/Orion_CANBUS.dbc");
50 updateBmsTest(&bms, "Tests/Systems/External/BmsFakeCanData.txt");
51 // FIXME Should this be equal or !equal? Original had it as equal is a failure
52 ASSERT(!equal(&bms, &expectedBms), "bms doesn't equal expected bms", "bms equals expected bms");
53 })
54}
#define BmsHz
Definition: BMSTest.c:30
void bms_main()
Definition: BMSTest.c:32
bool floatEqual(float a, float b)
Definition: BMSTest.c:10
#define FLOAT_ERROR
Definition: BMSTest.c:9
bool equal(Bms *bms1, Bms *bms2)
Definition: BMSTest.c:14
void updateBmsTest(void *bms, const char *canDataFn)
Updates the BMS data.
Definition: BMS.c:96
@ IDLE
Definition: BMS.h:12
void initBms(Bms *bms, int hz, const char *dbcFn)
Initializes the BMS with the given frequency.
Definition: BMS.c:7
Definition: BMS.h:15
float cellVoltageMin
Definition: BMS.h:22
float stateOfCharge
Definition: BMS.h:21
float cellVoltageMax
Definition: BMS.h:23
float cellTemperatureMax
Definition: BMS.h:25
float packVoltage
Definition: BMS.h:19
BmsChargeStatus chargeStatus
Definition: BMS.h:29
float totalPackCapacity
Definition: BMS.h:26
float cellTemperatureMin
Definition: BMS.h:24
float remainingPackCapacity
Definition: BMS.h:27
float packHealth
Definition: BMS.h:28
float packCurrent
Definition: BMS.h:20
#define TEST(NAME, BODY)
Definition: test.h:19
#define ASSERT_EQ(GOT, WANT, GOT_LABEL, WANT_LABEL)
Definition: test.h:32
#define ASSERT(BOOL, OK, ERR)
Definition: test.h:27