mock_tasks.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * mock_tasks.c
  3. *
  4. * Created on: Aug 23, 2024
  5. * Author: jakubski
  6. */
  7. #include "FreeRTOS.h"
  8. #include "task.h"
  9. #include "main.h"
  10. #include "cmsis_os.h"
  11. #include "mock_tasks.h"
  12. #include "measurements.h"
  13. #include "node-red-config.h"
  14. extern RESMeasurements resMeasurements[SLAVES_COUNT];
  15. extern SesnorsInfo sensorsInfo[SLAVES_COUNT];
  16. extern osMutexId_t resMeasurementsMutex;
  17. extern osMutexId_t sensorsInfoMutex;
  18. #ifdef USER_MOCKS
  19. extern RNG_HandleTypeDef hrng;
  20. float rndflt (float rng) {
  21. uint32_t rndVal = 0;
  22. HAL_RNG_GenerateRandomNumber (&hrng, &rndVal);
  23. return ((float)rndVal / (float)(RAND_MAX)) * rng;
  24. }
  25. #else
  26. float rndflt (float rng)
  27. {
  28. (void)rng;
  29. return 0.0;
  30. }
  31. #endif
  32. void MockMeasurmetsTaskInit(void) {
  33. osThreadAttr_t osThreadAttrMockMeasTask = { 0 };
  34. // osThreadId_t osThreadMockMeasurmetsTaskId;
  35. osThreadAttrMockMeasTask.name = "os_thread_mock_measurmets";
  36. osThreadAttrMockMeasTask.stack_size = configMINIMAL_STACK_SIZE * 2;
  37. osThreadAttrMockMeasTask.priority = (osPriority_t)osPriorityNormal;
  38. // osThreadMockMeasurmetsTaskId = osThreadNew (MockMeasurmetsTask, NULL, &osThreadAttrMockMeasTask);
  39. osThreadNew (MockMeasurmetsTask, NULL, &osThreadAttrMockMeasTask);
  40. }
  41. void MockMeasurmetsTask (void* argument) {
  42. uint8_t boardNumber = 0;
  43. uint16_t counter = 0;
  44. while (pdTRUE) {
  45. for( boardNumber = 0; boardNumber < SLAVES_COUNT; boardNumber++)
  46. {
  47. osMutexAcquire (resMeasurementsMutex, osWaitForever);
  48. RESMeasurements *resMeas = &resMeasurements[boardNumber];
  49. resMeas->voltagePeak[0] = 60 + (0.01 * (counter % 100));
  50. resMeas->voltagePeak[1] = 61 + (0.01 * (counter % 100));
  51. resMeas->voltagePeak[2] = 62 + (0.01 * (counter % 100));
  52. resMeas->voltageRMS[0] = 46 + (0.01 * (counter % 100));
  53. resMeas->voltageRMS[1] = 47 + (0.01 * (counter % 100));
  54. resMeas->voltageRMS[2] = 48 + (0.01 * (counter % 100));
  55. resMeas->currentPeak[0] = 3 + (0.01 * (counter % 100));
  56. resMeas->currentPeak[1] = 4 + (0.01 * (counter % 100));
  57. resMeas->currentPeak[2] = 5 + (0.01 * (counter % 100));
  58. resMeas->currentRMS[0] = 1 + (0.01 * (counter % 100));
  59. resMeas->currentRMS[1] = 2 + (0.01 * (counter % 100));
  60. resMeas->currentRMS[2] = 3 + (0.01 * (counter % 100));
  61. resMeas->power[0] = resMeas->voltagePeak[0] * resMeas->currentRMS[0];
  62. resMeas->power[1] = resMeas->voltagePeak[1] * resMeas->currentRMS[1];
  63. resMeas->power[2] = resMeas->voltagePeak[2] * resMeas->currentRMS[2];
  64. osMutexRelease(resMeasurementsMutex);
  65. osMutexAcquire (sensorsInfoMutex, osWaitForever);
  66. SesnorsInfo* sensors = &sensorsInfo[boardNumber];
  67. sensors->pvTemperature[0] = 50 + (0.01 * (counter % 100));
  68. sensors->pvTemperature[1] = 51 + (0.01 * (counter % 100));
  69. sensors->fanVoltage = 12 + (0.01 * (counter % 100));
  70. sensors->pvEncoderXraw = 15 + (0.01 * (counter % 100));
  71. sensors->pvEncoderYraw = 10 + (0.01 * (counter % 100));
  72. sensors->motorXStatus = (counter % 100) > 50 ? 1 : 0;
  73. sensors->motorYStatus = (counter % 100) > 75 ? 1 : 0;
  74. sensors->motorXAveCurrent = 3 + (0.01 * (counter % 100));
  75. sensors->motorYAveCurrent = 3 + (0.01 * (counter % 100));
  76. sensors->motorXPeakCurrent = 6 + (0.01 * (counter % 100));
  77. sensors->motorYPeakCurrent = 6 + (0.01 * (counter % 100));
  78. sensors->limitXSwitchUp = (counter % 100) > 50 ? 1 : 0;
  79. sensors->limitXSwitchDown = (counter % 100) < 25 ? 1 : 0;
  80. sensors->limitXSwitchCenter = (counter % 100) > 35 ? 1 : 0;
  81. sensors->limitYSwitchUp = (counter % 100) > 50 ? 1 : 0;
  82. sensors->limitYSwitchDown = (counter % 100) < 25 ? 1 : 0;
  83. sensors->limitYSwitchCenter = (counter % 100) > 35 ? 1 : 0;
  84. sensors->powerSupplyFailMask = 0;
  85. osMutexRelease(sensorsInfoMutex);
  86. }
  87. counter++;
  88. osDelay (pdMS_TO_TICKS (1000));
  89. }
  90. }