-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotionStateMachine.cpp
152 lines (136 loc) · 4.42 KB
/
MotionStateMachine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "MotionStateMachine.h"
#include "MotionController.h"
#include <Arduino.h>
Motion::MotionStateMachine::MotionStateMachine(MotionController *controller)
: m_controller(controller), m_currentState(STATE_NONE),
m_requestedState(STATE_NONE)
{
}
void Motion::MotionStateMachine::Step()
{
if (m_currentState != m_requestedState) {
if (m_currentState == Motion::MotionStateMachine::STATE_FAULTED &&
m_requestedState != Motion::MotionStateMachine::STATE_NOT_RUNNING) {
// Ignore transitions in fault that aren't stop
return;
}
switch (m_currentState) {
case Motion::MotionStateMachine::STATE_NOT_RUNNING:
OnStateNotRunningExit();
break;
case Motion::MotionStateMachine::STATE_MOVEMENT_REQUESTED:
OnStateMovementRequestedExit();
break;
case Motion::MotionStateMachine::STATE_MOVING:
OnStateMovingExit();
break;
case Motion::MotionStateMachine::STATE_FAULTED:
OnStateFaultedExit();
break;
default:
break;
}
switch (m_requestedState) {
case Motion::MotionStateMachine::STATE_NOT_RUNNING:
OnStateNotRunningEnter();
break;
case Motion::MotionStateMachine::STATE_MOVEMENT_REQUESTED:
OnStateMovementRequestedEnter();
break;
case Motion::MotionStateMachine::STATE_MOVING:
OnStateMovingEnter();
break;
case Motion::MotionStateMachine::STATE_FAULTED:
OnStateFaultedEnter();
break;
default:
break;
}
m_currentState = m_requestedState;
m_stateStartMillis = millis();
}
switch (m_currentState) {
case Motion::MotionStateMachine::STATE_NOT_RUNNING:
m_requestedState = OnStateNotRunningStep();
break;
case Motion::MotionStateMachine::STATE_MOVEMENT_REQUESTED:
m_requestedState = OnStateMovementRequestedStep();
break;
case Motion::MotionStateMachine::STATE_MOVING:
m_requestedState = OnStateMovingStep();
break;
case Motion::MotionStateMachine::STATE_FAULTED:
m_requestedState = OnStateFaultedStep();
break;
default:
break;
}
}
void Motion::MotionStateMachine::OnStateNotRunningEnter()
{
m_controller->StopMovement();
}
Motion::MotionStateMachine::STATE
Motion::MotionStateMachine::OnStateNotRunningStep()
{
// If there is a fault, transition to no movement
if (Fault::Handler::instance()->hasFault()) {
return Motion::MotionStateMachine::STATE_FAULTED;
}
return STATE_NOT_RUNNING;
}
void Motion::MotionStateMachine::OnStateNotRunningExit()
{
// no - op
}
void Motion::MotionStateMachine::OnStateMovementRequestedEnter()
{
Serial.println("Waiting for stabilization");
}
Motion::MotionStateMachine::STATE
Motion::MotionStateMachine::OnStateMovementRequestedStep()
{
// If there is a fault, transition to no movement
if (Fault::Handler::instance()->hasFault()) {
return Motion::MotionStateMachine::STATE_FAULTED;
}
// It is ok to move if stability has been reached once
if (m_controller->CheckStabilityStep())
return Motion::MotionStateMachine::STATE_MOVING;
return Motion::MotionStateMachine::STATE_MOVEMENT_REQUESTED;
}
void Motion::MotionStateMachine::OnStateMovementRequestedExit()
{
Serial.println("Stabilization target reached");
}
void Motion::MotionStateMachine::OnStateMovingEnter()
{
m_controller->StartMovement();
}
Motion::MotionStateMachine::STATE
Motion::MotionStateMachine::OnStateMovingStep()
{
// If there is a fault, transition to no movement
if (Fault::Handler::instance()->hasFault()) {
return Motion::MotionStateMachine::STATE_FAULTED;
}
m_controller->MovementAlgorithmStep();
return Motion::MotionStateMachine::STATE_MOVING;
}
void Motion::MotionStateMachine::OnStateMovingExit()
{
m_controller->StopMovement();
}
void Motion::MotionStateMachine::OnStateFaultedEnter()
{
Serial.println("Fault state entered");
}
Motion::MotionStateMachine::STATE
Motion::MotionStateMachine::OnStateFaultedStep()
{
return Motion::MotionStateMachine::STATE_FAULTED;
}
void Motion::MotionStateMachine::OnStateFaultedExit()
{
//
}