Page 1 of 1

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

Posted: Sat Apr 11, 2015 2:30 am
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: 8 bytes of data, start bit, length in bits
function CANbytes2dec(CAN_data,start_bit, length_bits)

	function toBits(num,bits)
		local numf = num
		local bitsf = bits
		local t={} -- will contain the bits        
		for b=bitsf,1,-1 do
			if numf % 2 == 0 then
				t[b]=0;
			else
				t[b]=1;
			end
			numf=(numf-t[b])/2
		end
		return t
	end


	--inputs: starting bit and bit length, multiplier and offset
	--output: Value in decimal for variable
	--given: 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 = {}
	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!

Posted: Tue Apr 14, 2015 7:03 pm
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).

Posted: Tue Apr 14, 2015 7:15 pm
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.

Posted: Tue Apr 14, 2015 7:21 pm
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!