forked from s-nakaoka/openhrp-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
OpenHRPControllerItem.cpp
271 lines (212 loc) · 7.27 KB
/
OpenHRPControllerItem.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
/*!
@file
@author Shin'ichiro Nakaoka
*/
#include "OpenHRPControllerItem.h"
#include "DynamicsSimulator_impl.h"
#include <cnoid/ControllerIO>
#include <cnoid/MessageView>
#include <cnoid/Archive>
#include <cnoid/Sleep>
#include <cnoid/PutPropertyFunction>
#include <fmt/format.h>
#include <iostream>
#include "gettext.h"
#if __cplusplus >= 201703L
#include <filesystem>
#else
#include <boost/filesystem.hpp>
namespace filesystem = boost::filesystem;
#endif
using namespace std;
using namespace std::placeholders;
using namespace cnoid;
using namespace OpenHRP;
using fmt::format;
OpenHRPControllerItem::OpenHRPControllerItem()
{
signalReadyStandardOutputConnected = false;
mv = MessageView::instance();
}
OpenHRPControllerItem::OpenHRPControllerItem(const OpenHRPControllerItem& org)
: ControllerItem(org)
{
controllerServerName = org.controllerServerName;
controllerServerCommand = org.controllerServerCommand;
signalReadyStandardOutputConnected = false;
mv = MessageView::instance();
}
OpenHRPControllerItem::~OpenHRPControllerItem()
{
controllerServerProcess.waitForFinished(100);
}
void OpenHRPControllerItem::onDisconnectedFromRoot()
{
if(controllerServerProcess.state() != QProcess::NotRunning){
controllerServerProcess.kill();
}
ControllerItem::onDisconnectedFromRoot();
}
Item* OpenHRPControllerItem::doDuplicate() const
{
return new OpenHRPControllerItem(*this);
}
void OpenHRPControllerItem::setControllerServerName(const std::string& name)
{
controllerServerName = name;
}
void OpenHRPControllerItem::setControllerServerCommand(const std::string& command)
{
controllerServerCommand = command;
}
bool OpenHRPControllerItem::initialize(ControllerIO* io)
{
ncHelper = getDefaultNamingContextHelper();
if(!ncHelper->isAlive()){
return false;
}
#ifdef OPENHRP_3_0
typedef OpenHRP::ControllerFactory_var ControllerServer_var;
typedef OpenHRP::ControllerFactory ControllerServer;
#elif OPENHRP_3_1
typedef OpenHRP::Controller_var ControllerServer_var;
typedef OpenHRP::Controller ControllerServer;
#endif
ControllerServer_var server = ncHelper->findObject<ControllerServer>(controllerServerName.c_str());
// do null check here
bool serverReady = ncHelper->isObjectAlive(server);
if(!serverReady){
// invoke the controller command
if(!controllerServerCommand.empty()){
if(controllerServerProcess.state() != QProcess::NotRunning){
controllerServerProcess.kill();
controllerServerProcess.waitForFinished(100);
}
string command(controllerServerCommand);
#ifdef _WIN32
if(filesystem::path(controllerServerCommand).extension() != ".exe"){
command += ".exe";
}
// quote the command string to support a path including spaces
controllerServerProcess.start(QString("\"") + command.c_str() + "\"", QStringList{});
#else
controllerServerProcess.start(command.c_str(), QStringList{});
#endif
if(!controllerServerProcess.waitForStarted()){
mv->put(format(_("Controller server process \"{}\" cannot be executed."), command));
if(!filesystem::exists(command)){
mv->putln(_(" This file does not exist."));
} else {
mv->putln("");
}
} else {
mv->putln(format(_("Controller server process \"{0}\" has been executed by {1}."),
command, name()));
for(int i=0; i < 20; ++i){
controllerServerProcess.waitForReadyRead(10);
server = ncHelper->findObject<ControllerServer>(controllerServerName.c_str());
serverReady = ncHelper->isObjectAlive(server);
if(serverReady){
if(!signalReadyStandardOutputConnected){
controllerServerProcess.sigReadyReadStandardOutput().connect(
std::bind(&OpenHRPControllerItem::onReadyReadServerProcessOutput, this));
signalReadyStandardOutputConnected = true;
}
break;
}
}
}
}
}
if(!serverReady){
mv->putln(format(_("Controller server object \"{}\" is not found in the name server."),
controllerServerName));
if(controllerServerProcess.state() != QProcess::NotRunning){
controllerServerProcess.kill();
}
return false;
}
Body* body = io->body();
#ifdef OPENHRP_3_0
controller = server->create(body->name().c_str());
// do null check here
mv->putln(format(_("The CORBA object of controller \"{0}\" has been created by the factory \"{1}\"."),
name(), controllerServerName));
#elif OPENHRP_3_1
controller = server;
controller->setModelName(body->name().c_str());
controller->initialize();
mv->putln(format(_("The CORBA object \"{0}\" of controller \"{1}\" has been obtained."),
controllerServerName, name()));
#endif
timeStep_ = io->timeStep();
dynamicsSimulator.reset(new DynamicsSimulator_impl(body));
controller->setDynamicsSimulator(dynamicsSimulator->_this());
controller->setTimeStep(timeStep_);
return true;
}
bool OpenHRPControllerItem::start()
{
controller->start();
return true;
}
double OpenHRPControllerItem::timeStep() const
{
return timeStep_;
}
void OpenHRPControllerItem::input()
{
controller->input();
}
bool OpenHRPControllerItem::control()
{
controller->control();
return true;
}
void OpenHRPControllerItem::output()
{
controller->output();
}
void OpenHRPControllerItem::stop()
{
controller->stop();
#ifdef OPENHRP_3_0
controller->destroy();
#endif
if(controllerServerProcess.state() != QProcess::NotRunning){
#ifdef OPENHRP_3_1
controller->destroy();
#endif
controllerServerProcess.kill();
}
}
void OpenHRPControllerItem::onReadyReadServerProcessOutput()
{
MessageView::instance()->put(QString(controllerServerProcess.readAll()));
}
void OpenHRPControllerItem::doPutProperties(PutPropertyFunction& putProperty)
{
ControllerItem::doPutProperties(putProperty);
putProperty(_("Controller server name"), controllerServerName,
[&](const string& name){ setControllerServerName(name); return true; });
putProperty(_("Controller server command"), controllerServerCommand,
[&](const string& command){ setControllerServerCommand(command); return true; });
}
bool OpenHRPControllerItem::store(Archive& archive)
{
if(!ControllerItem::store(archive)){
return false;
}
archive.write("controllerServerName", controllerServerName);
archive.writeRelocatablePath("controllerServerCommand", controllerServerCommand);
return true;
}
bool OpenHRPControllerItem::restore(const Archive& archive)
{
if(!ControllerItem::restore(archive)){
return false;
}
setControllerServerName(archive.get("controllerServerName", controllerServerName));
archive.readRelocatablePath("controllerServerCommand", controllerServerCommand);
return true;
}