What is the scaling on Lua Script CAN Tx messages?
setTickRate(10)
function onTick()
local speed = getGpsSpeed()
--format the speed in a CAN message. Speed is in the first byte
local msg = {speed}
txCAN(1, 1917, 0, msg)
end
If I want to transmit GpsSpeed, and this is packed into the first byte, what is the scaling? (I assume the offset is zero....) Scaling of 1, so FF = 255kph?
P.S. I assume in Lua Script the CAN ID is specified in decimal, so 1917 would be 0x77D, correct?
Scaling for CAN Tx messages?
Scaling is 1:1 dec:phys
I did a little testing, and it appears the scaling is 1:1 decimal:physical. The integer value of the signal is what is sent on CAN.
With a little help from this post:
Re-scale/ID CAN data to drive gauge cluster
I came up with this script to test, and confirm the theory.
So message 0x77E is sent with 0 digits of precision, 0 to 255dec is 0 to 255kph physical.
Message 0x77D basically has two digits of precision, 0 to 65535dec is 0 to 255kph physical. If my car was capable of more than 158.36mph then I'd have to chose a different scaling factor.....
With a little help from this post:
Re-scale/ID CAN data to drive gauge cluster
I came up with this script to test, and confirm the theory.
Code: Select all
setTickRate(10) --TickRate is in Hz
function onTick()
local speed = getGpsSpeed()*1.6092*255 --convert to kph and rescale for increased precision
--local speed = 16.6*255
speedL = bit.band(speed, 0xFF) --mask out high byte
--speedL = 3
speedH = bit.rshift(speed, 8) --shift high byte to the right
--speedH = 7
--format the speed in a CAN message. Speed is in the first two bytes
local msg = {speedL,speedH}
txCAN(1, 1917, 0, msg)
local speed11 = getGpsSpeed()*1.6092
--local speed11 = 16
--format the speed in a CAN message. Speed is in the first byte
local msg = {speed11}
txCAN(1, 1918, 0, msg)
end
Message 0x77D basically has two digits of precision, 0 to 65535dec is 0 to 255kph physical. If my car was capable of more than 158.36mph then I'd have to chose a different scaling factor.....
Re: Scaling is 1:1 dec:phys
Or use a 16 bit value for speed, instead of a single byte.GTIspirit wrote: If my car was capable of more than 158.36mph then I'd have to chose a different scaling factor.....
Re: Scaling is 1:1 dec:phys
Yes! That is exactly what I suggested in my example. I'll post the final code to make it easier to follow.ferg wrote:Or use a 16 bit value for speed, instead of a single byte.GTIspirit wrote: If my car was capable of more than 158.36mph then I'd have to chose a different scaling factor.....
Code: Select all
setTickRate(10) --TickRate is in Hz
function onTick()
local speed = getGpsSpeed()*1.6092*255 --convert to kph and rescale for increased precision
speedL = bit.band(speed, 0xFF) --mask out high byte
speedH = bit.rshift(speed, 8) --shift high byte to the right
--format the speed in a CAN message. Speed is in the first two bytes
local msg = {speedL,speedH}
txCAN(1, 1917, 0, msg)
end
So if your car is faster than that replace the 255 above with say 128 which would give 511.99kph = 318.16mph, more than enough for anyone on this forum!