Page 1 of 1

scripts for staged, variable blinking ShiftX light

Posted: Sat Jan 10, 2015 8:49 pm
by GTIspirit
I wanted the ShiftX lights to do something more than just turn on above a certain level. I wanted have them blink slowly once a lower threshold was exceeded, blink faster and faster as the upper threshold is reached, then stay on constant as that upper threshold is exceeded. Then repeat for the next light. In this manner they are staged, variable blinky. 8)

Here is my script to run this on the Digital I/O (GPIO) pins

Code: Select all

--Pin assignments--
--RPM is pin 20 / PulseIn 1
--First light is pin 17 / GPIO1 10Hz Output / #2 on shift light - green
--Second light is pin 16 / GPIO2 10Hz Output / #1 on shift light - yellow
--Third light is pin 15 / GPIO3 10Hz Output / #3 on shift light - red

-- CONSTANTS --
FREQ_HZ = 25  --1000*1/FREQ_HZ=loop time in ms. e.g. 25Hz=40ms--
loop_time = 1000 * 1 / FREQ_HZ
MAX_BLINK_PERIOD = 1000 --in ms (1s or 1000ms is reasonable value)--
MAX_ON_TIME = 500 --in ms (0.5s or 500ms, half off/on is reasonable value--
testRPM = 3000  --test interface for use on bench--
LL1 = 1000 --light 1 lower threshold to start blinking--
UL1 = 1500 --light 1 upper threshold above which goes solid--
LL2 = 1500 --light 2 lower threshold to start blinking--
UL2 = 2000 --light 2 upper threshold above which goes solid--
LL3 = 2000 --light 3 lower threshold to start blinking--
UL3 = 2500 --light 3 upper threshold above which goes solid--
-- FUNCTIONS --
function ctON(ul,ll)
	--RPM=getTimerRpm(0)
	RPM=testRPM
	factor = 0.5 * FREQ_HZ * (1 - (ul - RPM) / (ul - ll))
	if factor >= 0.5 * FREQ_HZ then --can't blink faster than tickRate/2--
	    	on_time = MAX_ON_TIME
	    elseif factor < 1 then --can't blink slower than max period--
	    	on_time = 0
	    else
	    	on_time = MAX_ON_TIME / factor
	end	
	return on_time / loop_time
end

function ctMAX&#40;ul,ll&#41;
	--RPM=getTimerRpm&#40;0&#41;
	RPM=testRPM
	factor = 0.5 * FREQ_HZ * &#40;1 - &#40;ul - RPM&#41; / &#40;ul - ll&#41;&#41;
	blink_period = MAX_BLINK_PERIOD / factor	
	return blink_period / loop_time
end

-- Ini --
setTickRate&#40;FREQ_HZ&#41;
j = 0
k = 0
l = 0
-- RUN --
function onTick&#40;&#41;
--start light 1 section--
	if j < ctON&#40;UL1,LL1&#41; then
            	setGpio&#40;0,0&#41;
        else
            	setGpio&#40;0,1&#41;
	end        
	if j < ctMAX&#40;UL1,LL1&#41; then
		j = j + 1
	else
		j = 0
	end
--end light 1 section--
--start light 2 section--
	if k < ctON&#40;UL2,LL2&#41; then
            	setGpio&#40;1,0&#41;
        else
            	setGpio&#40;1,1&#41;
	end        
	if k < ctMAX&#40;UL2,LL2&#41; then
		k = k + 1
	else
		k = 0
	end
--end light 2 section--
--start light 3 section--
	if l < ctON&#40;UL3,LL3&#41; then
            	setGpio&#40;2,0&#41;
        else
            	setGpio&#40;2,1&#41;
	end        
	if l < ctMAX&#40;UL3,LL3&#41; then
		l = l + 1
	else
		l = 0
	end
--end light 3 section--
end
And here is my script to run this on the Analog/Pulse Output (Vout)

Code: Select all

--Pin assignments--
--RPM is pin 20 / PulseIn 1
--set the jumpers to ON
--First light is pin 6 / Vout1 10Hz 100 100 0.1 / #2 on shift light - green
--Second light is pin 5 / Vout2 10Hz 100 100 0.1 / #1 on shift light - yellow
--Third light is pin 4 / Vout3 10Hz 100 100 0.1 / #3 on shift light - red

-- CONSTANTS --
FREQ_HZ = 25  --1000*1/FREQ_HZ=loop time in ms. e.g. 25Hz=40ms--
loop_time = 1000 * 1 / FREQ_HZ
MAX_BLINK_PERIOD = 1000 --in ms &#40;1s or 1000ms is reasonable value&#41;--
MAX_ON_TIME = 500 --in ms &#40;0.5s or 500ms, half off/on is reasonable value--
testRPM = 3000  --test interface for use on bench--
LL1 = 1000 --light 1 lower threshold to start blinking--
UL1 = 1500 --light 1 upper threshold above which goes solid--
LL2 = 1500 --light 2 lower threshold to start blinking--
UL2 = 2000 --light 2 upper threshold above which goes solid--
LL3 = 2000 --light 3 lower threshold to start blinking--
UL3 = 2500 --light 3 upper threshold above which goes solid--
-- FUNCTIONS --
function ctON&#40;ul,ll&#41;
	--RPM=getTimerRpm&#40;0&#41;
	RPM=testRPM
	factor = 0.5 * FREQ_HZ * &#40;1 - &#40;ul - RPM&#41; / &#40;ul - ll&#41;&#41;
	if factor >= 0.5 * FREQ_HZ then --can't blink faster than tickRate/2--
	    	on_time = MAX_ON_TIME
	    elseif factor < 1 then --can't blink slower than max period--
	    	on_time = 0
	    else
	    	on_time = MAX_ON_TIME / factor
	end	
	return on_time / loop_time
end

function ctMAX&#40;ul,ll&#41;
	--RPM=getTimerRpm&#40;0&#41;
	RPM=testRPM
	factor = 0.5 * FREQ_HZ * &#40;1 - &#40;ul - RPM&#41; / &#40;ul - ll&#41;&#41;
	blink_period = MAX_BLINK_PERIOD / factor	
	return blink_period / loop_time
end

-- Ini --
setTickRate&#40;FREQ_HZ&#41;
j = 0
k = 0
l = 0
-- RUN --
function onTick&#40;&#41;
--start light 1 section--
	if j < ctON&#40;UL1,LL1&#41; then
            	setAnalogOut&#40;0,0&#41;
        else
            	setAnalogOut&#40;0,10&#41; --with value of 5 measured 2.2V and dim light so set to 10, measured 4.75V
	end        
	if j < ctMAX&#40;UL1,LL1&#41; then
		j = j + 1
	else
		j = 0
	end
--end light 1 section--
--start light 2 section--
	if k < ctON&#40;UL2,LL2&#41; then
            	setAnalogOut&#40;1,0&#41;
        else
            	setAnalogOut&#40;1,10&#41; --with value of 5 measured 2.2V and dim light so set to 10, measured 4.75V
	end        
	if k < ctMAX&#40;UL2,LL2&#41; then
		k = k + 1
	else
		k = 0
	end
--end light 2 section--
--start light 3 section--
	if l < ctON&#40;UL3,LL3&#41; then
            	setAnalogOut&#40;2,0&#41;
        else
            	setAnalogOut&#40;2,10&#41; --with value of 5 measured 2.2V and dim light so set to 10, measured 4.75V
	end        
	if l < ctMAX&#40;UL3,LL3&#41; then
		l = l + 1
	else
		l = 0
	end
--end light 3 section--
end
Disclaimer I have only tested this on the bench by giving fixed RPM values through a calibration interface. To run in vehicle should only require commenting out the fixed RPM value and activating the getTimerRPM value.

Now I'm not a software engineer, calibration is my thing, but I know enough to dabble and be dangerous :lol: So improvement suggestions are welcome because I'm sure I didn't follow the best coding practices..... :oops:

Oh, and this is running with RaceAnalyzer V1.1.15 on MK1 hardware running firmware 1.2.7. (A little hesitant to jump on the V2 release since it is so new and everything works now as I need it.)

Posted: Sun Jan 11, 2015 9:07 pm
by GTIspirit
Evidence of the AnaOut script in action!

Yes, it was as easy as commenting out the fixed RPM test interface and activating the measured RPM values. May require a little tweaking, as the slowest blink rate, MAX_BLINK_PERIOD = 1000, seems faster in the car than on my sofa. May need to increase the max period to slow it down just a little.

Oops, can't upload .mov files. Best I can do is make a link to where I uploaded it to my Google+ site.

https://plus.google.com/u/0/10689601023 ... tvhS9iU67f

Posted: Mon Jan 12, 2015 6:01 am
by rdoherty
Wow, this is amazing! Thank you for sharing!

Posted: Mon Jan 12, 2015 6:04 am
by brentp
Your script is amazing! We will be sharing this video on facebook, unless you have a more extended video?

You should definitely try the latest firmware and app, you can always switch back, you know. :)

Posted: Mon Jan 12, 2015 5:33 pm
by brentp

Posted: Thu Jan 15, 2015 2:21 am
by GTIspirit
Cool 8) I deliberately made a short video, mainly so I could text it to someone, but also because sometimes less is more to get people's attention!

After I refine the timing for use in vehicle I can post another video more clearly showing how it starts blinking slowly once the lower threshold is crossed and keeps blinking faster and faster and faster until it goes solid at the upper threshold.

Posted: Fri Apr 24, 2015 7:52 pm
by gizmodo
This script is really cool, thanks for posting it! I am using it on GPIO and had to flip the values passed to setGpio. The lights were off when they should have been on, and on when they should have been off. Other than that it works great.

Posted: Wed Jul 29, 2015 2:49 am
by Ls_Rx7
Hopefully someone can show me what I am overlooking. I recently installed my shift x light and loaded this script into my MK1 but am having an issue. As soon as I turn the key and my MK1 gets power all of the lights on the shift light turn on and are solid.

I opened up the unit and changed the dip switch to ON for 1-3.
I turned on the Analog outputs ( set at 10HZ, Analog, Voltage)
Changed --RPM=getTimerRpm(0) to --RPM=getTimerRpm(1) since it is input 1. (Should this stay (0)?)

Posted: Wed Jul 29, 2015 1:15 pm
by GTIspirit
Ls_Rx7 wrote:Hopefully someone can show me what I am overlooking. I recently installed my shift x light and loaded this script into my MK1 but am having an issue. As soon as I turn the key and my MK1 gets power all of the lights on the shift light turn on and are solid.

Changed --RPM=getTimerRpm(0) to --RPM=getTimerRpm(1) since it is input 1. (Should this stay (0)?)
Depends on which pin you are using. Please see the comments in the script, the script is configured to use pin 20 for the RPM input and wouldn't require any changes if you were using that same input pin for your RPM signal.

Posted: Thu Jul 30, 2015 12:29 am
by Ls_Rx7
GTIspirit wrote:
Ls_Rx7 wrote:Hopefully someone can show me what I am overlooking. I recently installed my shift x light and loaded this script into my MK1 but am having an issue. As soon as I turn the key and my MK1 gets power all of the lights on the shift light turn on and are solid.

Changed --RPM=getTimerRpm(0) to --RPM=getTimerRpm(1) since it is input 1. (Should this stay (0)?)
Depends on which pin you are using. Please see the comments in the script, the script is configured to use pin 20 for the RPM input and wouldn't require any changes if you were using that same input pin for your RPM signal.
I changed the script back to what you originally had and verified I was using pin 20 for RPM. My shift light works but the lights are reversed. They are always on and turn off at the RPMs commanded. I am new to coding, what do I need to do to switch the lights so that they are normally off and only come on when the commanded RPMs are met?

EDIT: I think the script is fine. The issue is when all 4 wires of my shift light are connected the shift light turns on all lights even without a script loaded.

Posted: Sun Aug 02, 2015 2:13 am
by GTIspirit
Ls_Rx7 wrote: ...My shift light works but the lights are reversed. They are always on and turn off at the RPMs commanded. I am new to coding, what do I need to do to switch the lights so that they are normally off and only come on when the commanded RPMs are met?

EDIT: I think the script is fine. The issue is when all 4 wires of my shift light are connected the shift light turns on all lights even without a script loaded.
Hmm, here's my config:

Sounds like your settings are somehow inverted. I tried disconnecting the 5V supply and that didn't replicate the behavior, so I'm not sure what it could be.

@gizmodo, I remember having that problem too when using the GPIO channels, that the behavior was initially backwards from what I expected, so I had to reverse the one and off settings. Same principle would apply to the analog out, except that wouldn't affect the behavior when no script was even running.....

I believe the way the ShiftX works is the 5V supply from the RCPro unit to the V pad on the ShiftX provides power for all three sets of lights. If the signal for say the green was high, and high should be ~5V, there would be no potential difference and the light would be off. If the signal for green was pulled low, to near zero, there would be maximum potential difference and the light would be on brightest. Hence my comment in the script file regarding the voltage setting which at first glance wouldn't make sense.....

Posted: Sun Aug 02, 2015 2:29 am
by GTIspirit
Oh, and here's why I ended up using the Analog outputs, they make the lights brighter for whatever reason.
Why is ShiftX brighter on AnalogOut than GPIO?

Posted: Sun Sep 13, 2015 9:19 pm
by roostinds
How would I apply this if I am getting my RPM from can bus through my AEM Infinity. I would prefer to use the analog out as we race during the day so Higher light output is best.. Thanks.

Posted: Thu Sep 17, 2015 4:11 pm
by GTIspirit
roostinds wrote:How would I apply this if I am getting my RPM from can bus through my AEM Infinity. I would prefer to use the analog out as we race during the day so Higher light output is best.. Thanks.
Hmmm, no CAN bus on a '87 GTI, so no first hand experience.

In theory, all instances of "RPM=getTimerRpm(0)" would be replaced by RPM=whatever corresponds to CAN RPM signal.

FYI, I just realized the original scripts may have mislead people who simply wanted to copy and paste without looking into the LUA script commands.

The two -- comment out that command, and leave active the test interface for bench testing on the comfort of your living room sofa. :lol:

Code: Select all

   --RPM=getTimerRpm&#40;0&#41;
   RPM=testRPM 
So to run the script in vehicle, the REM characters need to be moved as follows:

Code: Select all

   RPM=getTimerRpm&#40;0&#41;
   --RPM=testRPM 

Posted: Thu Sep 17, 2015 10:45 pm
by roostinds
Sorry, i am such a noob at Lua scripting. currently this is my can bus information for the RPM

CAN_map = {
[32546816] = function(data)
map_chan(rpmId, data, 0, 2, 0.39063, 0)

How would i translate this?

RPM = [32546816] rpmId??

Sorry, still trying to wrap my head around Lua