mock_tasks.c 3.5 KB

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