From 0abd5e9b68a9c49ac5ad0591a3af32519d85b879 Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Tue, 15 Jul 2025 16:39:41 -0400 Subject: [PATCH] Bump to latest simapi for unix timestamp ticks. Push unix timestamp ticks to lua variable. Update test logic to test on 8000 rpms. Update example lua scripts with examples of led rpm blink. --- conf/rpms_and_flags.lua | 13 ++- conf/rpms_and_radar.lua | 91 ++++++++++++++++++++ src/monocoque/devices/serial/arduinoledlua.c | 18 ++++ src/monocoque/gameloop/gameloop.c | 10 +++ src/monocoque/simulatorapi/simapi | 2 +- 5 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 conf/rpms_and_radar.lua diff --git a/conf/rpms_and_flags.lua b/conf/rpms_and_flags.lua index eb4474f..936582d 100644 --- a/conf/rpms_and_flags.lua +++ b/conf/rpms_and_flags.lua @@ -10,8 +10,6 @@ -- SIMAPI_FLAG_BLACK_ORANGE = 8, -- SIMAPI_FLAG_ORANGE = 9 -led_clear_all() - if simdata.rpm > 0 and simdata.maxrpm > 0 then rpmmargin = .05*simdata.maxrpm; rpminterval = (simdata.maxrpm-rpmmargin) / 6; @@ -30,7 +28,16 @@ if simdata.rpm > 0 and simdata.maxrpm > 0 then if litleds >= 6 then color = RED end - set_led_range_to_color(1, litleds, color) + if litleds >= 6 then + + if simdata.mtick % 2 == 0 then + set_led_range_to_color(1, litleds, color) + else + led_clear_all() + end + else + set_led_range_to_color(1, litleds, color) + end end if simdata.playerflag == 0 then diff --git a/conf/rpms_and_radar.lua b/conf/rpms_and_radar.lua new file mode 100644 index 0000000..4337c51 --- /dev/null +++ b/conf/rpms_and_radar.lua @@ -0,0 +1,91 @@ +-- configuration +-- set these accordingly +right_leds_start = 7 +right_num_leds = 42 +left_leds_start = 49 +left_num_leds = 42 +-- + +max_radius = 10 -- radius is hardcoded in simapi +proxcars = simdata.proxcars -- also hard coded in simapi + +rightsideset = false +leftsideset = false + +-- right side +litleds = 0 +i = 1 +while(i <= proxcars and rightsideset == false) do + if simdata.pd[i].theta > 40 and simdata.pd[i].theta < 130 then + dist = math.abs(90 - simdata.pd[i].theta) + perct = (90 - dist)/90 + litleds = math.ceil(perct * right_num_leds) + + color_perct = simdata.pd[i].radius/10 + yellow = math.floor(color_perct * 255) + local rgb = (255 << 16) | (yellow << 8) | (0 << 0) + if simdata.pd[1].theta >= 90 then + -- car is behind + set_led_range_to_rgb_color(right_leds_start, right_leds_start + litleds, rgb) + else + set_led_range_to_rgb_color(right_leds_start + (right_num_leds - litleds), right_leds_start + right_num_leds, rgb) + end + rightsideset = true + end + i = i + 1 +end + +-- left side +litleds = 0 +i = 1 +while(i <= proxcars and leftsideset == false) do + if simdata.pd[i].theta > 230 and simdata.pd[i].theta < 320 then + dist = math.abs(270 - simdata.pd[1].theta) + perct = (90 - dist)/90 + litleds = math.ceil(perct * left_num_leds) + + color_perct = simdata.pd[i].radius/10 + yellow = math.floor(color_perct * 255) + local rgb = (255 << 16) | (yellow << 8) | (0 << 0) + if simdata.pd[1].theta <= 270 then + -- car is behind + set_led_range_to_rgb_color(left_leds_start, left_leds_start + litleds, rgb) + else + set_led_range_to_rgb_color(left_leds_start + (left_num_leds - litleds), left_leds_start + left_num_leds, rgb) + end + leftsideset = true + end + i = i + 1 +end + + +-- rpm stuff +if simdata.rpm > 0 and simdata.maxrpm > 0 then + rpmmargin = .05*simdata.maxrpm; + rpminterval = (simdata.maxrpm-rpmmargin) / 6; + + litleds = 0 + for i = 1,6 do + if simdata.rpm >= (rpminterval * i) then + litleds = i; + end + end + + color = GREEN + if litleds > 3 and litleds < 6 then + color = YELLOW + end + if litleds >= 6 then + color = RED + end + if litleds >= 6 then + + if simdata.mtick % 2 == 0 then + set_led_range_to_color(1, litleds, color) + else + led_clear_all() + end + else + set_led_range_to_color(1, litleds, color) + end +end diff --git a/src/monocoque/devices/serial/arduinoledlua.c b/src/monocoque/devices/serial/arduinoledlua.c index 09f5199..76b7b97 100644 --- a/src/monocoque/devices/serial/arduinoledlua.c +++ b/src/monocoque/devices/serial/arduinoledlua.c @@ -3,14 +3,29 @@ #include #include #include +#include #include "arduinoledlua.h" #include "arduino.h" #include "../../slog/slog.h" +long long ledTimeInMilliseconds(void) { + struct timeval tv; + + gettimeofday(&tv,NULL); + return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000); +} + int simdata_to_lua(lua_State *L, SimData* simdata) { + + // make the time up now if we are running in test mode + if(simdata->mtick == 0) + { + simdata->mtick = ledTimeInMilliseconds(); + } + lua_newtable(L); lua_pushinteger(L, simdata->playerflag); @@ -19,6 +34,9 @@ int simdata_to_lua(lua_State *L, SimData* simdata) { lua_pushinteger(L, simdata->rpms); lua_setfield(L, -2, "rpm"); + lua_pushinteger(L, simdata->mtick); + lua_setfield(L, -2, "mtick"); + lua_pushinteger(L, simdata->maxrpm); lua_setfield(L, -2, "maxrpm"); diff --git a/src/monocoque/gameloop/gameloop.c b/src/monocoque/gameloop/gameloop.c index be6b786..8c8a4bb 100644 --- a/src/monocoque/gameloop/gameloop.c +++ b/src/monocoque/gameloop/gameloop.c @@ -878,6 +878,16 @@ int tester(SimDevice* devices, int numdevices) devices[x].update(&devices[x], simdata); } } + for(int x = 0; x < 100; x++) + { + for (int x = 0; x < numdevices; x++) + { + if (devices[x].initialized == true) + { + devices[x].update(&devices[x], simdata); + } + } + } sleep(3); simdata->velocity = 0; diff --git a/src/monocoque/simulatorapi/simapi b/src/monocoque/simulatorapi/simapi index 735c7d3..64f1ed8 160000 --- a/src/monocoque/simulatorapi/simapi +++ b/src/monocoque/simulatorapi/simapi @@ -1 +1 @@ -Subproject commit 735c7d3b889810eabb42c728ae961f7c73b1c947 +Subproject commit 64f1ed8b2672e31556e63eb63ae62f717237b6a6