Scheduler_example5_StatusRequest.ino 4.0 KB

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