cancel
Showing results for 
Search instead for 
Did you mean: 

When transferring data via mcp2515 on stm32, the values are reduced by half.

sheqom
Associate III

I am transferring sensor data between Jetson Nano and STM32F411E-DISCO via can using 2 MCP2515. On the STM32 side, imu and encoder sensors are connected and constantly transmit data to Jetson nano via canbus.

However, due to a problem that I do not fully understand, I think there is bit loss in the data and the value of the transmitted data is transmitted by half, or positive data is transmitted as negative data. Although this does not always happen, it does happen from time to time and is undesirable.

For example, if the data received from the IMU (MPU9255) via I2C is 172.321, the transmitted data is either -173.321 or 86.6605.

Code for data transmission with MCP2515 on STM32 side:

 

uCAN_MSG ImuData;

void prepareData(float* data1, float* data2, uCAN_MSG* canData){
	uint8_t DataToArray[8];
	memcpy(DataToArray,data1,sizeof(float));
	memcpy(DataToArray + sizeof(float),data2,sizeof(float));
	canData->frame.data0 = DataToArray[0];
	canData->frame.data1 = DataToArray[1];
	canData->frame.data2 = DataToArray[2];
	canData->frame.data3 = DataToArray[3];

	// data 2
	canData->frame.data4 = DataToArray[4];
	canData->frame.data5 = DataToArray[5];
	canData->frame.data6 = DataToArray[6];
	canData->frame.data7 = DataToArray[7];
}

int main(void)
{
    HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_TIM4_Init();
  MX_TIM3_Init();
  MX_TIM2_Init();
  MX_TIM5_Init();
  MX_I2C1_Init();
  MX_SPI1_Init();
  CANSPI_Initialize();
  ImuData.frame.idType = dEXTENDED_CAN_MSG_ID_2_0B;
  ImuData.frame.id = 0x01;
  ImuData.frame.dlc = 8;
  while (1)
      {
    	  prepareData(&MPU9255.pitch, &MPU9255.yaw, &ImuData);
    	  CANSPI_Transmit(&ImuData);
          HAL_Delay(50);
       }

 

The code section where I took the data on the Jetson nano side and made it meaningful:

 

import can
bus = can.interface.Bus(channel = "can0",bustype = "socketcan")
while 1:
  msg = bus.recv()
  data = struct.iter_unpack("f",msg.data)
  print(*data)
  

 

For example original data PITCH = -15.51 , YAW = 327.82 on STM32

received data on Jetson Nano

PITCH : -23.011926651000977 YAW : 327.2798156738281
PITCH : -23.011926651000977 YAW : 327.2798156738281
PITCH : -23.01274299621582 YAW : 163.6371459960938
PITCH : -23.01274299621582 YAW : 163.6371459960938
PITCH : -23.10681915283203 YAW : 327.1934509277344
PITCH : -23.10681915283203 YAW : 327.1934509277344
PITCH : 23.227672576904297 YAW : 326.1050720214844
PITCH : 23.227672576904297 YAW : -163.6371459960938
PITCH : 15.577387809753418 YAW : 327.3647766113281
PITCH : 15.577387809753418 YAW : 327.3647766113281
PITCH : -15.53555965423584 YAW : 16.,6371459960938
PITCH : -15.53555965423584 YAW : 327.60906982421875
PITCH : 22.953519821166992 YAW : 327.57177734375

PITCH : 22.953519821166992 YAW : 327.57177734375
PITCH : 15.467488288879395 YAW : 327.48828125
PITCH : 15.467488288879395 YAW : 327.48828125


I am using https://github.com/eziya/STM32_SPI_MCP2515 library for mcp2515.

And using https://github.com/ibrahimcahit/MPU9255-Quaternion-AHRS-STM32 as MPU9255 library.

1 ACCEPTED SOLUTION

Accepted Solutions
sheqom
Associate III

https://forums.developer.nvidia.com/t/jetson-nano-4gb-mcp2515-can-bus-receiving-wrong-id/192810

Based on the solution here, I set the spi max frequency values to 2 Mhz and the problem was solved. I thank you.

View solution in original post

2 REPLIES 2
sheqom
Associate III

Update: Outgoing data from Jetson is transmitted with full accuracy, but there is a problem with the incoming data to Jetson. I suspect there may be a problem with the Jetson nano's MCP driver.

sheqom
Associate III

https://forums.developer.nvidia.com/t/jetson-nano-4gb-mcp2515-can-bus-receiving-wrong-id/192810

Based on the solution here, I set the spi max frequency values to 2 Mhz and the problem was solved. I thank you.