Skip to content

Commit

Permalink
feat(serial transmitter prototype): ✨ Prototype transmit frame error
Browse files Browse the repository at this point in the history
This drops a frame when the time jitter is more than two microseconds.
  • Loading branch information
ZZ-Cat committed Apr 25, 2024
1 parent b8f00e6 commit 4419b03
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions examples/platformio/serial_transmitter_prototype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef struct time_s
uint32_t time_us = 0;
uint32_t time_us_last = 0;
uint32_t time_us_delta = 0;
int32_t time_us_error = 0;
} time_t;

/* Time structure instance. */
Expand Down Expand Up @@ -99,8 +100,11 @@ void loop()
time->time_us = micros();
time->time_us_delta = time->time_us - time->time_us_last;

/* Check if the time delta is greater than the time in microseconds for the selected packet rate. */
if (time->time_us_delta >= packet_rate_us[selected_packet_rate])
/* Calculate the time error in microseconds. */
time->time_us_error = time->time_us - (time->time_us_last + packet_rate_us[selected_packet_rate]);

/* If the time delta is greater than or equal to the packet rate in microseconds and the time error is less than 2 microseconds. */
if (time->time_us_delta >= packet_rate_us[selected_packet_rate] && time->time_us_error < 2)
{
/* Print the time delta in microseconds. */
Serial.print("Time Delta: ");
Expand All @@ -116,6 +120,24 @@ void loop()
/* Increment the iteration. */
iteration++;
}

/* If the time error is greater than or equal to 2 microseconds. */
else if (time->time_us_error >= 2)
{
/* Print an error message to the serial monitor. */
Serial.println("Error: Time error is greater than or equal to 2 microseconds.");

/* Print how far the time error is from the packet rate in microseconds. */
Serial.print("Time Error: ");
Serial.print(time->time_us_error);
Serial.println(" us");

/* Increment the iteration. */
iteration++;

/* Set the last time in microseconds. */
time->time_us_last = time->time_us;
}
}
else
{
Expand Down

0 comments on commit 4419b03

Please sign in to comment.