Vehicle Control Unit 0.01
This is the c library for controlling the car.
Loading...
Searching...
No Matches
BMS.c
Go to the documentation of this file.
1#include "../../../Inc/Systems/External/BMS.h"
2#include "../../../Inc/Systems/PrintHelpers.h"
3
4#include <stdio.h>
5#include "../../../Inc/Systems/Comms/Can/Can.h"
6
7void initBms(Bms* bms, int hz, const char* dbcFn) {
8 if (bms == NULL) return;
9
10 initExternalSystem(&bms->extSystem, "Bms", hz, 0);
11
12 bms->packVoltage = 0.0f;
13 bms->packCurrent = 0.0f;
14 bms->stateOfCharge = 0.0f;
15 bms->cellVoltageMin = 0.0f;
16 bms->cellVoltageMax = 0.0f;
17 bms->cellTemperatureMin = 0.0f;
18 bms->cellTemperatureMax = 0.0f;
19 bms->totalPackCapacity = 0.0f;
20 bms->remainingPackCapacity = 0.0f;
21 bms->packHealth = 0.0f;
22 bms->chargeStatus = IDLE;
24}
25
26void assignBmsValue(Bms* bms, int id, float value, const char* name) {
27 switch (id) {
28 // TODO: Remember to update +1712
29 case 0:
30 if (strcmp(name, "Pack_Current") == 0) {
31 bms->packCurrent = value;
32 } else if (strcmp(name, "Pack_Inst_Voltage") == 0) {
33 bms->packVoltage = value;
34 } else if (strcmp(name, "Pack_SOC") == 0) {
35 bms->stateOfCharge = value;
36 } else if (strcmp(name, "Relay_State") == 0) {
37 /*bms->relayState = (int)value;*/
38 } else if (strcmp(name, "CRC_Checksum") == 0) {
39 // TODO: Deal with this
40
41 /*bms->crcChecksum = (int)value;*/
42 }
43 break;
44 case 1:
45 if (strcmp(name, "Pack_DCL") == 0) {
46 /*bms->packDCL = value;*/
47 } else if (strcmp(name, "Pack_CCL") == 0) {
48 /*bms->packCCL = value;*/
49 } else if (strcmp(name, "High_Temperature") == 0) {
50 /*bms->highTemperature = value;*/
51 } else if (strcmp(name, "Low_Temperature") == 0) {
52 /*bms->lowTemperature = value;*/
53 } else if (strcmp(name, "CRC_Checksum") == 0) {
54 /*bms->crcChecksum = (int)value;*/
55 }
56 break;
57 default:
58 // Handle invalid signal case if needed
59 break;
60 }
61}
62
63int bmsTransferFunction(Bms* bms, CAN_Message* canData) {
64// int index = canData->messageId;
65// Message* message = &bms->dbc->messages[index-1712];
66//
67// // Check if the message ID matches the expected one
68// if (message == NULL && message->id != canData->messageId) {
69// return 0; // Message ID not found or doesn't match
70// }
71//
72// // Decode signals within the message
73// for (int i = 0; i < message->signal_count; i++) {
74// Signal* signal = &message->signals[i];
75// float value = extractSignalValue(signal, canData->data);
76// assignBmsValue(bms, message->id, value, signal->name);
77// }
78// return 1;
79 return 1;
80}
81
82// TODO: Implemented this
83void updateBms(void* bms) {
84// Bms* myBms = (Bms*) bms;
85 printf("BMS Update Not implemented\n");
86 // CanMessage canData = fetchCanData(...);
87 // parseCanData(&canData, canDataFn);
88 //
89 // // Call the transfer function to decode the message
90 // if (!bmsTransferFunction(myBms, &canData)) {
91 // printf("Error: Transfer function failed.\n");
92 // }
93}
94
95// @warning For testing and debugging purposes only
96void updateBmsTest(void* bms, const char* canDataFn) {
97// Bms* myBms = (Bms*) bms;
98// CanMessage canData;
99// parseCanData(&canData, canDataFn);
100//
101// // Call the transfer function to decode the message
102// if (!bmsTransferFunction(myBms, &canData)) {
103// printf("Update Test Error: Transfer function failed.\n");
104// }
105}
void updateBmsTest(void *bms, const char *canDataFn)
Updates the BMS data.
Definition: BMS.c:96
int bmsTransferFunction(Bms *bms, CAN_Message *canData)
Definition: BMS.c:63
void updateBms(void *bms)
Transfers raw BMS data to a BmsData structure.
Definition: BMS.c:83
void initBms(Bms *bms, int hz, const char *dbcFn)
Initializes the BMS with the given frequency.
Definition: BMS.c:7
void assignBmsValue(Bms *bms, int id, float value, const char *name)
Definition: BMS.c:26
@ IDLE
Definition: BMS.h:12
void initExternalSystem(ExternalSystem *external, const char *name, int hz, ExternalType type)
Definition: ExternalSystem.c:3
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
ExternalSystem extSystem
Definition: BMS.h:16
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
Updateable updateable
Definition: System.h:18
void(* update)(void *self)
Definition: Updateable.h:26