This commit is contained in:
Sem van der Hoeven
2024-07-14 21:44:26 +02:00
parent 50fd4c813d
commit f9cef163b9
4 changed files with 307 additions and 153 deletions

View File

@@ -86,6 +86,7 @@ uint32_t ram_b_amount = 0; /* counts up to 96 to show the KB of ram */
char device_label_i = -1; /* counter in the current device label */ char device_label_i = -1; /* counter in the current device label */
char bt_state = BT_INITIALISING; char bt_state = BT_INITIALISING;
char update_slow = 0;
obd2_elm327_t elm327; obd2_elm327_t elm327;
@@ -160,7 +161,7 @@ void on_init_run()
display.print(" ", (display.getDisplayXSize() / 2) - (INIT_TEXT_WIDTH * display.getFontXsize() / 2) - (INIT_PERCENTAGE_WIDTH / 2), display.getDisplayYSize() / 2 + 50); display.print(" ", (display.getDisplayXSize() / 2) - (INIT_TEXT_WIDTH * display.getFontXsize() / 2) - (INIT_PERCENTAGE_WIDTH / 2), display.getDisplayYSize() / 2 + 50);
if (init_percent > 90) if (init_percent > 90)
{ {
update_percent(8); update_percent(8);
} }
} }
/* update initialization text */ /* update initialization text */
@@ -279,7 +280,7 @@ void on_init_run()
if (init_percent == 100) if (init_percent == 100)
{ {
statemachine_next(); statemachine_next();
} }
} }
@@ -304,29 +305,91 @@ void on_main_enter()
#if (DEBUG == 1) #if (DEBUG == 1)
Serial.println("Entering main loop"); Serial.println("Entering main loop");
#endif #endif
display.setColor(VGA_AQUA); Timer0.attachInterrupt(query_slow_obd2_values);
Timer0.start(MS(5000));
display.setColor(VGA_AQUA);
} }
void on_main_run() void on_main_run()
{ {
obd2_elm327_process(&elm327); if (update_slow)
{
update_slow = 0;
} else {
obd2_elm327_process_fast(&elm327);
}
if (elm327.value_updates & (1 << UPDATE_RPM_POS)) if (elm327.value_updates & (1 << UPDATE_RPM_POS))
{ {
display.print("rpm ",0,100);
display.printNumI(elm327.rpm,100,100,4,'0');
elm327.value_updates &= ~(1 << UPDATE_RPM_POS); elm327.value_updates &= ~(1 << UPDATE_RPM_POS);
#if (DEBUG == 1) int width = (int)(display.getDisplayXSize() * ((float)elm327.rpm / 7000.0));
Serial.println("rpm"); display.fillRect(0,20,width,20);
Serial.println(elm327.rpm); display.setColor(VGA_FUCHSIA);
#endif display.print("rpm", 0, 20);
display.printNumI(elm327.engine_load, 30, 20, 4, '0');
display.setColor(VGA_AQUA);
} }
if (elm327.value_updates & (1 << UPDATE_THROTTLE_POS)) if (elm327.value_updates & (1 << UPDATE_COOLANT_TEMP_POS))
{ {
display.printNumI(elm327.throttle_percent,100,20,3,'0'); elm327.value_updates &= ~(1 << UPDATE_COOLANT_TEMP_POS);
elm327.value_updates &= ~(1 << UPDATE_THROTTLE_POS); display.print("coolant temp", 0, 100);
} display.printNumI(elm327.engine_coolant_temp, 200, 100, 4, '0');
}
if (elm327.value_updates & (1 << UPDATE_FUEL_PRESSURE_POS))
{
elm327.value_updates &= ~(1 << UPDATE_FUEL_PRESSURE_POS);
display.print("fuel pressure", 0, 120);
display.printNumF(elm327.fuel_pressure, 200, 120, 4, '0');
}
if (elm327.value_updates & (1 << UPDATE_FUEL_LEVEL_POS))
{
elm327.value_updates &= ~(1 << UPDATE_FUEL_LEVEL_POS);
display.print("fuel level", 0, 140);
display.printNumF(elm327.fuel_level, 200, 140, 4, '0');
}
// if (elm327.value_updates & (1 << UPDATE_INTAKE_AIR_TEMP_POS))
// {
// elm327.value_updates &= ~(1 << UPDATE_INTAKE_AIR_TEMP_POS);
// display.print("intake air temp", 0, 160);
// display.printNumI(elm327.intake_air_temp, 200, 160, 4, '0');
// }
// if (elm327.value_updates & (1 << UPDATE_AMBIENT_AIR_TEMP_POS))
// {
// elm327.value_updates &= ~(1 << UPDATE_AMBIENT_AIR_TEMP_POS);
// display.print("ambient air temp", 0, 180);
// display.printNumI(elm327.ambient_air_temp, 200, 180, 4, '0');
// }
// if (elm327.value_updates & (1 << UPDATE_OIL_TEMP_POS))
// {
// elm327.value_updates &= ~(1 << UPDATE_OIL_TEMP_POS);
// display.print("oil temp", 0, 200);
// display.printNumF(elm327.engine_oil_temp, 200, 200, 4, '0');
// }
if (elm327.value_updates & (1 << UPDATE_ENGINE_LOAD_POS))
{
elm327.value_updates &= ~(1 << UPDATE_ENGINE_LOAD_POS);
int width = (int)(display.getDisplayXSize() * ((float)elm327.engine_load / 100.0));
display.fillRect(0,50,width,50);
display.setColor(VGA_FUCHSIA);
display.print("el", 0, 50);
display.printNumI(elm327.engine_load, 20, 50, 4, '0');
display.setColor(VGA_AQUA);
}
// if (elm327.value_updates & (1 << UPDATE_MANIFOLD_PRESSURE_POS))
// {
// elm327.value_updates &= ~(1 << UPDATE_MANIFOLD_PRESSURE_POS);
// display.print("manifold pressure", 0, 240);
// display.printNumI(elm327.manifold_pressure, 200, 240, 4, '0');
// }
} }
void update_init_text() void update_init_text()
@@ -432,7 +495,12 @@ void update_ram_kb()
void bt_state_changed() void bt_state_changed()
{ {
update_percent(10); update_percent(10);
}
void query_slow_obd2_values()
{
update_slow = 1;
} }
void setup() void setup()
@@ -460,7 +528,7 @@ void setup()
statemachine_register_state(&main_state, STATE_CAR_INFO); statemachine_register_state(&main_state, STATE_CAR_INFO);
// TODO remove // TODO remove
//statemachine_register_state(&main_state, 0); // statemachine_register_state(&main_state, 0);
statemachine_init(); statemachine_init();
display.clrScr(); display.clrScr();
@@ -473,14 +541,14 @@ void setup()
display.setColor(COLOR_ORANGE); display.setColor(COLOR_ORANGE);
display.print("Waiting for ELM327", (display.getDisplayXSize() / 2) - ((18 * display.getFontXsize()) / 2), 160); display.print("Waiting for ELM327", (display.getDisplayXSize() / 2) - ((18 * display.getFontXsize()) / 2), 160);
#if (DEBUG == 1) #if (DEBUG == 1)
Serial.println("checking for bt"); Serial.println("checking for bt");
#endif #endif
if (!obd2_elm327_init(&elm327)) if (!obd2_elm327_init(&elm327))
{ {
#if (DEBUG == 1) #if (DEBUG == 1)
Serial.println("Shit man its fucked"); Serial.println("Shit man its fucked");
#endif #endif
} }
elm327.on_state_change = &bt_state_changed; elm327.on_state_change = &bt_state_changed;
#if (DEBUG == 1) #if (DEBUG == 1)

View File

@@ -9,22 +9,24 @@
typedef enum elm327_current_value_e typedef enum elm327_current_value_e
{ {
RPM_V, RPM_V, // fast
THROTTLE_V, // THROTTLE_V,
COOLANT_TEMP_V, COOLANT_TEMP_V, // slow
INTAKE_AIR_TEMP_V, INTAKE_AIR_TEMP_V, // slow
AMBIENT_AIR_TEMP_V, AMBIENT_AIR_TEMP_V, // slow
OIL_TEMP_V, OIL_TEMP_V, // slow
ENGINE_LOAD_V, ENGINE_LOAD_V, // fast
BATTERY_VOLTAGE_V, // BATTERY_VOLTAGE_V,
FUEL_PRESSURE_V, FUEL_PRESSURE_V, // fast?
SPEED_V, // SPEED_V,
MANIFOLD_PRESSURE_V MANIFOLD_PRESSURE_V, // fast?
FUEL_LEVEL_V // slow
} elm327_current_value; } elm327_current_value;
ELM327 elm327_obj; ELM327 elm327_obj;
elm327_current_value current_value; elm327_current_value current_value_fast;
elm327_current_value current_value_slow;
char bt_states[2][BT_STATE_LENGTH] = {"Initializing", "Connected"}; char bt_states[2][BT_STATE_LENGTH] = {"Initializing", "Connected"};
@@ -65,81 +67,24 @@ void obd2_elm327_check_connection(obd2_elm327_t *elm327)
} }
} }
void obd2_elm327_process(obd2_elm327_t *elm327) void obd2_elm327_process_fast(obd2_elm327_t *elm327)
{ {
switch (current_value_fast)
switch (current_value)
{ {
case RPM_V: case RPM_V:
{ {
float rpm = elm327->elm327->rpm(); float rpm = elm327->elm327->rpm();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS) if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{ {
if (rpm > 0 && rpm < RPM_MAX)
elm327->rpm = (uint16_t)rpm; elm327->rpm = (uint16_t)rpm;
elm327->value_updates |= (1 << UPDATE_RPM_POS); elm327->value_updates |= (1 << UPDATE_RPM_POS);
current_value_fast = ENGINE_LOAD_V;
} }
current_value = THROTTLE_V; else if (elm327->elm327->nb_rx_state != ELM_GETTING_MSG)
break;
}
case THROTTLE_V:
{
float throttle = elm327->elm327->throttle();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{ {
if (throttle > 0 && throttle <= THROTTLE_MAX) // elm327->elm327->printError();
{ current_value_fast = ENGINE_LOAD_V;
elm327->throttle_percent = (uint8_t)throttle;
elm327->value_updates |= (1 << UPDATE_THROTTLE_POS);
}
} }
// current_value = COOLANT_TEMP_V;
//TODO remove
current_value = RPM_V;
break;
}
case COOLANT_TEMP_V:
{
float coolant_temp = elm327->elm327->engineCoolantTemp();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{
elm327->engine_coolant_temp = (uint8_t)coolant_temp;
elm327->value_updates |= (1 << UPDATE_COOLANT_TEMP_POS);
}
current_value = INTAKE_AIR_TEMP_V;
break;
}
case INTAKE_AIR_TEMP_V:
{
float intake_air_temp = elm327->elm327->intakeAirTemp();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{
elm327->intake_air_temp = (uint8_t)intake_air_temp;
elm327->value_updates |= (1 << UPDATE_INTAKE_AIR_TEMP_POS);
}
current_value = AMBIENT_AIR_TEMP_V;
break;
}
case AMBIENT_AIR_TEMP_V:
{
float ambient_air_temp = elm327->elm327->ambientAirTemp();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{
elm327->ambient_air_temp = (uint8_t)ambient_air_temp;
elm327->value_updates |= (1 << UPDATE_AMBIENT_AIR_TEMP_POS);
}
current_value = OIL_TEMP_V;
break;
}
case OIL_TEMP_V:
{
float engine_oil_temp = elm327->elm327->oilTemp();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{
elm327->engine_oil_temp = engine_oil_temp;
elm327->value_updates |= (1 << UPDATE_OIL_TEMP_POS);
}
current_value = ENGINE_LOAD_V;
break; break;
} }
case ENGINE_LOAD_V: case ENGINE_LOAD_V:
@@ -149,19 +94,13 @@ void obd2_elm327_process(obd2_elm327_t *elm327)
{ {
elm327->engine_load = (uint8_t)engine_load; elm327->engine_load = (uint8_t)engine_load;
elm327->value_updates |= (1 << UPDATE_ENGINE_LOAD_POS); elm327->value_updates |= (1 << UPDATE_ENGINE_LOAD_POS);
current_value_fast = FUEL_PRESSURE_V;
} }
current_value = BATTERY_VOLTAGE_V; else if (elm327->elm327->nb_rx_state != ELM_GETTING_MSG)
break;
}
case BATTERY_VOLTAGE_V:
{
float battery_voltage = elm327->elm327->batteryVoltage();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{ {
elm327->battery_voltage = battery_voltage; // elm327->elm327->printError();
elm327->value_updates |= (1 << UPDATE_BATTERY_VOLTAGE_POS); current_value_fast = FUEL_PRESSURE_V;
} }
current_value = FUEL_PRESSURE_V;
break; break;
} }
case FUEL_PRESSURE_V: case FUEL_PRESSURE_V:
@@ -169,21 +108,15 @@ void obd2_elm327_process(obd2_elm327_t *elm327)
float fuel_pressure = elm327->elm327->fuelPressure(); float fuel_pressure = elm327->elm327->fuelPressure();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS) if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{ {
elm327->fuel_pressure = fuel_pressure; elm327->fuel_pressure = (uint16_t)fuel_pressure;
elm327->value_updates |= (1 << UPDATE_FUEL_PRESSURE_POS); elm327->value_updates |= (1 << UPDATE_FUEL_PRESSURE_POS);
current_value_fast = MANIFOLD_PRESSURE_V;
} }
current_value = SPEED_V; else if (elm327->elm327->nb_rx_state != ELM_GETTING_MSG)
break;
}
case SPEED_V:
{
float speed = elm327->elm327->mph();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{ {
elm327->speed = (uint8_t)(speed * KM_IN_MILES); // elm327->elm327->printError();
elm327->value_updates |= (1 << UPDATE_SPEED_POS); current_value_fast = MANIFOLD_PRESSURE_V;
} }
current_value = MANIFOLD_PRESSURE_V;
break; break;
} }
case MANIFOLD_PRESSURE_V: case MANIFOLD_PRESSURE_V:
@@ -193,8 +126,101 @@ void obd2_elm327_process(obd2_elm327_t *elm327)
{ {
elm327->manifold_pressure = (uint8_t)manifold_pressure; elm327->manifold_pressure = (uint8_t)manifold_pressure;
elm327->value_updates |= (1 << UPDATE_MANIFOLD_PRESSURE_POS); elm327->value_updates |= (1 << UPDATE_MANIFOLD_PRESSURE_POS);
current_value_fast = RPM_V;
}
else if (elm327->elm327->nb_rx_state != ELM_GETTING_MSG)
{
// elm327->elm327->printError();
current_value_fast = RPM_V;
}
break;
}
}
}
void obd2_elm327_process_slow(obd2_elm327_t *elm327)
{
switch (current_value_slow)
{
case COOLANT_TEMP_V:
{
float coolant_temp = elm327->elm327->engineCoolantTemp();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{
elm327->engine_coolant_temp = (uint8_t)coolant_temp;
elm327->value_updates |= (1 << UPDATE_COOLANT_TEMP_POS);
current_value_slow = INTAKE_AIR_TEMP_V;
}
else if (elm327->elm327->nb_rx_state != ELM_GETTING_MSG)
{
// elm327->elm327->printError();
current_value_slow = INTAKE_AIR_TEMP_V;
}
break;
}
case INTAKE_AIR_TEMP_V:
{
float intake_air_temp = elm327->elm327->intakeAirTemp();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{
elm327->intake_air_temp = (uint8_t)intake_air_temp;
elm327->value_updates |= (1 << UPDATE_INTAKE_AIR_TEMP_POS);
current_value_slow = AMBIENT_AIR_TEMP_V;
}
else if (elm327->elm327->nb_rx_state != ELM_GETTING_MSG)
{
// elm327->elm327->printError();
current_value_slow = AMBIENT_AIR_TEMP_V;
}
break;
}
case AMBIENT_AIR_TEMP_V:
{
float ambient_air_temp = elm327->elm327->ambientAirTemp();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{
elm327->ambient_air_temp = (uint8_t)ambient_air_temp;
elm327->value_updates |= (1 << UPDATE_AMBIENT_AIR_TEMP_POS);
current_value_slow = OIL_TEMP_V;
}
else if (elm327->elm327->nb_rx_state != ELM_GETTING_MSG)
{
// elm327->elm327->printError();
current_value_slow = OIL_TEMP_V;
}
break;
}
case OIL_TEMP_V:
{
float engine_oil_temp = elm327->elm327->oilTemp();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{
elm327->engine_oil_temp = (uint16_t)engine_oil_temp;
elm327->value_updates |= (1 << UPDATE_OIL_TEMP_POS);
current_value_slow = FUEL_LEVEL_V;
}
else if (elm327->elm327->nb_rx_state != ELM_GETTING_MSG)
{
// elm327->elm327->printError();
current_value_slow = FUEL_LEVEL_V;
}
break;
}
case FUEL_LEVEL_V:
{
float fuel_level = elm327->elm327->fuelLevel();
if (elm327->elm327->nb_rx_state == ELM_SUCCESS)
{
elm327->fuel_level = (uint16_t)fuel_level;
elm327->value_updates |= (1 << UPDATE_FUEL_LEVEL_POS);
current_value_slow = COOLANT_TEMP_V;
}
else if (elm327->elm327->nb_rx_state != ELM_GETTING_MSG)
{
// elm327->elm327->printError();
current_value_slow = COOLANT_TEMP_V;
} }
current_value = RPM_V;
break; break;
} }
default: default:

View File

@@ -66,6 +66,7 @@ Be sure to disconnect the TX and RX pins from the BT module when programming the
#define UPDATE_FUEL_PRESSURE_POS 0x08 #define UPDATE_FUEL_PRESSURE_POS 0x08
#define UPDATE_SPEED_POS 0x09 #define UPDATE_SPEED_POS 0x09
#define UPDATE_MANIFOLD_PRESSURE_POS 0x0A #define UPDATE_MANIFOLD_PRESSURE_POS 0x0A
#define UPDATE_FUEL_LEVEL_POS 0x0B
enum BluetoothState enum BluetoothState
{ {
@@ -90,12 +91,13 @@ typedef struct obt2_elm327_tag
uint8_t engine_coolant_temp; uint8_t engine_coolant_temp;
uint8_t intake_air_temp; uint8_t intake_air_temp;
uint8_t ambient_air_temp; uint8_t ambient_air_temp;
float engine_oil_temp; uint16_t engine_oil_temp;
uint8_t engine_load; uint8_t engine_load;
float battery_voltage; float battery_voltage;
float fuel_pressure; uint16_t fuel_pressure;
uint8_t speed; uint8_t speed;
uint8_t manifold_pressure; uint8_t manifold_pressure;
uint16_t fuel_level;
} obd2_elm327_t; } obd2_elm327_t;
/** /**
@@ -107,11 +109,18 @@ typedef struct obt2_elm327_tag
char obd2_elm327_init(obd2_elm327_t *elm327); char obd2_elm327_init(obd2_elm327_t *elm327);
/** /**
* @brief processes the bluetooth module communication with the OBD2 reader * @brief processes the bluetooth module communication with the OBD2 reader for variables that change quickly
* *
* @param bt pointer to struct holding bluetooth driver data * @param bt pointer to struct holding bluetooth driver data
*/ */
void obd2_elm327_process(obd2_elm327_t *elm327); void obd2_elm327_process_fast(obd2_elm327_t *elm327);
/**
* @brief processes the bluetooth module communication with the OBD2 reader for variables that change slowly
*
* @param bt pointer to struct holding bluetooth driver data
*/
void obd2_elm327_process_slow(obd2_elm327_t *elm327);
void obd2_elm327_check_connection(obd2_elm327_t *elm327); void obd2_elm327_check_connection(obd2_elm327_t *elm327);

View File

@@ -6,9 +6,20 @@
ELM327 myELM327; ELM327 myELM327;
typedef enum
{
ENG_COOLANT_TEMP,
ENG_FUEL_PRESSURE,
ENG_MANIFOLD_PRESSURE,
RPM
} obd2_state;
uint32_t rpm = 0; float rpm = 0;
float cool_temp = 0;
uint8_t man_pres = 0;
float fuel_pres = 0;
obd2_state current_state = ENG_COOLANT_TEMP;
void setup() void setup()
{ {
@@ -46,38 +57,78 @@ void setup()
void loop() void loop()
{ {
float tempRPM = myELM327.rpm();
// float t = myELM327.
// float tempval3 = myELM327.fuelInjectTiming();
if (myELM327.nb_rx_state == ELM_SUCCESS) switch (current_state)
{ {
rpm = (uint32_t)tempRPM; case ENG_COOLANT_TEMP:
Serial.print("RPM: "); Serial.println(rpm); {
// Serial.print("feul t: "); Serial.println(tempval3); cool_temp = myELM327.engineCoolantTemp();
if (myELM327.nb_rx_state == ELM_SUCCESS)
{
Serial.print("coolant temp: ");
Serial.println(cool_temp);
current_state = ENG_FUEL_PRESSURE;
}
else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
{
myELM327.printError();
//current_state = ENG_FUEL_PRESSURE;
}
break;
}
case ENG_FUEL_PRESSURE:
{
fuel_pres = myELM327.fuelPressure();
if (myELM327.nb_rx_state == ELM_SUCCESS)
{
Serial.print("fuel pressure: ");
Serial.println(fuel_pres);
current_state = ENG_MANIFOLD_PRESSURE;
}
else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
{
myELM327.printError();
//current_state = ENG_MANIFOLD_PRESSURE;
}
break;
}
case ENG_MANIFOLD_PRESSURE:
{
man_pres = myELM327.manifoldPressure();
if (myELM327.nb_rx_state == ELM_SUCCESS)
{
Serial.print("manifold pressure: ");
Serial.println(man_pres);
current_state = RPM;
}
else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
{
myELM327.printError();
//current_state = RPM;
}
break;
}
case RPM:
{
rpm = myELM327.rpm();
if (myELM327.nb_rx_state == ELM_SUCCESS)
{
Serial.print("rpm: ");
Serial.println(rpm);
current_state = ENG_COOLANT_TEMP;
}
else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
{
myELM327.printError();
//current_state = ENG_COOLANT_TEMP;
}
break;
}
} }
else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
myELM327.printError();
float tempval1 = myELM327.engineCoolantTemp();
if (myELM327.nb_rx_state == ELM_SUCCESS)
{
Serial.print("coolant: "); Serial.println(tempval1);
}
else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
myELM327.printError();
float tempval2 = myELM327.oilTemp();
if (myELM327.nb_rx_state == ELM_SUCCESS)
{
Serial.print("oil: "); Serial.println(tempval2);
}
else if (myELM327.nb_rx_state != ELM_GETTING_MSG)
myELM327.printError();
} }