Page 1 of 1

Scaling for CAN Tx messages?

Posted: Sat Sep 28, 2019 4:24 pm
by GTIspirit
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

Posted: Sat Oct 05, 2019 10:31 pm
by GTIspirit
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: 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
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

Posted: Sun Oct 06, 2019 5:34 pm
by ferg
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.

Re: Scaling is 1:1 dec:phys

Posted: Mon Oct 07, 2019 9:59 pm
by GTIspirit
ferg wrote:
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: 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 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!

Posted: Fri Dec 06, 2019 6:36 pm
by brentp
Looks like you're figuring it out!

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