Skip to content

Commit

Permalink
add initialization functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lenvm committed Nov 12, 2023
1 parent ad17f5a commit 212244a
Showing 1 changed file with 60 additions and 31 deletions.
91 changes: 60 additions & 31 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,61 @@ uint8_t inverterAllowsContactorClosing = 1;

// Initialization
void setup() {
init_serial();

init_CAN();

init_LED();

init_contactors();

init_modbus();

inform_user_on_inverter();

inform_user_on_battery();
}

// Perform main program functions
void loop() {
// Input
receive_can(); // Receive CAN messages. Runs as fast as possible
#ifdef DUAL_CAN
receive_can2();
#endif

// Process
if (millis() - previousMillis10ms >= interval10) // Every 10ms
{
previousMillis10ms = millis();
handle_LED_state(); // Set the LED color according to state
#ifdef CONTACTOR_CONTROL
handle_contactors(); // Take care of startup precharge/contactor closing
#endif
}

if (millis() - previousMillisUpdateVal >= intervalUpdateValues) // Every 4.8s
{
previousMillisUpdateVal = millis();
update_values(); // Update values heading towards inverter. Prepare for sending on CAN, or write directly to Modbus.
}

// Output
send_can(); // Send CAN messages
#ifdef DUAL_CAN
send_can2();
#endif
}

// Initialization functions
void init_serial() {
// Init Serial monitor
Serial.begin(115200);
while (!Serial) {}
Serial.println("__ OK __");
}

void init_CAN() {
// CAN pins
pinMode(CAN_SE_PIN, OUTPUT);
digitalWrite(CAN_SE_PIN, LOW);
Expand All @@ -132,10 +182,14 @@ void setup() {
settings.mRequestedMode = ACAN2515Settings::NormalMode; // Select loopback mode
can.begin(settings, [] { can.isr(); });
#endif
}

void init_LED() {
// Init LED control
pixels.begin();
}

void init_contactors() {
// Init contactor pins
#ifdef CONTACTOR_CONTROL
pinMode(POSITIVE_CONTACTOR_PIN, OUTPUT);
Expand All @@ -153,7 +207,9 @@ void setup() {
pinMode(PRECHARGE_PIN, OUTPUT);
digitalWrite(PRECHARGE_PIN, LOW);
#endif
}

void init_modbus() {
// Set up Modbus RTU Server
pinMode(RS485_EN_PIN, OUTPUT);
digitalWrite(RS485_EN_PIN, HIGH);
Expand All @@ -178,7 +234,9 @@ void setup() {
// Start ModbusRTU background task
MBserver.begin(Serial2);
#endif
}

void inform_user_on_inverter() {
// Inform user what Inverter is used
#ifdef BYD_CAN
Serial.println("BYD CAN protocol selected");
Expand All @@ -203,7 +261,9 @@ void setup() {
intervalUpdateValues = 800; // This protocol also requires the values to be updated faster
Serial.println("SOLAX CAN protocol selected");
#endif
}

void inform_user_on_battery() {
// Inform user what battery is used
#ifdef BMW_I3_BATTERY
Serial.println("BMW i3 battery selected");
Expand All @@ -228,37 +288,6 @@ void setup() {
#endif
}

// Perform main program functions
void loop() {
// Input
receive_can(); // Receive CAN messages. Runs as fast as possible
#ifdef DUAL_CAN
receive_can2();
#endif

// Process
if (millis() - previousMillis10ms >= interval10) // Every 10ms
{
previousMillis10ms = millis();
handle_LED_state(); // Set the LED color according to state
#ifdef CONTACTOR_CONTROL
handle_contactors(); // Take care of startup precharge/contactor closing
#endif
}

if (millis() - previousMillisUpdateVal >= intervalUpdateValues) // Every 4.8s
{
previousMillisUpdateVal = millis();
update_values(); // Update values heading towards inverter. Prepare for sending on CAN, or write directly to Modbus.
}

// Output
send_can(); // Send CAN messages
#ifdef DUAL_CAN
send_can2();
#endif
}

// Functions
void receive_can() { // This section checks if we have a complete CAN message incoming
// Depending on which battery/inverter is selected, we forward this to their respective CAN routines
Expand Down

0 comments on commit 212244a

Please sign in to comment.