TolentinoCotesta

FSM with Servos

Sep 1st, 2020 (edited)
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <Servo.h>
  2. #include "YA_FSM.h"     // https://github.com/cotestatnt/YA_FSM
  3.  
  4. #define SERVO1_PIN      9
  5. #define SERVO2_PIN      10
  6.  
  7. // Target servos position
  8. unsigned int servo1_Target, servo2_Target;
  9.  
  10. // Servos state transitions
  11. bool servo1_Tr1, servo1_Tr2;
  12. bool servo2_Tr1, servo2_Tr2;
  13.  
  14. // States Aliases
  15. enum servo1_State {servo1_State1, servo1_State2};
  16. enum servo2_State {servo2_State1, servo2_State2};
  17.  
  18. // Create new FSM for servo1 (num States, num Transition)
  19. YA_FSM servo1_FSM(2, 2);
  20.  
  21. // Create new FSM for servo2 (num States, num Transition)
  22. YA_FSM servo2_FSM(2, 2);
  23.  
  24. // Create the servo objects
  25. Servo servo1, servo2;
  26.  
  27.  
  28. void setupStateMachine(){
  29.     // Add state transitions
  30.     servo1_FSM.AddTransition(servo1_State1, servo1_State2, servo1_Tr1 );   
  31.     servo1_FSM.AddTransition(servo1_State2, servo1_State1, servo1_Tr2 );
  32.  
  33.     // Add on leaving actions for each state
  34.     servo1_FSM.SetOnLeaving(servo1_State1, [](){servo1_Target = 30;} );
  35.     servo1_FSM.SetOnLeaving(servo1_State2, [](){servo1_Target = 70;} );
  36.  
  37.     // The two servos states has timeout and do nothing (lambda function => [](){})
  38.     servo1_FSM.SetOnState(servo1_State1, [](){}, 6000 );
  39.     servo1_FSM.SetOnState(servo1_State2, [](){}, 3000 );
  40.  
  41.     // Add transitions
  42.     servo2_FSM.AddTransition(servo2_State1, servo2_State2, servo2_Tr1 );   
  43.     servo2_FSM.AddTransition(servo2_State2, servo2_State1, servo2_Tr2 );
  44.  
  45.     // Add on leaving actions for each state
  46.     servo2_FSM.SetOnLeaving(servo2_State1, [](){servo2_Target = 30;} );
  47.     servo2_FSM.SetOnLeaving(servo2_State2, [](){servo2_Target = 70;} );
  48.  
  49.     // The two servos states has timeout and do nothing (lambda function => [](){})
  50.     servo2_FSM.SetOnState(servo2_State1, [](){}, 7000 );
  51.     servo2_FSM.SetOnState(servo2_State2, [](){}, 1000 );
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58. void setup(){  
  59.     Serial.begin(9600);
  60.     Serial.println(F("Starting State Machine...\n"));
  61.  
  62.     setupStateMachine();   
  63.  
  64.     servo1.attach(SERVO1_PIN);
  65.     servo1_Target = 70;
  66.     servo1.write(servo1_Target);  
  67.  
  68.     servo2.attach(SERVO2_PIN);
  69.     servo2_Target = 70;
  70.     servo2.write(servo2_Target);  
  71. }
  72.  
  73. void loop(){
  74.     // Update FSM for servo1
  75.     if( servo1_FSM.Update()){
  76.         Serial.print(millis());
  77.         Serial.print(" - Servo1 pos: ");
  78.         Serial.println(servo1_Target);     
  79.     }
  80.     servo1_Tr1 = servo1_FSM.GetTimeout(servo1_State1);
  81.     servo1_Tr2 = servo1_FSM.GetTimeout(servo1_State2);
  82.  
  83.     // Write position
  84.     servo1.write(servo1_Target);  
  85.    
  86.     // Update FSM for servo2
  87.     if(servo2_FSM.Update()){
  88.         Serial.print(millis());
  89.         Serial.print(" - Servo2 pos: ");
  90.         Serial.println(servo2_Target);
  91.     }
  92.     servo2_Tr1 = servo2_FSM.GetTimeout(servo2_State1);
  93.     servo2_Tr2 = servo2_FSM.GetTimeout(servo2_State2);
  94.  
  95.     // Write position
  96.     servo2.write(servo2_Target);  
  97.  
  98. }
  99.  
  100.  
Add Comment
Please, Sign In to add comment