Improved script for staged, variable blinking ShiftX light

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
GTIspirit
Posts: 249
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

Improved script for staged, variable blinking ShiftX light

Post by GTIspirit »

Previous scripts for staged, variable blinking ShiftX light was rather bloated and inefficient, running calculations and the timer when not necessary.

I couldn't add any virtual channels, and had to skinny down some comments to get just one virtual channel to fit. :(
Lua out of memory with two new lines? (Mk1)

But really I needed to add at least another virtual channel, so measured wheel speed (corrected for correlation to GPS speed) and GPS speed were both in kph.

I modified the script to only run the factor calculation when inside the limits, and otherwise just manually set the lights on or off when outside the limits. These improvements let me add that second virtual channel. So this is how it looks now, after a few rounds of improvements:

Code: Select all

--virtual channels
SpeedGPS = addChannel&#40;"SpeedGPS", 25, 2, 0, 200, "KPH"&#41;
SpeedWhl = addChannel&#40;"SpeedWhl", 25, 2, 0, 200, "KPH"&#41;

-- CONSTANTS --
FREQ_HZ = 25  --1000/FREQ_HZ=loop time in ms. e.g. 25Hz=40ms--
loop_time = 1000 / FREQ_HZ
MAX_BLINK_PERIOD = 1200 --in ms &#40;1000ms to 1500ms suggested&#41;--
DUTY_CYCLE = 0.5
--testRPM = 5200  --test interface--
LL1 = 5000 --lower limit to start blinking light 1--
L12 = 6000 --sets light 1 on, starts blinking light 2--
L23 = 6500 --sets light 2 on, starts blinking light 3--
UL3 = 7000 --light 3 upper limit above which goes solid--
-- FUNCTIONS --
function ctMAX&#40;ll,ul,rpm&#41;
	factor = &#40;ul - rpm&#41; / &#40;ul - ll&#41;
	blink_period = factor * MAX_BLINK_PERIOD
	return blink_period / loop_time
end
function variblink&#40;ll,ul,rpm,chan&#41;
	if j <= DUTY_CYCLE * ctMAX&#40;ll,ul,rpm&#41; then
		setAnalogOut&#40;chan,10&#41;
	else
		setAnalogOut&#40;chan,0&#41;
	end
	if j < ctMAX&#40;ll,ul,rpm&#41; then
		j = j + 1
	else
		j = 0
	end
end
function setLight&#40;ll,ul,rpm,chan&#41;
	if rpm < ll then
        setAnalogOut&#40;chan,10&#41;
    elseif rpm >= ul then
        setAnalogOut&#40;chan,0&#41; 
	else
	variblink&#40;ll,ul,rpm,chan&#41;	
	end
end	
-- Ini --
setTickRate&#40;FREQ_HZ&#41;
j = 0
-- RUN --
function onTick&#40;&#41;
setChannel&#40;SpeedGPS, getGpsSpeed&#40;&#41;*1.61&#41;
setChannel&#40;SpeedWhl, getTimerFreq&#40;1&#41;*0.92&#41;
RPM=getTimerRpm&#40;0&#41;
--RPM=testRPM

setLight&#40;LL1,L12,RPM,0&#41;
setLight&#40;L12,L23,RPM,1&#41;
setLight&#40;L23,UL3,RPM,2&#41;
end
Software programming isn't my thing, I don't know if any further improvements are possible. Any suggestions would be much appreciated!

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

Post by brentp »

This is great, thanks for the improvement!

Got a video of it in action?
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

GTIspirit
Posts: 249
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

Post by GTIspirit »

Yes! :lol:

https://www.youtube.com/watch?v=v9rRv96J8AM

Though sometimes the lights get a bit obscured :wink:

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

Post by brentp »

Nice video! mind if we share it, or do you have another one where you do a static test and talk through what you did?
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

GTIspirit
Posts: 249
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

Post by GTIspirit »

Thanks for asking, sure, feel free to share the action video. I think you already shared the video at the below link.

There was this short video from the bench testing, which would still be valid as the concept is the same:
viewtopic.php?p=22010#22010

Difference between bench and reality is that depending on engine power, under load the engine should sweep through the rpm range slower, so the varying frequency should be more visible while driving.

Placement of the ShiftX on the dashboard has two purposes for me:
1. driver notification
2. video recording

I find it helpful to capture the lights in the video because it gives some visual feedback on engine speed while watching the course video. In theory, the varying blink rate should give further indication if engine speed is at the lower, or upper end of the range. In reality, the pulse period may need to be adjusted a bit more to optimize for this usage.

Shiftlink
Posts: 29
Joined: Tue Aug 13, 2013 1:36 am
Location: EM96xa

Post by Shiftlink »

I would like to do what you are doing here, I just want to make sure I understand a couple of things.
Your virtual channels are not being used for controlling ShiftX, correct? This is just something you've added because you want it, I think.

Are you using the three Digital I/O's or the Pulse/Analog outs?

I have set up the basic lights from the ShiftX page and I have an issue where they are staying dimly lit when they should be off. I just copied this Lua from the website:
"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"

The other thing is that I can't copy/paste into the script box so I have to type it all out.

Here's a video of what I currently have:
https://www.youtube.com/watch?v=GTG83QUF-aE

Shiftlink
Posts: 29
Joined: Tue Aug 13, 2013 1:36 am
Location: EM96xa

Post by Shiftlink »

I guess I should mention that I'm working with an RCP2 for this.

No jumpers to swap, When I copy in your script without the virtual channels and some of the comments I hook the lights up, they just stay on all the time, I guess I need to figure out the settings in set-up on those channels.

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

Post by brentp »

Hi, you should be able to copy and paste by using the standard ctrl-a (select all) / ctrl-c (copy) / ctrl-v (paste)

The other thing is you'll need to ensure the GPIOs in the MK2 configuration are set to 'output'.

you can try printing the output of the 'r' variable to see if it's varying with RPM, and check the box 'poll log' under the scripting window.

e.g.

Code: Select all

setTickRate&#40;15&#41; 

function onTick&#40;&#41; 
local r = getTimerRpm&#40;0&#41; 
println&#40;'rpm = ' ..r&#41;
if r > 5000 then setGpio&#40;2,1&#41; else setGpio&#40;2,0&#41; end 
if r > 6000 then setGpio&#40;1,1&#41; else setGpio&#40;1,0&#41; end 
if r" > 7000 then setGpio&#40;0,1&#41; else setGpio&#40;0,0&#41; end 
end
Let us know what you find out!
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

Post Reply