-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProstateRobotPhaseBase.cpp
280 lines (253 loc) · 8.47 KB
/
ProstateRobotPhaseBase.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "ProstateRobotPhaseBase.hpp"
ProstateRobotPhaseBase::ProstateRobotPhaseBase()
{
this->NextWorkphase.clear();
}
ProstateRobotPhaseBase::~ProstateRobotPhaseBase()
{
}
int ProstateRobotPhaseBase::Enter(const char *queryID)
{
// Send acknowledgement message with query ID
std::stringstream ss;
std::string time(queryID);
ss << kCommunicationCmds.ACKNOWLEDGE << queryID;
this->SendStringMessage(ss.str().c_str(), this->Name());
Logger &log = Logger::GetInstance();
log.Log("Changed Workphase to " + string(this->Name()), ss.str().substr(4, std::string::npos), logger::INFO, false);
// Send phase message
this->SendStatusMessage(kMsgBody.CURRENT_STATUS.c_str(), 1, 0, this->Name());
return this->Initialize();
}
int ProstateRobotPhaseBase::Process()
{
// Create a message buffer to receive header
igtl::MessageHeader::Pointer headerMsg;
headerMsg = igtl::MessageHeader::New();
ReceiveMessageHeader(headerMsg, 0);
// If there is any workphase change request,
// set NextWorkphase (done in the subroutine) and return 1.
if (this->CheckWorkphaseChange(headerMsg))
{
return 1;
}
// If NOT empty message
if (strcmp(headerMsg->GetDeviceName(), "") != 0)
{
this->NextWorkphase = this->Name();
}
// Common messages are handled here.
if (!MessageHandler(headerMsg))
{
this->CheckCommonMessage(headerMsg);
}
return 0;
}
int ProstateRobotPhaseBase::MessageHandler(igtl::MessageHeader *headerMsg)
{
return 0;
}
int ProstateRobotPhaseBase::CheckWorkphaseChange(igtl::MessageHeader *headerMsg)
{
// Check if the message requests phase transition
if (strcmp(headerMsg->GetDeviceType(), kMsgTypes.STRING.c_str()) == 0 &&
strncmp(headerMsg->GetDeviceName(), kCommunicationCmds.COMMAND.c_str(), 4) == 0)
{
igtl::StringMessage::Pointer stringMsg;
stringMsg = igtl::StringMessage::New();
stringMsg->SetMessageHeader(headerMsg);
stringMsg->AllocatePack();
bool timeout(false);
// The code waits here for new messages received from the socket
int r = this->Socket->Receive(stringMsg->GetPackBodyPointer(), stringMsg->GetPackBodySize(), timeout);
if (r < 0)
{
std::cerr << "ERROR: Timeout." << std::endl;
this->Socket->CloseSocket();
return 0;
}
else if (r == 0)
{
std::cerr << "ERROR: Socket closed while reading a message." << std::endl;
this->Socket->CloseSocket();
return 0;
}
// Deserialize the string message
// If you want to skip CRC check, call Unpack() without argument.
int c = stringMsg->Unpack(1);
if (c & igtl::MessageHeader::UNPACK_BODY) // if CRC check is OK
{
if (stringMsg->GetEncoding() == 3)
{
this->NextWorkphase = stringMsg->GetString();
// Get the query ID
std::string msgName = headerMsg->GetDeviceName();
this->QueryID = msgName.substr(4, std::string::npos);
return 1;
}
else
{
this->NextWorkphase = "Unknown";
return 1;
}
}
else
{
std::cerr << "ERROR: Invalid CRC." << std::endl;
this->NextWorkphase = "Unknown";
return 1;
}
}
else
{
return 0;
}
}
// Checks for general communication messages that can be triggered from Slicer regardless of the current state.
int ProstateRobotPhaseBase::CheckCommonMessage(igtl::MessageHeader *headerMsg)
{
// Check if the received message is string
if (strcmp(headerMsg->GetDeviceType(), kMsgTypes.STRING.c_str()) == 0)
{
string string_msg_content;
ReceiveString(headerMsg, string_msg_content);
if (strcmp(headerMsg->GetDeviceName(), kMsgBody.CURRENT_POSITION.c_str()) == 0)
{
// Get the current position of the robot's needle tip
igtl::Matrix4x4 currentPosition;
RStatus->GetCurrentPosition(currentPosition);
// Send navigation your current needle tip position
SendTransformMessage(kMsgBody.CURRENT_POSITION.c_str(), currentPosition);
// Send string msg flag for targeting position
SendStringMessage(kMsgBody.IS_IN_TARGETING_POS.c_str(), to_string(robot->clinical_mode->isInTargetingPos()).c_str());
// Send string msg flag for insertion depth
SendStringMessage(kMsgBody.HAS_REACHED_TARGET.c_str(), to_string(robot->clinical_mode->hasReachedTarget()).c_str());
return 1;
}
else if (strcmp(headerMsg->GetDeviceName(), kMsgBody.CURRENT_STATUS.c_str()) == 0)
{
this->SendStatusMessage(this->Name(), igtl::StatusMessage::STATUS_OK, 0);
return 1;
}
// else if (strcmp(headerMsg->GetDeviceName(), kMsgBody.SET_DEADBAND.c_str()) == 0)
// {
// try
// {
// int deadband = std::stoi(string_msg_content);
// RStatus->SetDeadband(deadband);
// Logger &log = Logger::GetInstance();
// log.Log("Deadband of " + string_msg_content + " was received and set in the code.", logger::INFO, true);
// }
// catch(const std::invalid_argument &e)
// {
// std::cerr << "Bad deadband value. Message ignored!" << std::endl;
// }
// return 1;
// }
else if (strcmp(headerMsg->GetDeviceName(), kMsgBody.ALPHA.c_str()) == 0)
{
try
{
double alpha_d = std::stod(string_msg_content);
RStatus->SetAlpha(alpha_d);
}
catch(const std::invalid_argument&e)
{
std::cerr << e.what() << std::endl;
}
return 1;
}
// else if (strcmp(headerMsg->GetDeviceName(), kMsgBody.TARGET_ANGLE.c_str()) == 0)
// {
// try
// {
// double theta_d = std::stod(string_msg_content);
// RStatus->SetTargetAngle(theta_d);
// }
// catch(const std::invalid_argument&e)
// {
// std::cerr << e.what() << std::endl;
// }
// return 1;
// }
else if (strcmp(headerMsg->GetDeviceName(), kMsgBody.CURV_METHOD.c_str()) == 0)
{
try
{
this->RStatus->SetCurvMethod(string_msg_content);
SendStatusMessage(kMsgBody.CURV_METHOD.c_str(), igtl::StatusMessage::STATUS_OK, 0);
}
catch(const std::invalid_argument&e)
{
std::cerr << e.what() << std::endl;
SendStatusMessage(kMsgBody.CURV_METHOD.c_str(), igtl::StatusMessage::STATUS_INVALID, 0);
}
// Send acknowledgment for successful needle retraction.
return 1;
}
// else if (strcmp(headerMsg->GetDeviceName(), kMsgBody.SET_STEERING.c_str()) == 0)
// {
// try
// {
// RStatus->SetSteeringMethod(string_msg_content);
// }
// catch(const std::invalid_argument &e)
// {
// std::cerr << e.what() << std::endl;
// }
// return 1;
// }
// Catch all undefined string messages
else
{
std::string msg_name = headerMsg->GetDeviceName();
// Acknowledge receipt of the undefined message with INVALID status
std::stringstream ss;
ss << kCommunicationCmds.ACKNOWLEDGE.c_str() << msg_name.c_str();
SendStatusMessage(ss.str().c_str(), igtl::StatusMessage::STATUS_INVALID, 0);
Logger &log = Logger::GetInstance();
string message = "STRING message: " + msg_name + " was received but was not expected. Message is discarded.";
log.Log(message, logger::WARNING, true);
return 1;
}
}
else if (strcmp(headerMsg->GetDeviceType(), kMsgTypes.STATUS.c_str()) == 0)
{
int code;
int subcode;
std::string name;
std::string status;
ReceiveStatus(headerMsg, code, subcode, name, status);
return 1;
}
else if (strcmp(headerMsg->GetDeviceType(), kMsgTypes.TRANSFORM.c_str()) == 0)
{
igtl::Matrix4x4 transform;
// Update the base to zframe
Logger &log = Logger::GetInstance();
string message = "TRANSFORM message: " + string(headerMsg->GetDeviceName()) + " was received but was not expected. Message is discarded.";
log.Log(message, logger::WARNING, true);
ReceiveTransform(headerMsg,transform);
return 1;
}
return 0;
}
void ProstateRobotPhaseBase::OnExit()
{
// specific behaviour should be defined in child classes
}
/* This method checks if the transition to a specific desired workphase is allowed and returns true for allowed transitions.*/
bool ProstateRobotPhaseBase::IsTransitionAllowed(const std::string &desired_transition)
{
for (auto state : forbidden_transition_list)
{
if (desired_transition == state)
{
Logger &log = Logger::GetInstance();
string message = "Transition from " + string(this->Name()) + " to " + desired_transition + " is not allowed!";
log.Log(message, logger::ERROR);
return false;
}
}
return true;
};