Robot
The embedded systems for UTRA's autonomous humanoid soccer-playing robot
rx_helper.cpp
1 
14 /********************************* Includes **********************************/
15 #include "rx_helper.h"
16 #include "Notification.h"
17 #include "cmsis_os.h"
18 #include "robotGoal.h"
19 #include "robotState.h"
20 #include "Communication.h"
21 #include "usart.h"
22 
23 /****************************** Public Variables *****************************/
24 extern osThreadId CommandTaskHandle;
25 extern osThreadId IMUTaskHandle;
26 extern osThreadId PCUARTHandle;
27 
28 /***************************** Private Variables *****************************/
29 static uint8_t robotGoalData[sizeof(RobotGoal)];
30 static uint8_t *robotGoalDataPtr;
31 static uint8_t buffRx[92];
32 static uint8_t startSeqCount;
33 static uint8_t totalBytesRead;
34 
35 static HAL_StatusTypeDef status;
36 static uint32_t notification;
37 
38 /******************************** Functions ********************************/
39 /*****************************************************************************/
40 /* StartRxTask Helper Functions */
41 /* */
42 /* */
43 /* */
44 /* */
45 /* */
46 /* */
47 /*****************************************************************************/
65 void initializeVars(void) {
66  //sending
67  robotGoal.id = 0;
68  robotGoalDataPtr = robotGoalData;
69  startSeqCount = 0;
70  totalBytesRead = 0;
71  //receiving
72  robotState.id = 0;
73  robotState.start_seq = UINT32_MAX;
74  robotState.end_seq = 0;
75 }
76 
82 void initiateDMATransfer(void) {
83  HAL_UART_Receive_DMA(&huart5, (uint8_t*) buffRx, sizeof(buffRx));
84 }
85 
96  do {
97  xTaskNotifyWait(0, NOTIFIED_FROM_RX_ISR, &notification, portMAX_DELAY);
98  } while ((notification & NOTIFIED_FROM_RX_ISR) != NOTIFIED_FROM_RX_ISR);
99 }
100 
112 void updateStatusToPC(void) {
113  do {
114  xSemaphoreTake(PCUARTHandle, 1);
115  status = HAL_UART_Receive_DMA(&huart5, (uint8_t*) buffRx,
116  sizeof(buffRx));
117  xSemaphoreGive(PCUARTHandle);
118  } while (status != HAL_OK);
119 }
120 
129 void receiveDataBuffer(void) {
130  for (uint8_t i = 0; i < sizeof(buffRx); i++) {
131  if (startSeqCount == 4) {
132  // This control block is entered when the header sequence of
133  // 0xFFFFFFFF has been received; thus we know the data we
134  // receive will be in tact
135 
136  *robotGoalDataPtr = buffRx[i];
137  robotGoalDataPtr++;
138  totalBytesRead++;
139 
140  if (totalBytesRead == sizeof(RobotGoal)) {
141  // If, after the last couple of receive interrupts, we have
142  // received sizeof(RobotGoal) bytes, then we copy the data
143  // buffer into the robotGoal structure and wake the control
144  // thread to distribute states to each actuator
145  memcpy(&robotGoal, robotGoalData, sizeof(RobotGoal));
147 
148  // Reset the variables to help with reception of a RobotGoal
149  robotGoalDataPtr = robotGoalData;
150  startSeqCount = 0;
151  totalBytesRead = 0;
152 
153  xTaskNotify(CommandTaskHandle, NOTIFIED_FROM_TASK, eSetBits);// Wake control task
154  xTaskNotify(IMUTaskHandle, NOTIFIED_FROM_TASK, eSetBits);// Wake MPU task
155  continue;
156  }
157  } else {
158  // This control block is used to verify that the data header is in tact
159  if (buffRx[i] == 0xFF) {
160  startSeqCount++;
161  } else {
162  startSeqCount = 0;
163  }
164  }
165  }
166 
167 }
168 
172 /* end - RxHelperFunctions */
173 
177 /* end Helpers */
178 
uint32_t id
Definition: robotGoal.h:26
This file defines resources used throughout various parts of the system that engage in non-blocking I...
void waitForNotificationRX(void)
Waits until notified from ISR.
Definition: rx_helper.cpp:95
void initializeVars(void)
Initializes the private variables for StartRxTask.
Definition: rx_helper.cpp:65
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.
RobotGoal robotGoal
Definition: Communication.c:30
void initiateDMATransfer(void)
Initiates DMA receive transfer using DMA.
Definition: rx_helper.cpp:82
uint32_t id
Definition: robotState.h:25
Data structure sent from the PC to the MCU. Contains "goal" motor positions.
Definition: robotGoal.h:25
void updateStatusToPC(void)
Makes calls to the UART module responsible for PC communication atomic.
Definition: rx_helper.cpp:112
uint32_t end_seq
Definition: robotState.h:28
Defines the RobotGoal data structure used in communication with the high-level software. RobotGoal is the data structure sent from the PC to the MCU; it contains motor trajectories.
#define NOTIFIED_FROM_RX_ISR
Definition: Notification.h:36
Header for top-level communication module.
void receiveDataBuffer(void)
Reads the received data buffer.
Definition: rx_helper.cpp:129
struct robot_goal RobotGoal
Data structure sent from the PC to the MCU. Contains "goal" motor positions.
#define NOTIFIED_FROM_TASK
Definition: Notification.h:37
RobotState robotState
Definition: Communication.c:38
uint32_t start_seq
Definition: robotState.h:23
Header for the helper file used to aid StartRXTask() in freertos.cpp.