mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-05 19:42:08 +02:00
WIP
This commit is contained in:
parent
25b0a7da65
commit
a9b7c0b2a7
1 changed files with 42 additions and 1 deletions
|
@ -30,7 +30,9 @@ static const int interval1000 = 1000; // interval (ms) at which send CA
|
||||||
static const int interval2000 = 2000; // interval (ms) at which send CAN Messages
|
static const int interval2000 = 2000; // interval (ms) at which send CAN Messages
|
||||||
static const int interval5000 = 5000; // interval (ms) at which send CAN Messages
|
static const int interval5000 = 5000; // interval (ms) at which send CAN Messages
|
||||||
static const int interval10000 = 10000; // interval (ms) at which send CAN Messages
|
static const int interval10000 = 10000; // interval (ms) at which send CAN Messages
|
||||||
static uint8_t CANstillAlive = 12; //counter for checking if CAN is still alive
|
static uint8_t CANstillAlive = 12; // counter for checking if CAN is still alive
|
||||||
|
static uint16_t CANerror = 0; // counter on how many CAN errors encountered
|
||||||
|
#define MAX_CAN_FAILURES 5000; // Amount of malformed CAN messages to allow before raising a warning
|
||||||
|
|
||||||
static const uint16_t WUPonDuration = 477; // in milliseconds how long WUP should be ON after poweron
|
static const uint16_t WUPonDuration = 477; // in milliseconds how long WUP should be ON after poweron
|
||||||
static const uint16_t WUPoffDuration = 105; // in milliseconds how long WUP should be OFF after on pulse
|
static const uint16_t WUPoffDuration = 105; // in milliseconds how long WUP should be OFF after on pulse
|
||||||
|
@ -39,6 +41,26 @@ unsigned long turnOnTime; // Variables to store timestamps
|
||||||
enum State { POWERON, STATE_ON, STATE_OFF, STATE_STAY_ON };
|
enum State { POWERON, STATE_ON, STATE_OFF, STATE_STAY_ON };
|
||||||
static State WUPState = POWERON;
|
static State WUPState = POWERON;
|
||||||
|
|
||||||
|
const unsigned char crc8_table[256] = {
|
||||||
|
0x00, 0x64, 0xC8, 0xAC, 0xE1, 0x85, 0x29, 0x4D, 0xB3, 0xD7, 0x7B, 0x1F, 0x52, 0x36, 0x9A, 0xFE,
|
||||||
|
0x17, 0x73, 0xDF, 0xBB, 0xF6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xB7, 0x1B, 0x7F,
|
||||||
|
0xE4, 0x80, 0x2C, 0x48, 0x05, 0x61, 0xCD, 0xA9, 0x57, 0x33, 0x9F, 0xFB, 0xB6, 0xD2, 0x7E, 0x1A,
|
||||||
|
0xF3, 0x97, 0x3B, 0x5F, 0x12, 0x76, 0xDA, 0xBE, 0x40, 0x24, 0x88, 0xEC, 0xA1, 0xC5, 0x69, 0x0D,
|
||||||
|
0xCA, 0xAE, 0x02, 0x66, 0x2B, 0x4F, 0xE3, 0x87, 0x79, 0x1D, 0xB1, 0xD5, 0x98, 0xFC, 0x50, 0x34,
|
||||||
|
0xDD, 0xB9, 0x15, 0x71, 0x3C, 0x58, 0xF4, 0x90, 0x6E, 0x0A, 0xA6, 0xC2, 0x8F, 0xEB, 0x47, 0x23
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
CAN_frame_t BMW_10B = {.FIR = {.B =
|
CAN_frame_t BMW_10B = {.FIR = {.B =
|
||||||
{
|
{
|
||||||
.DLC = 3,
|
.DLC = 3,
|
||||||
|
@ -581,6 +603,13 @@ void update_values_battery() { //This function maps all the values fetched via
|
||||||
CANstillAlive--;
|
CANstillAlive--;
|
||||||
clear_event(EVENT_CAN_RX_FAILURE);
|
clear_event(EVENT_CAN_RX_FAILURE);
|
||||||
}
|
}
|
||||||
|
// Check if we have encountered any malformed CAN messages
|
||||||
|
if (CANerror >
|
||||||
|
MAX_CAN_FAILURES) //Also check if we have recieved too many malformed CAN messages. If so, signal via LED
|
||||||
|
{
|
||||||
|
errorCode = 10;
|
||||||
|
set_event(EVENT_CAN_RX_WARNING, 0);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef DEBUG_VIA_USB
|
#ifdef DEBUG_VIA_USB
|
||||||
Serial.print("Battery values: ");
|
Serial.print("Battery values: ");
|
||||||
|
@ -631,6 +660,10 @@ void receive_can_battery(CAN_frame_t rx_frame) {
|
||||||
DC_link = rx_frame.data.u8[7];
|
DC_link = rx_frame.data.u8[7];
|
||||||
break;
|
break;
|
||||||
case 0x239: //BMS [200ms]
|
case 0x239: //BMS [200ms]
|
||||||
|
if (is_message_corrupt(rx_frame)) {
|
||||||
|
CANerror++;
|
||||||
|
break; //Message content malformed, abort reading data from it
|
||||||
|
}
|
||||||
TemperatureMaxMaybe = rx_frame.data.u8[2];
|
TemperatureMaxMaybe = rx_frame.data.u8[2];
|
||||||
break;
|
break;
|
||||||
case 0x2F5: //BMS [100ms]
|
case 0x2F5: //BMS [100ms]
|
||||||
|
@ -990,6 +1023,14 @@ void send_can_battery() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool is_message_corrupt(CAN_frame_t rx_frame, uint8_t length) {
|
||||||
|
uint8_t crc = 0;
|
||||||
|
for (uint8_t j = 0; j < length; j++) {
|
||||||
|
crc = crc8_table[(crc ^ static_cast<uint8_t>(rx_frame.data.u8[j])) % 256];
|
||||||
|
}
|
||||||
|
return crc != rx_frame.data.u8[length];
|
||||||
|
}
|
||||||
|
|
||||||
void setup_battery(void) { // Performs one time setup at startup
|
void setup_battery(void) { // Performs one time setup at startup
|
||||||
Serial.println("BMW i3 battery selected");
|
Serial.println("BMW i3 battery selected");
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue