diff --git a/.vscode/settings.json b/.vscode/settings.json index f3ac2bf..a51ad4a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,6 +10,8 @@ "stm32_hal_legacy.h": "c", "stm32f3xx.h": "c", "stm32f303xe.h": "c", - "robotpins.h": "c" + "robotpins.h": "c", + "stm32f3xx_it.h": "c", + "communicationstructs.h": "c" } } \ No newline at end of file diff --git a/app/inc/CanBusTask.h b/app/inc/CanBusTask.h deleted file mode 100644 index 28e0dd9..0000000 --- a/app/inc/CanBusTask.h +++ /dev/null @@ -1,131 +0,0 @@ - -/** - * CANTask.c - * - * Created on: January 12, 2024 - * Author: Erick Daniel Ortiz Cervantes - * Reference: https://www.youtube.com/watch?v=KHNRftBa1Vc - */ - -#pragma once - -#include "CommunicationStructs.h" -#include "RobomasterCanProtocol.h" -#include "can.h" -#include "cmsis_os.h" -#include "main.h" -#include "semphr.h" - -// ======================================================= -// Robomaster protocol -// ======================================================= -#define STACK_SIZE 128 - -// TODO: define which one should stayed defined, and it's value -// Supposedly the bitrate is 1Mbps -// Tho, the supported baud rate is 1000 kbps -#define CAN_BITRATE -#define CAN_BAUDRATE - -#define CAN_TIMEOUT_MS 5 -// https://arm-software.github.io/CMSIS_5/RTOS/html/group__CMSIS__RTOS__SemaphoreMgmt.html - -// Each motor sends it's info 1 KHz - -// Can filter example -// https://schulz-m.github.io/2017/03/23/stm32-can-id-filter/ -// On GNU arm you can't use __attribute__((__packed)) -// See https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Packed-Structures.html - -#pragma pack(push, 1) - typedef struct { - - uint8_t SOF : 1; - uint16_t ID : 11; - uint8_t RTR : 1; - uint8_t Reserved : 1; - uint8_t DLC : 4; - } beginCANFrame; - - typedef struct { - // TODO: Check what is CRC_TypeDef - uint16_t CRC_ : 15; - uint8_t CRCDeliter : 1; - uint8_t Ack : 1; - uint8_t AckDelimiter : 1; - uint8_t EOF : 7; - } endCANFrame; -#pragma pack(pop) - -static uint8_t CANframeLength = sizeof(beginCANFrame) + (RM_DLC) * 8 + sizeof(endCANFrame); - -// ======================================================= - -// ======================================================= -// Communication structs -// ======================================================= - -void StartCANTxTask(void* argument); -void StartCANRxTask(void* argument); - - -osThreadDef(StartCANTxTask, osPriorityNormal, 3, 0, 128 * 4); -osThreadDef(StartCARRxTask, osPriorityNormal, 3, 0, 128 * 4); - -void StartCANRxTask(void* argument); - -void StartCANTxTask(void* argument); - -void HAL_CAN_TxMailbox0CompleteCallback(CAN_HandleTypeDef* hcan); - -void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef* hcan); - -// ======================================================= -// STREAM BUFFER --- nOT USED FOR NOW -// ======================================================= - -/* -static uint8_t rxData[RM_DLC]; -static uint8_t txData[RM_DLC]; - -static StreamBufferHandle_t rxStream = NULL; - -static bool rxInProgress = false; -static uint_fast16_t rxLen = 0; - -// There is no DMA for can -// static DMA_HandleTypeDef usart2DmaRx; - -// Queues for receiving data -osMessageQId queue_id; // Define the queue handle -osMessageQDef(queue, 10, uint32_t); // Define the queue with 10 elements of uint32_t type - -// Create the queue -queue_id = osMessageCreate(osMessageQ(queue), NULL); -*/ - -// ================== -/* -file:///Users/jperezch/Downloads/um2217-description-of-stm32h7-hal-and-lowlayer-drivers-stmicroelectronics.pdf - - - ============================================================================== - ##### Control functions ##### - ============================================================================== - [..] This section provides functions allowing to: - (+) HAL_CAN_Start : Start the CAN module - (+) HAL_CAN_Stop : Stop the CAN module - (+) HAL_CAN_RequestSleep : Request sleep mode entry. - (+) HAL_CAN_WakeUp : Wake up from sleep mode. - (+) HAL_CAN_IsSleepActive : Check is sleep mode is active. - (+) HAL_CAN_AddTxMessage : Add a message to the Tx mailboxes - and activate the corresponding - transmission request - (+) HAL_CAN_AbortTxRequest : Abort transmission request - (+) HAL_CAN_GetTxMailboxesFreeLevel : Return Tx mailboxes free level - (+) HAL_CAN_IsTxMessagePending : Check if a transmission request is - pending on the selected Tx mailbox - (+) HAL_CAN_GetRxMessage : Get a CAN frame from the Rx FIFO - (+) HAL_CAN_GetRxFifoFillLevel : Return Rx FIFO fill level - -*/ \ No newline at end of file diff --git a/app/inc/CommunicationStructs.h b/app/inc/CommunicationStructs.h index 7a296d7..f8937c4 100644 --- a/app/inc/CommunicationStructs.h +++ b/app/inc/CommunicationStructs.h @@ -8,8 +8,8 @@ #pragma once -#include "main.h" #include "cmsis_os.h" +#include "main.h" /** * @brief Represents a CAN bus message splitted into the velocity @@ -35,10 +35,30 @@ typedef struct { uint8_t vMotor_pitch; } GimballControlMessage; +/** + * @brief Represents a Control Data Message + */ +#pragma pack(push, 1) +typedef struct control_data { + uint8_t joystickAx; + uint8_t joystickAy; + uint8_t joystickBx; + uint8_t joystickBy; + uint8_t knobA; + uint8_t knobB; + uint8_t switchA; + uint8_t switchB; + uint8_t switchC; + uint8_t switchD; +} control_data; +#pragma pack(pop) -// Message queues from protocols +// Message queues from protocols extern osPoolId can_rx_mpool; extern osPoolId can_tx_mpool; +extern osPoolId remote_pool; + +extern osMessageQId outputQueueChassis; +extern osMessageQId inputQueueChassis; -extern osMessageQId outputQueueChassis; -extern osMessageQId inputQueueChassis; \ No newline at end of file +extern osMessageQId remoteQueue; diff --git a/app/inc/RobomasterCanProtocol.h b/app/inc/RobomasterCanProtocol.h deleted file mode 100644 index 19b0bd8..0000000 --- a/app/inc/RobomasterCanProtocol.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * RobomasterCanProtocol.h - * - * Created on: May 24, 2023 - * Author: @yourName - */ - -#ifndef RobomasterCanProtocol_H -#define RobomasterCanProtocol_H - -// TODO: Fix assignment -#define CAN_ID_1 0x1U -#define CAN_ID_2 0x2U -#define CAN_ID_3 0x3U -#define CAN_ID_4 0x4U -#define CAN_ID_5 0x5U -#define CAN_ID_6 0x6U -#define CAN_ID_7 0x7U -#define CAN_ID_8 0x8U - -// Read information from a given motor -// The MSG is defined by -// FEEDBACK_ID + MotorID -#define FEEDBACK_ID 0x204 -#define FEEDBACK_ID_ALT 0x200 - -// Sent control output to motors -#define CONTROL_ID_1FF 0x1FF -#define CONTROL_ID_2FF 0x2FF -#define CONTROL_ID_200 0x200 - -// Read motor status -#define CONTROL_ANGLE_BEND 0x0 -#define CONTROL_ANGLE_LEND 0x1 - -#define CONTROL_SPEED_BEND 0x2 -#define CONTROL_SPEED_LEND 0x3 - -// TODO: SET AS TORQUE? -#define READ_CURRENT_BEND 0x4 -#define READ_CURRENT_LEND 0x5 - -#define READ_TEMP 0x6 -// Data Length Code 8 BYTES -#define RM_DLC 0x8 - -typedef struct { - uint16_t ecd; - int16_t speed_rpm; - int16_t current; - uint8_t temperature; - int16_t last_ecd; -} rmMotorFeedback; - -#endif /* RobomasterCanProtocol_H */ \ No newline at end of file diff --git a/app/src/remote_control.c b/app/src/remote_control.c new file mode 100644 index 0000000..a815b02 --- /dev/null +++ b/app/src/remote_control.c @@ -0,0 +1,40 @@ +#include +#include +#include + +#include "CommunicationStructs.h" +#include "cmsis_os.h" +#include "usart.h" + +control_data control1; +UART_HandleTypeDef huart1; +uint8_t UART1_rxBuffer; + +osMessageQDef(remoteQueue, 16, unsigned int); + +void UART_Init(void) {} + +void sbus_translate(control_data* _control, uint8_t* _buffer) { + _control->joystickAx = ((_buffer[1] >> 3) | (_buffer[2] << 5)) & 0xff; + _control->joystickAy = ((_buffer[2] >> 6) | (_buffer[3] << 2) | (_buffer[4] << 10)) & 0xff; + _control->joystickBx = ((_buffer[4] >> 1) | (_buffer[5] << 7)) & 0x0ff; + _control->joystickBy = ((_buffer[5] >> 1) | (_buffer[6] << 7)) & 0x0ff; + _control->knobA = ((_buffer[6] >> 7) | (_buffer[7] << 1)) & 0xff; + _control->knobB = ((_buffer[7]) | (_buffer[8])) & 0xff; + _control->switchA = (_buffer[9] & 0x07) >> 2; + _control->switchB = (_buffer[10] & 0x20) >> 5; + _control->switchC = ((_buffer[12] >> 3) & 0x03); + _control->switchD = ((_buffer[13] & 0x08) >> 3); +} + +void UART_Task(void* argument) { + HAL_UART_Receive_IT(&huart1, &UART1_rxBuffer, 12); + osPoolDef(remote_pool, 16, control_data); + remoteQueue = osMessageCreate(osMessageQ(remoteQueue), NULL); + for (;;) { + sbus_translate(&control1, &UART1_rxBuffer); + osMessagePut(remoteQueue, (unsigned int)&control1, osWaitForever); + } +} + +void HAL_UART_RxCpltCallback(UART_HandleTypeDef* uart) { HAL_UART_Receive_IT(&huart1, &UART1_rxBuffer, 12); } diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..e69de29