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 Oct 30, 2023
1 parent b94c69b commit 62be73a
Showing 1 changed file with 63 additions and 35 deletions.
98 changes: 63 additions & 35 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ CAN_message_t canMsgTx;

unsigned long previousCanICLMillis = 0;

bool engineIsRunning = true;
bool engineIsRunning = false;
int previousACStatus = LOW;
int acSignalRequest = 0;

void setup() {
initialize();
Expand All @@ -35,26 +36,29 @@ void initialize() {

pinMode(EXTERNAL_TEMPERATURE_SENSOR, INPUT_ANALOG);

pinMode(PC13, OUTPUT);

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

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

processCanWriteICL(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(PC13, engineIsRunning ? HIGH : LOW);
}

void processCanWriteICL(long currentMillis) {
Expand Down Expand Up @@ -89,37 +93,61 @@ void processCanWriteICL3() {
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 (acStatus != previousACStatus) {
// 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;

switch (acStatus) {
case HIGH:
if (acSignalRequest >= 1) {
acSignalRequest = 0;
previousACStatus = acStatus;
}
canMsgTx.buf[1] = calculateFanStage(LOW);
break;
default: // OFF
if (acSignalRequest > 1) {
acSignalRequest = 0;
previousACStatus = acStatus;
canMsgTx.buf[1] = calculateFanStage(LOW);
} else if (acSignalRequest == 1) {
canMsgTx.buf[1] = calculateFanStage(LOW);
} else {
canMsgTx.buf[1] = calculateFanStage(HIGH);
}
break;
}

acSignalRequest++;
Can1.write(canMsgTx);
return;
}

switch (acStatus) {
case HIGH:
canMsgTx.buf[0] = 0xD2;
break;
default: // OFF
canMsgTx.buf[0] = 0x00;
break;
}

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

Expand Down

0 comments on commit 62be73a

Please sign in to comment.