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. //Stepper test
  63. forward(50);
  64. delay(3000);
  65. backward(50);
  66. delay(3000);
  67. rotateAntiClockwise(100);
  68. delay(1000);
  69. //rotate backward
  70. rotateClockwise(100);
  71. delay(3000);
  72. //Servo test
  73. //Push button angle: 140
  74. //Servo arm vertical alignment angle: 125
  75. servoCoverPusher.write(90);
  76. delay(1000);
  77. servoSwitchPusher.write(130);
  78. delay(1000);
  79. servoCoverPusher.write(0);
  80. servoSwitchPusher.write(0);
  81. delay(3000);
  82. //Servo install position = 125 (vertical)
  83. //servoSwitchPusher.write(125);
  84. setAnimationCode('j');
  85. delay(10000);
  86. }
  87. }
  88. //Core 1 code, for animation rendering
  89. void AnimationController( void * pvParameters ) {
  90. Serial.println("Animation render started on core " + String(xPortGetCoreID()));
  91. for (;;) {
  92. char anicode = getAnimationCode();
  93. handleAnimationRendering(anicode);
  94. }
  95. }