Skip to content

Commit ecebc86

Browse files
committed
Some code cleanup
1 parent f152c2e commit ecebc86

File tree

2 files changed

+129
-83
lines changed

2 files changed

+129
-83
lines changed

BT-DASH-4B.shsds

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
"AutomaticReconnect": true,
33
"SerialPortName": "COM8",
44
"IsConnecting": false,
5-
"IsEnabled": true,
5+
"IsEnabled": false,
66
"LogIncomingData": false,
7-
"IsConnected": true,
7+
"IsConnected": false,
88
"BaudRate": 115200,
99
"UpdateMessages": [
1010
{
1111
"Message": {
12-
"Expression": "'{'+isnull([Rpms], '0')+'&'+isnull([SpeedKmh], '0')+'&'+isnull([DataCorePlugin.Computed.Fuel_Percent], '0')+'&'+isnull([WaterTemperature], '0')+'&'+isnull([TurnIndicatorLeft], '0')+'&'+isnull([TurnIndicatorRight], '0')+'&'+isnull([Handbrake], '0')+'&'+isnull([OilTemperature], '0')+'}'"
12+
"Expression": "'{'+round(isnull([Rpms], '0'), 0)+'&'+round(isnull([SpeedKmh], '0'), 0)+'&'+round(isnull([DataCorePlugin.Computed.Fuel_Percent], '0'), 0)+'&'+round(isnull([WaterTemperature], '0'), 2)+'&'+isnull([TurnIndicatorLeft], '0')+'&'+isnull([TurnIndicatorRight], '0')+'&'+isnull([Handbrake], '0')+'&'+round(isnull([OilTemperature], '0'), 2)+'}'"
1313
},
1414
"IsEnabled": true,
1515
"MaximumFrequency": 0
1616
}
1717
],
1818
"OnConnectMessage": {
19-
"Expression": ""
19+
"Expression": "'{0&0&100&0&0&0&0&0}'"
2020
},
2121
"OnDisconnectMessage": {
2222
"Expression": ""
@@ -26,7 +26,7 @@
2626
"EditorExpanded": true,
2727
"Name": "Custom Serial device",
2828
"Description": "BT-DASH-4B",
29-
"LastErrorDate": "0001-01-01T00:00:00",
29+
"LastErrorDate": "2021-05-25T23:51:50.5672249+02:00",
3030
"LastErrorMessage": null,
3131
"IsFreezed": false,
3232
"SettingsBuilder": {

ESP32-SimHubBT-4B.ino

Lines changed: 124 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
#define DUTY_MAX ((1 << LEDC_TIMER_10_BIT) -1 )
1616
#define FREQ_MIN_Hz 1 /* Do not decrease it! */
1717

18+
#define FUEL_DUTY_CYCLE_MIN 100 /* FULL */
19+
#define FUEL_DUTY_CYCLE_MAX 540 /* EMPTY */
20+
#define TEMP_DUTY_CYCLE_MIN 26 /* 60°C */
21+
#define TEMP_DUTY_CYCLE_MAX 512 /* >120°C */
22+
#define OILTEMP_DUTY_CYCLE_MIN 12 /* 60°C */
23+
#define OILTEMP_DUTY_CYCLE_MAX 600 /* >180°C */
24+
#define OIL_WARN_TRESH_RPM 300
1825

1926
#define TURN_R_PIN 5
2027
#define FUEL_PIN 2
@@ -32,25 +39,11 @@
3239
#define DIMM_PIN 33
3340
#define PWR_PIN 17
3441

35-
#define BUF_SIZE 128
42+
#define BUF_SIZE 64
3643

37-
char simHubMessageBuf[BUF_SIZE];
38-
BluetoothSerial btSerial;
44+
char simhub_message_buf[BUF_SIZE];
45+
BluetoothSerial bt_serial;
3946

40-
float revs;
41-
float speed_kmh;
42-
float fuel_percent;
43-
float water_temperature_degC;
44-
int turn_left;
45-
int turn_right;
46-
int brake;
47-
float oil_temperature_degC;
48-
49-
unsigned int temp_duty_cycle = 0;
50-
float speed_Hz = 0;
51-
float rpm_Hz = 0;
52-
unsigned int fuel_duty_cycle = 0;
53-
unsigned int oil_temp_duty_cycle = 0;
5447

5548
void ledc_init(uint8_t pin, float freq_Hz, ledc_channel_t channel, ledc_timer_t timer) {
5649
const char * ME = __func__;
@@ -137,23 +130,32 @@ void setup() {
137130

138131
digitalWrite(PWR_PIN, HIGH);
139132

140-
memset(simHubMessageBuf, 0x0, BUF_SIZE);
141-
btSerial.begin(BT_NAME);
133+
memset(simhub_message_buf, 0x0, BUF_SIZE);
134+
bt_serial.begin(BT_NAME);
142135
}
143136

144137
void loop() {
145-
if (btSerial.available() > 0)
138+
if (bt_serial.available() > 0)
146139
{
147-
btSerial.readBytesUntil('{', simHubMessageBuf, BUF_SIZE);
148-
int readCount = btSerial.readBytesUntil('}', simHubMessageBuf, BUF_SIZE);
149-
simHubMessageBuf[min(readCount, BUF_SIZE - 1)] = 0x0;
150-
processMessage();
151-
memset(simHubMessageBuf, 0x0, BUF_SIZE);
140+
bt_serial.readBytesUntil('{', simhub_message_buf, BUF_SIZE);
141+
int readCount = bt_serial.readBytesUntil('}', simhub_message_buf, BUF_SIZE);
142+
simhub_message_buf[min(readCount, BUF_SIZE - 1)] = 0x0;
143+
process_message();
144+
memset(simhub_message_buf, 0x0, BUF_SIZE);
152145
}
153146
}
154147

155-
void processMessage() {
156-
sscanf(simHubMessageBuf, "%f&%f&%f&%f&%d&%d&%d&%f",
148+
void process_message() {
149+
unsigned int revs;
150+
unsigned int speed_kmh;
151+
unsigned int fuel_percent;
152+
float water_temperature_degC;
153+
int turn_left;
154+
int turn_right;
155+
int brake;
156+
float oil_temperature_degC;
157+
158+
sscanf(simhub_message_buf, "%u&%u&%u&%f&%d&%d&%d&%f",
157159
&revs,
158160
&speed_kmh,
159161
&fuel_percent,
@@ -164,17 +166,75 @@ void processMessage() {
164166
&oil_temperature_degC
165167
);
166168

167-
if (revs >= 200)
169+
float rpm_Hz = rpm_get_Hz(revs);
170+
float speed_Hz = speed_get_Hz(speed_kmh);
171+
unsigned int fuel_duty_cycle = fuel_duty_cycle_get(fuel_percent);
172+
unsigned int temp_duty_cycle = water_temperature_duty_cycle_get(water_temperature_degC);
173+
unsigned int oil_temp_duty_cycle = oil_temperature_duty_cycle_get(oil_temperature_degC);
174+
175+
handle_lights(turn_left, turn_right, brake, revs);
176+
177+
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, temp_duty_cycle);
178+
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
179+
180+
ledc_set_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_1, rpm_Hz);
181+
ledc_set_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_2, speed_Hz);
182+
183+
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, fuel_duty_cycle);
184+
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3);
185+
186+
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, oil_temp_duty_cycle);
187+
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4);
188+
}
189+
190+
float rpm_get_Hz(float revs) {
191+
float freq_Hz = 0.05 * revs;
192+
193+
if (freq_Hz < FREQ_MIN_Hz)
168194
{
169-
digitalWrite(OIL_PIN, HIGH);
195+
return FREQ_MIN_Hz;
170196
}
171-
if (revs < 200)
197+
198+
return freq_Hz;
199+
}
200+
201+
float speed_get_Hz(float speed_kmh) {
202+
float freq_Hz = 1.087 * speed_kmh - 0.6;
203+
204+
if (freq_Hz < FREQ_MIN_Hz)
172205
{
173-
digitalWrite(OIL_PIN, LOW);
206+
return FREQ_MIN_Hz;
174207
}
175208

176-
rpm_Hz = 0.05 * revs;
209+
return freq_Hz;
210+
}
177211

212+
unsigned int fuel_duty_cycle_get(float fuel_percent) {
213+
unsigned int fuel_duty_cycle = FUEL_DUTY_CYCLE_MIN;
214+
215+
if (fuel_percent > 0.0 && fuel_percent <= 25.0)
216+
{
217+
fuel_duty_cycle = -9.6 * fuel_percent + 540;
218+
}
219+
else if (fuel_percent > 25.0 && fuel_percent <= 50.0)
220+
{
221+
fuel_duty_cycle = -4.4 * fuel_percent + 410;
222+
}
223+
else if (fuel_percent > 50.0 && fuel_percent <= 75.0)
224+
{
225+
fuel_duty_cycle = -2.08 * fuel_percent + 294;
226+
}
227+
else if (fuel_percent > 75.0 && fuel_percent <= 100.0)
228+
{
229+
fuel_duty_cycle = -1.52 * fuel_percent + 252;
230+
}
231+
232+
return fuel_duty_cycle;
233+
}
234+
235+
unsigned int water_temperature_duty_cycle_get(float water_temperature_degC) {
236+
unsigned int temp_duty_cycle = TEMP_DUTY_CYCLE_MIN;
237+
178238
if (water_temperature_degC > 60.0 && water_temperature_degC <= 80.0)
179239
{
180240
temp_duty_cycle = water_temperature_degC - 34;
@@ -192,25 +252,17 @@ void processMessage() {
192252
temp_duty_cycle = 21.0 * water_temperature_degC - 2100;
193253
}
194254

195-
speed_Hz = 1.087 * speed_kmh - 0.6;
196-
197-
if (fuel_percent > 0.0 && fuel_percent <= 25.0)
255+
if (temp_duty_cycle >= TEMP_DUTY_CYCLE_MAX)
198256
{
199-
fuel_duty_cycle = -9.6 * fuel_percent + 540;
200-
}
201-
else if (fuel_percent > 25.0 && fuel_percent <= 50.0)
202-
{
203-
fuel_duty_cycle = -4.4 * fuel_percent + 410;
204-
}
205-
else if (fuel_percent > 50.0 && fuel_percent <= 75.0)
206-
{
207-
fuel_duty_cycle = -2.08 * fuel_percent + 294;
208-
}
209-
else if (fuel_percent > 75.0 && fuel_percent <= 100.0)
210-
{
211-
fuel_duty_cycle = -1.52 * fuel_percent + 252;
257+
return TEMP_DUTY_CYCLE_MAX;
212258
}
213259

260+
return temp_duty_cycle;
261+
}
262+
263+
unsigned int oil_temperature_duty_cycle_get(float oil_temperature_degC) {
264+
unsigned int oil_temp_duty_cycle = OILTEMP_DUTY_CYCLE_MIN;
265+
214266
if (oil_temperature_degC > 60.0 && oil_temperature_degC <= 100.0)
215267
{
216268
oil_temp_duty_cycle = 0.9 * oil_temperature_degC - 42;
@@ -232,6 +284,30 @@ void processMessage() {
232284
oil_temp_duty_cycle = 17.5 * oil_temperature_degC - 2570;
233285
}
234286

287+
if (oil_temp_duty_cycle >= OILTEMP_DUTY_CYCLE_MAX)
288+
{
289+
return OILTEMP_DUTY_CYCLE_MAX;
290+
}
291+
292+
return oil_temp_duty_cycle;
293+
}
294+
295+
void handle_lights(
296+
int turn_left,
297+
int turn_right,
298+
int brake,
299+
unsigned int revs
300+
)
301+
{
302+
if (revs >= OIL_WARN_TRESH_RPM)
303+
{
304+
digitalWrite(OIL_PIN, HIGH);
305+
}
306+
if (revs < OIL_WARN_TRESH_RPM)
307+
{
308+
digitalWrite(OIL_PIN, LOW);
309+
}
310+
235311
if (turn_right != 0)
236312
{
237313
digitalWrite(TURN_R_PIN, HIGH);
@@ -256,34 +332,4 @@ void processMessage() {
256332
{
257333
digitalWrite(BRAKE_PIN, LOW);
258334
}
259-
260-
if (temp_duty_cycle >= 26 && temp_duty_cycle <= 512)
261-
{
262-
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, temp_duty_cycle);
263-
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
264-
}
265-
266-
if (rpm_Hz < FREQ_MIN_Hz)
267-
{
268-
rpm_Hz = FREQ_MIN_Hz;
269-
}
270-
ledc_set_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_1, rpm_Hz);
271-
272-
if (speed_Hz < FREQ_MIN_Hz)
273-
{
274-
speed_Hz = FREQ_MIN_Hz;
275-
}
276-
ledc_set_freq(LEDC_HIGH_SPEED_MODE, LEDC_TIMER_2, speed_Hz);
277-
278-
if (fuel_duty_cycle >= 100 && fuel_duty_cycle <= 540)
279-
{
280-
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3, fuel_duty_cycle);
281-
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_3);
282-
}
283-
284-
if (oil_temp_duty_cycle >= 12 && oil_temp_duty_cycle <= 600)
285-
{
286-
ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4, oil_temp_duty_cycle);
287-
ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_4);
288-
}
289335
}

0 commit comments

Comments
 (0)