Robot
The embedded systems for UTRA's autonomous humanoid soccer-playing robot
tx_helper.cpp
1 
9 /********************************* Includes **********************************/
10 #include "tx_helper.h"
11 #include "cmsis_os.h"
12 #include "UART_Handler.h"
13 #include "DynamixelProtocolV1.h"
14 #include "robotState.h"
15 #include "Communication.h"
16 #include "MPU6050.h"
17 #include "Notification.h"
18 
19 /********************************** Externs ***********************************/
20 extern osThreadId PCUARTHandle;
21 extern osThreadId TXQueueHandle;
22 
23 /***************************** Private Variables *****************************/
24 static TXData_t receivedData;
25 static Dynamixel_HandleTypeDef* motorPtr = nullptr;
26 static IMUStruct* imuPtr = nullptr;
27 static char* const pIMUXGyroData = &robotState.msg[ROBOT_STATE_MPU_DATA_OFFSET];
28 
29 static HAL_StatusTypeDef status;
30 static uint32_t notification;
31 static uint32_t dataReadyFlags = 0; // Bits in this are set based on which sensor data is ready
32 
33 static uint32_t NOTIFICATION_MASK = 0x80000000;
34 
35 /******************************** Functions **********************************/
36 /* StartTxTask Helper Functions */
37 /* */
38 /* */
39 /* */
40 /* */
41 /* */
42 /* */
43 /*****************************************************************************/
60  for (uint8_t i = 1; i <= 12; i++) {
61  NOTIFICATION_MASK |= (1 << i);
62  }
63 }
64 
73  while ((dataReadyFlags & NOTIFICATION_MASK) != NOTIFICATION_MASK) {
74  while (xQueueReceive(TXQueueHandle, &receivedData, portMAX_DELAY) != pdTRUE);
75 
76  switch (receivedData.eDataType) {
77  case eMotorData:
78  motorPtr = (Dynamixel_HandleTypeDef*) receivedData.pData;
79 
80  if (motorPtr == NULL) {
81  break;
82  }
83 
84  // Validate data and store it in robotState
85  if (motorPtr->_ID <= NUM_MOTORS) {
86  // Copy sensor data for this motor into its section of robotState.msg
87  memcpy(&robotState.msg[4 * (motorPtr->_ID - 1)],
88  &(motorPtr->_lastPosition), sizeof(float));
89 
90  // Set flag indicating the motor with this id has reported in with position data
91  dataReadyFlags |= (1 << motorPtr->_ID);
92  }
93  break;
94  case eIMUData:
95  imuPtr = (IMUStruct*)receivedData.pData;
96 
97  if(imuPtr == NULL){ break; }
98 
99  // Copy sensor data into the IMU data section of robotState.msg
100  memcpy(pIMUXGyroData, (&imuPtr->_x_Gyro), sizeof(IMUStruct));
101 
102  // Set flag indicating IMU data has reported in
103  dataReadyFlags |= 0x80000000;
104  break;
105  default:
106  break;
107  }
108  }
109  dataReadyFlags = 0; // Clear all flags
110 }
111 
124  do {
125  xSemaphoreTake(PCUARTHandle, 1);
126  status = HAL_UART_Transmit_DMA(&huart5, (uint8_t*) &robotState,
127  sizeof(RobotState));
128  xSemaphoreGive(PCUARTHandle);
129  } while (status != HAL_OK);
130 }
131 
140  do {
141  xTaskNotifyWait(0, NOTIFIED_FROM_TX_ISR, &notification, portMAX_DELAY);
142  } while ((notification & NOTIFIED_FROM_TX_ISR) != NOTIFIED_FROM_TX_ISR);
143 }
144 
145 /*****************************************************************************/
149 /* end - TxHelperFunctions */
This file defines resources used throughout various parts of the system that engage in non-blocking I...
#define ROBOT_STATE_MPU_DATA_OFFSET
Definition: robotState.h:32
Organizes all the information relevant to a motor.
This is the data structure copied into the sensor queue, and read by the thread that sends data to th...
Definition: UART_Handler.h:103
Defines the RobotState data structure used in communication with the high-level software. RobotState is the data structure sent from the MCU to the PC; it contains sensor data.
osMessageQId TXQueueHandle
Definition: freertos.cpp:129
Header for the helper file used to aid StartTxTask() in freertos.cpp.
The data structure which represents the data of the IMU, which is sent in queues between tasks...
Definition: UART_Handler.h:83
void shiftNotificationMask(void)
Shifts bits of NOTIFICATION_MASK.
Definition: tx_helper.cpp:59
void transmitStatusFromPC(void)
Makes calls to the UART module responsible for PC communication atomic.
Definition: tx_helper.cpp:123
void * pData
Definition: UART_Handler.h:105
#define NUM_MOTORS
Used to determine buffer sizes. Also used in application-level code.
Data structure sent from the MCU to the PC. Contains sensor data.
Definition: robotState.h:22
#define NOTIFIED_FROM_TX_ISR
Definition: Notification.h:35
Header for top-level communication module.
void copySensorDataToSend(void)
Validates and copies sensor data to transmit.
Definition: tx_helper.cpp:72
Header file for the UART event processor, which is called whenever there are commands that need to be...
eTXData_t eDataType
Definition: UART_Handler.h:104
RobotState robotState
Definition: Communication.c:38
float _x_Gyro
Definition: UART_Handler.h:84
Header code for the MPU6050 class.
void waitForNotificationTX(void)
Waits until notified from ISR.
Definition: tx_helper.cpp:139
char msg[80]
Definition: robotState.h:26
Common header code for the AX12A library and MX28 library. It is generic in that any Dynamixel actuat...