Scheduler_example05_StatusRequest.ino 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /** This test emulates querying 3 sensors once every 10 seconds, each could respond with a different delay
  2. * (ultrasonic sensors for instance) and printing a min value of the three when all three have reported their values.
  3. * The overall timeout of 1 second is setup as well.
  4. * An error message needs to be printed if a timeout occurred instead of a value.
  5. */
  6. #define _TASK_SLEEP_ON_IDLE_RUN
  7. #define _TASK_STATUS_REQUEST
  8. #include <TaskScheduler.h>
  9. #ifdef ARDUINO_ARCH_STM32F1
  10. #define A0 3
  11. #endif
  12. StatusRequest measure;
  13. Scheduler ts;
  14. // Callback methods prototypes
  15. void CycleCallback();
  16. void MeasureCallback();
  17. bool MeasureEnable();
  18. void MeasureDisable();
  19. void CalcCallback();
  20. void S1Callback(); bool S1Enable();
  21. void S2Callback(); bool S2Enable();
  22. void S3Callback(); bool S3Enable();
  23. // Tasks
  24. Task tCycle(10000, TASK_FOREVER, &CycleCallback, &ts, true);
  25. Task tMeasure(1000, TASK_ONCE, &MeasureCallback, &ts, false, &MeasureEnable, &MeasureDisable);
  26. Task tCalculate(&CalcCallback, &ts);
  27. Task tSensor1(0, TASK_ONCE, &S1Callback, &ts, false, &S1Enable);
  28. Task tSensor2(0, TASK_ONCE, &S2Callback, &ts, false, &S2Enable);
  29. Task tSensor3(0, TASK_ONCE, &S3Callback, &ts, false, &S3Enable);
  30. long distance, d1, d2, d3;
  31. void CycleCallback() {
  32. Serial.println("CycleCallback: Initiating measurement cycle every 10 seconds");
  33. tMeasure.restartDelayed();
  34. }
  35. bool MeasureEnable() {
  36. Serial.println("MeasureEnable: Activating sensors");
  37. distance = 0;
  38. measure.setWaiting(3); // Set the StatusRequest to wait for 3 signals.
  39. tCalculate.waitFor(&measure);
  40. tSensor1.restartDelayed();
  41. tSensor2.restartDelayed();
  42. tSensor3.restartDelayed();
  43. return true;
  44. }
  45. void MeasureCallback() {
  46. Serial.println("MeasureCallback: Invoked by calculate task or one second later");
  47. if (measure.pending()) {
  48. tCalculate.disable();
  49. measure.signalComplete(-1); // signal error
  50. Serial.println("MeasureCallback: Timeout!");
  51. }
  52. else {
  53. Serial.print("MeasureCallback: Min distance=");Serial.println(distance);
  54. }
  55. }
  56. void MeasureDisable() {
  57. Serial.println("MeasureDisable: Cleaning up");
  58. tSensor1.disable();
  59. tSensor2.disable();
  60. tSensor3.disable();
  61. }
  62. void CalcCallback() {
  63. Serial.println("CalcCallback: calculating");
  64. distance = -1;
  65. if ( measure.getStatus() >= 0) { // only calculate if statusrequest ended successfully
  66. distance = d1 < d2 ? d1 : d2;
  67. distance = d3 < distance ? d3 : distance;
  68. tMeasure.forceNextIteration();
  69. }
  70. }
  71. /** Simulation code for sensor 1
  72. * ----------------------------
  73. */
  74. bool S1Enable() {
  75. Serial.print("S1Enable: Triggering sensor1. Delay=");
  76. tSensor1.setInterval( random(1200) ); // Simulating sensor delay, which could go over 1 second and cause timeout
  77. d1 = 0;
  78. Serial.println( tSensor1.getInterval() );
  79. return true;
  80. }
  81. void S1Callback() {
  82. Serial.print("S1Callback: Emulating measurement. d1=");
  83. d1 = random(501); // pick a value from 0 to 500 "centimeters" simulating a measurement
  84. measure.signal();
  85. Serial.println(d1);
  86. }
  87. /** Simulation code for sensor 2
  88. * ----------------------------
  89. */
  90. bool S2Enable() {
  91. Serial.print("S2Enable: Triggering sensor2. Delay=");
  92. tSensor2.setInterval( random(1200) ); // Simulating sensor delay, which could go over 1 second and cause timeout
  93. d2 = 0;
  94. Serial.println( tSensor2.getInterval() );
  95. return true;
  96. }
  97. void S2Callback() {
  98. Serial.print("S2Callback: Emulating measurement. d2=");
  99. d2 = random(501); // pick a value from 0 to 500 "centimeters" simulating a measurement
  100. measure.signal();
  101. Serial.println(d2);
  102. }
  103. /** Simulation code for sensor 3
  104. * ----------------------------
  105. */
  106. bool S3Enable() {
  107. Serial.print("S3Enable: Triggering sensor3. Delay=");
  108. tSensor3.setInterval( random(1200) ); // Simulating sensor delay, which could go over 1 second and cause timeout
  109. d3 = 0;
  110. Serial.println( tSensor3.getInterval() );
  111. return true;
  112. }
  113. void S3Callback() {
  114. Serial.print("S3Callback: Emulating measurement. d3=");
  115. d3 = random(501); // pick a value from 0 to 500 "centimeters" simulating a measurement
  116. measure.signal();
  117. Serial.println(d3);
  118. }
  119. /** Main Arduino code
  120. * Not much is left here - everything is taken care of by the framework
  121. */
  122. void setup() {
  123. Serial.begin(115200);
  124. Serial.println("TaskScheduler StatusRequest Sensor Emulation Test. Complex Test.");
  125. #ifdef ARDUINO_ARCH_STM32F1
  126. pinMode(A0, INPUT_ANALOG);
  127. #endif
  128. randomSeed(analogRead(A0)+millis());
  129. }
  130. void loop() {
  131. ts.execute();
  132. }