Trying to Write Script to Parse CAN by Bit - Help Needed

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

Post Reply
JMcDonough
Posts: 70
Joined: Tue Mar 10, 2015 3:31 pm
Location: Detroit, MI

Trying to Write Script to Parse CAN by Bit - Help Needed

Post by JMcDonough »

Background: The vehicle I'm trying to log CAN data on has signals that do not fall evenly on the bytes like the script at the bottom of this page provides: http://www.autosportlabs.net/CAN_Bus_Integration So, I'm trying to write my own.

I've been using http://www.lua.org/cgi-bin/demo to test lua code and I've got something that works there, but when trying it on the RCP, the script crashes (red light, script doesn't run). I'm a lua noob, so perhaps I'm using some functions that work in the demo site, but not on the controller. Anyway, here's the code:

Code: Select all

--Take 8 bytes of CAN Data and return a decimal value

--Inputs&#58; 8 bytes of data, start bit, length in bits
function CANbytes2dec&#40;CAN_data,start_bit, length_bits&#41;

	function toBits&#40;num,bits&#41;
		local numf = num
		local bitsf = bits
		local t=&#123;&#125; -- will contain the bits        
		for b=bitsf,1,-1 do
			if numf % 2 == 0 then
				t&#91;b&#93;=0;
			else
				t&#91;b&#93;=1;
			end
			numf=&#40;numf-t&#91;b&#93;&#41;/2
		end
		return t
	end


	--inputs&#58; starting bit and bit length, multiplier and offset
	--output&#58; Value in decimal for variable
	--given&#58; function for turning decimal values into bits


	local end_bit = start_bit + length_bits - 1

	--create an array of 1's and 0's for whether each 
	--byte is part of the message
	--create a local variable and replace it each 
	--time the function for dec2bin is called

	local var_bits = &#123;&#125;
	local count_bits = 0;
	for i = 1,8,1 do
		--check if byte is included and bits are left to be added
		if &#40;&#40;i*8+1&#41;> start_bit&#41; and &#40;count_bits < length_bits&#41; then
			local bits_from_dec = &#123;&#125;
			bits_from_dec = toBits&#40;CAN_data&#91;i&#93;,8&#41;
			for j=1,8,1 do
				if &#40;&#40;i-1&#41;*8+j>=start_bit&#41; and &#40;&#40;i-1&#41;*8+j<=end_bit&#41; then
					count_bits = count_bits+1;
					var_bits&#91;count_bits&#93; = bits_from_dec&#91;j&#93;;
				end
			end
		end
	end
--take bits and convert to raw decimal value by multiplying by 2^x
	local dec_val_raw = 0
	local sum_bit = 0;
	for n = count_bits,1,-1 do
		sum_bit = sum_bit+1;
		dec_val_raw = dec_val_raw + var_bits&#91;sum_bit&#93;*2^&#40;n-1&#41;;
	end
	
	return dec_val_raw
end

local Test_CAN_Data = &#123;147, 231, 125, 145, 68, 129, 146, 228&#125; 
local test_start_bit = 6
local test_length_bits = 10
function onTick&#40;&#41;
	dec_val_test = CANbytes2dec&#40;Test_CAN_Data,test_start_bit, test_length_bits&#41;
	print&#40;dec_val_test&#41;
end
--onTick&#40;&#41; <- remove this comment to run on demo page
Any suggestions would be appreciated. TIA!
Josh

brentp
Site Admin
Posts: 5986
Joined: Wed Jan 24, 2007 6:36 am

Post by brentp »

Hi, typically when the script crashes, it will output an error message to the log window below the script. Do you see anything there? Or does the unit show a red light and 'reset'?

Scanned your script and didn't see anything out of the ordinary (e.g. calling non-existent system functions, etc).
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

JMcDonough
Posts: 70
Joined: Tue Mar 10, 2015 3:31 pm
Location: Detroit, MI

Post by JMcDonough »

I watch the log as the script starts to run and all I saw was indication that the lua script crashed. No indication of what line or anything. Is that typical?

Red light comes on. Does that mean it has reset?

I can post the log file output if that would be helpful.
Josh

brentp
Site Admin
Posts: 5986
Joined: Wed Jan 24, 2007 6:36 am

Post by brentp »

Red light means the script caused RCP to crash, and it re-booted in 'safe mode' so you can recover from the situation.

I'd recommend simplifying the script and adding it back in bit by bit and seeing where the problem crops up. We'll give it a check as well.

Let us know what you find out!
Brent Picasso
CEO and Founder, Autosport Labs
Facebook | Twitter

Post Reply