RxMsg byte value changing unexpectedly

Universal CAN Converter
Locked
rixccaptv
Posts: 11
Joined: Wed 14. Apr 2021, 03:23

RxMsg byte value changing unexpectedly

Post by rixccaptv » Thu 9. Dec 2021, 07:26

Some background:
I'm building a gateway for vehicle CAN.
Almost all of the signals are being translated as expected, with one exception.
I'm not sure what is causing the issue, but it seems one of the bytes from the Rx message is modified unexpectedly.

Troubleshooting so far:
Using CANalyzer, I am feeding in a CAN log and monitoring the gateway output.
The translated value for just ONE signal wasn't making any sense (steering angle in this case).
I checked the scaling and offset, and that part seems to be correct.
By selectively commenting things out, re-compiling, re-flashing and monitoring each time, I managed to narrow the source of the issue to where the input msg is copied over to a variable in a struct.
I disabled all normal outputs and added a new debug message which should basically only mirror bytes 4 and 5 of the received message.
HOWEVER, while debug byte 4 is the same as RxMsg byte 4, debug byte 5 is DIFFERENT than RxMsg byte5...

I'm thinking this might be some general C language mistake, but after much time I still have no clue why this is happening.

Here are the relevant code snippets:
Here is the definition of my vcan signals struct. All values are initialized to 0.

Code: Select all

typedef struct
{
	u8_t gear;
	double vehicleSpeed;
	double wheelSpeedFR;
	double wheelSpeedFL;
	double wheelSpeedRR;
	double wheelSpeedRL;
	double yawRate;
	double steeringAngle;
	u16_t debug_u16;
	u32_t debug_u32;
	u8_t debug_u8_1;
	u8_t debug_u8_2;
}vcanSignals;
In the main.c loop, I'm passing the struct address to this function like so:

Code: Select all

		// process messages from CAN1: VCAN input
		if (CAN_UserRead (CAN_BUS1, &RxMsg) != 0)
		{
			// parse and save vcan signals
			processInputs_xxxx(&RxMsg, &signals);
		}
signals struct pointer is dereferenced and modified using '->' operator

Code: Select all

void  processInputs_xxxx (CANMsg_t *RxMsg, vcanSignals *signals)
{
	// yaw rate, steering angle
	if (RxMsg->Id == 0xAB) //VDC_A6
	{
		u16_t tmp;

		signals->debug_u8_1 = RxMsg->Data8[4];
		signals->debug_u8_2 = RxMsg->Data8[5];
		signals->debug_u16  = RxMsg->Data16[2];
		signals->debug_u32  = RxMsg->Data32[1];

		tmp = ((u16_t)(RxMsg->Data8[4] & 0xFF) << 8) + ((u16_t)(RxMsg->Data8[5] & 0xFE) >> 1);
		signals->steeringAngle = (((double)tmp * 0.1) - 1080) * 0.0174532925199433;
	}
	
}
Finally, in another function (also called by main.c), I am outputting the messages (here I'm only showing the debug msg though).

Code: Select all

void output_xxxx (vcanSignals *signals)
{
	CANMsg_t  DebugMsg;
	DebugMsg.Id   = 0x3;
	DebugMsg.Len  = 8;
	DebugMsg.Type = CAN_MSG_STANDARD;
	DebugMsg.Data8[0] = signals->debug_u8_1;
	DebugMsg.Data8[1] = signals->debug_u8_2;
	DebugMsg.Data16[1] = signals->debug_u16;
	DebugMsg.Data32[1] = signals->debug_u32;
	CAN_UserWrite (CAN_BUS2, &DebugMsg);
}
Does anything stand out as a potential issue?

rixccaptv
Posts: 11
Joined: Wed 14. Apr 2021, 03:23

Re: RxMsg byte value changing unexpectedly

Post by rixccaptv » Thu 9. Dec 2021, 08:51

Please disregard this post.

I figured out the issue.

I had 2 things wrong: a Intel/Motorola byte order misunderstanding plus a bit shift error.

M.Heidemann
Sales & Support
Sales & Support
Posts: 1083
Joined: Fri 20. Sep 2019, 13:31

Re: RxMsg byte value changing unexpectedly

Post by M.Heidemann » Thu 9. Dec 2021, 09:19

Hello,

Thank you for your feedback!

We'll close this thread.

Best Regards

Marvin
---
Marvin Heidemann
PEAK-Support Team

Locked