  Scaling for CAN Tx messages?
Author Message Scaling for CAN Tx messages?
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 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.

###### Code:
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

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.....  Re: Scaling is 1:1 dec:phys
###### GTIspirit wrote:
If my car was capable of more than 158.36mph then I'd have to chose a different scaling factor.....

Or use a 16 bit value for speed, instead of a single byte.

_________________
David Ferguson
Veracity Racing Data
Autosport Labs Preferred Dealer
https://veracitydata.com  Re: Scaling is 1:1 dec:phys
###### GTIspirit wrote:
If my car was capable of more than 158.36mph then I'd have to chose a different scaling factor.....

Or use a 16 bit value for speed, instead of a single byte.

Yes! That is exactly what I suggested in my example. I'll post the final code to make it easier to follow.
###### Code:
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 65535 divided by 255 gives 257kph, which is 159.71mph.
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! Looks like you're figuring it out!

We have a GPS / IMU broadcast script here that could help:
https://gist.github.com/brentpicasso/0ccfb49690e8de6f98c216e80c86694c

_________________
Brent Picasso
CEO and Founder, Autosport Labs 