tasks.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Multi-core Task Handler*/
  2. TaskHandle_t primaryTask;
  3. TaskHandle_t animationTask;
  4. SemaphoreHandle_t animationMutex;
  5. /* Semaphore for handling mutex */
  6. void createSemaphore() {
  7. animationMutex = xSemaphoreCreateMutex();
  8. xSemaphoreGive(animationMutex);
  9. }
  10. void mutexLock() {
  11. xSemaphoreTake(animationMutex, portMAX_DELAY);
  12. }
  13. void mutexUnlock() {
  14. xSemaphoreGive(animationMutex);
  15. }
  16. //Set the animation code, suppose to be used in core 0 only
  17. void setAnimationCode(char filename) {
  18. Serial.println("Updating animation frame to " + String(filename));
  19. mutexLock();
  20. animation = filename;
  21. mutexUnlock();
  22. }
  23. //Get the animation code, suppose to be used in core 1 only
  24. char getAnimationCode() {
  25. char anicode = 'a';
  26. mutexLock();
  27. anicode = animation;
  28. mutexUnlock();
  29. return anicode;
  30. }
  31. //Get the current state of the switch
  32. // true = After human pushed
  33. // false = After robot pushed
  34. bool getSwitchState(){
  35. int switchState = digitalRead(TOGGLE_SWITCH);
  36. return (switchState == 1);
  37. }
  38. /* Multi-core process definations */
  39. void startCoreTasks() {
  40. //core 1
  41. xTaskCreatePinnedToCore(
  42. AnimationController, /* Task function. */
  43. "animator", /* name of task. */
  44. 10000, /* Stack size of task */
  45. NULL, /* parameter of the task */
  46. 1, /* priority of the task */
  47. &animationTask, /* Task handle to keep track of created task */
  48. 1
  49. );
  50. delay(500);
  51. //core 0
  52. xTaskCreatePinnedToCore(
  53. PrimaryController, /* Task function. */
  54. "primary", /* name of task. */
  55. 10000, /* Stack size of task */
  56. NULL, /* parameter of the task */
  57. 1, /* priority of the task */
  58. &primaryTask, /* Task handle to keep track of created task */
  59. 0
  60. ); /* pin task to core 0 */
  61. }
  62. //Core 0 code, for movement and primary logics
  63. void PrimaryController( void * pvParameters ) {
  64. Serial.println("Primary logic process started on core " + String(xPortGetCoreID()));
  65. clearFrame();
  66. for (;;) {
  67. //Loop of primary logics
  68. //setAnimationCode('a');
  69. //delay(10000);
  70. //Stepper test
  71. /*
  72. forward(50);
  73. delay(3000);
  74. backward(50);
  75. delay(3000);
  76. rotateAntiClockwise(100);
  77. delay(1000);
  78. //rotate backward
  79. rotateClockwise(100);
  80. delay(3000);
  81. //Servo test
  82. //Push button angle: 140
  83. //Servo arm vertical alignment angle: 125
  84. servoCoverPusher.write(90);
  85. delay(1000);
  86. servoSwitchPusher.write(130);
  87. delay(1000);
  88. servoCoverPusher.write(0);
  89. servoSwitchPusher.write(0);
  90. delay(3000);
  91. */
  92. bool switchPushed = getSwitchState();
  93. if (switchPushed){
  94. //Human pushed
  95. setAnimationCode('a');
  96. delay(4000);
  97. Serial.println("ON");
  98. pushSwitchNow();
  99. clearFrame();
  100. delay(3000);
  101. }else{
  102. delay(100);
  103. }
  104. //Servo install position = 125 (vertical)
  105. //servoSwitchPusher.write(125);
  106. //setAnimationCode('j');
  107. //delay(10000);
  108. }
  109. }
  110. //Core 1 code, for animation rendering
  111. void AnimationController( void * pvParameters ) {
  112. Serial.println("Animation render started on core " + String(xPortGetCoreID()));
  113. for (;;) {
  114. char anicode = getAnimationCode();
  115. handleAnimationRendering(anicode);
  116. }
  117. }