meas_tasks.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * meas_tasks.c
  3. *
  4. * Created on: Sep 5, 2024
  5. * Author: jakubski
  6. */
  7. #include <math.h>
  8. #include <stdio.h>
  9. #include "adc_buffers.h"
  10. #include "meas_tasks.h"
  11. #include "measurements.h"
  12. #include "node-red-config.h"
  13. #include "peripherial.h"
  14. #include "arm_math.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "cmsis_os.h"
  19. #include "main.h"
  20. #ifdef PV_BOARD
  21. #define CHANNELS_COUNT 1
  22. #else
  23. #define CHANNELS_COUNT 3
  24. #endif
  25. #define CIRC_BUFF_LEN 10
  26. osThreadId_t adc1MeasTaskHandle = NULL;
  27. osThreadId_t adc2MeasTaskHandle = NULL;
  28. osThreadId_t adc3MeasTaskHandle = NULL;
  29. osThreadId_t limiterSwitchTaskHandle = NULL;
  30. osThreadId_t encoderXTaskHandle = NULL;
  31. osThreadId_t encoderYTaskHandle = NULL;
  32. osMessageQueueId_t adc1MeasDataQueue = NULL;
  33. osMessageQueueId_t adc2MeasDataQueue = NULL;
  34. osMessageQueueId_t adc3MeasDataQueue = NULL;
  35. osMutexId_t vRefmVMutex;
  36. osMutexId_t resMeasurementsMutex;
  37. osMutexId_t sensorsInfoMutex;
  38. osMutexId_t ILxRefMutex;
  39. volatile uint32_t vRefmV = 3000;
  40. #ifdef MOCK_VOLTAGES_AND_CURRENS
  41. #define SAMPLE_BUFFER_LENGTH 1024
  42. #define SAMPLE_BUFFER_LENGTH_HALF (SAMPLE_BUFFER_LENGTH/2)
  43. #define FFT_Length SAMPLE_BUFFER_LENGTH
  44. float32_t voltageWave[3][SAMPLE_BUFFER_LENGTH] = { 0 };
  45. float32_t currentWave[3][SAMPLE_BUFFER_LENGTH] = { 0 };
  46. float fft_output[SAMPLE_BUFFER_LENGTH] = { 0 };
  47. float fft_power_scaled[SAMPLE_BUFFER_LENGTH_HALF] = { 0 };
  48. float fft_power[SAMPLE_BUFFER_LENGTH_HALF] = { 0 };
  49. uint8_t ifftFlag = 0;
  50. #endif
  51. RESMeasurements resMeasurements __attribute__ ((aligned (32))) = { 0 };
  52. SesnorsInfo sensorsInfo __attribute__ ((aligned (32))) = { 0 };
  53. //uint16_t ILxRef[CURRENTS_COUNT] __attribute__ ((aligned (32))) = { 0 };
  54. EncoderTaskArg encoderXTaskArg __attribute__ ((aligned (32))) = { 0 };
  55. EncoderTaskArg encoderYTaskArg __attribute__ ((aligned (32))) = { 0 };
  56. extern TIM_HandleTypeDef htim3;
  57. extern TIM_OC_InitTypeDef motorXYTimerConfigOC;
  58. extern osTimerId_t motorXTimerHandle;
  59. extern osTimerId_t motorYTimerHandle;
  60. void GenenarateWaveSamples(float32_t amplitude, float32_t phase, uint16_t samplesPerPeriod, uint16_t periods, float32_t* outBuff)
  61. {
  62. float32_t arg = 0;
  63. float32_t delta = 2*PI/samplesPerPeriod;
  64. uint32_t samples = samplesPerPeriod * periods;
  65. for(uint32_t i = 0; i < samples; i++)
  66. {
  67. arg = delta*i + phase;
  68. outBuff[i] = amplitude * arm_sin_f32(arg);
  69. }
  70. }
  71. void MeasTasksInit (void) {
  72. vRefmVMutex = osMutexNew (NULL);
  73. resMeasurementsMutex = osMutexNew (NULL);
  74. sensorsInfoMutex = osMutexNew (NULL);
  75. ILxRefMutex = osMutexNew (NULL);
  76. adc1MeasDataQueue = osMessageQueueNew (8, sizeof (ADC1_Data), NULL);
  77. adc2MeasDataQueue = osMessageQueueNew (8, sizeof (ADC2_Data), NULL);
  78. adc3MeasDataQueue = osMessageQueueNew (8, sizeof (ADC3_Data), NULL);
  79. osThreadAttr_t osThreadAttradc1MeasTask = { 0 };
  80. osThreadAttr_t osThreadAttradc3MeasTask = { 0 };
  81. osThreadAttradc1MeasTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  82. osThreadAttradc1MeasTask.priority = (osPriority_t)osPriorityRealtime;
  83. osThreadAttradc3MeasTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  84. osThreadAttradc3MeasTask.priority = (osPriority_t)osPriorityNormal;
  85. adc1MeasTaskHandle = osThreadNew (ADC1MeasTask, NULL, &osThreadAttradc1MeasTask);
  86. adc3MeasTaskHandle = osThreadNew (ADC3MeasTask, NULL, &osThreadAttradc3MeasTask);
  87. osThreadAttr_t osThreadAttradc1LimiterSwitchTask = { 0 };
  88. osThreadAttradc1LimiterSwitchTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  89. osThreadAttradc1LimiterSwitchTask.priority = (osPriority_t)osPriorityNormal;
  90. limiterSwitchTaskHandle = osThreadNew (LimiterSwitchTask, NULL, &osThreadAttradc1LimiterSwitchTask);
  91. encoderXTaskArg.dbgLed = DBG_LED2;
  92. encoderXTaskArg.pvEncoder = &(sensorsInfo.pvEncoderX);
  93. encoderXTaskArg.currentPosition = &(sensorsInfo.currentXPosition);
  94. osMessageQueueAttr_t encoderMsgQueueAttr = { 0 };
  95. encoderXTaskArg.dataQueue = osMessageQueueNew (16, sizeof (uint32_t), &encoderMsgQueueAttr);
  96. encoderXTaskArg.initPinStates = ((HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_15) << 1) | HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_14)) & 0x3;
  97. encoderYTaskArg.dbgLed = DBG_LED3;
  98. encoderYTaskArg.pvEncoder = &(sensorsInfo.pvEncoderY);
  99. encoderYTaskArg.currentPosition = &(sensorsInfo.currentYPosition);
  100. encoderYTaskArg.dataQueue = osMessageQueueNew (16, sizeof (uint32_t), &encoderMsgQueueAttr);
  101. encoderYTaskArg.initPinStates = ((HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_11) << 1) | HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10)) & 0x3;
  102. osThreadAttr_t osThreadAttrEncoderTask = { 0 };
  103. osThreadAttrEncoderTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  104. osThreadAttrEncoderTask.priority = (osPriority_t)osPriorityRealtime;
  105. encoderXTaskHandle = osThreadNew (EncoderTask, &encoderXTaskArg, &osThreadAttrEncoderTask);
  106. encoderYTaskHandle = osThreadNew (EncoderTask, &encoderYTaskArg, &osThreadAttrEncoderTask);
  107. #ifdef MOCK_VOLTAGES_AND_CURRENS
  108. GenenarateWaveSamples(325.269, 0, 128, 8, voltageWave[0]);
  109. GenenarateWaveSamples(324.269, 0, 128, 8, voltageWave[1]);
  110. GenenarateWaveSamples(323.269, 0, 128, 8, voltageWave[2]);
  111. GenenarateWaveSamples(1.414213562, 0, 128, 8, currentWave[0]);
  112. GenenarateWaveSamples(1.314213562, 0, 128, 8, currentWave[1]);
  113. GenenarateWaveSamples(1.214213562, 0, 128, 8, currentWave[2]);
  114. #endif
  115. // arm_hanning_f32
  116. // arm_rfft_fast_instance_f32 fft;
  117. // arm_rfft_fast_init_f32(&fft, FFT_Length);
  118. // arm_rfft_fast_f32(&fft, waveOne, fft_output, ifftFlag);
  119. // arm_cmplx_mag_f32(fft_output, fft_power, SAMPLE_BUFFER_LENGTH_HALF);
  120. // float32_t scale = 2.0f/FFT_Length;
  121. // arm_scale_f32(fft_power, scale, fft_power_scaled, SAMPLE_BUFFER_LENGTH_HALF);
  122. // float32_t maxValue;
  123. // uint32_t maxIndex;
  124. // arm_max_f32(fft_power_scaled, SAMPLE_BUFFER_LENGTH_HALF, &maxValue, &maxIndex);
  125. // printf("maxValue %f, index %ld\n", maxValue, maxIndex);
  126. }
  127. void ADC1MeasTask (void* arg) {
  128. float voltageAcc[CHANNELS_COUNT] = { 0 };
  129. float currentAcc[CHANNELS_COUNT] = { 0 };
  130. float powerAcc[CHANNELS_COUNT] = { 0 };
  131. uint32_t samplesCounter = 0;
  132. ADC1_Data adcData = { 0 };
  133. float gainCorrection = 1.0;
  134. while (pdTRUE) {
  135. osMessageQueueGet (adc1MeasDataQueue, &adcData, 0, osWaitForever);
  136. #ifdef GAIN_AUTO_CORRECTION
  137. if (osMutexAcquire (vRefmVMutex, osWaitForever) == osOK) {
  138. gainCorrection = (float)vRefmV;
  139. osMutexRelease (vRefmVMutex);
  140. }
  141. gainCorrection = gainCorrection / EXT_VREF_mV;
  142. #endif
  143. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  144. for (uint8_t i = 0; i < CHANNELS_COUNT; i++) {
  145. #ifdef MOCK_VOLTAGES_AND_CURRENS
  146. float voltage = voltageWave[i][samplesCounter % SAMPLE_BUFFER_LENGTH];
  147. float current = currentWave[i][samplesCounter % SAMPLE_BUFFER_LENGTH];
  148. #else
  149. float voltage = adcData.adcDataBuffer[UL1 + i] * deltaADC * U_CHANNEL_CONST * gainCorrection * U_MeasCorrectionData[i].gain + U_MeasCorrectionData[i].offset;
  150. float ref = (float)adcData.adcDataBuffer[IL1Ref + i];
  151. float adcVal = (float)adcData.adcDataBuffer[IIL1 + i];
  152. float current = (adcVal - ref) * deltaADC * I_CHANNEL_CONST * gainCorrection * I_MeasCorrectionData[i].gain + I_MeasCorrectionData[i].offset;
  153. #endif
  154. voltageAcc[i] += voltage * voltage;
  155. currentAcc[i] += current * current;
  156. powerAcc[i] += voltage * current;
  157. if (fabs (resMeasurements.voltagePeak[i]) < fabs (voltage)) {
  158. resMeasurements.voltagePeak[i] = voltage;
  159. }
  160. if (fabs (resMeasurements.currentPeak[i]) < fabs (current)) {
  161. resMeasurements.currentPeak[i] = current;
  162. }
  163. }
  164. samplesCounter += 1;
  165. if (samplesCounter > 33332)
  166. {
  167. for (uint8_t i = 0; i < CHANNELS_COUNT; i++) {
  168. resMeasurements.voltageRMS[i] = sqrtf(voltageAcc[i] / samplesCounter);
  169. resMeasurements.currentRMS[i] = sqrtf(currentAcc[i] / samplesCounter);
  170. resMeasurements.power[i] = powerAcc[i] / samplesCounter;
  171. voltageAcc[i] = 0;
  172. currentAcc[i] = 0;
  173. powerAcc[i] = 0;
  174. }
  175. samplesCounter = 0;
  176. DbgLEDToggle(DBG_LED3);
  177. }
  178. float fanFBVoltage = adcData.adcDataBuffer[FanFB] * deltaADC * -4.35 + 12;
  179. sensorsInfo.fanVoltage = fanFBVoltage;
  180. osMutexRelease (resMeasurementsMutex);
  181. }
  182. }
  183. }
  184. void ADC3MeasTask (void* arg) {
  185. float motorXSensCircBuffer[CIRC_BUFF_LEN] = { 0 };
  186. float motorYSensCircBuffer[CIRC_BUFF_LEN] = { 0 };
  187. #ifdef PV_BOARD
  188. float pvT1CircBuffer[CIRC_BUFF_LEN] = { 0 };
  189. float pvT2CircBuffer[CIRC_BUFF_LEN] = { 0 };
  190. #endif
  191. uint32_t circBuffPos = 0;
  192. ADC3_Data adcData = { 0 };
  193. while (pdTRUE) {
  194. osMessageQueueGet (adc3MeasDataQueue, &adcData, 0, osWaitForever);
  195. uint32_t vRef = __LL_ADC_CALC_VREFANALOG_VOLTAGE (adcData.adcDataBuffer[VrefInt], LL_ADC_RESOLUTION_16B);
  196. if (osMutexAcquire (vRefmVMutex, osWaitForever) == osOK) {
  197. vRefmV = vRef;
  198. osMutexRelease (vRefmVMutex);
  199. }
  200. float motorXCurrentSense = adcData.adcDataBuffer[motorXSense] * deltaADC * 10 / 8.33333;
  201. float motorYCurrentSense = adcData.adcDataBuffer[motorYSense] * deltaADC * 10 / 8.33333;
  202. motorXSensCircBuffer[circBuffPos] = motorXCurrentSense;
  203. motorYSensCircBuffer[circBuffPos] = motorYCurrentSense;
  204. #ifdef PV_BOARD
  205. pvT1CircBuffer[circBuffPos] = adcData.adcDataBuffer[pvTemp1] * deltaADC * 45.33333333 - 63;
  206. pvT2CircBuffer[circBuffPos] = adcData.adcDataBuffer[pvTemp2] * deltaADC * 45.33333333 - 63;
  207. #endif
  208. float motorXAveCurrent = 0;
  209. float motorYAveCurrent = 0;
  210. float pvT1AveTemp = 0;
  211. float pvT2AveTemp = 0;
  212. for (uint8_t i = 0; i < CIRC_BUFF_LEN; i++) {
  213. motorXAveCurrent += motorXSensCircBuffer[i];
  214. motorYAveCurrent += motorYSensCircBuffer[i];
  215. #ifdef PV_BOARD
  216. pvT1AveTemp += pvT1CircBuffer[i];
  217. pvT2AveTemp += pvT2CircBuffer[i];
  218. #endif
  219. }
  220. motorXAveCurrent /= CIRC_BUFF_LEN;
  221. motorYAveCurrent /= CIRC_BUFF_LEN;
  222. pvT1AveTemp /= CIRC_BUFF_LEN;
  223. pvT2AveTemp /= CIRC_BUFF_LEN;
  224. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  225. if (sensorsInfo.motorXStatus == 1) {
  226. sensorsInfo.motorXAveCurrent = motorXAveCurrent;
  227. if (sensorsInfo.motorXPeakCurrent < motorXCurrentSense) {
  228. sensorsInfo.motorXPeakCurrent = motorXCurrentSense;
  229. }
  230. }
  231. if (sensorsInfo.motorYStatus == 1) {
  232. sensorsInfo.motorYAveCurrent = motorYAveCurrent;
  233. if (sensorsInfo.motorYPeakCurrent < motorYCurrentSense) {
  234. sensorsInfo.motorYPeakCurrent = motorYCurrentSense;
  235. }
  236. }
  237. sensorsInfo.pvTemperature[0] = pvT1AveTemp;
  238. sensorsInfo.pvTemperature[1] = pvT2AveTemp;
  239. osMutexRelease (sensorsInfoMutex);
  240. }
  241. ++circBuffPos;
  242. circBuffPos = circBuffPos % CIRC_BUFF_LEN;
  243. }
  244. }
  245. void LimiterSwitchTask (void* arg) {
  246. uint8_t limitXSwitchDownPrevState = 0;
  247. uint8_t limitXSwitchCenterPrevState = 0;
  248. uint8_t limitXSwitchUpPrevState = 0;
  249. uint8_t limitYSwitchDownPrevState = 0;
  250. uint8_t limitYSwitchCenterPrevState = 0;
  251. uint8_t limitYSwitchUpPrevState = 0;
  252. uint8_t pinStates = 0;
  253. uint8_t limiterXTriggered = 0;
  254. uint8_t limiterYTriggered = 0;
  255. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  256. sensorsInfo.positionXWeak = 1;
  257. sensorsInfo.positionYWeak = 1;
  258. osMutexRelease (sensorsInfoMutex);
  259. }
  260. while (pdTRUE) {
  261. osDelay (pdMS_TO_TICKS (100));
  262. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  263. sensorsInfo.limitXSwitchDown = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_12);
  264. pinStates = (limitXSwitchDownPrevState << 1) | sensorsInfo.limitXSwitchDown;
  265. if ((pinStates & 0x3) == 0x1) {
  266. limiterXTriggered = 1;
  267. sensorsInfo.currentXPosition = 0;
  268. sensorsInfo.positionXWeak = 0;
  269. }
  270. limitXSwitchDownPrevState = sensorsInfo.limitXSwitchDown;
  271. sensorsInfo.limitXSwitchUp = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_13);
  272. pinStates = (limitXSwitchUpPrevState << 1) | sensorsInfo.limitXSwitchUp;
  273. if ((pinStates & 0x3) == 0x1) {
  274. limiterXTriggered = 1;
  275. sensorsInfo.currentXPosition = 100;
  276. sensorsInfo.positionXWeak = 0;
  277. }
  278. limitXSwitchUpPrevState = sensorsInfo.limitXSwitchUp;
  279. sensorsInfo.limitXSwitchCenter = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_10);
  280. pinStates = (limitXSwitchCenterPrevState << 1) | sensorsInfo.limitXSwitchCenter;
  281. if ((pinStates & 0x3) == 0x1) {
  282. sensorsInfo.currentXPosition = AXE_X_MIDDLE_VALUE;
  283. sensorsInfo.positionXWeak = 0;
  284. }
  285. limitXSwitchCenterPrevState = sensorsInfo.limitXSwitchCenter;
  286. sensorsInfo.limitYSwitchDown = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_11);
  287. pinStates = (limitYSwitchDownPrevState << 1) | sensorsInfo.limitYSwitchDown;
  288. if ((pinStates & 0x3) == 0x1) {
  289. limiterYTriggered = 1;
  290. sensorsInfo.currentYPosition = 0;
  291. sensorsInfo.positionYWeak = 0;
  292. }
  293. limitYSwitchDownPrevState = sensorsInfo.limitYSwitchDown;
  294. sensorsInfo.limitYSwitchUp = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_9);
  295. pinStates = (limitYSwitchUpPrevState << 1) | sensorsInfo.limitYSwitchUp;
  296. if ((pinStates & 0x3) == 0x1) {
  297. limiterYTriggered = 1;
  298. sensorsInfo.currentYPosition = 100;
  299. sensorsInfo.positionYWeak = 0;
  300. }
  301. limitYSwitchUpPrevState = sensorsInfo.limitYSwitchUp;
  302. sensorsInfo.limitYSwitchCenter = HAL_GPIO_ReadPin (GPIOD, GPIO_PIN_8);
  303. pinStates = (limitYSwitchCenterPrevState << 1) | sensorsInfo.limitYSwitchCenter;
  304. if ((pinStates & 0x3) == 0x1) {
  305. sensorsInfo.currentYPosition = AXE_Y_MIDDLE_VALUE;
  306. sensorsInfo.positionYWeak = 0;
  307. }
  308. limitYSwitchCenterPrevState = sensorsInfo.limitYSwitchCenter;
  309. if (((sensorsInfo.limitXSwitchDown == 1) || (sensorsInfo.limitXSwitchUp == 1)) && (limiterXTriggered == 1)) {
  310. sensorsInfo.motorXStatus = MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_1, TIM_CHANNEL_2, motorXTimerHandle, 0, 0, sensorsInfo.limitXSwitchUp, sensorsInfo.limitXSwitchDown);
  311. }
  312. if (((sensorsInfo.limitYSwitchDown == 1) || (sensorsInfo.limitYSwitchUp == 1)) && (limiterYTriggered == 1)) {
  313. sensorsInfo.motorYStatus = MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_3, TIM_CHANNEL_4, motorYTimerHandle, 0, 0, sensorsInfo.limitYSwitchUp, sensorsInfo.limitYSwitchDown);
  314. }
  315. limiterXTriggered = 0;
  316. limiterYTriggered = 0;
  317. osMutexRelease (sensorsInfoMutex);
  318. }
  319. }
  320. }
  321. void EncoderTask (void* arg) {
  322. // 01 11 10 00
  323. const uint32_t encoderStates[4] = { 0x00, 0x01, 0x03, 0x02 };
  324. uint8_t step = 0;
  325. EncoderTaskArg* encoderTaskArg = (EncoderTaskArg*)arg;
  326. uint32_t pinStates = encoderTaskArg->initPinStates;
  327. for (uint8_t i = 0; i < 4; i++) {
  328. if (pinStates == encoderStates[i]) {
  329. step = i;
  330. break;
  331. }
  332. }
  333. while (pdTRUE) {
  334. float encoderValue = *encoderTaskArg->pvEncoder;
  335. osMessageQueueGet (encoderTaskArg->dataQueue, &pinStates, 0, osWaitForever);
  336. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  337. if (encoderStates[(step + 1) % 4] == pinStates) {
  338. step++;
  339. encoderValue += 360.0 / ENCODER_X_IMP_PER_TURN;
  340. // printf ("Forward\n");
  341. } else if (encoderStates[(step - 1) % 4] == pinStates) {
  342. encoderValue -= 360.0 / ENCODER_X_IMP_PER_TURN;
  343. if (encoderValue < 0) {
  344. encoderValue = 360.0 + encoderValue;
  345. }
  346. // printf ("Reverse\n");
  347. step--;
  348. } else {
  349. printf ("Forbidden\n");
  350. }
  351. step = step % 4;
  352. // *encoderTaskArg->pvEncoder = fmodf (encoderValue, 360.0);
  353. *encoderTaskArg->pvEncoder = encoderValue;
  354. *encoderTaskArg->currentPosition = 100 * (*encoderTaskArg->pvEncoder) / MAX_X_AXE_ANGLE;
  355. osMutexRelease (sensorsInfoMutex);
  356. }
  357. DbgLEDToggle (encoderTaskArg->dbgLed);
  358. }
  359. }