## Scaling for CAN Tx messages?

Discussion on the Lua Scripting capabilities for RaceCapture/Pro. Also see the <a href="http://autosportlabs.net/RaceCapturePro_Lua_Scripting">Lua Scripting Guide</a>

Moderators: JeffC, stieg

GTIspirit
Posts: 233
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

### 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?

GTIspirit
Posts: 233
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

### 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: Select all

``````setTickRate&#40;10&#41; --TickRate is in Hz
function onTick&#40;&#41;
local speed = getGpsSpeed&#40;&#41;*1.6092*255  --convert to kph and rescale for increased precision
--local speed = 16.6*255
speedL = bit.band&#40;speed, 0xFF&#41; --mask out high byte
--speedL = 3
speedH = bit.rshift&#40;speed, 8&#41; --shift high byte to the right
--speedH = 7
--format the speed in a CAN message. Speed is in the first two bytes
local msg = &#123;speedL,speedH&#125;
txCAN&#40;1, 1917, 0, msg&#41;
local speed11 = getGpsSpeed&#40;&#41;*1.6092
--local speed11 = 16
--format the speed in a CAN message. Speed is in the first byte
local msg = &#123;speed11&#125;
txCAN&#40;1, 1918, 0, msg&#41;
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.....

ferg
Posts: 36
Joined: Mon Dec 03, 2018 7:56 pm
Location: Paso Robles, California
Contact:

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

GTIspirit
Posts: 233
Joined: Wed Jan 09, 2013 11:20 am
Location: SE Michigan

### Re: Scaling is 1:1 dec:phys

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&#40;10&#41; --TickRate is in Hz
function onTick&#40;&#41;
local speed = getGpsSpeed&#40;&#41;*1.6092*255  --convert to kph and rescale for increased precision
speedL = bit.band&#40;speed, 0xFF&#41; --mask out high byte
speedH = bit.rshift&#40;speed, 8&#41; --shift high byte to the right
--format the speed in a CAN message. Speed is in the first two bytes
local msg = &#123;speedL,speedH&#125;
txCAN&#40;1, 1917, 0, msg&#41;
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!

brentp