|
@@ -6,9 +6,10 @@
|
|
|
*/
|
|
|
|
|
|
#include <math.h>
|
|
|
+#include <stdio.h>
|
|
|
|
|
|
-#include "meas_tasks.h"
|
|
|
#include "adc_buffers.h"
|
|
|
+#include "meas_tasks.h"
|
|
|
#include "measurements.h"
|
|
|
#include "node-red-config.h"
|
|
|
#include "peripherial.h"
|
|
@@ -30,13 +31,12 @@ osThreadId_t adc1MeasTaskHandle = NULL;
|
|
|
osThreadId_t adc2MeasTaskHandle = NULL;
|
|
|
osThreadId_t adc3MeasTaskHandle = NULL;
|
|
|
osThreadId_t limiterSwitchTaskHandle = NULL;
|
|
|
-osThreadId_t encoderTaskHandle = NULL;
|
|
|
+osThreadId_t encoderXTaskHandle = NULL;
|
|
|
+osThreadId_t encoderYTaskHandle = NULL;
|
|
|
|
|
|
-osMessageQueueId_t adc1MeasDataQueue = NULL;
|
|
|
-osMessageQueueId_t adc2MeasDataQueue = NULL;
|
|
|
-osMessageQueueId_t adc3MeasDataQueue = NULL;
|
|
|
-osMessageQueueId_t limiterSwitchDataQueue = NULL;
|
|
|
-osMessageQueueId_t encoderDataQueue = NULL;
|
|
|
+osMessageQueueId_t adc1MeasDataQueue = NULL;
|
|
|
+osMessageQueueId_t adc2MeasDataQueue = NULL;
|
|
|
+osMessageQueueId_t adc3MeasDataQueue = NULL;
|
|
|
|
|
|
osMutexId_t vRefmVMutex;
|
|
|
osMutexId_t resMeasurementsMutex;
|
|
@@ -45,16 +45,17 @@ osMutexId_t ILxRefMutex;
|
|
|
|
|
|
volatile uint32_t vRefmV = 3000;
|
|
|
|
|
|
-RESMeasurements resMeasurements = { 0 };
|
|
|
-SesnorsInfo sensorsInfo = { 0 };
|
|
|
+RESMeasurements resMeasurements __attribute__ ((aligned (32))) = { 0 };
|
|
|
+SesnorsInfo sensorsInfo __attribute__ ((aligned (32))) = { 0 };
|
|
|
|
|
|
-uint16_t ILxRef[CURRENTS_COUNT] = { 0 };
|
|
|
+uint16_t ILxRef[CURRENTS_COUNT] __attribute__ ((aligned (32))) = { 0 };
|
|
|
+EncoderTaskArg encoderXTaskArg __attribute__ ((aligned (32))) = { 0 };
|
|
|
+EncoderTaskArg encoderYTaskArg __attribute__ ((aligned (32))) = { 0 };
|
|
|
|
|
|
extern TIM_HandleTypeDef htim3;
|
|
|
extern TIM_OC_InitTypeDef motorXYTimerConfigOC;
|
|
|
extern osTimerId_t motorXTimerHandle;
|
|
|
extern osTimerId_t motorYTimerHandle;
|
|
|
-//extern osMutexId_t positionSettingMutex;
|
|
|
|
|
|
|
|
|
void MeasTasksInit (void) {
|
|
@@ -83,19 +84,29 @@ void MeasTasksInit (void) {
|
|
|
adc2MeasTaskHandle = osThreadNew (ADC2MeasTask, NULL, &osThreadAttradc2MeasTask);
|
|
|
adc3MeasTaskHandle = osThreadNew (ADC3MeasTask, NULL, &osThreadAttradc3MeasTask);
|
|
|
|
|
|
- limiterSwitchDataQueue = osMessageQueueNew (8, sizeof (LimiterSwitchData), NULL);
|
|
|
-
|
|
|
osThreadAttr_t osThreadAttradc1LimiterSwitchTask = { 0 };
|
|
|
osThreadAttradc1LimiterSwitchTask.stack_size = configMINIMAL_STACK_SIZE * 2;
|
|
|
osThreadAttradc1LimiterSwitchTask.priority = (osPriority_t)osPriorityNormal;
|
|
|
limiterSwitchTaskHandle = osThreadNew (LimiterSwitchTask, NULL, &osThreadAttradc1LimiterSwitchTask);
|
|
|
|
|
|
- encoderDataQueue = osMessageQueueNew (16, sizeof (EncoderData), NULL);
|
|
|
+ encoderXTaskArg.dbgLed = DBG_LED2;
|
|
|
+ encoderXTaskArg.pvEncoder = &(sensorsInfo.pvEncoderX);
|
|
|
+ encoderXTaskArg.currentPosition = &(sensorsInfo.currentXPosition);
|
|
|
+ osMessageQueueAttr_t encoderMsgQueueAttr = { 0 };
|
|
|
+ encoderXTaskArg.dataQueue = osMessageQueueNew (16, sizeof (uint32_t), &encoderMsgQueueAttr);
|
|
|
+ encoderXTaskArg.initPinStates = ((HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_15) << 1) | HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_14)) & 0x3;
|
|
|
+
|
|
|
+ encoderYTaskArg.dbgLed = DBG_LED3;
|
|
|
+ encoderYTaskArg.pvEncoder = &(sensorsInfo.pvEncoderY);
|
|
|
+ encoderYTaskArg.currentPosition = &(sensorsInfo.currentYPosition);
|
|
|
+ encoderYTaskArg.dataQueue = osMessageQueueNew (16, sizeof (uint32_t), &encoderMsgQueueAttr);
|
|
|
+ encoderYTaskArg.initPinStates = ((HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_11) << 1) | HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10)) & 0x3;
|
|
|
|
|
|
osThreadAttr_t osThreadAttrEncoderTask = { 0 };
|
|
|
osThreadAttrEncoderTask.stack_size = configMINIMAL_STACK_SIZE * 2;
|
|
|
- osThreadAttrEncoderTask.priority = (osPriority_t)osPriorityNormal;
|
|
|
- encoderTaskHandle = osThreadNew (EncoderTask, encoderDataQueue, &osThreadAttrEncoderTask);
|
|
|
+ osThreadAttrEncoderTask.priority = (osPriority_t)osPriorityRealtime;
|
|
|
+ encoderXTaskHandle = osThreadNew (EncoderTask, &encoderXTaskArg, &osThreadAttrEncoderTask);
|
|
|
+ encoderYTaskHandle = osThreadNew (EncoderTask, &encoderYTaskArg, &osThreadAttrEncoderTask);
|
|
|
}
|
|
|
|
|
|
void ADC1MeasTask (void* arg) {
|
|
@@ -251,121 +262,122 @@ void ADC3MeasTask (void* arg) {
|
|
|
}
|
|
|
|
|
|
void LimiterSwitchTask (void* arg) {
|
|
|
- LimiterSwitchData limiterSwitchData = { 0 };
|
|
|
- limiterSwitchData.gpioPin = GPIO_PIN_8;
|
|
|
- for (uint8_t i = 0; i < 6; i++) {
|
|
|
- limiterSwitchData.pinState = HAL_GPIO_ReadPin (GPIOD, limiterSwitchData.gpioPin);
|
|
|
- osMessageQueuePut (limiterSwitchDataQueue, &limiterSwitchData, 0, 0);
|
|
|
- limiterSwitchData.gpioPin = limiterSwitchData.gpioPin << 1;
|
|
|
- if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
|
|
|
- sensorsInfo.positionXWeak = 1;
|
|
|
- sensorsInfo.positionYWeak = 1;
|
|
|
- osMutexRelease (sensorsInfoMutex);
|
|
|
- }
|
|
|
+ uint8_t limitXSwitchDownPrevState = 0;
|
|
|
+ uint8_t limitXSwitchCenterPrevState = 0;
|
|
|
+ uint8_t limitXSwitchUpPrevState = 0;
|
|
|
+ uint8_t limitYSwitchDownPrevState = 0;
|
|
|
+ uint8_t limitYSwitchCenterPrevState = 0;
|
|
|
+ uint8_t limitYSwitchUpPrevState = 0;
|
|
|
+ uint8_t pinStates = 0;
|
|
|
+ uint8_t limiterXTriggered = 0;
|
|
|
+ uint8_t limiterYTriggered = 0;
|
|
|
+ if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
|
|
|
+ sensorsInfo.positionXWeak = 1;
|
|
|
+ sensorsInfo.positionYWeak = 1;
|
|
|
+ osMutexRelease (sensorsInfoMutex);
|
|
|
}
|
|
|
while (pdTRUE) {
|
|
|
- osMessageQueueGet (limiterSwitchDataQueue, &limiterSwitchData, 0, osWaitForever);
|
|
|
+ osDelay (pdMS_TO_TICKS (100));
|
|
|
if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
|
|
|
- switch (limiterSwitchData.gpioPin) {
|
|
|
- case GPIO_PIN_8:
|
|
|
- sensorsInfo.limitYSwitchCenter = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
|
|
|
- if (sensorsInfo.limitYSwitchCenter == 1)
|
|
|
- {
|
|
|
- sensorsInfo.currentYPosition = AXE_Y_MIDDLE_VALUE;
|
|
|
- sensorsInfo.positionYWeak = 0;
|
|
|
- }
|
|
|
- break;
|
|
|
- case GPIO_PIN_9:
|
|
|
- sensorsInfo.limitYSwitchDown = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
|
|
|
- if (sensorsInfo.limitYSwitchDown == 1)
|
|
|
- {
|
|
|
- sensorsInfo.currentYPosition = 0;
|
|
|
- sensorsInfo.positionYWeak = 0;
|
|
|
- }
|
|
|
- break;
|
|
|
- case GPIO_PIN_10:
|
|
|
- sensorsInfo.limitXSwitchCenter = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
|
|
|
- if (sensorsInfo.limitXSwitchCenter == 1)
|
|
|
- {
|
|
|
- sensorsInfo.currentXPosition = AXE_X_MIDDLE_VALUE;
|
|
|
- sensorsInfo.positionXWeak = 0;
|
|
|
- }
|
|
|
- break;
|
|
|
- case GPIO_PIN_11:
|
|
|
- sensorsInfo.limitYSwitchUp = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
|
|
|
- if (sensorsInfo.limitYSwitchUp == 1)
|
|
|
- {
|
|
|
- sensorsInfo.currentYPosition = 100;
|
|
|
- sensorsInfo.positionYWeak = 0;
|
|
|
- }
|
|
|
- break;
|
|
|
- case GPIO_PIN_12:
|
|
|
- sensorsInfo.limitXSwitchUp = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
|
|
|
- if (sensorsInfo.limitXSwitchUp == 1)
|
|
|
- {
|
|
|
- sensorsInfo.currentXPosition = 100;
|
|
|
- sensorsInfo.positionXWeak = 0;
|
|
|
- }
|
|
|
- break;
|
|
|
- case GPIO_PIN_13:
|
|
|
- sensorsInfo.limitXSwitchDown = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
|
|
|
- if (sensorsInfo.limitXSwitchDown == 1)
|
|
|
- {
|
|
|
- sensorsInfo.currentXPosition = 0;
|
|
|
- sensorsInfo.positionXWeak = 0;
|
|
|
- }
|
|
|
- break;
|
|
|
- default: break;
|
|
|
+ sensorsInfo.limitXSwitchDown = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_13);
|
|
|
+ pinStates = (limitXSwitchDownPrevState << 1) | sensorsInfo.limitXSwitchDown;
|
|
|
+ if ((pinStates & 0x3) == 0x1) {
|
|
|
+ limiterXTriggered = 1;
|
|
|
+ sensorsInfo.currentXPosition = 0;
|
|
|
+ sensorsInfo.positionXWeak = 0;
|
|
|
+ }
|
|
|
+ limitXSwitchDownPrevState = sensorsInfo.limitXSwitchDown;
|
|
|
+
|
|
|
+ sensorsInfo.limitXSwitchUp = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_12);
|
|
|
+ pinStates = (limitXSwitchUpPrevState << 1) | sensorsInfo.limitXSwitchUp;
|
|
|
+ if ((pinStates & 0x3) == 0x1) {
|
|
|
+ limiterXTriggered = 1;
|
|
|
+ sensorsInfo.currentXPosition = 100;
|
|
|
+ sensorsInfo.positionXWeak = 0;
|
|
|
}
|
|
|
- if ((sensorsInfo.limitXSwitchDown == 1) || (sensorsInfo.limitXSwitchUp == 1)) {
|
|
|
+ limitXSwitchUpPrevState = sensorsInfo.limitXSwitchUp;
|
|
|
+
|
|
|
+ sensorsInfo.limitXSwitchCenter = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_10);
|
|
|
+ pinStates = (limitXSwitchCenterPrevState << 1) | sensorsInfo.limitXSwitchCenter;
|
|
|
+ if ((pinStates & 0x3) == 0x1) {
|
|
|
+ sensorsInfo.currentXPosition = AXE_X_MIDDLE_VALUE;
|
|
|
+ sensorsInfo.positionXWeak = 0;
|
|
|
+ }
|
|
|
+ limitXSwitchCenterPrevState = sensorsInfo.limitXSwitchCenter;
|
|
|
+
|
|
|
+ sensorsInfo.limitYSwitchDown = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_9);
|
|
|
+ pinStates = (limitYSwitchDownPrevState << 1) | sensorsInfo.limitYSwitchDown;
|
|
|
+ if ((pinStates & 0x3) == 0x1) {
|
|
|
+ limiterYTriggered = 1;
|
|
|
+ sensorsInfo.currentYPosition = 0;
|
|
|
+ sensorsInfo.positionYWeak = 0;
|
|
|
+ }
|
|
|
+ limitYSwitchDownPrevState = sensorsInfo.limitYSwitchDown;
|
|
|
+
|
|
|
+ sensorsInfo.limitYSwitchUp = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_11);
|
|
|
+ pinStates = (limitYSwitchUpPrevState << 1) | sensorsInfo.limitYSwitchUp;
|
|
|
+ if ((pinStates & 0x3) == 0x1) {
|
|
|
+ limiterYTriggered = 1;
|
|
|
+ sensorsInfo.currentYPosition = 100;
|
|
|
+ sensorsInfo.positionYWeak = 0;
|
|
|
+ }
|
|
|
+ limitYSwitchUpPrevState = sensorsInfo.limitYSwitchUp;
|
|
|
+
|
|
|
+ sensorsInfo.limitYSwitchCenter = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_8);
|
|
|
+ pinStates = (limitYSwitchCenterPrevState << 1) | sensorsInfo.limitYSwitchCenter;
|
|
|
+ if ((pinStates & 0x3) == 0x1) {
|
|
|
+ sensorsInfo.currentYPosition = AXE_Y_MIDDLE_VALUE;
|
|
|
+ sensorsInfo.positionYWeak = 0;
|
|
|
+ }
|
|
|
+ limitYSwitchCenterPrevState = sensorsInfo.limitYSwitchCenter;
|
|
|
+ if (((sensorsInfo.limitXSwitchDown == 1) || (sensorsInfo.limitXSwitchUp == 1)) && (limiterXTriggered == 1)) {
|
|
|
sensorsInfo.motorXStatus = MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_1, TIM_CHANNEL_2, motorXTimerHandle, 0, 0, sensorsInfo.limitXSwitchUp, sensorsInfo.limitXSwitchDown);
|
|
|
}
|
|
|
- if ((sensorsInfo.limitYSwitchDown == 1) || (sensorsInfo.limitYSwitchUp == 1)) {
|
|
|
+ if (((sensorsInfo.limitYSwitchDown == 1) || (sensorsInfo.limitYSwitchUp == 1)) && (limiterYTriggered == 1)) {
|
|
|
sensorsInfo.motorYStatus = MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_3, TIM_CHANNEL_4, motorYTimerHandle, 0, 0, sensorsInfo.limitYSwitchUp, sensorsInfo.limitYSwitchDown);
|
|
|
}
|
|
|
+ limiterXTriggered = 0;
|
|
|
+ limiterYTriggered = 0;
|
|
|
osMutexRelease (sensorsInfoMutex);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void EncoderTask (void* arg) {
|
|
|
- EncoderData encoderData = { 0 };
|
|
|
- osMessageQueueId_t encoderQueue = (osMessageQueueId_t)arg;
|
|
|
- while (pdTRUE) {
|
|
|
- osMessageQueueGet (encoderQueue, &encoderData, 0, osWaitForever);
|
|
|
- if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
|
|
|
- if (encoderData.axe == encoderAxeX) {
|
|
|
- if (encoderData.direction == encoderCW) {
|
|
|
- sensorsInfo.pvEncoderX += 360.0 / ENCODER_X_IMP_PER_TURN;
|
|
|
- } else {
|
|
|
- sensorsInfo.pvEncoderX -= 360.0 / ENCODER_X_IMP_PER_TURN;
|
|
|
- if(sensorsInfo.pvEncoderX < 0)
|
|
|
- {
|
|
|
- sensorsInfo.pvEncoderX = 360.0 + sensorsInfo.pvEncoderX;
|
|
|
- }
|
|
|
- }
|
|
|
- sensorsInfo.pvEncoderX = fmodf(sensorsInfo.pvEncoderX, 360.0);
|
|
|
- float currentPercentPos = 100 * sensorsInfo.pvEncoderX / MAX_X_AXE_ANGLE;
|
|
|
- currentPercentPos = currentPercentPos < 0 ? 0 : currentPercentPos;
|
|
|
- sensorsInfo.currentXPosition = currentPercentPos > 100 ? 100 : currentPercentPos;
|
|
|
- DbgLEDToggle(DBG_LED2);
|
|
|
- } else {
|
|
|
- if (encoderData.direction == encoderCW) {
|
|
|
- sensorsInfo.pvEncoderY += 360.0 / ENCODER_Y_IMP_PER_TURN;
|
|
|
- } else {
|
|
|
- sensorsInfo.pvEncoderY -= 360.0 / ENCODER_Y_IMP_PER_TURN;
|
|
|
- if(sensorsInfo.pvEncoderY < 0)
|
|
|
- {
|
|
|
- sensorsInfo.pvEncoderY = 360.0 + sensorsInfo.pvEncoderY;
|
|
|
- }
|
|
|
- }
|
|
|
- sensorsInfo.pvEncoderY = fmodf(sensorsInfo.pvEncoderY, 360.0);
|
|
|
- float currentPercentPos = 100 * sensorsInfo.pvEncoderY / MAX_X_AXE_ANGLE;
|
|
|
- currentPercentPos = currentPercentPos < 0 ? 0 : currentPercentPos;
|
|
|
- sensorsInfo.currentXPosition = currentPercentPos > 100 ? 100 : currentPercentPos;
|
|
|
- DbgLEDToggle(DBG_LED3);
|
|
|
- }
|
|
|
- osMutexRelease (sensorsInfoMutex);
|
|
|
+ // 01 11 10 00
|
|
|
+ const uint32_t encoderStates[4] = { 0x00, 0x01, 0x03, 0x02 };
|
|
|
+ uint8_t step = 0;
|
|
|
+ EncoderTaskArg* encoderTaskArg = (EncoderTaskArg*)arg;
|
|
|
+ uint32_t pinStates = encoderTaskArg->initPinStates;
|
|
|
+ for (uint8_t i = 0; i < 4; i++) {
|
|
|
+ if (pinStates == encoderStates[i]) {
|
|
|
+ step = i;
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
+ while (pdTRUE) {
|
|
|
+ float encoderValue = *encoderTaskArg->pvEncoder;
|
|
|
+ osMessageQueueGet (encoderTaskArg->dataQueue, &pinStates, 0, osWaitForever);
|
|
|
+ if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
|
|
|
+ if (encoderStates[(step + 1) % 4] == pinStates) {
|
|
|
+ step++;
|
|
|
+ encoderValue += 360.0 / ENCODER_X_IMP_PER_TURN;
|
|
|
+// printf ("Forward\n");
|
|
|
+ } else if (encoderStates[(step - 1) % 4] == pinStates) {
|
|
|
+ encoderValue -= 360.0 / ENCODER_X_IMP_PER_TURN;
|
|
|
+ if (encoderValue < 0) {
|
|
|
+ encoderValue = 360.0 + encoderValue;
|
|
|
+ }
|
|
|
+// printf ("Reverse\n");
|
|
|
+ step--;
|
|
|
+ } else {
|
|
|
+ printf ("Forbidden\n");
|
|
|
+ }
|
|
|
+ step = step % 4;
|
|
|
+ *encoderTaskArg->pvEncoder = fmodf (encoderValue, 360.0);
|
|
|
+ *encoderTaskArg->currentPosition = 100 * (*encoderTaskArg->pvEncoder) / MAX_X_AXE_ANGLE;
|
|
|
+ osMutexRelease (sensorsInfoMutex);
|
|
|
+ }
|
|
|
+ DbgLEDToggle (encoderTaskArg->dbgLed);
|
|
|
+ }
|
|
|
}
|