RaceCapturePro Lua Scripting Examples: Difference between revisions

(→‎Receive a CAN message and set a virtual channel: fixed example b/c lua is one based)
Line 144: Line 144:
   id, ext, data = rxCAN(100)
   id, ext, data = rxCAN(100)
   if id ~= nil then
   if id ~= nil then
     setChannel(rpmId, data[0])
     setChannel(rpmId, data[1])
   end
   end
  end
  end

Revision as of 15:33, 29 August 2014

API Reference

Complete API Reference

Automatically Start Logging When Powered On

This script will automatically start logging the moment RaceCapture/Pro turns on

function onTick()
  startLogging()
end

Automatically Start Logging based on GPS Speed

This script will automatically start logging to SD card when speed exceeds 10MPH and stop when it falls below.

function onTick()
  if getGpsSpeed() > 10 then
    startLogging()
  else
    stopLogging()
  end
end

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

Activate a GPIO when start finish line is crossed

This script will pulse one of the GPIO outputs when the start/finish line is detected. First, the onTick rate is set to 10hz, then setGpio() is called with the result of the call to getAtStartFinish()

setTickRate(10)

function onTick()
  if getAtStartFinish() == 1 then
    setGpio(0, 1)
  else
    setGpio(0, 0)
  end
end

or

setTickRate(10)

function onTick()
  setGpio(0, getAtStartFinish())
end

Temperature Warning Indicator Light

This script will activate an output if an analog input exceeds a threshold. It's assumed a temperature sensor is connected to the Analog input channel 0 and is calibrated.

More information: Installation Guide, Sensor Guide, Operation Guide

function onTick()
 if getAnalog(0) > 212 then
   setGpio(0, 1)
 else
   setGpio(0, 0)
 end
end

Enable an LED if fuel level drops below 10%

  • Using PWM/Analog output
    • Fuel sensor is on Analog 0, scaled 0-100%
    • LED indicator connected to PWM 0. When fuel level drops below 10%, Analog/PWM output 0 will show 5v; 0v if fuel level is higher
    • PWM channel settings are set to factory default
function onTick()
 local p = 0
 if getAnalog(0) < 10 then p = 100 end
 setPWMDutyCycle(0,p)
end
  • Using GPIO in output mode
    • When fuel level drops below 10%, the GPIO is activated (output is grounded). Can be used to drive a relay or other device up to 1A load
    • GPIO jumper set to output mode
    • GPIO setting in firmware set to match jumper setting
function onTick()
 local p = 0
 if getAnalog(0) < 10 then p = 1 end
 setGpio(0, p)
end

3 stage Sequential Shift Light

Activates a 3 stage sequential shift light. Also see the Sequential Shift Light project

Given:

  • RPM sensor on timer input 0


setTickRate(15)

function onTick() 
	local r = getTimerRpm(0) 
	if r > 5000 then setGpio(2,1) else setGpio(2,0) end 
	if r > 6000 then setGpio(1,1) else setGpio(1,0) end 
	if r > 7000 then setGpio(0,1) else setGpio(0,0) end
end

Send A CAN message with a temperature value

available in future Firmware version 2.0

Given:

  • Analog 0 reads a calibrated temperature value between 0 and 255
  • Destination CAN device is looking for a message with ID 1234
  • Standard (11 bit) CAN identifer
function onTick()
  t = readAnalog(0)
  msg = {t}
  txCAN(1234, 0, msg)
end

Receive a CAN message and set a virtual channel

Given:

  • creates a channel named "RPM" that logs at 10Hz
    • Note, the channel "RPM" needs to exist in RaceCapture/Pro's known list of channels
  • Sets tick rate to 10Hz
  • Receive a CAN message, with 100ms timeout
  • if data received is valid (by checking the CAN message ID is not nil), then set the virtual channel with the first element in the CAN message data
rpmId = addChannel("RPM", 10)
setTickRate(10)

function onTick()
  id, ext, data = rxCAN(100)
  if id ~= nil then
    setChannel(rpmId, data[1])
  end
end