|
| 1 | +// -*- coding: utf-8 -*- |
| 2 | +/** \example mujincreateikparam.cpp |
| 3 | +
|
| 4 | + Shows how to create ikparam using current coordinate |
| 5 | + example: mujincreateikparam --controller_hostname localhost --task_scenepk machine.mujin.dae --iktype Transform6D --object_name object --taskparameters '{"robotname":"robot","toolname":"tool"}' |
| 6 | + */ |
| 7 | + |
| 8 | +#include <mujincontrollerclient/binpickingtask.h> |
| 9 | +#include <iostream> |
| 10 | +#include <cmath> |
| 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 | +namespace mujinjson = mujinclient::mujinjson_external; |
| 19 | + |
| 20 | +#include <boost/program_options.hpp> |
| 21 | +#include <boost/bind.hpp> |
| 22 | + |
| 23 | +namespace bpo = boost::program_options; |
| 24 | +using namespace std; |
| 25 | + |
| 26 | +/// \brief parse command line options and store in a map |
| 27 | +/// \param argc number of arguments |
| 28 | +/// \param argv arguments |
| 29 | +/// \param opts map where parsed options are stored |
| 30 | +/// \return true if non-help options are parsed succesfully. |
| 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 | + ("taskparameters", bpo::value<string>()->default_value("{}"), "binpicking task parameters, e.g. {\"robotname\": \"robot\", \"toolname\": \"tool\"}") |
| 46 | + ("zmq_port", bpo::value<unsigned int>()->default_value(11000), "port of the binpicking task on the mujin controller") |
| 47 | + ("heartbeat_port", bpo::value<unsigned int>()->default_value(11001), "port of the binpicking task's heartbeat signal on the mujin controller") |
| 48 | + ; |
| 49 | + |
| 50 | + try { |
| 51 | + bpo::store(bpo::parse_command_line(argc, argv, desc, bpo::command_line_style::unix_style ^ bpo::command_line_style::allow_short), opts); |
| 52 | + } |
| 53 | + catch (const exception& ex) { |
| 54 | + stringstream errss; |
| 55 | + errss << "Caught exception " << ex.what(); |
| 56 | + cerr << errss.str() << endl; |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + bool badargs = false; |
| 61 | + try { |
| 62 | + bpo::notify(opts); |
| 63 | + } |
| 64 | + catch(const exception& ex) { |
| 65 | + stringstream errss; |
| 66 | + errss << "Caught exception " << ex.what(); |
| 67 | + cerr << errss.str() << endl; |
| 68 | + badargs = true; |
| 69 | + } |
| 70 | + |
| 71 | + if(opts.count("help") || badargs) { |
| 72 | + cout << "Usage: " << argv[0] << " [OPTS]" << endl; |
| 73 | + cout << endl; |
| 74 | + cout << desc << endl; |
| 75 | + return false; |
| 76 | + } |
| 77 | + return true; |
| 78 | +} |
| 79 | + |
| 80 | +/// \brief initialize BinPickingTask and establish communication with controller |
| 81 | +/// \param opts options parsed from command line |
| 82 | +/// \param pBinPickingTask bin picking task to be initialized |
| 83 | +void InitializeTask(const bpo::variables_map& opts, |
| 84 | + boost::shared_ptr<zmq::context_t>& zmqcontext, |
| 85 | + BinPickingTaskResourcePtr& pBinpickingTask) |
| 86 | +{ |
| 87 | + const string controllerUsernamePass = opts["controller_username_password"].as<string>(); |
| 88 | + const double controllerCommandTimeout = opts["controller_command_timeout"].as<double>(); |
| 89 | + const string taskparameters = opts["taskparameters"].as<string>(); |
| 90 | + const string locale = opts["locale"].as<string>(); |
| 91 | + const unsigned int taskZmqPort = opts["zmq_port"].as<unsigned int>(); |
| 92 | + const string hostname = opts["controller_hostname"].as<string>(); |
| 93 | + const unsigned int controllerPort = opts["controller_port"].as<unsigned int>(); |
| 94 | + stringstream urlss; |
| 95 | + urlss << "http://" << hostname << ":" << controllerPort; |
| 96 | + |
| 97 | + const unsigned int heartbeatPort = opts["heartbeat_port"].as<unsigned int>(); |
| 98 | + string slaverequestid = opts["slave_request_id"].as<string>(); |
| 99 | + string taskScenePk = opts["task_scenepk"].as<string>(); |
| 100 | + |
| 101 | + const bool needtoobtainfromheartbeat = taskScenePk.empty() || slaverequestid.empty(); |
| 102 | + if (needtoobtainfromheartbeat) { |
| 103 | + stringstream endpoint; |
| 104 | + endpoint << "tcp://" << hostname << ":" << heartbeatPort; |
| 105 | + cout << "connecting to heartbeat at " << endpoint.str() << endl; |
| 106 | + string heartbeat; |
| 107 | + const size_t num_try_heartbeat(10); |
| 108 | + for (size_t it_try_heartbeat = 0; it_try_heartbeat < num_try_heartbeat; ++it_try_heartbeat) { |
| 109 | + heartbeat = utils::GetHeartbeat(endpoint.str()); |
| 110 | + if (!heartbeat.empty()) { |
| 111 | + break; |
| 112 | + } |
| 113 | + cout << "Failed to get heart beat " << it_try_heartbeat << "/" << num_try_heartbeat << "\n"; |
| 114 | + boost::this_thread::sleep(boost::posix_time::milliseconds(100)); |
| 115 | + } |
| 116 | + if (heartbeat.empty()) { |
| 117 | + throw MujinException(boost::str(boost::format("Failed to obtain heartbeat from %s. Is controller running?")%endpoint.str())); |
| 118 | + } |
| 119 | + |
| 120 | + if (taskScenePk.empty()) { |
| 121 | + taskScenePk = utils::GetScenePkFromHeartbeat(heartbeat); |
| 122 | + cout << "task_scenepk: " << taskScenePk << " is obtained from heartbeat\n"; |
| 123 | + } |
| 124 | + if (slaverequestid.empty()) { |
| 125 | + slaverequestid = utils::GetSlaveRequestIdFromHeartbeat(heartbeat); |
| 126 | + cout << "slave_request_id: " << slaverequestid << " is obtained from heartbeat\n"; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // cout << taskparameters << endl; |
| 131 | + const string tasktype = "realtimeitlplanning3"; |
| 132 | + |
| 133 | + // connect to mujin controller |
| 134 | + ControllerClientPtr controllerclient = CreateControllerClient(controllerUsernamePass, urlss.str()); |
| 135 | + |
| 136 | + cout << "connected to mujin controller at " << urlss.str() << endl; |
| 137 | + |
| 138 | + SceneResourcePtr scene(new SceneResource(controllerclient, taskScenePk)); |
| 139 | + |
| 140 | + // initialize binpicking task |
| 141 | + pBinpickingTask = scene->GetOrCreateBinPickingTaskFromName_UTF8(tasktype+string("task1"), tasktype, TRO_EnableZMQ); |
| 142 | + const string userinfo = "{\"username\": \"" + controllerclient->GetUserName() + "\", ""\"locale\": \"" + locale + "\"}"; |
| 143 | + cout << "initialzing binpickingtask with userinfo=" + userinfo << " taskparameters=" << taskparameters << endl; |
| 144 | + |
| 145 | + pBinpickingTask->Initialize(taskparameters, taskZmqPort, heartbeatPort, zmqcontext, false, controllerCommandTimeout, controllerCommandTimeout, userinfo, slaverequestid); |
| 146 | + |
| 147 | +} |
| 148 | + |
| 149 | +void ReinitializeTask(boost::shared_ptr<zmq::context_t>& zmqcontext, |
| 150 | + BinPickingTaskResourcePtr& pBinpickingTask) |
| 151 | +{ |
| 152 | + const string taskparameters("{\"robotname\": \"robot\"}"); |
| 153 | + const unsigned int taskZmqPort(11000); |
| 154 | + const double controllerCommandTimeout(10); |
| 155 | + const string userinfo(""); |
| 156 | + const string slaverequestid("controller71_slave0"); |
| 157 | + const unsigned int heartbeatPort(11001); |
| 158 | + pBinpickingTask->Initialize(taskparameters, taskZmqPort, heartbeatPort, zmqcontext, false, controllerCommandTimeout, controllerCommandTimeout, userinfo, slaverequestid); |
| 159 | +} |
| 160 | + |
| 161 | +int main(int argc, char ** argv) |
| 162 | +{ |
| 163 | + // parsing options |
| 164 | + bpo::variables_map opts; |
| 165 | + if (!ParseOptions(argc, argv, opts)) { |
| 166 | + // parsing option failed |
| 167 | + return 1; |
| 168 | + } |
| 169 | + |
| 170 | + // initializing |
| 171 | + BinPickingTaskResourcePtr pBinpickingTask; |
| 172 | + boost::shared_ptr<zmq::context_t> zmqcontext(new zmq::context_t(1)); |
| 173 | + InitializeTask(opts, zmqcontext, pBinpickingTask); |
| 174 | + |
| 175 | + const double timeout = opts["controller_command_timeout"].as<double>(); |
| 176 | + |
| 177 | + pBinpickingTask->EndJogMode(); |
| 178 | + return 0; |
| 179 | +} |
0 commit comments