How to blink a light on the GPIO?

Discussion on the Lua Scripting capabilities for RaceCapture/Pro. Also see the <a href="http://autosportlabs.net/RaceCapturePro_Lua_Scripting">Lua Scripting Guide</a>

Moderators: JeffC, stieg

Post Reply
jakekooser
Posts: 64
Joined: Wed Apr 16, 2014 11:03 am
Location: Florida
Contact:

How to blink a light on the GPIO?

Post by jakekooser »

I have a light on my dash that I want to use to indicate when my RCP is armed, like for the autocross script. I would like to have it blink to indicate that the RCP is ready and then after the script gives the 'startLogging()' command, have it turn on solid until logging stops.

Right now it is connected to one of the GPIO ports, with the jumper and software set to output, and it just stays on constantly.

I still don't have the script in the box, nor the 'arm' switch hooked up, but will do that tomorrow.

Can someone help me get pointed in the right direction?

brentp
Site Admin
Posts: 6274
Joined: Wed Jan 24, 2007 6:36 am

Post by brentp »

Hi Jake,

We can help with that. Can you expand on your requirements? What does it mean to be 'armed' - does it mean if the unit is simply powered up?

Generally to toggle the output (flash the LED) it would involve alternating the output state of the unit on every tick. like this:

Code: Select all

setTickRate&#40;10&#41; -- 10 Hz 
ledState = 0
function onTick&#40;&#41;
  setGpio&#40;0, ledState&#41;
  if ledState == 0 then ledState = 1 else ledState = 0 end
end

Make sure in your GPIO config you have the GPIO channel mode (in this case, the first one) set to "output"
http://autosportlabs.net/RaceCapturePro ... _state_.29
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

jakekooser
Posts: 64
Joined: Wed Apr 16, 2014 11:03 am
Location: Florida
Contact:

Post by jakekooser »

Brent,

when I write 'armed' I was referring to the 'AutoX' script in the examples:
Automatically start Logging upon Launch (AutoX / Rally / Hill Climb)

This script will start Logging when a dash mounted "ARM" switch is activated via an input and G-force exceeds a threshold Given:
GPIO 0 configured as input and connected to dash mounted "Arm" switch
Default RaceCapture/Pro mounting orientation (terminal block facing forward, mounted upright)
G-force launch threshold is -0.1 G
flipping the ARM switch to 'Off' will stop logging
setTickRate(30)

function onTick()
local arm = getGpio(0)
local g = getAccel(1)
if arm == 0 then
stopLogging()
end
if arm == 1 and g < 0.1 then
startLogging()
end
end
I have a dpdt switch that I want to use to select either 'autocross' or 'normal' modes for autoX and track, respectively. I posted a separate question about how to do that.

I would like to select, for example, the AutoX mode, have the RCP start logging upon launch as that script does, and to have it blink the LED on the dash once logging is started. To my non-programmer mind, it seems as simple as maybe calling a function, just like one does to start logging, but the LUA scripting is a bit confusing to me right now.

How would one handle this?

brentp
Site Admin
Posts: 6274
Joined: Wed Jan 24, 2007 6:36 am

Post by brentp »

Here's an improved example of the launch script that flashes an external LED when logging. Note, I wrote this blind (without direct testing), but should work. :)

Use the logging window to check for problems once you upload and run the script.

Code: Select all

--GPIO 0 is configured for input &#40;connects to switch&#41;
--GPIO 1 is configured for output &#40;connects to LED&#41;
--IMU channel 1 mapped to Fore/aft accelerometer channel

gForceThreshold = 0.25 -- launch threshold in G force
imuChannel = 1  --IMU channel 1 mapped to Fore/aft accelerometer channel

setTickRate&#40;30&#41; --onTick&#40;&#41; is called 30 times/sec

ledState = 0
function toggleLed&#40;&#41;
  setGpio&#40;1, ledState&#41;
  if ledState == 0 then ledState = 1 else ledState = 0 end
end

function onTick&#40;&#41; 
  local arm = getGpio&#40;0&#41; 
  local g = getAccel&#40;imuChannel&#41;
  if arm == 0 then 
    stopLogging&#40;&#41;
  end 
  if arm == 1 and g > gForceThreshold then 
    startLogging&#40;&#41; 
    toggleLed&#40;&#41;
  end 
end

Post Reply