Scheduler_example05_StatusRequest.ino 4.3 KB

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