Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lfr_state_machine.h 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. class LFR_StateMachine;
  3. class LFR_State
  4. {
  5. public:
  6. virtual void enter(LFR_StateMachine* stateMachine) = 0;
  7. virtual void exit(LFR_StateMachine* stateMachine) = 0;
  8. virtual ~LFR_State() {}
  9. };
  10. namespace State
  11. {
  12. class Idle: public LFR_State
  13. {
  14. Idle() {}
  15. Idle(const Idle& other) {};
  16. Idle& operator= (const Idle& other);
  17. public:
  18. void enter(LFR_StateMachine*);
  19. void exit(LFR_StateMachine*);
  20. static LFR_State& getInstance();
  21. };
  22. class Autonomous: public LFR_State
  23. {
  24. Autonomous() {}
  25. Autonomous(const Autonomous& other) {};
  26. Autonomous& operator= (const Autonomous& other);
  27. public:
  28. void enter(LFR_StateMachine*);
  29. void exit(LFR_StateMachine*);
  30. static LFR_State& getInstance();
  31. };
  32. class Manual: public LFR_State
  33. {
  34. Manual() {}
  35. Manual(const Manual& other) {};
  36. Manual& operator= (const Manual& other);
  37. public:
  38. void enter(LFR_StateMachine*);
  39. void exit(LFR_StateMachine*);
  40. static LFR_State& getInstance();
  41. };
  42. }
  43. class LFR_StateMachine
  44. {
  45. LFR_State* currentState;
  46. public:
  47. LFR_StateMachine(){
  48. setState(State::Idle::getInstance());
  49. }
  50. inline LFR_State* getCurrentState() const {return currentState;}
  51. void setState(LFR_State& newState)
  52. {
  53. currentState->exit(this);
  54. currentState = &newState;
  55. currentState->enter(this);
  56. }
  57. };