Skip to content

Commit

Permalink
Update routine that send AC request to active eFan deactivation
Browse files Browse the repository at this point in the history
  • Loading branch information
ffrizzo committed Nov 6, 2023
1 parent b94c69b commit 5c4c9f3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 44 deletions.
124 changes: 81 additions & 43 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#include "main.h"

const byte FAN_STAGE_TO_HEX[16] = {0x0, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0};
const byte LED_ON_STATE = LOW;
const byte LEF_OFF_STATE = HIGH;
const byte FAN_STAGE_TO_HEX[16] = {0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0};

STM32_CAN Can1(CAN1, DEF);

CAN_message_t canMsgRx;
CAN_message_t canMsgTx;

unsigned long previousCanReadMillis = 0;
unsigned long previousCanICLMillis = 0;

bool engineIsRunning = true;
bool engineIsRunning = true; // TODO
int previousACStatus = LOW;
int acSignalRequest = 0;

void setup() {
initialize();
Expand All @@ -35,30 +39,43 @@ void initialize() {

pinMode(EXTERNAL_TEMPERATURE_SENSOR, INPUT_ANALOG);

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LEF_OFF_STATE);

Can1.begin();
Can1.setBaudRate(500000);
}

void processCan(long currentMillis) {
// if (Can1.read(canMsgRx)) {
// switch (canMsgRx.id) {
// case DME2:
// processCanReadDME2(canMsgRx);
// break;
// }
// }

// processCanRead(currentMillis); // TODO
processCanWriteICL(currentMillis);
}

void processCanRead(long currentMillis) {
if ((currentMillis - previousCanReadMillis) < CAN_READ_INTERVAL) {
return;
}

if (Can1.read(canMsgRx)) {
switch (canMsgRx.id) {
case DME2:
processCanReadDME2(canMsgRx);
break;
}
}

previousCanReadMillis = currentMillis;
}

void processCanReadDME2(CAN_message_t msg) {
byte byte3 = msg.buf[3];
engineIsRunning = bitRead(byte3, 3) == 1;
Serial.printf("Engine is running %s\n", engineIsRunning);
engineIsRunning = bitRead(byte3, 3) == HIGH;

digitalWrite(LED_BUILTIN, engineIsRunning ? LED_ON_STATE : LEF_OFF_STATE);
}

void processCanWriteICL(long currentMillis) {
if ((currentMillis - previousCanICLMillis) < CAN_INTERVAL) {
if ((currentMillis - previousCanICLMillis) < CAN_ICL_WRITE_INTERVAL) {
return;
}

Expand All @@ -83,43 +100,66 @@ void processCanWriteICL2() {
}

void processCanWriteICL3() {
int acStatus = digitalRead(AC_INPUT);
Serial.printf("AC status is: %s\n", acStatus == 1 ? "on" : "off");
int currentACStatusRequest = digitalRead(AC_INPUT);
Serial.printf("AC status is: %s\n", currentACStatusRequest == HIGH ? "on" : "off");

canMsgTx.id = CAN_ICL3;
canMsgTx.len = 8;

if (engineIsRunning) {
if (acStatus != previousACStatus) {
// E46 sends a signal of ac requesting before sending the actual request for compressor activation
// this is valid for both states on/off
canMsgTx.buf[0] = 0x80;
canMsgTx.buf[1] = calculateFanStage(previousACStatus);
} else {
switch (acStatus) {
case HIGH:
canMsgTx.buf[0] = 0xD9; // E36 AC Compressor does not have variable control
break;
default: // OFF
canMsgTx.buf[0] = 0x00;
break;
}

canMsgTx.buf[1] = calculateFanStage(acStatus);
}

previousACStatus = acStatus;
} else {
canMsgTx.buf[0] = 0x00;
canMsgTx.buf[1] = 0x00;
}

canMsgTx.buf[2] = 0x00;
canMsgTx.buf[3] = readTemperatureSensor();
canMsgTx.buf[4] = 0x00;
canMsgTx.buf[5] = 0x00;
canMsgTx.buf[6] = 0x00;
canMsgTx.buf[7] = 0x00;

if (!engineIsRunning) {
canMsgTx.buf[0] = 0x00;
canMsgTx.buf[1] = 0x00;
Can1.write(canMsgTx);
return;
}

if (currentACStatusRequest != previousACStatus) {
byte fanStage = FAN_STAGE_TO_HEX[0];

switch (currentACStatusRequest) {
case HIGH:
if (acSignalRequest > 1) {
previousACStatus = currentACStatusRequest;
}
break;
default: // OFF
if (acSignalRequest > 3) {
previousACStatus = currentACStatusRequest;
}

if (acSignalRequest <= 1) {
canMsgTx.buf[1] = calculateFanStage(HIGH);
}
break;
}

acSignalRequest++;
// E46 sends a signal of ac requesting before sending the actual request for compressor state change
// this is valid for both states on/off
canMsgTx.buf[0] = 0x80;
canMsgTx.buf[1] = fanStage;
Can1.write(canMsgTx);
return;
}

acSignalRequest = 0;
switch (currentACStatusRequest) {
case HIGH:
canMsgTx.buf[0] = 0xD2; // Torque request for ac activation
break;
default: // OFF
canMsgTx.buf[0] = 0x00;
break;
}

canMsgTx.buf[1] = calculateFanStage(currentACStatusRequest);
Can1.write(canMsgTx);
}

Expand Down Expand Up @@ -183,7 +223,5 @@ byte calculateFanStageWithPressureSwitch(int acStatus) {

byte readTemperatureSensor() {
long tempSensor = analogRead(EXTERNAL_TEMPERATURE_SENSOR);
Serial.printf("External temperature sensor %d\n", tempSensor);

return 0x1E; // Temperature Fixed in 30C
return 0x1B; // Temperature Fixed in 27C
}
5 changes: 4 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#define CAN_ICL2 0x613
#define CAN_ICL3 0x615

#define CAN_INTERVAL 200 // milliseconds
#define CAN_READ_INTERVAL 10 // milliseconds
#define CAN_ICL_WRITE_INTERVAL 200 // milliseconds

#define MIN_FAN_STAGE 1
#define MAX_FAN_STAGE 15
Expand All @@ -29,6 +30,8 @@
void initialize();
void processCan(long currentMillis);

void processCanRead(long currentMillis);

// DME2 t/index.php?title=Siemens_MS43_CAN_Bus#DME2_0x329
void processCanReadDME2(CAN_message_t msg);

Expand Down

0 comments on commit 5c4c9f3

Please sign in to comment.