First drafto of new parser
This commit is contained in:
parent
fd743c755e
commit
1c0c7fb00f
@ -11,8 +11,55 @@
|
||||
#include "drivers/usart.h"
|
||||
|
||||
#define ARDUINO_BAUD 115200
|
||||
#define ARDUINO_DMA_SIZE 15
|
||||
#define ARDUINO_DMA_SIZE 20
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: RX packet structure from arduino com *
|
||||
* INFORMATION: Contains the whole compass message *
|
||||
***********************************************************************/
|
||||
typedef struct compass_data_t {
|
||||
uint8_t header;
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
int16_t z;
|
||||
uint8_t crc;
|
||||
} compass_data_t;
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: RX packet structure from arduino com *
|
||||
* INFORMATION: Contains the whole gps data message *
|
||||
***********************************************************************/
|
||||
typedef struct gps_data_t {
|
||||
uint8_t header;
|
||||
float latitude;
|
||||
float longitude;
|
||||
uint8_t crc;
|
||||
} gps_data_t;
|
||||
|
||||
/* An instance of the GPS data read from Arduino Com */
|
||||
gps_data_t gps_data;
|
||||
|
||||
/* An instance of the compass data read from Arduino Com */
|
||||
compass_data_t compass_data;
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Initializes the UART for Arduino com *
|
||||
* INFORMATION: A DMA Buffer starts storing the bytes received from RX *
|
||||
***********************************************************************/
|
||||
void arduinoCom_init(USART_TypeDef* usart_inst);
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Checks if new RX packet is available *
|
||||
* INFORMATION: Is called by the scheduler *
|
||||
***********************************************************************/
|
||||
bool arduino_frame_available();
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Updates "gps_data" and "compass_data" *
|
||||
* INFORMATION: Is called by the scheduler *
|
||||
***********************************************************************/
|
||||
void arduino_read();
|
||||
|
||||
#endif /* DRIVERS_ARDUINO_COM_H_ */
|
||||
|
@ -19,25 +19,6 @@ enum packet_ids {
|
||||
GPS_PACKET_ID = 0xB1,
|
||||
};
|
||||
|
||||
typedef struct compass_data_t {
|
||||
uint8_t header;
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
int16_t z;
|
||||
uint8_t crc;
|
||||
} compass_data_t;
|
||||
|
||||
compass_data_t compass_data = {0};
|
||||
|
||||
typedef struct gps_data_t {
|
||||
uint8_t header;
|
||||
float latitude;
|
||||
float longitude;
|
||||
uint8_t crc;
|
||||
} gps_data_t;
|
||||
|
||||
gps_data_t gps_data = {0};
|
||||
|
||||
typedef struct arduino_data_t {
|
||||
uint8_t size; //Size of the data
|
||||
void * dataPtr; //pointer to the data
|
||||
@ -51,22 +32,32 @@ enum arduino_data_e {
|
||||
|
||||
arduino_data_t data_arr[ARDUINO_DATA_COUNT] = {
|
||||
[COMPASS_DATA_ID] = {
|
||||
.size = sizeof(compass_data),
|
||||
.size = 8,
|
||||
.dataPtr = &compass_data,
|
||||
},
|
||||
[GPS_DATA_ID] = {
|
||||
.size = sizeof(gps_data),
|
||||
.size = 10,
|
||||
.dataPtr = &gps_data,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Initializes the UART for Arduino com *
|
||||
* INFORMATION: A DMA Buffer starts storing the bytes received from RX *
|
||||
***********************************************************************/
|
||||
void arduinoCom_init(USART_TypeDef* usart_inst)
|
||||
{
|
||||
/* initialize the USART with a dma buffer */
|
||||
usart_init_dma(usart_inst, &dmaHandler, ARDUINO_BAUD, STOP_BITS_1, PARITY_NONE, ARDUINO_DMA_SIZE, 0);
|
||||
// usart_transmit(&dmaHandler.usart_pro, "data", 4, 100000);
|
||||
}
|
||||
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Checks if new RX packet is available *
|
||||
* INFORMATION: Is called by the scheduler *
|
||||
***********************************************************************/
|
||||
bool arduino_frame_available()
|
||||
{
|
||||
/* We read data from DMA */
|
||||
@ -75,6 +66,11 @@ bool arduino_frame_available()
|
||||
return raw_dma_data_t.new_data;
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Private function that looks for the correct data struct from *
|
||||
* the header data *
|
||||
* INFORMATION: *
|
||||
***********************************************************************/
|
||||
arduino_data_t find_packet_from_header(uint8_t header)
|
||||
{
|
||||
arduino_data_t arduino_data = {
|
||||
@ -82,6 +78,7 @@ arduino_data_t find_packet_from_header(uint8_t header)
|
||||
.size = 0,
|
||||
};
|
||||
|
||||
/* Check what header it is and return the correct datapointer if correct header*/
|
||||
switch (header)
|
||||
{
|
||||
case COMPASS_PACKET_ID:
|
||||
@ -96,6 +93,83 @@ arduino_data_t find_packet_from_header(uint8_t header)
|
||||
return arduino_data;
|
||||
}
|
||||
|
||||
void arduino_parse_message(uint8_t data)
|
||||
{
|
||||
static uint8_t arduino_arr[ARDUINO_DMA_SIZE];
|
||||
static bool find_header = true;
|
||||
static uint8_t message_it = 0;
|
||||
static uint8_t secondary_message_it = 0;
|
||||
static arduino_data_t msg_header_and_size = { .size = 0, .dataPtr = NULL };
|
||||
static uint8_t crc = 0;
|
||||
|
||||
if(find_header)
|
||||
{
|
||||
msg_header_and_size = find_packet_from_header(data);
|
||||
|
||||
if(msg_header_and_size.size != 0)
|
||||
{
|
||||
find_header = false;
|
||||
arduino_arr[(message_it)] = data;
|
||||
message_it++;
|
||||
crc ^= data;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If we find any new possible header then we should be able to return to that point in time */
|
||||
if (((arduino_data_t)find_packet_from_header(data)).size != 0 && secondary_message_it == 0)
|
||||
{
|
||||
secondary_message_it = message_it; //save the value of the position in The buffer array, not the dma array index
|
||||
}
|
||||
|
||||
/* Reading the message except the crc byte */
|
||||
if ((message_it) < (msg_header_and_size.size - 1))
|
||||
{
|
||||
arduino_arr[(message_it)] = data;
|
||||
crc ^= data;
|
||||
message_it++;
|
||||
}
|
||||
else if ((message_it) == (msg_header_and_size.size - 1))
|
||||
{
|
||||
/* put the crc code into the data buffer as well */
|
||||
arduino_arr[(message_it)] = data;
|
||||
|
||||
/* TODO: Replace with check for CRC */
|
||||
if (crc == data)
|
||||
{
|
||||
/* Clear necessary variables in order to fill the buffer with new ones */
|
||||
message_it = 0;
|
||||
find_header = true;
|
||||
crc = 0;
|
||||
|
||||
memcpy(msg_header_and_size.dataPtr, arduino_arr, msg_header_and_size.size);
|
||||
}
|
||||
else
|
||||
{
|
||||
int size = msg_header_and_size.size;
|
||||
int new_iter = secondary_message_it;
|
||||
|
||||
crc = 0;
|
||||
find_header = true;
|
||||
message_it = 0;
|
||||
secondary_message_it = 0;
|
||||
|
||||
if(new_iter > 0)
|
||||
{
|
||||
for(int i = new_iter; i < size; i++)
|
||||
{
|
||||
arduino_parse_message(arduino_arr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***********************************************************************
|
||||
* BRIEF: Updates "gps_data" and "compass_data" *
|
||||
* INFORMATION: Is called by the scheduler *
|
||||
***********************************************************************/
|
||||
void arduino_read()
|
||||
{
|
||||
static uint8_t arduino_arr[ARDUINO_DMA_SIZE];
|
||||
@ -103,96 +177,98 @@ void arduino_read()
|
||||
static uint32_t missedMsg = 0;
|
||||
static uint8_t message_it_secondary_head = 0;
|
||||
static bool new_header = false;
|
||||
//static uint8_t current_packet_size = 0;
|
||||
static uint8_t current_header = 0;
|
||||
static uint8_t crc = 0;
|
||||
static arduino_data_t msg_header_and_size = {0};
|
||||
arduino_frame_available();
|
||||
static arduino_data_t msg_header_and_size = { .size = 0, .dataPtr = NULL };
|
||||
static bool find_new_header = true;
|
||||
|
||||
if (raw_dma_data_t.new_data)
|
||||
{
|
||||
|
||||
for (int i = 0; i < ARDUINO_DMA_SIZE; i++)
|
||||
{
|
||||
arduino_parse_message(raw_dma_data_t.buff[i]);
|
||||
|
||||
uint8_t msg = raw_dma_data_t.buff[i];
|
||||
msg_header_and_size = find_packet_from_header(msg);
|
||||
|
||||
// Look for the beginning of a frame
|
||||
if ( message_it == 0 )
|
||||
{
|
||||
|
||||
|
||||
|
||||
if (msg_header_and_size.size != 0)
|
||||
{
|
||||
arduino_arr[(message_it)] = msg;
|
||||
message_it++;
|
||||
current_header = msg;
|
||||
new_header = false; // Just received one
|
||||
crc ^= msg;
|
||||
}
|
||||
}
|
||||
// Look for the end of sbus frame
|
||||
else
|
||||
{
|
||||
if (msg_header_and_size.size != 0 && new_header == false)
|
||||
{
|
||||
new_header = true;
|
||||
message_it_secondary_head = message_it; //save the value of the position in The buffer array, not the dma array index
|
||||
}
|
||||
|
||||
/* Reading the message */
|
||||
if ((message_it) < msg_header_and_size.size)
|
||||
{
|
||||
arduino_arr[(message_it)] = msg;
|
||||
crc ^= msg;
|
||||
message_it++;
|
||||
}
|
||||
|
||||
if ((message_it) == msg_header_and_size.size)
|
||||
{
|
||||
missedMsg++;
|
||||
|
||||
/* TODO: Replace with check for CRC */
|
||||
if (crc == msg)
|
||||
{
|
||||
message_it = 0;
|
||||
missedMsg--;
|
||||
arduino_data_t current_header_and_size = find_packet_from_header(current_header);
|
||||
|
||||
uint8_t sizeof_data = current_header_and_size.size;
|
||||
uint8_t* tmp_ptr_to;
|
||||
tmp_ptr_to = current_header_and_size.dataPtr;
|
||||
|
||||
memcpy(tmp_ptr_to,arduino_arr,sizeof_data);
|
||||
|
||||
}
|
||||
|
||||
/* If CRC does not match */
|
||||
else
|
||||
{
|
||||
int temp_secondaryHeader = message_it_secondary_head;
|
||||
message_it = message_it - temp_secondaryHeader; //update the counter to the empty part of the updated array
|
||||
new_header = false; //set new header to false, this is true if there is another header within the buffer
|
||||
|
||||
//Move all the remaning messages in the buffer to the start of the buffer
|
||||
for (int i = temp_secondaryHeader; i < ARDUINO_DMA_SIZE; i++)
|
||||
{
|
||||
int innerCount = i-temp_secondaryHeader;
|
||||
arduino_arr[innerCount] = arduino_arr[i];
|
||||
|
||||
//check if we find another possible header inside the rest of the buffer and save that
|
||||
if (((arduino_data_t)(find_packet_from_header(innerCount))).size != 0 && innerCount > 0 && new_header == false )
|
||||
{
|
||||
new_header = true;
|
||||
message_it_secondary_head = innerCount;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
// uint8_t msg = raw_dma_data_t.buff[i]; //raw_dma_data_t.buff[i];
|
||||
// if(find_new_header)
|
||||
// {
|
||||
// msg_header_and_size = find_packet_from_header(msg);
|
||||
// if(msg_header_and_size.size != 0)
|
||||
// {
|
||||
// find_new_header = false;
|
||||
// arduino_arr[(message_it)] = msg;
|
||||
// message_it++;
|
||||
// new_header = false; // Just received one
|
||||
// crc ^= msg;
|
||||
// }
|
||||
// }
|
||||
// // Look for the end of sbus frame
|
||||
// else
|
||||
// {
|
||||
// /* If we find any new possible header then we should be able to return to that point in time */
|
||||
// if (((arduino_data_t)find_packet_from_header(msg)).size != 0 && new_header == false)
|
||||
// {
|
||||
// new_header = true;
|
||||
// message_it_secondary_head = message_it; //save the value of the position in The buffer array, not the dma array index
|
||||
// }
|
||||
//
|
||||
// /* Reading the message except the crc byte */
|
||||
// if ((message_it) < (msg_header_and_size.size - 1))
|
||||
// {
|
||||
// arduino_arr[(message_it)] = msg;
|
||||
// crc ^= msg;
|
||||
// message_it++;
|
||||
// }
|
||||
// else if ((message_it) == (msg_header_and_size.size - 1))
|
||||
// {
|
||||
// /* put the crc code into the data buffer as well */
|
||||
// arduino_arr[(message_it)] = msg;
|
||||
// missedMsg++;
|
||||
//
|
||||
// /* TODO: Replace with check for CRC */
|
||||
// if (crc == msg)
|
||||
// {
|
||||
// /* Clear necessary variables in order to fill the buffer with new ones */
|
||||
// message_it = 0;
|
||||
// find_new_header = true;
|
||||
// missedMsg--;
|
||||
// crc = 0;
|
||||
//
|
||||
// memcpy(msg_header_and_size.dataPtr, arduino_arr, msg_header_and_size.size);
|
||||
// }
|
||||
//
|
||||
// /* If CRC does not match */
|
||||
// else
|
||||
// {
|
||||
//
|
||||
// int temp_secondaryHeader = message_it_secondary_head;
|
||||
// int length = msg_header_and_size.size;
|
||||
// int new_struct_size = 0;
|
||||
// message_it = message_it - temp_secondaryHeader; //update the counter to the empty part of the updated array
|
||||
//
|
||||
// if(new_header)
|
||||
// msg_header_and_size = find_packet_from_header(arduino_arr[temp_secondaryHeader]);
|
||||
//
|
||||
// /* Clear necessary variables so that a new message can be read into the buffer correctly */
|
||||
// new_header = false; //set new header to false, this is true if there is another header within the buffer
|
||||
// crc = 0;
|
||||
//
|
||||
// //Move all the remaning messages in the buffer to the start of the buffer
|
||||
// for (int j = temp_secondaryHeader; j < length; j++)
|
||||
// {
|
||||
// int innerCount = j - temp_secondaryHeader;
|
||||
// arduino_arr[innerCount] = arduino_arr[j];
|
||||
//
|
||||
// crc ^= arduino_arr[j];
|
||||
//
|
||||
// //check if we find another possible header inside the rest of the buffer and save that
|
||||
// if (((arduino_data_t)(find_packet_from_header(innerCount))).size != 0 && innerCount > 0 && new_header == false )
|
||||
// {
|
||||
// new_header = true;
|
||||
// message_it_secondary_head = innerCount;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -61,7 +61,9 @@ void init_system()
|
||||
// TODO!! TEMP code
|
||||
arduinoCom_init(USART1);
|
||||
while (1) {
|
||||
arduino_read();
|
||||
if(arduino_frame_available())
|
||||
arduino_read();
|
||||
HAL_Delay(15);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user