shiftx & getChannel

Q&A For ShiftX2, RGB sequential shift light <a href="https://wiki.autosportlabs.com/ShiftX2">Installation and Operation Guide</a>
Post Reply
MrBlahh
Posts: 45
Joined: Tue Sep 27, 2016 11:13 pm

shiftx & getChannel

Post by MrBlahh »

Not sure I'm doing this right, trying to use the sample with 3 mapped channels, I verified they are working and showing up in the dashboard but I'm not getting anything out of the shiftx. I did confirm shiftx is working with the disco demo


tickRate = 30
--the CAN baud rate
CAN_baud = 500000
--CAN channel to listen on. 0=first CAN channel, 1=second
CAN_chan = 0

-- What CAN bus ShiftX2 is connected to. 0=CAN1, 1=CAN2
sxCan = 1
-- 0=first ShiftX2 on bus, 1=second ShiftX2 (if ADR1 jumper is cut)
sxId=0
--Brightness, 0-100. 0=automatic brightness
sxBright=0

--add your virtual channels here
--format addChannel(<name>, <sample rate>, <precision>, <min>, <max>, [units])
tempId = addChannel("EngineTempX", 1, 0, 0, 255, "F")
rpmId = addChannel("RPMX", 10, 0, 0, 10000)
fuelId = addChannel("FuelX", 1, 0, 0, 100, "%")

--Convert C to F
function toF(value)
return value * 1.8 + 32
end

function fuelFilter(value)
--adjust for 7 bit value
value = bit.band(value, 0x7F)
--convert liters to %
return value / 0.48
end
--Brightness, 0-100. 0=automatic brightness
sxBright=0

function sxOnUpdate()
--add your code to update ShiftX2 alerts or linear graph during run time.
--Runs continuously based on tickRate.

--uncomment the below for OBDII RPM PID
--sxUpdateLinearGraph(readOBD2(12))

--uncomment the below for Direct RPM from lua
sxUpdateLinearGraph(getChannel("RPMX"))

--update engine temp alert
sxUpdateAlert(0, getChannel("EngineTempX"))

--update Fuel Level
sxUpdateAlert(1, getChannel("FuelX"))
end

function sxOnInit()
--config shift light
sxCfgLinearGraph(0,0,0,7000) --left to right graph, linear style, 0 - 7000 RPM range

sxSetLinearThresh(0,0,500,0,255,0,0) --green at 3000 RPM
sxSetLinearThresh(1,0,5000,255,255,0,0) --yellow at 5000 RPM
sxSetLinearThresh(2,0,6000,255,0,0,10) --red+flash at 6500 RPM

--configure first alert (right LED) as engine temperature (F)
sxSetAlertThresh(0,0,100,255,255,0,0) --yellow warning at 205F
sxSetAlertThresh(0,1,225,255,0,0,10) -- red flash at 225F

--configure second alert (left LED) as fuel (%)
sxSetAlertThresh(1,0,0,255,0,0,10) --red flash below 20 %
sxSetAlertThresh(1,1,25,255,255,0,5) --yellow flash 25-40 PSI
sxSetAlertThresh(1,2,40,0,0,0,0) --above 40, no alert
end

function sxOnBut(b)
--called if the button state changes
println('button: ' ..b)
end

---ShiftX2 functions

function sxSetLed(i,l,r,g,b,f)
sxTx(10,{i,l,r,g,b,f})
end

function sxSetLinearThresh(id,s,th,r,g,b,f)
sxTx(41,{id,s,spl(th),sph(th),r,g,b,f})
end

function sxSetAlertThresh(id,tid,th,r,g,b,f)
sxTx(21,{id,tid,spl(th),sph(th),r,g,b,f})
end

function setBaseConfig(bright)
sxTx(3,{bright})
end

function sxSetAlert(id,r,g,b,f)
sxTx(20,{id,r,g,b,f})
end

function sxUpdateAlert(id,v)
if v~=nil then sxTx(22,{id,spl(v),sph(v)}) end
end

function sxCfgLinearGraph(rs,ls,lr,hr)
sxTx(40,{rs,ls,spl(lr),sph(lr),spl(hr),sph(hr)})
end

function sxUpdateLinearGraph(v)
if v ~= nil then sxTx(42,{spl(v),sph(v)}) end
end

function sxInit()
println('config shiftX2')
setBaseConfig(sxBright)
if sxOnInit~=nil then sxOnInit() end
end

function sxChkCan()
id,ext,data=rxCAN(sxCan,0)
if id==sxCanId then sxInit() end
if id==sxCanId+60 and sxOnBut~=nil then sxOnBut(data[1]) end
end

function sxProcess()
sxChkCan()
if sxOnUpdate~=nil then sxOnUpdate() end
end

function sxTx(offset, data)
txCAN(sxCan, sxCanId + offset, 1, data)
sleep(10)
end

function spl(v) return bit.band(v,0xFF) end
function sph(v) return bit.rshift(bit.band(v,0xFF00),8) end

function onTick()
processCAN(CAN_chan)
sxProcess()
end

sxCanId = 0xE3600 + (256 * sxId)
println('shiftx2 base id ' ..sxCanId)

----------------------------------------
--customize here for CAN channel mapping
--format is:
--[CAN Id] = function(data) map_chan(<chan_id>, <data>, <CAN offset>, <CAN length>, <multiplier>,
-- <adder>, [filter])
----------------------------------------
CAN_map = {

[809] = function(data) map_chan(tempId, data, 1, 1, 0.75, -48, toF) end,
[790] = function(data) map_chan(rpmId, data, 2, 2, 0.15625, 0, rpmFilter) end,
[1555] = function (data) map_chan(fuelId, data, 2, 1, 1, 0, fuelFilter) end
}

--function onTick()
-- processCAN(CAN_chan)
--end
--===========do not edit below===========
function processCAN(chan)
local msg = 0
repeat
local id, e, data = rxCAN(chan, 0)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
msg = msg + 1
until id == nil or msg > 100
end

--Map CAN channel, little endian format
function map_chan(cid, data, offset, len, mult, add, filter)
if offset + len > #data then return end
offset = offset + 1
local value = 0
local shift = 1
while len > 0 do
value = value + (data[offset] * shift)
shift = shift * 256
offset = offset + 1
len = len - 1
end
local cv = value * mult + add
if filter ~= nil then cv = filter(cv) end
setChannel(cid, cv)
end
initCAN(CAN_chan, CAN_baud)
setTickRate(tickRate)

MrBlahh
Posts: 45
Joined: Tue Sep 27, 2016 11:13 pm

Post by MrBlahh »

fixed, was missing an init for the shiftx

for now this is what I'm using, fuel will probably switch to AFR or IAT for meth logging

is it possible to have multiple alerts on the same led? so I can make one led blink red for temp, blink orange for IAT, blink blue for fuel

--This example configured for Mini Cooper CAN
-- Automatically starts logging with engine 'on' (RPM triggered)
--how frequently we poll for CAN messages
tickRate = 30
--the CAN baud rate
CAN_baud = 500000
--CAN channel to listen on. 0=first CAN channel, 1=second
CAN_chan = 0

-- What CAN bus ShiftX2 is connected to. 0=CAN1, 1=CAN2
sxCan = 1
-- 0=first ShiftX2 on bus, 1=second ShiftX2 (if ADR1 jumper is cut)
sxId=0
--Brightness, 0-100. 0=automatic brightness
sxBright=0

--add your virtual channels here
--format addChannel(<name>, <sample rate>, <precision>, <min>, <max>, [units])
tempId = addChannel("EngineTempX", 1, 0, 0, 255, "F")
rpmId = addChannel("RPMX", 10, 0, 0, 10000)
fuelId = addChannel("FuelX", 1, 0, 0, 100, "%")

--Convert C to F
function toF(value)
return value * 1.8 + 32
end

function fuelFilter(value)
--adjust for 7 bit value
value = bit.band(value, 0x7F)
--convert liters to %
return value / 0.48
end
--Brightness, 0-100. 0=automatic brightness
sxBright=0

function sxOnUpdate()
--add your code to update ShiftX2 alerts or linear graph during run time.
--Runs continuously based on tickRate.

--uncomment the below for OBDII RPM PID
--sxUpdateLinearGraph(readOBD2(12))

--uncomment the below for Direct RPM on input 0
sxUpdateLinearGraph(getChannel("RPMX"))

--update engine temp alert
sxUpdateAlert(0, getChannel("EngineTempX"))

--update oil pressure alert
sxUpdateAlert(1, getChannel("FuelX"))
end

function sxOnInit()
--config shift light
sxCfgLinearGraph(0,0,0,7000) --left to right graph, linear style, 0 - 7000 RPM range

sxSetLinearThresh(0,0,4000,0,255,0,0) --green at 4000 RPM
sxSetLinearThresh(1,0,6600,255,255,0,0) --yellow at 6600 RPM
sxSetLinearThresh(2,0,6950,255,0,0,10) --red+flash at 6950 RPM

--configure first alert (right LED) as engine temperature (F)
sxSetAlertThresh(0,0,235,255,255,0,0) --yellow warning at 205F
sxSetAlertThresh(0,1,250,255,0,0,10) -- red flash at 225F

--configure second alert (left LED) as fuel (%)
sxSetAlertThresh(1,0,0,255,0,0,10) --red flash below 20 %
sxSetAlertThresh(1,1,25,255,255,0,5) --yellow flash 25-40 PSI
sxSetAlertThresh(1,2,40,0,0,0,0) --above 40, no alert
end

function sxOnBut(b)
--called if the button state changes
println('button: ' ..b)
end

---ShiftX2 functions

function sxSetLed(i,l,r,g,b,f)
sxTx(10,{i,l,r,g,b,f})
end

function sxSetLinearThresh(id,s,th,r,g,b,f)
sxTx(41,{id,s,spl(th),sph(th),r,g,b,f})
end

function sxSetAlertThresh(id,tid,th,r,g,b,f)
sxTx(21,{id,tid,spl(th),sph(th),r,g,b,f})
end

function setBaseConfig(bright)
sxTx(3,{bright})
end

function sxSetAlert(id,r,g,b,f)
sxTx(20,{id,r,g,b,f})
end

function sxUpdateAlert(id,v)
if v~=nil then sxTx(22,{id,spl(v),sph(v)}) end
end

function sxCfgLinearGraph(rs,ls,lr,hr)
sxTx(40,{rs,ls,spl(lr),sph(lr),spl(hr),sph(hr)})
end

function sxUpdateLinearGraph(v)
if v ~= nil then sxTx(42,{spl(v),sph(v)}) end
end

function sxInit()
println('config shiftX2')
setBaseConfig(sxBright)
if sxOnInit~=nil then sxOnInit() end
end

function sxChkCan()
id,ext,data=rxCAN(sxCan,0)
if id==sxCanId then sxInit() end
if id==sxCanId+60 and sxOnBut~=nil then sxOnBut(data[1]) end
end

function sxProcess()
sxChkCan()
if sxOnUpdate~=nil then sxOnUpdate() end
end

function sxTx(offset, data)
txCAN(sxCan, sxCanId + offset, 1, data)
sleep(10)
end

function spl(v) return bit.band(v,0xFF) end
function sph(v) return bit.rshift(bit.band(v,0xFF00),8) end

function onTick()
processCAN(CAN_chan)
sxProcess()
end

sxCanId = 0xE3600 + (256 * sxId)
println('shiftx2 base id ' ..sxCanId)

----------------------------------------
--customize here for CAN channel mapping
--format is:
--[CAN Id] = function(data) map_chan(<chan_id>, <data>, <CAN offset>, <CAN length>, <multiplier>,
-- <adder>, [filter])
----------------------------------------
CAN_map = {

[809] = function(data) map_chan(tempId, data, 1, 1, 0.75, -48, toF) end,
[790] = function(data) map_chan(rpmId, data, 2, 2, 0.15625, 0, rpmFilter) end,
[1555] = function (data) map_chan(fuelId, data, 2, 1, 1, 0, fuelFilter) end
}

--function onTick()
-- processCAN(CAN_chan)
--end
--===========do not edit below===========
function processCAN(chan)
local msg = 0
repeat
local id, e, data = rxCAN(chan, 0)
if id ~= nil then
local map = CAN_map[id]
if map ~= nil then
map(data)
end
end
msg = msg + 1
until id == nil or msg > 100
end

--Map CAN channel, little endian format
function map_chan(cid, data, offset, len, mult, add, filter)
if offset + len > #data then return end
offset = offset + 1
local value = 0
local shift = 1
while len > 0 do
value = value + (data[offset] * shift)
shift = shift * 256
offset = offset + 1
len = len - 1
end
local cv = value * mult + add
if filter ~= nil then cv = filter(cv) end
setChannel(cid, cv)
end
initCAN(CAN_chan, CAN_baud)
setTickRate(tickRate)
sxInit()

MrBlahh
Posts: 45
Joined: Tue Sep 27, 2016 11:13 pm

Post by MrBlahh »

is there a way to add a delay or sleep to the LUA so it waits a few seconds before it starts to run?

I'm running the shiftx on the same poewr switch as the racecapture, I think the shiftx is booting slower than the racecapture and not getting the init commands, if I power up the shiftx first then power up the mk2 it works fine but if they both power up at the same time the shiftx is not reading the data

boggie1688
Posts: 138
Joined: Fri Apr 07, 2017 3:47 pm
Location: Oakland, CA

Post by boggie1688 »

sleep(ms) command - this will stall the lua script for the specified time

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

Post by brentp »

Hi - glad you got it working!

For multiple alerts on the same channel you'd have to take discrete control of the alert logic and set the alert value directly, rather than use the alert threshold facility, which is designed to facilitate the tracking of a single value.
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

Post Reply