12 #if defined(_WIN32) || defined(_WIN64)
13 #undef GetUserName // clashes with ControllerClient::GetUserName
14 #endif // defined(_WIN32) || defined(_WIN64)
17 using namespace mujinclient;
19 #include <boost/program_options.hpp>
21 namespace bpo = boost::program_options;
31 bool ParseOptions(
int argc,
char ** argv, bpo::variables_map& opts)
34 bpo::options_description desc(
"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")
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")
58 bpo::store(bpo::parse_command_line(argc, argv, desc, bpo::command_line_style::unix_style ^ bpo::command_line_style::allow_short), opts);
60 catch (
const exception& ex) {
62 errss <<
"Caught exception " << ex.what();
63 cerr << errss.str() << endl;
71 catch(
const exception& ex) {
73 errss <<
"Caught exception " << ex.what();
74 cerr << errss.str() << endl;
78 if(opts.count(
"help") || badargs) {
79 cout <<
"Usage: " << argv[0] <<
" [OPTS]" << endl;
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>();
101 urlss <<
"http://" << hostname <<
":" << controllerPort;
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>();
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;
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()) {
119 cout <<
"Failed to get heart beat " << it_try_heartbeat <<
"/" << num_try_heartbeat <<
"\n";
120 boost::this_thread::sleep(boost::posix_time::seconds(1));
122 if (heartbeat.empty()) {
123 throw MujinException(boost::str(boost::format(
"Failed to obtain heartbeat from %s. Is controller running?")%endpoint.str()));
126 if (taskScenePk.empty()) {
127 taskScenePk = utils::GetScenePkFromHeatbeat(heartbeat);
128 cout <<
"task_scenepk: " << taskScenePk <<
" is obtained from heatbeat\n";
130 if (slaverequestid.empty()) {
131 slaverequestid = utils::GetSlaveRequestIdFromHeatbeat(heartbeat);
132 cout <<
"slave_request_id: " << slaverequestid <<
" is obtained from heatbeat\n";
137 const string tasktype =
"realtimeitlplanning";
142 cout <<
"connected to mujin controller at " << urlss.str() << endl;
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;
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()) {
159 cout <<
"robot name: " <<
s_robotname <<
" is obtained from scene\n";
164 throw MujinException(
"Robot name was not given by command line option. Also, failed to obtain robot name from scene.\n");
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);
180 ss <<
"Failed to obtain robot state: joint values have "
184 throw std::runtime_error(ss.str());
188 ss << state.
timestamp <<
" (ms): joint:";
211 const string& goaltype,
212 const vector<double>& goals,
214 const string& robotname,
215 const string& toolname,
220 pTask->GetPublishedTaskState(result, robotname,
"mm", 1.0);
225 pTask->MoveToolLinear(goaltype, goals, robotname, toolname, speed);
228 pTask->MoveToHandPosition(goaltype, goals, robotname, toolname, speed);
232 pTask->GetPublishedTaskState(result, robotname,
"mm", 1.0);
236 int main(
int argc,
char ** argv)
239 bpo::variables_map opts;
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>();
256 Run(pBinpickingTask, goaltype, goals, speed,
s_robotname, toolname, movelinearly);