meas_tasks.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * meas_tasks.c
  3. *
  4. * Created on: Sep 5, 2024
  5. * Author: jakubski
  6. */
  7. #include "meas_tasks.h"
  8. #include "adc_buffers.h"
  9. #include "measurements.h"
  10. #include "node-red-config.h"
  11. #include "peripherial.h"
  12. #include "cmsis_os.h"
  13. #include "main.h"
  14. #ifdef PV_BOARD
  15. #define VOLTAGES_COUNT 1
  16. #define CURRENTS_COUNT 1
  17. #else
  18. #define VOLTAGES_COUNT 3
  19. #define CURRENTS_COUNT 3
  20. #endif
  21. #define CIRC_BUFF_LEN 10
  22. osThreadId_t adc1MeasTaskHandle = NULL;
  23. osThreadId_t adc2MeasTaskHandle = NULL;
  24. osThreadId_t adc3MeasTaskHandle = NULL;
  25. osThreadId_t limiterSwitchTaskHandle = NULL;
  26. osThreadId_t encoderTaskHandle = NULL;
  27. osMessageQueueId_t adc1MeasDataQueue = NULL;
  28. osMessageQueueId_t adc2MeasDataQueue = NULL;
  29. osMessageQueueId_t adc3MeasDataQueue = NULL;
  30. osMessageQueueId_t limiterSwitchDataQueue = NULL;
  31. osMessageQueueId_t encoderDataQueue = NULL;
  32. osMutexId_t vRefmVMutex;
  33. osMutexId_t resMeasurementsMutex;
  34. osMutexId_t sensorsInfoMutex;
  35. osMutexId_t ILxRefMutex;
  36. volatile uint32_t vRefmV = 3000;
  37. RESMeasurements resMeasurements = { 0 };
  38. SesnorsInfo sensorsInfo = { 0 };
  39. uint16_t ILxRef[CURRENTS_COUNT] = { 0 };
  40. extern TIM_HandleTypeDef htim3;
  41. extern TIM_OC_InitTypeDef motorXYTimerConfigOC;
  42. extern osTimerId_t motorXTimerHandle;
  43. extern osTimerId_t motorYTimerHandle;
  44. void MeasTasksInit (void) {
  45. vRefmVMutex = osMutexNew (NULL);
  46. resMeasurementsMutex = osMutexNew (NULL);
  47. sensorsInfoMutex = osMutexNew (NULL);
  48. ILxRefMutex = osMutexNew (NULL);
  49. adc1MeasDataQueue = osMessageQueueNew (8, sizeof (ADC1_Data), NULL);
  50. adc2MeasDataQueue = osMessageQueueNew (8, sizeof (ADC2_Data), NULL);
  51. adc3MeasDataQueue = osMessageQueueNew (8, sizeof (ADC3_Data), NULL);
  52. osThreadAttr_t osThreadAttradc1MeasTask = { 0 };
  53. osThreadAttr_t osThreadAttradc2MeasTask = { 0 };
  54. osThreadAttr_t osThreadAttradc3MeasTask = { 0 };
  55. osThreadAttradc1MeasTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  56. osThreadAttradc1MeasTask.priority = (osPriority_t)osPriorityRealtime;
  57. osThreadAttradc2MeasTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  58. osThreadAttradc2MeasTask.priority = (osPriority_t)osPriorityRealtime;
  59. osThreadAttradc3MeasTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  60. osThreadAttradc3MeasTask.priority = (osPriority_t)osPriorityNormal;
  61. adc1MeasTaskHandle = osThreadNew (ADC1MeasTask, NULL, &osThreadAttradc1MeasTask);
  62. adc2MeasTaskHandle = osThreadNew (ADC2MeasTask, NULL, &osThreadAttradc2MeasTask);
  63. adc3MeasTaskHandle = osThreadNew (ADC3MeasTask, NULL, &osThreadAttradc3MeasTask);
  64. limiterSwitchDataQueue = osMessageQueueNew (8, sizeof (LimiterSwitchData), NULL);
  65. osThreadAttr_t osThreadAttradc1LimiterSwitchTask = { 0 };
  66. osThreadAttradc1LimiterSwitchTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  67. osThreadAttradc1LimiterSwitchTask.priority = (osPriority_t)osPriorityNormal;
  68. limiterSwitchTaskHandle = osThreadNew (LimiterSwitchTask, NULL, &osThreadAttradc1LimiterSwitchTask);
  69. encoderDataQueue = osMessageQueueNew (16, sizeof (EncoderData), NULL);
  70. osThreadAttr_t osThreadAttrEncoderTask = { 0 };
  71. osThreadAttrEncoderTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  72. osThreadAttrEncoderTask.priority = (osPriority_t)osPriorityNormal;
  73. encoderTaskHandle = osThreadNew (EncoderTask, encoderDataQueue, &osThreadAttrEncoderTask);
  74. }
  75. void ADC1MeasTask (void* arg) {
  76. float circBuffer[VOLTAGES_COUNT][CIRC_BUFF_LEN] = { 0 };
  77. float rms[VOLTAGES_COUNT] = { 0 };
  78. ;
  79. ADC1_Data adcData = { 0 };
  80. uint32_t circBuffPos = 0;
  81. float gainCorrection = 1.0;
  82. while (pdTRUE) {
  83. osMessageQueueGet (adc1MeasDataQueue, &adcData, 0, osWaitForever);
  84. #ifdef GAIN_AUTO_CORRECTION
  85. if (osMutexAcquire (vRefmVMutex, osWaitForever) == osOK) {
  86. gainCorrection = (float)vRefmV;
  87. osMutexRelease (vRefmVMutex);
  88. }
  89. gainCorrection = gainCorrection / EXT_VREF_mV;
  90. #endif
  91. for (uint8_t i = 0; i < VOLTAGES_COUNT; i++) {
  92. float val = adcData.adcDataBuffer[i] * deltaADC * U_CHANNEL_CONST * gainCorrection * U_MeasCorrectionData[i].gain + U_MeasCorrectionData[i].offset;
  93. circBuffer[i][circBuffPos] = val;
  94. rms[i] = 0.0;
  95. for (uint8_t c = 0; c < CIRC_BUFF_LEN; c++) {
  96. rms[i] += circBuffer[i][c];
  97. }
  98. rms[i] = rms[i] / CIRC_BUFF_LEN;
  99. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  100. if (fabs (resMeasurements.voltagePeak[i]) < fabs (val)) {
  101. resMeasurements.voltagePeak[i] = val;
  102. }
  103. resMeasurements.voltageRMS[i] = rms[i];
  104. resMeasurements.power[i] = resMeasurements.voltageRMS[i] * resMeasurements.currentRMS[i];
  105. osMutexRelease (resMeasurementsMutex);
  106. }
  107. }
  108. ++circBuffPos;
  109. circBuffPos = circBuffPos % CIRC_BUFF_LEN;
  110. if (osMutexAcquire (ILxRefMutex, osWaitForever) == osOK) {
  111. uint8_t refIdx = 0;
  112. for (uint8_t i = (uint8_t)IL1Ref; i <= (uint8_t)IL3Ref; i++) {
  113. ILxRef[refIdx++] = adcData.adcDataBuffer[i];
  114. }
  115. osMutexRelease (ILxRefMutex);
  116. }
  117. float fanFBVoltage = adcData.adcDataBuffer[FanFB] * deltaADC * -4.35 + 12;
  118. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  119. sensorsInfo.fanVoltage = fanFBVoltage;
  120. osMutexRelease (sensorsInfoMutex);
  121. }
  122. }
  123. }
  124. void ADC2MeasTask (void* arg) {
  125. float circBuffer[CURRENTS_COUNT][CIRC_BUFF_LEN] = { 0 };
  126. float rms[CURRENTS_COUNT] = { 0 };
  127. ADC2_Data adcData = { 0 };
  128. uint32_t circBuffPos = 0;
  129. float gainCorrection = 1.0;
  130. while (pdTRUE) {
  131. osMessageQueueGet (adc2MeasDataQueue, &adcData, 0, osWaitForever);
  132. if (osMutexAcquire (vRefmVMutex, osWaitForever) == osOK) {
  133. gainCorrection = (float)vRefmV;
  134. osMutexRelease (vRefmVMutex);
  135. }
  136. gainCorrection = gainCorrection / EXT_VREF_mV;
  137. float ref[CURRENTS_COUNT] = { 0 };
  138. if (osMutexAcquire (ILxRefMutex, osWaitForever) == osOK) {
  139. for (uint8_t i = 0; i < CURRENTS_COUNT; i++) {
  140. ref[i] = (float)ILxRef[i];
  141. }
  142. osMutexRelease (ILxRefMutex);
  143. }
  144. for (uint8_t i = 0; i < CURRENTS_COUNT; i++) {
  145. float adcVal = (float)adcData.adcDataBuffer[i];
  146. float val = (adcVal - ref[i]) * deltaADC * I_CHANNEL_CONST * gainCorrection * I_MeasCorrectionData[i].gain + I_MeasCorrectionData[i].offset;
  147. circBuffer[i][circBuffPos] = val;
  148. rms[i] = 0.0;
  149. for (uint8_t c = 0; c < CIRC_BUFF_LEN; c++) {
  150. rms[i] += circBuffer[i][c];
  151. }
  152. rms[i] = rms[i] / CIRC_BUFF_LEN;
  153. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  154. if (resMeasurements.currentPeak[i] < val) {
  155. resMeasurements.currentPeak[i] = val;
  156. }
  157. resMeasurements.currentRMS[i] = rms[i];
  158. osMutexRelease (resMeasurementsMutex);
  159. }
  160. }
  161. ++circBuffPos;
  162. circBuffPos = circBuffPos % CIRC_BUFF_LEN;
  163. }
  164. }
  165. void ADC3MeasTask (void* arg) {
  166. float motorXSensCircBuffer[CIRC_BUFF_LEN] = { 0 };
  167. float motorYSensCircBuffer[CIRC_BUFF_LEN] = { 0 };
  168. float pvT1CircBuffer[CIRC_BUFF_LEN] = { 0 };
  169. float pvT2CircBuffer[CIRC_BUFF_LEN] = { 0 };
  170. uint32_t circBuffPos = 0;
  171. ADC3_Data adcData = { 0 };
  172. while (pdTRUE) {
  173. osMessageQueueGet (adc3MeasDataQueue, &adcData, 0, osWaitForever);
  174. uint32_t vRef = __LL_ADC_CALC_VREFANALOG_VOLTAGE (adcData.adcDataBuffer[VrefInt], LL_ADC_RESOLUTION_16B);
  175. if (osMutexAcquire (vRefmVMutex, osWaitForever) == osOK) {
  176. vRefmV = vRef;
  177. osMutexRelease (vRefmVMutex);
  178. }
  179. float motorXCurrentSense = adcData.adcDataBuffer[motorXSense] * deltaADC * 10 / 8.33333;
  180. float motorYCurrentSense = adcData.adcDataBuffer[motorYSense] * deltaADC * 10 / 8.33333;
  181. motorXSensCircBuffer[circBuffPos] = motorXCurrentSense;
  182. motorYSensCircBuffer[circBuffPos] = motorYCurrentSense;
  183. pvT1CircBuffer[circBuffPos] = adcData.adcDataBuffer[pvTemp1] * deltaADC * 45.33333333 - 63;
  184. pvT2CircBuffer[circBuffPos] = adcData.adcDataBuffer[pvTemp2] * deltaADC * 45.33333333 - 63;
  185. float motorXAveCurrent = 0;
  186. float motorYAveCurrent = 0;
  187. float pvT1AveTemp = 0;
  188. float pvT2AveTemp = 0;
  189. for (uint8_t i = 0; i < CIRC_BUFF_LEN; i++) {
  190. motorXAveCurrent += motorXSensCircBuffer[i];
  191. motorYAveCurrent += motorYSensCircBuffer[i];
  192. #ifdef PV_BOARD
  193. pvT1AveTemp += pvT1CircBuffer[i];
  194. pvT2AveTemp += pvT2CircBuffer[i];
  195. #endif
  196. }
  197. motorXAveCurrent /= CIRC_BUFF_LEN;
  198. motorYAveCurrent /= CIRC_BUFF_LEN;
  199. pvT1AveTemp /= CIRC_BUFF_LEN;
  200. pvT2AveTemp /= CIRC_BUFF_LEN;
  201. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  202. if (sensorsInfo.motorXStatus == 1) {
  203. sensorsInfo.motorXAveCurrent = motorXAveCurrent;
  204. if (sensorsInfo.motorXPeakCurrent < motorXCurrentSense) {
  205. sensorsInfo.motorXPeakCurrent = motorXCurrentSense;
  206. }
  207. }
  208. if (sensorsInfo.motorYStatus == 1) {
  209. sensorsInfo.motorYAveCurrent = motorYAveCurrent;
  210. if (sensorsInfo.motorYPeakCurrent < motorYCurrentSense) {
  211. sensorsInfo.motorYPeakCurrent = motorYCurrentSense;
  212. }
  213. }
  214. sensorsInfo.pvTemperature[0] = pvT1AveTemp;
  215. sensorsInfo.pvTemperature[1] = pvT2AveTemp;
  216. osMutexRelease (sensorsInfoMutex);
  217. }
  218. ++circBuffPos;
  219. circBuffPos = circBuffPos % CIRC_BUFF_LEN;
  220. }
  221. }
  222. void LimiterSwitchTask (void* arg) {
  223. LimiterSwitchData limiterSwitchData = { 0 };
  224. limiterSwitchData.gpioPin = GPIO_PIN_8;
  225. for (uint8_t i = 0; i < 6; i++) {
  226. limiterSwitchData.pinState = HAL_GPIO_ReadPin (GPIOD, limiterSwitchData.gpioPin);
  227. osMessageQueuePut (limiterSwitchDataQueue, &limiterSwitchData, 0, 0);
  228. limiterSwitchData.gpioPin = limiterSwitchData.gpioPin << 1;
  229. }
  230. while (pdTRUE) {
  231. osMessageQueueGet (limiterSwitchDataQueue, &limiterSwitchData, 0, osWaitForever);
  232. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  233. switch (limiterSwitchData.gpioPin) {
  234. case GPIO_PIN_8: sensorsInfo.limitYSwitchCenter = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0; break;
  235. case GPIO_PIN_9: sensorsInfo.limitYSwitchDown = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0; break;
  236. case GPIO_PIN_10: sensorsInfo.limitXSwitchCenter = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0; break;
  237. case GPIO_PIN_11: sensorsInfo.limitYSwitchUp = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0; break;
  238. case GPIO_PIN_12: sensorsInfo.limitXSwitchUp = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0; break;
  239. case GPIO_PIN_13: sensorsInfo.limitXSwitchDown = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0; break;
  240. default: break;
  241. }
  242. if ((sensorsInfo.limitXSwitchDown == 1) || (sensorsInfo.limitXSwitchUp == 1)) {
  243. sensorsInfo.motorXStatus = motorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_1, TIM_CHANNEL_2, motorXTimerHandle, 0, 0, sensorsInfo.limitXSwitchUp, sensorsInfo.limitXSwitchDown);
  244. }
  245. if ((sensorsInfo.limitYSwitchDown == 1) || (sensorsInfo.limitYSwitchUp == 1)) {
  246. sensorsInfo.motorYStatus = motorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_3, TIM_CHANNEL_4, motorYTimerHandle, 0, 0, sensorsInfo.limitYSwitchUp, sensorsInfo.limitYSwitchDown);
  247. }
  248. osMutexRelease (sensorsInfoMutex);
  249. }
  250. }
  251. }
  252. void EncoderTask (void* arg) {
  253. EncoderData encoderData = { 0 };
  254. osMessageQueueId_t encoderQueue = (osMessageQueueId_t)arg;
  255. while (pdTRUE) {
  256. osMessageQueueGet (encoderQueue, &encoderData, 0, osWaitForever);
  257. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  258. if (encoderData.axe == encoderAxeX) {
  259. if (encoderData.direction == encoderCW) {
  260. sensorsInfo.pvEncoderX += 360.0 / ENCODER_X_IMP_PER_TURN;
  261. } else {
  262. sensorsInfo.pvEncoderX -= 360.0 / ENCODER_X_IMP_PER_TURN;
  263. }
  264. DbgLEDToggle(DBG_LED2);
  265. } else {
  266. if (encoderData.direction == encoderCW) {
  267. sensorsInfo.pvEncoderY += 360.0 / ENCODER_Y_IMP_PER_TURN;
  268. } else {
  269. sensorsInfo.pvEncoderY -= 360.0 / ENCODER_Y_IMP_PER_TURN;
  270. }
  271. DbgLEDToggle(DBG_LED3);
  272. }
  273. osMutexRelease (sensorsInfoMutex);
  274. }
  275. }
  276. }