cancel
Showing results for 
Search instead for 
Did you mean: 

How to measure time difference between successive occurrence of two CAN messages

KA
Associate II

Hello All,

How to obtain the time difference between successive occurrence of two CAN messages .I am sending some CANIDs every 0.7 seconds and my aim is to measure the time difference between the successive occurrence of the CANids on reception by another STM32 Node.

For eg: I am sending CAN ID 13 and 14 approximately every 0.7 seconds . So when i use the "HAL_RTC_GetTime" function for every occurrence of this CANID and compute the time difference between the current occurence and the previous occurrence, i should get a floating point value correct to 4 decimal places..

Steps i performed

  • I implemented RTC functionality
  • In the "RTC_CalendarConfig" function, i gave a value of 255 for the "Seconds Fraction" parameter of the RTC_TimeTypeDef.
  • I then implemented the "HAL_RTC_GetTime" callback in the "HAL_CAN_RxFifo0MsgPendingCallback"function
  • As and when i receive the CAN ID 13 and 14 , the time is getting printed. However, i am unable to understand the subseconds part. How should i interpret it. If for example the time stamp in "Hours:Minutes:Seconds:Subseconds" output is "15: 7: 6:101" for the first time CAN ID 13 occurs and for the second time the time stamp is "15: 7: 7:178", should i interpret the time stamp like this 15: 7: 6 .101 and 15: 7: 7 .178or am i missing something here?
  • All i want to know is , i have the timestamps and i want to compute the time difference between them and my answer should be a decimal value of approximately 0.7 seconds (correct to 4 decimal places. The exact value would be 0.6998 seonds). If there is any other method to measure the time difference, Please advise.

Please help.

Please find the code below

----------------------------------------

void RTC_CalendarConfig(void)

{

//configured today's date and time which is 04/09/2019 Wednesday 14:19

//use APIs set time and set date from rtc.c

RTC_TimeTypeDef RTC_Time_Settings;

RTC_Time_Settings.TimeFormat =RTC_HOURFORMAT12_AM;

RTC_Time_Settings.Hours=14;

RTC_Time_Settings.Minutes=19;

RTC_Time_Settings.Seconds= 5;

RTC_Time_Settings.SecondFraction= 255; // Please advise if have used the correct value here

if (HAL_RTC_SetTime(&hrtc, &RTC_Time_Settings,RTC_FORMAT_BIN)!=HAL_OK)

{

Error_Handler();

}

RTC_DateTypeDef RTC_Date_Settings;

RTC_Date_Settings.Date= 4;

RTC_Date_Settings.Month=RTC_MONTH_SEPTEMBER;

RTC_Date_Settings.Year= 19;

RTC_Date_Settings.WeekDay= RTC_WEEKDAY_WEDNESDAY;

if (HAL_RTC_SetDate(&hrtc, &RTC_Date_Settings,RTC_FORMAT_BIN)!=HAL_OK)

{

Error_Handler();

}

}

void Current_Time_Date(uint32_t CANID)

{

RTC_TimeTypeDef RTC_Read_Current_Time;

RTC_DateTypeDef RTC_Read_Current_Date;

//char time_msg[300];

if (HAL_RTC_GetTime(&hrtc, &RTC_Read_Current_Time,RTC_FORMAT_BIN)!=HAL_OK)

{

Error_Handler();

}

if (HAL_RTC_GetDate(&hrtc, &RTC_Read_Current_Date,RTC_FORMAT_BIN)!=HAL_OK)

{

Error_Handler();

}

printmsg("CAN ID %x received at : %2d:%2d:%2d:%3d\r\n",CANID,RTC_Read_Current_Time.Hours,\

RTC_Read_Current_Time.Minutes,RTC_Read_Current_Time.Seconds,RTC_Read_Current_Time.SubSeconds);

}

uint8_t i=0;

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)

{

 uint8_t rcvd_msg1[8];

   uint8_t m;

   uint32_t CAN_ID[200];

if (HAL_CAN_GetRxMessage(&hcan1,CAN_RX_FIFO0,&RxHeader1,rcvd_msg1)!= HAL_OK)

{

Error_Handler();

}

Current_Time_Date(RxHeader1.StdId); // Function call to print the time stamp

CAN_ID[i]= RxHeader1.StdId;

i=i+1;

if (i==300)

{

i=0;

for(m=0;m<100;m++)

{

printmsg("The CAN ID is :%lx\r\n",CAN_ID[m]);

}

}

}

-------------------------------------------------------------------------

Output observed in Teraterm

CAN ID 13 received at : 15: 7: 6:101-- first occurrence

CAN ID 14 received at : 15: 7: 6:100

CAN ID 13 received at : 15: 7: 7:178-- second occurrence

----------------------------------------------------------------------------

I wanted the output to be like this. This is an output of my python code , which decodes a serial data obtained from a USB2CAN device, which i have connected to my CAN bus.

0x13 at time 15:28:28.322565 - first occurrence

0x14 at time 15:28:28.512265

0x13 at time 15:28:29.023463 - second occurrence

Now if compute the time difference between the second and first occurrence, i get the value 0.7008 sec

Thanks and with regards

Krishna

1 ACCEPTED SOLUTION

Accepted Solutions

uint32_t num_ticks = 0;

...

if (RxHeader1.StdId==0x13)

{

 static uint32_t last_tick = 0;

 uint32_t current_tick = HAL_GetTick()

 num_ticks = current_tick - last_tick; // compute time elapsed in milli-seconds

 last_tick = current_tick; // remember for next time

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

4 REPLIES 4

You could also just use HAL Ticks as a unit of time measurement, usually counting off in milliseconds.

The subsecond format for the RTC is a function of the prescalers as I recall, the Reference Manual presumably providing detail or a formula.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hello Clive,

Thank you for the response. I don't know how to use the HAL Ticks . Should i just call the function HAL_GetTick() on reception of each CAN message ?

Should i code something as below . Define a global variable as num_ticks tostore the HAL ticks.

void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)

{

 uint8_t rcvd_msg1[8];

   uint8_t m;

   uint32_t CAN_ID[200];

if (HAL_CAN_GetRxMessage(&hcan1,CAN_RX_FIFO0,&RxHeader1,rcvd_msg1)!= HAL_OK)

{

Error_Handler();

}

if (RxHeader1.StdId==0x13)

{

num_ticks = HAL_GetTick();

}

}

uint32_t num_ticks = 0;

...

if (RxHeader1.StdId==0x13)

{

 static uint32_t last_tick = 0;

 uint32_t current_tick = HAL_GetTick()

 num_ticks = current_tick - last_tick; // compute time elapsed in milli-seconds

 last_tick = current_tick; // remember for next time

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
KA
Associate II

Thank you so much for the help. I am getting an output like this , which i am printing via UART

---------

num_ticks = 700

num_ticks= 700

---------

The value 700 implies 700 ms have elapsed.. So it is 0.700 in seconds. Is there a way to obtain a floating point precision to the value 700?

This is how i am printing the value . Implemented your code from above

if (RxHeader1.StdId==0x13)

      {

      static uint32_t last_tick = 0;

      current_tick = HAL_GetTick();

      num_ticks = current_tick-last_tick; // compute time elapsed in milli-seconds

      last_tick = current_tick; // remember for next time

      printmsg("%lu\r\n",num_ticks);

      }