Last active
April 5, 2018 17:06
-
-
Save brentpicasso/4529401359cbef729905c7b13c84f983 to your computer and use it in GitHub Desktop.
RaceTechnology DL1 serial integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------------------------------------------- | |
--Race Technology Serial Data stream interface for PodiumConnect | |
--Customize your analog and rpm channels in setup() | |
--GPS position, altitude, speed, and accelerometer channels | |
--are built-in | |
--https://www.race-technology.com/wiki/index.php/General/SerialDataFormat | |
----------------------------------------------------------------- | |
--set to true for metric speed and altitude units | |
metric = false | |
--set to true to see debug messages | |
debug = false | |
function setup() | |
--add your RT analog channels here | |
--addAnalog(RT_analog_channel_number, channel_Name, sample_rate, precision, min, max, multiplier, adder) | |
addAnalog(1,'Batt', 10, 2, 0, 5, 'V', 1, 0) | |
--add your RT RPM channels here | |
--addFreq(RT_freq_channel_number, channel_name,sample_rate, precision, min, max, multiplier, divider) | |
addFreq(5, 'RPM', 10, 0, 0, 10000, '', 60, 1) | |
end | |
function onUserTick() | |
--put your tick things in here | |
end | |
------------------- | |
--DO NOT EDIT BELOW | |
------------------- | |
function setupCore() | |
speedId=addChannel('Speed', 10, 2, 0 , 200, metric == true and 'Kph' or 'MPH') | |
altId=addChannel('Altitude', 10, 1, 0, 10000, metric == true and 'M' or 'Ft') | |
accelXId=addChannel('AccelX', 10, 1, -2, 2, 'G') | |
accelYId=addChannel('AccelY', 10, 1, -2, 2, 'G') | |
latId=addChannel('Latitude', 10, 6, -180,180, '') | |
lonId=addChannel('Longitude', 10, 6, -180,180, '') | |
end | |
analogMap={} | |
function addAnalog(RTChannel, name, sr, prec, min, max, units, mult, add) | |
local id=addChannel(name, sr, prec, min, max, units) | |
analogMap[RTChannel]={id, mult, add} | |
end | |
freqMap={} | |
function addFreq(RTChannel, name, sr, prec, min, max, units, mult, div) | |
local id=addChannel(name, sr, prec, min, max, units) | |
freqMap[RTChannel]={id, mult, div} | |
end | |
function extract32(msg, i) | |
return (bit.lshift(msg[i],24) + bit.lshift(msg[i+1],16) + bit.lshift(msg[i+2],8) + msg[i+3]) | |
end | |
function gpsLoc(msg) | |
local lon=extract32(msg,2)*0.0000001 | |
local lat=extract32(msg,6)*0.0000001 | |
setChannel(latId,lat) | |
setChannel(lonId,lon) | |
end | |
function gpsAlt(msg) | |
local alt=extract32(msg,2) * 0.001 | |
alt=alt*(metric==true and 1 or 3.28084) | |
setChannel(altId, alt) | |
end | |
function speed(msg) | |
local speed=extract32(msg,2) * 0.01 | |
speed=speed * (metric==true and 3.6 or 2.23694) | |
setChannel(speedId, speed) | |
end | |
function adc(msg) | |
local volts=(bit.lshift(msg[2],8) + msg[3]) * 0.001 | |
local amap=analogMap[msg[1]-19] | |
if amap ~= nil then | |
local scaled=volts * amap[2] + amap[3] | |
setChannel(amap[1], scaled) | |
end | |
end | |
function freq(msg) | |
local fmap=freqMap[msg[1]-13] | |
if fmap ~= nil then | |
local scaled=1/((bit.lshift(msg[2],16) + bit.lshift(msg[3],8) + msg[4]) * 0.000000166666666667) | |
scaled=(scaled*fmap[2])/fmap[3] | |
setChannel(fmap[1], scaled) | |
end | |
end | |
function accel(msg) | |
local lat=bit.band(msg[2], 0x7F) + msg[3] / 0x100 | |
if bit.band(msg[2], 0x80) ~= 0 then lat = -lat end | |
local lon=bit.band(msg[4], 0x7F) + msg[5] / 0x100 | |
if bit.band(msg[4], 0x80) ~= 0 then lon = -lon end | |
setChannel(accelXId, -lon) | |
setChannel(accelYId, -lat) | |
end | |
function prdebug(msg) | |
if debug == false then return end | |
println(msg) | |
end | |
function printmsg(msg,len) | |
print('RT msg : ') | |
for i = 1,len do | |
print(msg[i] ..' ') | |
end | |
println('') | |
end | |
--RT message map | |
mmap = { | |
[1]={9}, | |
[6]={6}, | |
[8]={6,accel}, | |
[9]={5}, | |
[10]={14,gpsLoc}, | |
[11]={10,speed}, | |
[14]={5,freq}, | |
[15]={5,freq}, | |
[16]={5,freq}, | |
[17]={5,freq}, | |
[18]={5,freq}, | |
[20]={4,adc}, | |
[21]={4,adc}, | |
[22]={4,adc}, | |
[23]={4,adc}, | |
[24]={4,adc}, | |
[25]={4,adc}, | |
[26]={4,adc}, | |
[27]={4,adc}, | |
[56]={10}, | |
[57]={10,gpsAlt}, | |
[63]={3}, | |
} | |
msg = {} | |
function mapRT() | |
local mid=readCSer(6) | |
if mid == nil then return end | |
local map=mmap[mid] | |
if map == nil then | |
prdebug('RT unknown msgid: ' ..mid) | |
return | |
end | |
local func=map[2] | |
local len=map[1] | |
local csum=mid | |
msg[1] = mid | |
for i=2,len-1 do | |
local d=readCSer(6) | |
csum=csum + d | |
msg[i] = d | |
end | |
msg[len]=readCSer(6) | |
csum=bit.band(csum, 0xFF) | |
if msg[len] == csum then | |
if debug == true then printmsg(msg, len) end | |
if func ~= nil then func(msg) end | |
else | |
prdebug('RT Msg failed checksum ' ..mid) | |
end | |
end | |
function onTick() | |
mapRT() | |
onUserTick() | |
end | |
setTickRate(1000) | |
setupCore() | |
setup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment