|
@@ -11,6 +11,7 @@
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
+#include <math.h>
|
|
|
|
|
|
#include "adc_buffers.h"
|
|
|
#include "interprocess_data.h"
|
|
@@ -19,6 +20,7 @@
|
|
|
#include "mock_tasks.h"
|
|
|
#include "peripherial.h"
|
|
|
#include "uart_tasks.h"
|
|
|
+#include "position_task.h"
|
|
|
|
|
|
enum SerialReceiverStates { srWaitForHeader, srCheckCrc, srRecieveData, srExecuteCmd, srFail, srFinish, srLast };
|
|
|
|
|
@@ -46,21 +48,12 @@ uint8_t uart8RxBuffer[UART8_RX_BUFF_SIZE] = { 0 };
|
|
|
uint8_t uart8TxBuffer[UART8_TX_BUFF_SIZE] = { 0 };
|
|
|
uint8_t uart8TaskFrameData[INPUT_DATA_BUFF_SIZE] = { 0 };
|
|
|
|
|
|
-uint8_t boardToUartNumberMap[SLAVES_COUNT] = { /*1*/ 8, 3, 6, 2 };
|
|
|
-
|
|
|
UartTaskData uart1TaskData = { 0 }; // Board 1
|
|
|
-UartTaskData uart3TaskData = { 0 }; // Board 2
|
|
|
-UartTaskData uart6TaskData = { 0 }; // Board 3
|
|
|
-UartTaskData uart2TaskData = { 0 }; // Board 4
|
|
|
UartTaskData uart8TaskData = { 0 }; // Debug
|
|
|
|
|
|
-UartTaskData* uartTasks[] = { &uart8TaskData, NULL };
|
|
|
-
|
|
|
uint8_t outputDataBuffer[OUTPUT_DATA_BUFF_SIZE];
|
|
|
uint16_t outputDataBufferPos = 0;
|
|
|
|
|
|
-uint32_t slaveLastSeen[SLAVES_COUNT] = { 0 };
|
|
|
-
|
|
|
extern RNG_HandleTypeDef hrng;
|
|
|
|
|
|
void UartTasksInit (void) {
|
|
@@ -75,7 +68,22 @@ void UartTasksInit (void) {
|
|
|
uart1TaskData.processDataCb = Uart1ReceivedDataProcessCallback;
|
|
|
uart1TaskData.processRxDataMsgBuffer = NULL;
|
|
|
|
|
|
+ uart8TaskData.uartRxBuffer = uart8RxBuffer;
|
|
|
+ uart8TaskData.uartRxBufferLen = UART8_RX_BUFF_SIZE;
|
|
|
+ uart8TaskData.uartTxBuffer = uart8TxBuffer;
|
|
|
+ uart8TaskData.uartRxBufferLen = UART8_TX_BUFF_SIZE;
|
|
|
+ uart8TaskData.frameData = uart8TaskFrameData;
|
|
|
+ uart8TaskData.frameDataLen = UART8_RX_BUFF_SIZE;
|
|
|
+ uart8TaskData.huart = &huart8;
|
|
|
+ uart8TaskData.uartNumber = 8;
|
|
|
+ uart8TaskData.processDataCb = Uart8ReceivedDataProcessCallback;
|
|
|
+ uart8TaskData.processRxDataMsgBuffer = NULL;
|
|
|
+
|
|
|
+#ifdef USE_UART8_INSTEAD_UART1
|
|
|
+ UartTaskCreate (&uart8TaskData);
|
|
|
+#else
|
|
|
UartTaskCreate (&uart1TaskData);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
void UartTaskCreate (UartTaskData* uartTaskData) {
|
|
@@ -85,26 +93,6 @@ void UartTaskCreate (UartTaskData* uartTaskData) {
|
|
|
uartTaskData->uartRecieveTaskHandle = osThreadNew (UartRxTask, uartTaskData, &osThreadAttrRxUart);
|
|
|
}
|
|
|
|
|
|
-void Uart8TasksInit (void) {
|
|
|
- osThreadAttr_t osThreadAttrRxUart = { 0 };
|
|
|
-
|
|
|
- uart8TaskData.processDataCb = Uart8ReceivedDataProcessCallback;
|
|
|
-
|
|
|
- osThreadAttrRxUart.name = "os_thread_uart8_rx";
|
|
|
- osThreadAttrRxUart.stack_size = configMINIMAL_STACK_SIZE * 2;
|
|
|
- osThreadAttrRxUart.priority = (osPriority_t)osPriorityHigh;
|
|
|
-
|
|
|
- uart8TaskData.uartRxBuffer = uart8RxBuffer;
|
|
|
- uart8TaskData.uartRxBufferLen = UART8_RX_BUFF_SIZE;
|
|
|
- uart8TaskData.uartTxBuffer = uart8TxBuffer;
|
|
|
- uart8TaskData.uartRxBufferLen = UART8_TX_BUFF_SIZE;
|
|
|
- uart8TaskData.frameData = uart8TaskFrameData;
|
|
|
- uart8TaskData.frameDataLen = UART8_RX_BUFF_SIZE;
|
|
|
- uart8TaskData.huart = &huart8;
|
|
|
- uart8TaskData.uartNumber = 8;
|
|
|
- uart8TaskData.uartRecieveTaskHandle = osThreadNew (UartRxTask, &uart8TaskData, &osThreadAttrRxUart);
|
|
|
-}
|
|
|
-
|
|
|
void HAL_UART_RxCpltCallback (UART_HandleTypeDef* huart) {
|
|
|
}
|
|
|
|
|
@@ -161,7 +149,9 @@ void UartRxTask (void* argument) {
|
|
|
} else {
|
|
|
if (frameTimeout == pdFALSE) {
|
|
|
proceed = pdTRUE;
|
|
|
+#ifdef SERIAL_PROTOCOL_DBG
|
|
|
printf ("Uart%d: RX bytes received: %ld\n", uartTaskData->uartNumber, bytesRec);
|
|
|
+#endif
|
|
|
} else {
|
|
|
if (uartTaskData->huart->RxState == HAL_UART_STATE_READY) {
|
|
|
HAL_UARTEx_ReceiveToIdle_IT (uartTaskData->huart, uartTaskData->uartRxBuffer, uartTaskData->uartRxBufferLen);
|
|
@@ -216,7 +206,9 @@ void UartRxTask (void* argument) {
|
|
|
osMutexRelease (uartTaskData->rxDataBufferMutex);
|
|
|
crcPass = frameCrc == crc;
|
|
|
if (crcPass) {
|
|
|
+#ifdef SERIAL_PROTOCOL_DBG
|
|
|
printf ("Uart%d: Frame CRC PASS\n", uartTaskData->uartNumber);
|
|
|
+#endif
|
|
|
receverState = srExecuteCmd;
|
|
|
} else {
|
|
|
receverState = srFail;
|
|
@@ -244,17 +236,23 @@ void UartRxTask (void* argument) {
|
|
|
dataToSend = 0;
|
|
|
if ((frameTimeout == pdTRUE) && (frameBytesCount > 2)) {
|
|
|
dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData.frameHeader.frameId, spFrameData.frameHeader.frameCommand, spTimeout, NULL, 0);
|
|
|
+#ifdef SERIAL_PROTOCOL_DBG
|
|
|
printf ("Uart%d: RX data receiver timeout!\n", uartTaskData->uartNumber);
|
|
|
+#endif
|
|
|
} else if (!crcPass) {
|
|
|
dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData.frameHeader.frameId, spFrameData.frameHeader.frameCommand, spCrcFail, NULL, 0);
|
|
|
+#ifdef SERIAL_PROTOCOL_DBG
|
|
|
printf ("Uart%d: Frame CRC FAIL\n", uartTaskData->uartNumber);
|
|
|
+#endif
|
|
|
} else {
|
|
|
dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData.frameHeader.frameId, spFrameData.frameHeader.frameCommand, spInternalError, NULL, 0);
|
|
|
}
|
|
|
if (dataToSend > 0) {
|
|
|
HAL_UART_Transmit_IT (uartTaskData->huart, uartTaskData->uartTxBuffer, dataToSend);
|
|
|
}
|
|
|
+#ifdef SERIAL_PROTOCOL_DBG
|
|
|
printf ("Uart%d: TX bytes sent: %d\n", dataToSend, uartTaskData->uartNumber);
|
|
|
+#endif
|
|
|
receverState = srFinish;
|
|
|
break;
|
|
|
case srFinish:
|
|
@@ -329,6 +327,10 @@ void Uart1ReceivedDataProcessCallback (void* arg, SerialProtocolFrameData* spFra
|
|
|
uint8_t comparatorOutput = HAL_COMP_GetOutputLevel (&hcomp1) == COMP_OUTPUT_LEVEL_HIGH ? 1 : 0;
|
|
|
sensorsInfo.powerSupplyFailMask = ~((comparatorOutput << 1) | HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_3)) & 0x01;
|
|
|
WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.powerSupplyFailMask, sizeof (uint8_t));
|
|
|
+ WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.currentXPosition, sizeof (float));
|
|
|
+ WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.currentYPosition, sizeof (float));
|
|
|
+ WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.positionXWeak, sizeof (uint8_t));
|
|
|
+ WriteDataToBuffer (outputDataBuffer, &outputDataBufferPos, &sensorsInfo.positionYWeak, sizeof (uint8_t));
|
|
|
osMutexRelease (sensorsInfoMutex);
|
|
|
respStatus = spOK;
|
|
|
} else {
|
|
@@ -365,7 +367,7 @@ void Uart1ReceivedDataProcessCallback (void* arg, SerialProtocolFrameData* spFra
|
|
|
ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorXTimerPeriod);
|
|
|
if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
|
|
|
motorXStatus =
|
|
|
- motorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_1, TIM_CHANNEL_2, motorXTimerHandle, motorXPWMPulse, motorXTimerPeriod, sensorsInfo.limitXSwitchUp, sensorsInfo.limitXSwitchDown);
|
|
|
+ MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_1, TIM_CHANNEL_2, motorXTimerHandle, motorXPWMPulse, motorXTimerPeriod, sensorsInfo.limitXSwitchUp, sensorsInfo.limitXSwitchDown);
|
|
|
sensorsInfo.motorXStatus = motorXStatus;
|
|
|
if (motorXStatus == 1) {
|
|
|
sensorsInfo.motorXPeakCurrent = 0.0;
|
|
@@ -384,7 +386,7 @@ void Uart1ReceivedDataProcessCallback (void* arg, SerialProtocolFrameData* spFra
|
|
|
ReadWordFromBufer (spFrameData->dataBuffer, &inputDataBufferPos, (uint32_t*)&motorYTimerPeriod);
|
|
|
if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
|
|
|
motorYStatus =
|
|
|
- motorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_3, TIM_CHANNEL_4, motorYTimerHandle, motorYPWMPulse, motorYTimerPeriod, sensorsInfo.limitYSwitchUp, sensorsInfo.limitYSwitchDown);
|
|
|
+ MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_3, TIM_CHANNEL_4, motorYTimerHandle, motorYPWMPulse, motorYTimerPeriod, sensorsInfo.limitYSwitchUp, sensorsInfo.limitYSwitchDown);
|
|
|
sensorsInfo.motorYStatus = motorYStatus;
|
|
|
if (motorYStatus == 1) {
|
|
|
sensorsInfo.motorYPeakCurrent = 0.0;
|
|
@@ -509,13 +511,55 @@ void Uart1ReceivedDataProcessCallback (void* arg, SerialProtocolFrameData* spFra
|
|
|
__disable_irq();
|
|
|
NVIC_SystemReset();
|
|
|
break;
|
|
|
+ case spSetPositonX:
|
|
|
+ PositionControlTaskData posXData = { 0 };
|
|
|
+ if (positionXControlTaskInitArg.positionSettingQueue != NULL)
|
|
|
+ {
|
|
|
+ float posXPercent = 0;
|
|
|
+ ReadFloatFromBuffer(spFrameData->dataBuffer, &inputDataBufferPos, &posXPercent);
|
|
|
+ float posXDegress = MAX_X_AXE_ANGLE * posXPercent * 0.01;
|
|
|
+ float angleDelta = 360 / ENCODER_X_IMP_PER_TURN;
|
|
|
+ float rest = fmodf(posXDegress, angleDelta);
|
|
|
+ if ( rest > (angleDelta/2))
|
|
|
+ {
|
|
|
+ posXData.positionSettingValue = 100 * (posXDegress - rest + angleDelta) / MAX_X_AXE_ANGLE;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ posXData.positionSettingValue = 100 * (posXDegress - rest) / MAX_X_AXE_ANGLE;
|
|
|
+ }
|
|
|
+ osMessageQueuePut(positionXControlTaskInitArg.positionSettingQueue, &posXData, 0, 0);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case spSetPositonY:
|
|
|
+ PositionControlTaskData posYData = { 0 };
|
|
|
+ if (positionYControlTaskInitArg.positionSettingQueue != NULL)
|
|
|
+ {
|
|
|
+ float posYPercent = 0;
|
|
|
+ ReadFloatFromBuffer(spFrameData->dataBuffer, &inputDataBufferPos, &posYPercent);
|
|
|
+ float posYDegress = MAX_Y_AXE_ANGLE * posYPercent * 0.01;
|
|
|
+ float angleDelta = 360 / ENCODER_Y_IMP_PER_TURN;
|
|
|
+ float rest = fmodf(posYDegress, angleDelta);
|
|
|
+ if ( rest > (angleDelta/2))
|
|
|
+ {
|
|
|
+ posYData.positionSettingValue = 100 * (posYDegress - rest + angleDelta) / MAX_Y_AXE_ANGLE;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ posYData.positionSettingValue = 100 * (posYDegress - rest) / MAX_Y_AXE_ANGLE;
|
|
|
+ }
|
|
|
+ osMessageQueuePut(positionYControlTaskInitArg.positionSettingQueue, &posYData, 0, 0);
|
|
|
+ }
|
|
|
+ break;
|
|
|
default: respStatus = spUnknownCommand; break;
|
|
|
}
|
|
|
dataToSend = PrepareRespFrame (uartTaskData->uartTxBuffer, spFrameData->frameHeader.frameId, spFrameData->frameHeader.frameCommand, respStatus, outputDataBuffer, outputDataBufferPos);
|
|
|
if (dataToSend > 0) {
|
|
|
HAL_UART_Transmit_IT (uartTaskData->huart, uartTaskData->uartTxBuffer, dataToSend);
|
|
|
}
|
|
|
+#ifdef SERIAL_PROTOCOL_DBG
|
|
|
printf ("Uart%d: TX bytes sent: %d\n", uartTaskData->uartNumber, dataToSend);
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
|