meas_tasks.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. //extern osMutexId_t positionSettingMutex;
  45. void MeasTasksInit (void) {
  46. vRefmVMutex = osMutexNew (NULL);
  47. resMeasurementsMutex = osMutexNew (NULL);
  48. sensorsInfoMutex = osMutexNew (NULL);
  49. ILxRefMutex = osMutexNew (NULL);
  50. adc1MeasDataQueue = osMessageQueueNew (8, sizeof (ADC1_Data), NULL);
  51. adc2MeasDataQueue = osMessageQueueNew (8, sizeof (ADC2_Data), NULL);
  52. adc3MeasDataQueue = osMessageQueueNew (8, sizeof (ADC3_Data), NULL);
  53. osThreadAttr_t osThreadAttradc1MeasTask = { 0 };
  54. osThreadAttr_t osThreadAttradc2MeasTask = { 0 };
  55. osThreadAttr_t osThreadAttradc3MeasTask = { 0 };
  56. osThreadAttradc1MeasTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  57. osThreadAttradc1MeasTask.priority = (osPriority_t)osPriorityRealtime;
  58. osThreadAttradc2MeasTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  59. osThreadAttradc2MeasTask.priority = (osPriority_t)osPriorityRealtime;
  60. osThreadAttradc3MeasTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  61. osThreadAttradc3MeasTask.priority = (osPriority_t)osPriorityNormal;
  62. adc1MeasTaskHandle = osThreadNew (ADC1MeasTask, NULL, &osThreadAttradc1MeasTask);
  63. adc2MeasTaskHandle = osThreadNew (ADC2MeasTask, NULL, &osThreadAttradc2MeasTask);
  64. adc3MeasTaskHandle = osThreadNew (ADC3MeasTask, NULL, &osThreadAttradc3MeasTask);
  65. limiterSwitchDataQueue = osMessageQueueNew (8, sizeof (LimiterSwitchData), NULL);
  66. osThreadAttr_t osThreadAttradc1LimiterSwitchTask = { 0 };
  67. osThreadAttradc1LimiterSwitchTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  68. osThreadAttradc1LimiterSwitchTask.priority = (osPriority_t)osPriorityNormal;
  69. limiterSwitchTaskHandle = osThreadNew (LimiterSwitchTask, NULL, &osThreadAttradc1LimiterSwitchTask);
  70. encoderDataQueue = osMessageQueueNew (16, sizeof (EncoderData), NULL);
  71. osThreadAttr_t osThreadAttrEncoderTask = { 0 };
  72. osThreadAttrEncoderTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  73. osThreadAttrEncoderTask.priority = (osPriority_t)osPriorityNormal;
  74. encoderTaskHandle = osThreadNew (EncoderTask, encoderDataQueue, &osThreadAttrEncoderTask);
  75. }
  76. void ADC1MeasTask (void* arg) {
  77. float circBuffer[VOLTAGES_COUNT][CIRC_BUFF_LEN] = { 0 };
  78. float rms[VOLTAGES_COUNT] = { 0 };
  79. ;
  80. ADC1_Data adcData = { 0 };
  81. uint32_t circBuffPos = 0;
  82. float gainCorrection = 1.0;
  83. while (pdTRUE) {
  84. osMessageQueueGet (adc1MeasDataQueue, &adcData, 0, osWaitForever);
  85. #ifdef GAIN_AUTO_CORRECTION
  86. if (osMutexAcquire (vRefmVMutex, osWaitForever) == osOK) {
  87. gainCorrection = (float)vRefmV;
  88. osMutexRelease (vRefmVMutex);
  89. }
  90. gainCorrection = gainCorrection / EXT_VREF_mV;
  91. #endif
  92. for (uint8_t i = 0; i < VOLTAGES_COUNT; i++) {
  93. float val = adcData.adcDataBuffer[i] * deltaADC * U_CHANNEL_CONST * gainCorrection * U_MeasCorrectionData[i].gain + U_MeasCorrectionData[i].offset;
  94. circBuffer[i][circBuffPos] = val;
  95. rms[i] = 0.0;
  96. for (uint8_t c = 0; c < CIRC_BUFF_LEN; c++) {
  97. rms[i] += circBuffer[i][c];
  98. }
  99. rms[i] = rms[i] / CIRC_BUFF_LEN;
  100. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  101. if (fabs (resMeasurements.voltagePeak[i]) < fabs (val)) {
  102. resMeasurements.voltagePeak[i] = val;
  103. }
  104. resMeasurements.voltageRMS[i] = rms[i];
  105. resMeasurements.power[i] = resMeasurements.voltageRMS[i] * resMeasurements.currentRMS[i];
  106. osMutexRelease (resMeasurementsMutex);
  107. }
  108. }
  109. ++circBuffPos;
  110. circBuffPos = circBuffPos % CIRC_BUFF_LEN;
  111. if (osMutexAcquire (ILxRefMutex, osWaitForever) == osOK) {
  112. uint8_t refIdx = 0;
  113. for (uint8_t i = (uint8_t)IL1Ref; i <= (uint8_t)IL3Ref; i++) {
  114. ILxRef[refIdx++] = adcData.adcDataBuffer[i];
  115. }
  116. osMutexRelease (ILxRefMutex);
  117. }
  118. float fanFBVoltage = adcData.adcDataBuffer[FanFB] * deltaADC * -4.35 + 12;
  119. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  120. sensorsInfo.fanVoltage = fanFBVoltage;
  121. osMutexRelease (sensorsInfoMutex);
  122. }
  123. }
  124. }
  125. void ADC2MeasTask (void* arg) {
  126. float circBuffer[CURRENTS_COUNT][CIRC_BUFF_LEN] = { 0 };
  127. float rms[CURRENTS_COUNT] = { 0 };
  128. ADC2_Data adcData = { 0 };
  129. uint32_t circBuffPos = 0;
  130. float gainCorrection = 1.0;
  131. while (pdTRUE) {
  132. osMessageQueueGet (adc2MeasDataQueue, &adcData, 0, osWaitForever);
  133. if (osMutexAcquire (vRefmVMutex, osWaitForever) == osOK) {
  134. gainCorrection = (float)vRefmV;
  135. osMutexRelease (vRefmVMutex);
  136. }
  137. gainCorrection = gainCorrection / EXT_VREF_mV;
  138. float ref[CURRENTS_COUNT] = { 0 };
  139. if (osMutexAcquire (ILxRefMutex, osWaitForever) == osOK) {
  140. for (uint8_t i = 0; i < CURRENTS_COUNT; i++) {
  141. ref[i] = (float)ILxRef[i];
  142. }
  143. osMutexRelease (ILxRefMutex);
  144. }
  145. for (uint8_t i = 0; i < CURRENTS_COUNT; i++) {
  146. float adcVal = (float)adcData.adcDataBuffer[i];
  147. float val = (adcVal - ref[i]) * deltaADC * I_CHANNEL_CONST * gainCorrection * I_MeasCorrectionData[i].gain + I_MeasCorrectionData[i].offset;
  148. circBuffer[i][circBuffPos] = val;
  149. rms[i] = 0.0;
  150. for (uint8_t c = 0; c < CIRC_BUFF_LEN; c++) {
  151. rms[i] += circBuffer[i][c];
  152. }
  153. rms[i] = rms[i] / CIRC_BUFF_LEN;
  154. if (osMutexAcquire (resMeasurementsMutex, osWaitForever) == osOK) {
  155. if (resMeasurements.currentPeak[i] < val) {
  156. resMeasurements.currentPeak[i] = val;
  157. }
  158. resMeasurements.currentRMS[i] = rms[i];
  159. osMutexRelease (resMeasurementsMutex);
  160. }
  161. }
  162. ++circBuffPos;
  163. circBuffPos = circBuffPos % CIRC_BUFF_LEN;
  164. }
  165. }
  166. void ADC3MeasTask (void* arg) {
  167. float motorXSensCircBuffer[CIRC_BUFF_LEN] = { 0 };
  168. float motorYSensCircBuffer[CIRC_BUFF_LEN] = { 0 };
  169. float pvT1CircBuffer[CIRC_BUFF_LEN] = { 0 };
  170. float pvT2CircBuffer[CIRC_BUFF_LEN] = { 0 };
  171. uint32_t circBuffPos = 0;
  172. ADC3_Data adcData = { 0 };
  173. while (pdTRUE) {
  174. osMessageQueueGet (adc3MeasDataQueue, &adcData, 0, osWaitForever);
  175. uint32_t vRef = __LL_ADC_CALC_VREFANALOG_VOLTAGE (adcData.adcDataBuffer[VrefInt], LL_ADC_RESOLUTION_16B);
  176. if (osMutexAcquire (vRefmVMutex, osWaitForever) == osOK) {
  177. vRefmV = vRef;
  178. osMutexRelease (vRefmVMutex);
  179. }
  180. float motorXCurrentSense = adcData.adcDataBuffer[motorXSense] * deltaADC * 10 / 8.33333;
  181. float motorYCurrentSense = adcData.adcDataBuffer[motorYSense] * deltaADC * 10 / 8.33333;
  182. motorXSensCircBuffer[circBuffPos] = motorXCurrentSense;
  183. motorYSensCircBuffer[circBuffPos] = motorYCurrentSense;
  184. pvT1CircBuffer[circBuffPos] = adcData.adcDataBuffer[pvTemp1] * deltaADC * 45.33333333 - 63;
  185. pvT2CircBuffer[circBuffPos] = adcData.adcDataBuffer[pvTemp2] * deltaADC * 45.33333333 - 63;
  186. float motorXAveCurrent = 0;
  187. float motorYAveCurrent = 0;
  188. float pvT1AveTemp = 0;
  189. float pvT2AveTemp = 0;
  190. for (uint8_t i = 0; i < CIRC_BUFF_LEN; i++) {
  191. motorXAveCurrent += motorXSensCircBuffer[i];
  192. motorYAveCurrent += motorYSensCircBuffer[i];
  193. #ifdef PV_BOARD
  194. pvT1AveTemp += pvT1CircBuffer[i];
  195. pvT2AveTemp += pvT2CircBuffer[i];
  196. #endif
  197. }
  198. motorXAveCurrent /= CIRC_BUFF_LEN;
  199. motorYAveCurrent /= CIRC_BUFF_LEN;
  200. pvT1AveTemp /= CIRC_BUFF_LEN;
  201. pvT2AveTemp /= CIRC_BUFF_LEN;
  202. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  203. if (sensorsInfo.motorXStatus == 1) {
  204. sensorsInfo.motorXAveCurrent = motorXAveCurrent;
  205. if (sensorsInfo.motorXPeakCurrent < motorXCurrentSense) {
  206. sensorsInfo.motorXPeakCurrent = motorXCurrentSense;
  207. }
  208. }
  209. if (sensorsInfo.motorYStatus == 1) {
  210. sensorsInfo.motorYAveCurrent = motorYAveCurrent;
  211. if (sensorsInfo.motorYPeakCurrent < motorYCurrentSense) {
  212. sensorsInfo.motorYPeakCurrent = motorYCurrentSense;
  213. }
  214. }
  215. sensorsInfo.pvTemperature[0] = pvT1AveTemp;
  216. sensorsInfo.pvTemperature[1] = pvT2AveTemp;
  217. osMutexRelease (sensorsInfoMutex);
  218. }
  219. ++circBuffPos;
  220. circBuffPos = circBuffPos % CIRC_BUFF_LEN;
  221. }
  222. }
  223. void LimiterSwitchTask (void* arg) {
  224. LimiterSwitchData limiterSwitchData = { 0 };
  225. limiterSwitchData.gpioPin = GPIO_PIN_8;
  226. for (uint8_t i = 0; i < 6; i++) {
  227. limiterSwitchData.pinState = HAL_GPIO_ReadPin (GPIOD, limiterSwitchData.gpioPin);
  228. osMessageQueuePut (limiterSwitchDataQueue, &limiterSwitchData, 0, 0);
  229. limiterSwitchData.gpioPin = limiterSwitchData.gpioPin << 1;
  230. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  231. sensorsInfo.positionXWeak = 1;
  232. sensorsInfo.positionYWeak = 1;
  233. osMutexRelease (sensorsInfoMutex);
  234. }
  235. }
  236. while (pdTRUE) {
  237. osMessageQueueGet (limiterSwitchDataQueue, &limiterSwitchData, 0, osWaitForever);
  238. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  239. switch (limiterSwitchData.gpioPin) {
  240. case GPIO_PIN_8:
  241. sensorsInfo.limitYSwitchCenter = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
  242. if (sensorsInfo.limitYSwitchCenter == 1)
  243. {
  244. sensorsInfo.currentYPosition = AXE_Y_MIDDLE_VALUE;
  245. sensorsInfo.positionYWeak = 0;
  246. }
  247. break;
  248. case GPIO_PIN_9:
  249. sensorsInfo.limitYSwitchDown = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
  250. if (sensorsInfo.limitYSwitchDown == 1)
  251. {
  252. sensorsInfo.currentYPosition = 0;
  253. sensorsInfo.positionYWeak = 0;
  254. }
  255. break;
  256. case GPIO_PIN_10:
  257. sensorsInfo.limitXSwitchCenter = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
  258. if (sensorsInfo.limitXSwitchCenter == 1)
  259. {
  260. sensorsInfo.currentXPosition = AXE_X_MIDDLE_VALUE;
  261. sensorsInfo.positionXWeak = 0;
  262. }
  263. break;
  264. case GPIO_PIN_11:
  265. sensorsInfo.limitYSwitchUp = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
  266. if (sensorsInfo.limitYSwitchUp == 1)
  267. {
  268. sensorsInfo.currentYPosition = 100;
  269. sensorsInfo.positionYWeak = 0;
  270. }
  271. break;
  272. case GPIO_PIN_12:
  273. sensorsInfo.limitXSwitchUp = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
  274. if (sensorsInfo.limitXSwitchUp == 1)
  275. {
  276. sensorsInfo.currentXPosition = 100;
  277. sensorsInfo.positionXWeak = 0;
  278. }
  279. break;
  280. case GPIO_PIN_13:
  281. sensorsInfo.limitXSwitchDown = limiterSwitchData.pinState == GPIO_PIN_SET ? 1 : 0;
  282. if (sensorsInfo.limitXSwitchDown == 1)
  283. {
  284. sensorsInfo.currentXPosition = 0;
  285. sensorsInfo.positionXWeak = 0;
  286. }
  287. break;
  288. default: break;
  289. }
  290. if ((sensorsInfo.limitXSwitchDown == 1) || (sensorsInfo.limitXSwitchUp == 1)) {
  291. sensorsInfo.motorXStatus = MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_1, TIM_CHANNEL_2, motorXTimerHandle, 0, 0, sensorsInfo.limitXSwitchUp, sensorsInfo.limitXSwitchDown);
  292. }
  293. if ((sensorsInfo.limitYSwitchDown == 1) || (sensorsInfo.limitYSwitchUp == 1)) {
  294. sensorsInfo.motorYStatus = MotorControl (&htim3, &motorXYTimerConfigOC, TIM_CHANNEL_3, TIM_CHANNEL_4, motorYTimerHandle, 0, 0, sensorsInfo.limitYSwitchUp, sensorsInfo.limitYSwitchDown);
  295. }
  296. osMutexRelease (sensorsInfoMutex);
  297. }
  298. }
  299. }
  300. void EncoderTask (void* arg) {
  301. EncoderData encoderData = { 0 };
  302. osMessageQueueId_t encoderQueue = (osMessageQueueId_t)arg;
  303. while (pdTRUE) {
  304. osMessageQueueGet (encoderQueue, &encoderData, 0, osWaitForever);
  305. if (osMutexAcquire (sensorsInfoMutex, osWaitForever) == osOK) {
  306. if (encoderData.axe == encoderAxeX) {
  307. if (encoderData.direction == encoderCW) {
  308. sensorsInfo.pvEncoderX += 360.0 / ENCODER_X_IMP_PER_TURN;
  309. } else {
  310. sensorsInfo.pvEncoderX -= 360.0 / ENCODER_X_IMP_PER_TURN;
  311. }
  312. float currentPercentPos = 100 * sensorsInfo.pvEncoderX / MAX_X_AXE_ANGLE;
  313. currentPercentPos = currentPercentPos < 0 ? 0 : currentPercentPos;
  314. sensorsInfo.currentXPosition = currentPercentPos > 100 ? 100 : currentPercentPos;
  315. DbgLEDToggle(DBG_LED2);
  316. } else {
  317. if (encoderData.direction == encoderCW) {
  318. sensorsInfo.pvEncoderY += 360.0 / ENCODER_Y_IMP_PER_TURN;
  319. } else {
  320. sensorsInfo.pvEncoderY -= 360.0 / ENCODER_Y_IMP_PER_TURN;
  321. }
  322. float currentPercentPos = 100 * sensorsInfo.pvEncoderY / MAX_X_AXE_ANGLE;
  323. currentPercentPos = currentPercentPos < 0 ? 0 : currentPercentPos;
  324. sensorsInfo.currentXPosition = currentPercentPos > 100 ? 100 : currentPercentPos;
  325. DbgLEDToggle(DBG_LED3);
  326. }
  327. osMutexRelease (sensorsInfoMutex);
  328. }
  329. }
  330. }