Skip to content

Commit

Permalink
orin comm tested
Browse files Browse the repository at this point in the history
  • Loading branch information
jia-xie committed Apr 4, 2024
1 parent a5ff243 commit 84f1053
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/app/inc/robot_tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void Robot_Tasks_Jetson_Orin(void const *argument)
{
portTickType xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
const TickType_t TimeIncrement = pdMS_TO_TICKS(10);
const TickType_t TimeIncrement = pdMS_TO_TICKS(JETSON_ORIN_PERIOD);
while (1)
{
Jetson_Orin_Send_Data();
Expand Down
7 changes: 5 additions & 2 deletions src/app/src/debug_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "robot.h"
#include "referee_system.h"
#include "jetson_orin.h"
#include "bsp_daemon.h"

extern Robot_State_t g_robot_state;
extern DJI_Motor_Handle_t *g_yaw;
Expand All @@ -17,11 +18,13 @@ extern Swerve_Module_t g_swerve_fl;
extern Remote_t g_remote;
extern Launch_Target_t g_launch_target;
extern uint64_t t;
extern Daemon_Instance_t *g_daemon_instances[3];

void Debug_Task_Loop(void)
{
static uint32_t counter = 0;
// DEBUG_PRINTF(&huart6, "time=%.1f,a=%f,b=%f,c=%f,d=%d\r\n", (float) counter / 1000.0f * DEBUG_PERIOD,
// g_imu.deg.yaw, g_imu.deg.pitch, g_imu.deg.roll, counter);
// DEBUG_PRINTF(&huart6, "time=%.1f,a=%d,b=%d,c=%f,d=%d\r\n", (float) counter / 1000.0f * DEBUG_PERIOD,
// g_daemon_instances[1]->counter, g_daemon_instances[0]->counter, g_imu.deg.roll, counter);
counter++;
if (counter > 0xFFFFFFFF) {
counter = 0;
Expand Down
14 changes: 9 additions & 5 deletions src/devices/inc/jetson_orin.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
#define JETSON_ORIN_H

#include <stdint.h>
#include "usart.h"
#include "bsp_uart.h"

#define ORIN_DATA_RX_BUFER_SIZE (20)
#define ORIN_DATA_TX_BUFER_SIZE (33)

#define ORIN_TIMEOUT_MS (3000)
#define JETSON_ORIN_PERIOD (100)

typedef struct
{
UART_HandleTypeDef *huartx;

uint8_t rx_buffer[20];
uint8_t tx_buffer[33];
uint8_t rx_buffer[ORIN_DATA_RX_BUFER_SIZE];
uint8_t tx_buffer[ORIN_DATA_TX_BUFER_SIZE];

struct
{
Expand Down Expand Up @@ -67,7 +72,6 @@ typedef struct

extern Jetson_Orin_Data_t g_orin_data;
void Jetson_Orin_Init(UART_HandleTypeDef *huartx);
void Jetson_Orin_Decode(void);
void Jetson_Orin_Send_Data(void);

#endif
43 changes: 38 additions & 5 deletions src/devices/src/jetson_orin.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
#include <string.h>

#include "imu_task.h"
#include "bsp_uart.h"
#include "bsp_daemon.h"

Jetson_Orin_Data_t g_orin_data;

void Jetson_Orin_Decode(void)
UART_Instance_t *g_orin_uart_instance_ptr;
Daemon_Instance_t *g_orin_daemon_instance_ptr;

void Jetson_Orin_Rx_Callback(UART_Instance_t *uart_instance)
{
UNUSED(uart_instance);
// TODO: update buffer reference from uart_instance
if(g_orin_data.rx_buffer[0] == 0xAA)
{
// If matching frame header, reload Daemon
Daemon_Reload(g_orin_daemon_instance_ptr);

// Decode data
g_orin_data.receiving.frame_id = g_orin_data.rx_buffer[0];
g_orin_data.receiving.frame_type = g_orin_data.rx_buffer[1];
switch(g_orin_data.receiving.frame_type)
Expand Down Expand Up @@ -42,14 +51,37 @@ void Jetson_Orin_Decode(void)
}
}

/**
* @brief Callback function for timeout of Jetson Orin used by Daemon
* @details When not receiving data from Orin for a certain time, will try
* to reinitialize the UART service. Daemon is reloaded to prevent
* continuous reinitialization at high frequency.
* @param void
*/
void Jetson_Orin_Timeout_Callback()
{
// Attemp to reinitialize UART service
UART_Service_Init(g_orin_uart_instance_ptr);

// Reload Daemon to prevent continuous reinitialization
Daemon_Reload(g_orin_daemon_instance_ptr);
}

void Jetson_Orin_Init(UART_HandleTypeDef *huartx)
{
g_orin_data.huartx = huartx;
HAL_UART_Receive_DMA(huartx, g_orin_data.rx_buffer, sizeof(g_orin_data.rx_buffer));
// register UART instance
g_orin_uart_instance_ptr = UART_Register(huartx, g_orin_data.rx_buffer, ORIN_DATA_RX_BUFER_SIZE, Jetson_Orin_Rx_Callback);

// register Daemon instance
// timeout is defined in the header file
uint16_t reload_value = ORIN_TIMEOUT_MS / DAEMON_PERIOD;
uint16_t initial_counter = reload_value;
g_orin_daemon_instance_ptr = Daemon_Register(reload_value, initial_counter, Jetson_Orin_Timeout_Callback);
}

void Jetson_Orin_Send_Data(void)
{
// update data to be sent
g_orin_data.sending.pitch_angle = g_imu.rad.pitch;
g_orin_data.sending.pitch_angular_rate = g_imu.bmi088_raw.gyro[1];
g_orin_data.sending.yaw_angular_rate = g_imu.bmi088_raw.gyro[2];
Expand All @@ -59,6 +91,7 @@ void Jetson_Orin_Send_Data(void)
g_orin_data.sending.velocity_x = 0;
g_orin_data.sending.velocity_y = 0;

// float to byte conversion
g_orin_data.sending.float_byte.data[0] = g_orin_data.sending.pitch_angle;
g_orin_data.sending.float_byte.data[1] = g_orin_data.sending.pitch_angular_rate;
g_orin_data.sending.float_byte.data[2] = g_orin_data.sending.yaw_angular_rate;
Expand All @@ -71,5 +104,5 @@ void Jetson_Orin_Send_Data(void)
g_orin_data.tx_buffer[0] = 0xAA;
memcpy(&g_orin_data.tx_buffer[1],&g_orin_data.sending.float_byte.data_bytes[0], 32*sizeof(uint8_t));

HAL_UART_Transmit_DMA(g_orin_data.huartx, g_orin_data.tx_buffer, sizeof(g_orin_data.tx_buffer));
//UART_Transmit(g_orin_uart_instance_ptr, g_orin_data.tx_buffer, sizeof(g_orin_data.tx_buffer), UART_DMA);
}

0 comments on commit 84f1053

Please sign in to comment.