controllerclientcpp  0.6.1
 全て クラス ネームスペース ファイル 関数 変数 型定義 列挙型 列挙型の値 マクロ定義 ページ
mujinmovetool.cpp
説明を見る。
1 // -*- coding: utf-8 -*-
10 #include <iostream>
11 
12 #if defined(_WIN32) || defined(_WIN64)
13 #undef GetUserName // clashes with ControllerClient::GetUserName
14 #endif // defined(_WIN32) || defined(_WIN64)
15 
16 
17 using namespace mujinclient;
18 
19 #include <boost/program_options.hpp>
20 
21 namespace bpo = boost::program_options;
22 using namespace std;
23 
24 static string s_robotname;
25 
31 bool ParseOptions(int argc, char ** argv, bpo::variables_map& opts)
32 {
33  // parse command line arguments
34  bpo::options_description desc("Options");
35 
36  desc.add_options()
37  ("help,h", "produce help message")
38  ("controller_hostname", bpo::value<string>()->required(), "hostname or ip of the mujin controller, e.g. controllerXX or 192.168.0.1")
39  ("controller_port", bpo::value<unsigned int>()->default_value(80), "port of the mujin controller")
40  ("slave_request_id", bpo::value<string>()->default_value(""), "request id of the mujin slave, e.g. controller20_slave0. If empty, uses ")
41  ("controller_username_password", bpo::value<string>()->default_value("testuser:pass"), "username and password to the mujin controller, e.g. username:password")
42  ("controller_command_timeout", bpo::value<double>()->default_value(10), "command timeout in seconds, e.g. 10")
43  ("locale", bpo::value<string>()->default_value("en_US"), "locale to use for the mujin controller client")
44  ("task_scenepk", bpo::value<string>()->default_value(""), "scene pk of the binpicking task on the mujin controller, e.g. officeboltpicking.mujin.dae.")
45  ("robotname", bpo::value<string>()->default_value(""), "robot name.")
46  ("taskparameters", bpo::value<string>()->default_value("{}"), "binpicking task parameters, e.g. {'robotname': 'robot', 'robots':{'robot': {'externalCollisionIO':{}, 'gripperControlInfo':{}, 'robotControllerUri': '', robotDeviceIOUri': '', 'toolname': 'tool'}}}")
47  ("zmq_port", bpo::value<unsigned int>()->default_value(11000), "port of the binpicking task on the mujin controller")
48  ("heartbeat_port", bpo::value<unsigned int>()->default_value(11001), "port of the binpicking task's heartbeat signal on the mujin controller")
49 
50  ("toolname", bpo::value<string>()->default_value(""), "tool name, e.g. flange")
51  ("goaltype", bpo::value<string>()->default_value("transform6d"), "mode to move tool with. Either transform6d or translationdirection5d")
52  ("goals", bpo::value<vector<double> >()->multitoken(), "goal to move tool to, \'X Y Z RX RY RZ\'. Units are in mm and deg.")
53  ("speed", bpo::value<double>()->default_value(0.1), "speed to move at")
54  ("movelinear", bpo::value<bool>()->default_value(false), "whether to move tool linearly")
55  ;
56 
57  try {
58  bpo::store(bpo::parse_command_line(argc, argv, desc, bpo::command_line_style::unix_style ^ bpo::command_line_style::allow_short), opts);
59  }
60  catch (const exception& ex) {
61  stringstream errss;
62  errss << "Caught exception " << ex.what();
63  cerr << errss.str() << endl;
64  return false;
65  }
66 
67  bool badargs = false;
68  try {
69  bpo::notify(opts);
70  }
71  catch(const exception& ex) {
72  stringstream errss;
73  errss << "Caught exception " << ex.what();
74  cerr << errss.str() << endl;
75  badargs = true;
76  }
77 
78  if(opts.count("help") || badargs) {
79  cout << "Usage: " << argv[0] << " [OPTS]" << endl;
80  cout << endl;
81  cout << desc << endl;
82  return false;
83  }
84  return true;
85 }
86 
90 void InitializeTask(const bpo::variables_map& opts,
91  BinPickingTaskResourcePtr& pBinpickingTask)
92 {
93  const string controllerUsernamePass = opts["controller_username_password"].as<string>();
94  const double controllerCommandTimeout = opts["controller_command_timeout"].as<double>();
95  const string taskparameters = opts["taskparameters"].as<string>();
96  const string locale = opts["locale"].as<string>();
97  const unsigned int taskZmqPort = opts["zmq_port"].as<unsigned int>();
98  const string hostname = opts["controller_hostname"].as<string>();
99  const unsigned int controllerPort = opts["controller_port"].as<unsigned int>();
100  stringstream urlss;
101  urlss << "http://" << hostname << ":" << controllerPort;
102 
103  const unsigned int heartbeatPort = opts["heartbeat_port"].as<unsigned int>();
104  string slaverequestid = opts["slave_request_id"].as<string>();
105  string taskScenePk = opts["task_scenepk"].as<string>();
106 
107  const bool needtoobtainfromheatbeat = taskScenePk.empty() || slaverequestid.empty();
108  if (needtoobtainfromheatbeat) {
109  stringstream endpoint;
110  endpoint << "tcp:\/\/" << hostname << ":" << heartbeatPort;
111  cout << "connecting to heartbeat at " << endpoint.str() << endl;
112  string heartbeat;
113  const size_t num_try_heartbeat(5);
114  for (size_t it_try_heartbeat = 0; it_try_heartbeat < num_try_heartbeat; ++it_try_heartbeat) {
115  heartbeat = utils::GetHeartbeat(endpoint.str());
116  if (!heartbeat.empty()) {
117  break;
118  }
119  cout << "Failed to get heart beat " << it_try_heartbeat << "/" << num_try_heartbeat << "\n";
120  boost::this_thread::sleep(boost::posix_time::seconds(1));
121  }
122  if (heartbeat.empty()) {
123  throw MujinException(boost::str(boost::format("Failed to obtain heartbeat from %s. Is controller running?")%endpoint.str()));
124  }
125 
126  if (taskScenePk.empty()) {
127  taskScenePk = utils::GetScenePkFromHeatbeat(heartbeat);
128  cout << "task_scenepk: " << taskScenePk << " is obtained from heatbeat\n";
129  }
130  if (slaverequestid.empty()) {
131  slaverequestid = utils::GetSlaveRequestIdFromHeatbeat(heartbeat);
132  cout << "slave_request_id: " << slaverequestid << " is obtained from heatbeat\n";
133  }
134  }
135 
136  // cout << taskparameters << endl;
137  const string tasktype = "realtimeitlplanning";
138 
139  // connect to mujin controller
140  ControllerClientPtr controllerclient = CreateControllerClient(controllerUsernamePass, urlss.str());
141 
142  cout << "connected to mujin controller at " << urlss.str() << endl;
143 
144  SceneResourcePtr scene(new SceneResource(controllerclient, taskScenePk));
145 
146  // initialize binpicking task
147  pBinpickingTask = scene->GetOrCreateBinPickingTaskFromName_UTF8(tasktype+string("task1"), tasktype, TRO_EnableZMQ);
148  const string userinfo = "{\"username\": \"" + controllerclient->GetUserName() + "\", ""\"locale\": \"" + locale + "\"}";
149  cout << "initialzing binpickingtask with userinfo=" + userinfo << " taskparameters=" << taskparameters << endl;
150 
151  s_robotname = opts["robotname"].as<string>();
152  if (s_robotname.empty()) {
153  vector<SceneResource::InstObjectPtr> instobjects;
154  scene->GetInstObjects(instobjects);
155  for (vector<SceneResource::InstObjectPtr>::const_iterator it = instobjects.begin();
156  it != instobjects.end(); ++it) {
157  if (!(**it).dofvalues.empty()) {
158  s_robotname = (**it).name;
159  cout << "robot name: " << s_robotname << " is obtained from scene\n";
160  break;
161  }
162  }
163  if (s_robotname.empty()) {
164  throw MujinException("Robot name was not given by command line option. Also, failed to obtain robot name from scene.\n");
165  }
166  }
167 
168 
169  boost::shared_ptr<zmq::context_t> zmqcontext(new zmq::context_t(1));
170  pBinpickingTask->Initialize(taskparameters, taskZmqPort, heartbeatPort, zmqcontext, false, 10, controllerCommandTimeout, userinfo, slaverequestid);
171 }
172 
177 {
178  if (state.currentJointValues.empty() || state.currentToolValues.size() < 6) {
179  stringstream ss;
180  ss << "Failed to obtain robot state: joint values have "
181  << state.currentJointValues.size() << " elements and tool values have "
182  << state.currentToolValues.size() << " elements\n";
183 
184  throw std::runtime_error(ss.str());
185  }
186 
187  stringstream ss;
188  ss << state.timestamp << " (ms): joint:";
189  for (size_t i = 0; i < state.currentJointValues.size(); ++i) {
190  ss << state.jointNames[i] << "=" << state.currentJointValues[i] << " ";
191  }
192 
193  ss << "X=" << state.currentToolValues[0] << " ";
194  ss << "Y=" << state.currentToolValues[1] << " ";
195  ss << "Z=" << state.currentToolValues[2] << " ";
196  ss << "RX=" << state.currentToolValues[3] << " ";
197  ss << "RY=" << state.currentToolValues[4] << " ";
198  ss << "RZ=" << state.currentToolValues[5];
199  return ss.str();
200 }
201 
211  const string& goaltype,
212  const vector<double>& goals,
213  double speed,
214  const string& robotname,
215  const string& toolname,
216  bool movelinear)
217 {
218  // print state
220  pTask->GetPublishedTaskState(result, robotname, "mm", 1.0);
221  cout << "Starting:\n" << ConvertStateToString(result) << endl;
222 
223  // start moving
224  if (movelinear) {
225  pTask->MoveToolLinear(goaltype, goals, robotname, toolname, speed);
226  }
227  else {
228  pTask->MoveToHandPosition(goaltype, goals, robotname, toolname, speed);
229  }
230 
231  // print state
232  pTask->GetPublishedTaskState(result, robotname, "mm", 1.0);
233  cout << "Finished:\n" << ConvertStateToString(result) << endl;
234 }
235 
236 int main(int argc, char ** argv)
237 {
238  // parsing options
239  bpo::variables_map opts;
240  if (!ParseOptions(argc, argv, opts)) {
241  // parsing option failed
242  return 1;
243  }
244  const string robotname = opts["robotname"].as<string>();
245  const string toolname = opts["toolname"].as<string>();
246  const vector<double> goals = opts["goals"].as<vector<double> >();
247  const string goaltype = opts["goaltype"].as<string>();
248  const double speed = opts["speed"].as<double>();
249  const bool movelinearly = opts["movelinear"].as<bool>();
250 
251  // initializing
252  BinPickingTaskResourcePtr pBinpickingTask;
253  InitializeTask(opts, pBinpickingTask);
254 
255  // do interesting part
256  Run(pBinpickingTask, goaltype, goals, speed, s_robotname, toolname, movelinearly);
257 
258  return 0;
259 }