RaceCapturePro Lua Scripting Examples: Difference between revisions

(added virtual channel example)
Line 73: Line 73:
setTickRate(10)
setTickRate(10)


--create channel "SensorAvg"
--Create channel: "SensorAvg"
--sample rate = 10Hz
--Sample rate: 10Hz
--precision = 2 decimal points
--Logging precision: 2 decimal points
--min/max = 0/5
--min/max: 0/5
--Units = "Volts"
--Units: "Volts"
avgId = addChannel("SensorAvg", 10, 2, 0, 5, "Volts")
avgId = addChannel("SensorAvg", 10, 2, 0, 5, "Volts")


Line 86: Line 86:
end
end
</pre>
</pre>


=Alerts/Notifications=
=Alerts/Notifications=

Revision as of 18:43, 16 April 2015

API Reference

Complete API Reference

Automatic logging control

Automatically Start Logging When Powered On

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

  • Note, this will create a file with an incorrect timestamp if GPS lock is not established.
    • To get a properly time-stamped file, use the GPS speed triggered approach.
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 when engine is running

This script will automatically start logging to SD card when battery voltage shows that the engine is running

  • When battery voltage is greater than 13 volts, we assume engine is running.
  • System battery voltage is tied to the last Analog channel (7)
function onTick()
  if getAnalog(7) > 13 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)
 overRevId = addChannel("OverRev", 10)


 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

Virtual Channels (AKA Math Channels)

Average two sensor values and set to a virtual channel

--10Hz update
setTickRate(10)

--Create channel: "SensorAvg"
--Sample rate: 10Hz
--Logging precision: 2 decimal points
--min/max: 0/5
--Units: "Volts"
avgId = addChannel("SensorAvg", 10, 2, 0, 5, "Volts")

function onTick()
  local a1 = getAnalog(0)
  local a2 = getAnalog(1)
  setChannel(avgId, (a1 + a2) / 2)
end

Alerts/Notifications

Create an over-rev marker

This script will mark the datalog with an over-rev alert marker whenever RPMs are above a certain threshold. Given:

  • RPM is connected to the first Timer input channel
  • RPM over-rev condition is 8000
overRevId = addChannel("OverRev", 10)
setTickRate(10)

function onTick()
  local rpm = getTimerRpm(0)
  local overRev = 0
  if rpm > 8000 then overRev = 1 end
  setChannel(overRevId, overRev)
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

CAN bus scripts

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
  • CAN1 channel is used
function onTick()
  t = readAnalog(0)
  msg = {t}
  txCAN(0, 1234, 0, msg)
end


Use RCP as a general purpose CAN data logger

The following script will output any received CAN bus message to the log window.

  • You can access the log window by enabling polling in the script window under the RaceCapture app configuration view
  • You can also access the log window by connecting to RaceCapture/Pro from a terminal program (hyperterminal, Minicom, etc) and issuing the command: viewLog
setTickRate(30) --30Hz

function onTick()
    repeat --will drain CAN buffer on each tick
        id, e, data = rxCAN(0)
	if id ~= nil then
		print(id ..':')
		for i=1,#data do
			print(data[i] ..' ')
		end
		println('')
	end
    until id == nil
end

Receive a CAN message and set a virtual channel

Given:

  • creates a channel named "MyChannel" that logs at 10Hz
  • Sets tick rate to 10Hz
  • Receive a CAN message on CAN1 channel, 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
channelId = addChannel("MyChannel", 10)
setTickRate(10)

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

Note: To map real CAN bus data, consult your CAN bus protocol documentation for correctly mapping CAN bus data to virtual channels.

Gear Calculation

Calculate gear position based on drive train ratios

--Developed by Luther Lloyd III 8/22/14 for use by Autosport Labs Community
--Copy this text into the scripting window of Race Analyzer
--Edit the gear ratios to mathc your car
--Add another gear row if needed for 6th gear or delete extraneous ones



--below values are constants for the vehicle
local 1stGear = 3.5
local 2ndGear = 2.7
local 3rdGear = 2.0
local 4thGear = 1.4
local 5thGear = 0.9
local FinalDrive = 3.21
--diameter in inches
local TireDia = 25.0  
--allowable error of gear ratio to allow for measurement variation
local gearErr = 0.1
local rpmSpeedRatio = 0
--initialized to 0 so if it doesn't work you know
local gearPos = 0 --this is the gear channel variable

function onTick() --updates gear position every second by default

--assumes Pulse Input channel one is for the RPM signal and speed in MPH
local speed = getGpsSpeed()
local rpm = getTimerRpm(0)

--this part only works for firmware version 2.0 per the RCP page
gearId = addChannel("Gear",5)

if speed > 10 then
    --makes sure your rolling so as not to divide by 0 
    
   rpmSpeedRatio = (rpm/speed)/(FinalDrive*1056/(TireDia*3.14159))

    if ((1stGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 1 end
    if ((2ndGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 2 end
    if ((3rdGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 3 end
    if ((4thGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 4 end
    if ((5thGear - rpmSpeedRatio)^2) < (gearErr^2) then gearPos = 5 end

else gearPos = 0 end

setChannel(gearId, gearPos) --outputs to virtual channel

end