-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriveStateController.cpp
More file actions
184 lines (169 loc) · 5.34 KB
/
DriveStateController.cpp
File metadata and controls
184 lines (169 loc) · 5.34 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "DriveStateController.hpp"
#include "CANopen.hpp"
bool DriveStateController::transition_state(const DriveState_en &from, const DriveState_en &to)
{
uint8_t bytes_received = 0U;
uint16_t control_word = 0U;
auto rx_sdo_1 = m_canopen.sdo_read(OBJ_Control_Word, 0, nullptr, 0, &control_word, &bytes_received);
if (rx_sdo_1.reserved_0) {
printf("Timeout waiting to read control word in transition_state\n");
return false;
}
bool transition_ok = DriveStateMachine::valid_transition(from, to, &control_word);
if (!transition_ok || control_word >= CW_MAX)
{
printf("Invalid transition from %s to %s\n", DriveStateMachine::drive_state_to_string(from),
DriveStateMachine::drive_state_to_string(to));
return false;
}
uint16_t status_word = 0U;
auto rx_sdo_2 = m_canopen.sdo_write(OBJ_Control_Word, 0, &control_word, sizeof(control_word));
if (rx_sdo_2.reserved_0) {
printf("Timeout waiting to clear control word in transition_state\n");
return false;
}
if (get_current_state() != DriveState_en::DS_SWITCHED_ON)
{
printf("Not transitioned from %s to %s\n", DriveStateMachine::drive_state_to_string(from),
DriveStateMachine::drive_state_to_string(to));
return false;
}
return true;
}
bool DriveStateController::handle_not_ready()
{
printf("Drive State: NOT READY, still initialising\n");
return false;
}
bool DriveStateController::handle_switch_on_disabled()
{
switch (m_target_state)
{
case DriveState_en::DS_SWITCH_ON_READY:
case DriveState_en::DS_SWITCHED_ON:
case DriveState_en::DS_OPERATION_ENABLED: {
printf("Drive State: SWITCH ON DISABLED, attempting to transition to SWITCH ON READY\n");
return transition_state(DriveState_en::DS_SWITCH_ON_DISABLED, DriveState_en::DS_SWITCH_ON_READY);
}
default: {
return false;
}
}
}
bool DriveStateController::handle_switch_on_ready()
{
switch (m_target_state)
{
case DriveState_en::DS_SWITCH_ON_READY: {
printf("Drive State: SWITCH ON READY, staying in SWITCH ON READY\n");
return true;
}
case DriveState_en::DS_SWITCHED_ON:
case DriveState_en::DS_OPERATION_ENABLED: {
printf("Drive State: SWITCH ON READY, attempting to transition to SWITCHED ON\n");
return transition_state(DriveState_en::DS_SWITCH_ON_READY, DriveState_en::DS_SWITCHED_ON);
}
default: {
return false;
}
}
}
bool DriveStateController::handle_switched_on()
{
switch (m_target_state)
{
case DriveState_en::DS_SWITCH_ON_READY:
case DriveState_en::DS_SWITCHED_ON:
case DriveState_en::DS_OPERATION_ENABLED: {
printf("Drive State: SWITCHED ON, attempting to transition to %s\n",
DriveStateMachine::drive_state_to_string(m_target_state));
return transition_state(DriveState_en::DS_SWITCHED_ON, m_target_state);
}
default: {
return false;
}
}
}
bool DriveStateController::handle_operation_enabled()
{
switch (m_target_state)
{
case DriveState_en::DS_SWITCH_ON_READY:
case DriveState_en::DS_SWITCHED_ON: {
printf("Drive State: OPERATION ENABLED, attempting to transition to SWITCHED ON\n");
return transition_state(DriveState_en::DS_OPERATION_ENABLED, DriveState_en::DS_SWITCHED_ON);
}
case DriveState_en::DS_OPERATION_ENABLED: {
printf("Drive State: OPERATION ENABLED, staying in OPERATION ENABLED\n");
return true;
}
default: {
return false;
}
}
}
bool DriveStateController::handle_quick_stop_active()
{
switch (m_target_state)
{
case DriveState_en::DS_SWITCH_ON_READY:
case DriveState_en::DS_SWITCHED_ON:
case DriveState_en::DS_OPERATION_ENABLED: {
printf("Drive State: QUICK STOP ACTIVE, attempting to transition to SWITCH ON DISABLED\n");
return transition_state(DriveState_en::DS_QUICK_STOP_ACTIVE, DriveState_en::DS_SWITCH_ON_DISABLED);
}
default: {
return false;
}
}
}
bool DriveStateController::handle_fault()
{
printf("Drive State: FAULT, attempting to transition to SWITCH ON DISABLED\n");
return transition_state(DriveState_en::DS_FAULT, DriveState_en::DS_SWITCH_ON_DISABLED);
}
bool DriveStateController::handle_fault_reaction_active()
{
printf("Drive State: FAULT REACTION ACTIVE, waiting for fault reaction to complete\n");
return false;
}
void DriveStateController::update()
{
using DriveState_en = DriveStateMachine::DriveState_en;
switch (get_current_state())
{
case DriveState_en::DS_NOT_READY: {
handle_not_ready();
break;
}
case DriveState_en::DS_SWITCH_ON_DISABLED: {
handle_switch_on_disabled();
break;
}
case DriveState_en::DS_SWITCH_ON_READY: {
handle_switch_on_ready();
break;
}
case DriveState_en::DS_SWITCHED_ON: {
handle_switched_on();
break;
}
case DriveState_en::DS_OPERATION_ENABLED: {
handle_operation_enabled();
break;
}
case DriveState_en::DS_QUICK_STOP_ACTIVE: {
handle_quick_stop_active();
break;
}
case DriveState_en::DS_FAULT: {
handle_fault();
break;
}
case DriveState_en::DS_FAULT_REACTION_ACTIVE: {
handle_fault_reaction_active();
break;
}
default: break;
}
}