-
Notifications
You must be signed in to change notification settings - Fork 0
/
speeduino-serial-sim.ino
63 lines (55 loc) · 1.64 KB
/
speeduino-serial-sim.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "ecu/EngineStatus.h"
#include "ecu/EngineSimulation.cpp"
// Constants for serial communication
#define SERIAL_BAUD_RATE 115200
#define COMMAND_TRIGGER 'A'
// Global variable to hold real-time engine status
EngineStatus engineStatus;
/**
* @brief Setup function executed once at program start.
*
* Initializes serial communication and engine status.
*/
void setup() {
Serial.begin(SERIAL_BAUD_RATE);
initializeEngineStatus(engineStatus);
}
/**
* @brief Loop function executed repeatedly.
*
* Listens for commands from the serial port and responds with simulated engine data.
* Uses the 'A' command to trigger data generation.
*/
void loop() {
static unsigned long lastRpmChangeTime = 0;
static bool increasingRpm = true;
// Simulate RPM changes every RPM_CHANGE_INTERVAL milliseconds
if (millis() - lastRpmChangeTime >= RPM_CHANGE_INTERVAL) {
if (increasingRpm) {
// Simulate increasing RPM
engineStatus.rpmhi += 500;
if (engineStatus.rpmhi >= MAX_RPM) {
engineStatus.rpmhi = MAX_RPM;
increasingRpm = false;
}
} else {
// Simulate decreasing RPM
engineStatus.rpmhi -= 500;
if (engineStatus.rpmhi <= IDLE_RPM) {
engineStatus.rpmhi = IDLE_RPM;
increasingRpm = true;
}
}
lastRpmChangeTime = millis();
}
// Simulate other parameters based on RPM
simulateParameters(engineStatus);
// Send simulated engine data
if (Serial.available() > 0) {
char command = Serial.read();
if (command == COMMAND_TRIGGER) {
generateSimulatedEngineData(engineStatus);
Serial.write((uint8_t*)&engineStatus, sizeof(engineStatus));
}
}
}