tasks.ino 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /* Multi-core process definations */
  32. void startCoreTasks() {
  33. //core 1
  34. xTaskCreatePinnedToCore(
  35. AnimationController, /* Task function. */
  36. "animator", /* name of task. */
  37. 10000, /* Stack size of task */
  38. NULL, /* parameter of the task */
  39. 1, /* priority of the task */
  40. &animationTask, /* Task handle to keep track of created task */
  41. 1
  42. );
  43. delay(500);
  44. //core 0
  45. xTaskCreatePinnedToCore(
  46. PrimaryController, /* Task function. */
  47. "primary", /* name of task. */
  48. 10000, /* Stack size of task */
  49. NULL, /* parameter of the task */
  50. 1, /* priority of the task */
  51. &primaryTask, /* Task handle to keep track of created task */
  52. 0
  53. ); /* pin task to core 0 */
  54. }
  55. //Core 0 code, for movement and primary logics
  56. void PrimaryController( void * pvParameters ) {
  57. Serial.println("Primary logic process started on core " + String(xPortGetCoreID()));
  58. for (;;) {
  59. //Loop of primary logics
  60. //setAnimationCode('a');
  61. //delay(10000);
  62. /*
  63. //Stepper test
  64. forward(50);
  65. delay(3000);
  66. backward(50);
  67. delay(3000);
  68. rotateAntiClockwise(100);
  69. delay(1000);
  70. //rotate backward
  71. rotateClockwise(100);
  72. delay(3000);
  73. */
  74. //Servo test
  75. //Push button angle: 140
  76. //Servo arm vertical alignment angle: 125
  77. servoCoverPusher.write(90);
  78. delay(1000);
  79. servoSwitchPusher.write(130);
  80. delay(1000);
  81. servoCoverPusher.write(0);
  82. servoSwitchPusher.write(0);
  83. delay(3000);
  84. //Servo install position = 125 (vertical)
  85. //servoSwitchPusher.write(125);
  86. setAnimationCode('j');
  87. delay(10000);
  88. }
  89. }
  90. //Core 1 code, for animation rendering
  91. void AnimationController( void * pvParameters ) {
  92. Serial.println("Animation render started on core " + String(xPortGetCoreID()));
  93. for (;;) {
  94. char anicode = getAnimationCode();
  95. handleAnimationRendering(anicode);
  96. }
  97. }