RegisterSearchFAQMemberlistUsergroupsLog in
Reply to topic Page 1 of 1
Improved script for staged, variable blinking ShiftX light
Author Message
Reply with quote
Post Improved script for staged, variable blinking ShiftX light 
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. Sad
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:

--virtual channels
SpeedGPS = addChannel("SpeedGPS", 25, 2, 0, 200, "KPH")
SpeedWhl = addChannel("SpeedWhl", 25, 2, 0, 200, "KPH")

-- 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 (1000ms to 1500ms suggested)--
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(ll,ul,rpm)
   factor = (ul - rpm) / (ul - ll)
   blink_period = factor * MAX_BLINK_PERIOD
   return blink_period / loop_time
end
function variblink(ll,ul,rpm,chan)
   if j <= DUTY_CYCLE * ctMAX(ll,ul,rpm) then
      setAnalogOut(chan,10)
   else
      setAnalogOut(chan,0)
   end
   if j < ctMAX(ll,ul,rpm) then
      j = j + 1
   else
      j = 0
   end
end
function setLight(ll,ul,rpm,chan)
   if rpm < ll then
        setAnalogOut(chan,10)
    elseif rpm >= ul then
        setAnalogOut(chan,0)
   else
   variblink(ll,ul,rpm,chan)   
   end
end   
-- Ini --
setTickRate(FREQ_HZ)
j = 0
-- RUN --
function onTick()
setChannel(SpeedGPS, getGpsSpeed()*1.61)
setChannel(SpeedWhl, getTimerFreq(1)*0.92)
RPM=getTimerRpm(0)
--RPM=testRPM

setLight(LL1,L12,RPM,0)
setLight(L12,L23,RPM,1)
setLight(L23,UL3,RPM,2)
end


Software programming isn't my thing, I don't know if any further improvements are possible. Any suggestions would be much appreciated!

View user's profile Send private message
Reply with quote
Post  
This is great, thanks for the improvement!

Got a video of it in action?


_________________
Brent Picasso
Founder, Autosport Labs
Facebook | Twitter
View user's profile Send private message Send e-mail
Reply with quote
Post  
Yes! Laughing

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

Though sometimes the lights get a bit obscured Wink

View user's profile Send private message
Reply with quote
Post  
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
Founder, Autosport Labs
Facebook | Twitter
View user's profile Send private message Send e-mail
Reply with quote
Post  
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:
https://forum.autosportlabs.com/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.

View user's profile Send private message
Reply with quote
Post  
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

View user's profile Send private message
Reply with quote
Post  
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.

View user's profile Send private message
Reply with quote
Post  
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:

setTickRate(15)

function onTick()
local r = getTimerRpm(0)
println('rpm = ' ..r)
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


Let us know what you find out!


_________________
Brent Picasso
Founder, Autosport Labs
Facebook | Twitter
View user's profile Send private message Send e-mail
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum