11 #include <boost/program_options.hpp>
15 #if defined(_WIN32) || defined(_WIN64)
16 #undef GetUserName // clashes with ControllerClient::GetUserName
17 #endif // defined(_WIN32) || defined(_WIN64)
19 using namespace mujinclient;
20 namespace bpo = boost::program_options;
32 bool ParseOptions(
int argc,
char ** argv, bpo::variables_map& opts)
35 bpo::options_description desc(
"Options");
38 (
"help,h",
"produce help message")
39 (
"controller_hostname", bpo::value<string>()->required(),
"hostname or ip of the mujin controller, e.g. controllerXX or 192.168.0.1")
40 (
"controller_port", bpo::value<unsigned int>()->default_value(80),
"port of the mujin controller")
41 (
"slave_request_id", bpo::value<string>()->default_value(
""),
"request id of the mujin slave, e.g. controller20_slave0. If empty, uses ")
42 (
"controller_username_password", bpo::value<string>()->default_value(
"testuser:pass"),
"username and password to the mujin controller, e.g. username:password")
43 (
"controller_command_timeout", bpo::value<double>()->default_value(10),
"command timeout in seconds, e.g. 10")
44 (
"locale", bpo::value<string>()->default_value(
"en_US"),
"locale to use for the mujin controller client")
45 (
"task_scenepk", bpo::value<string>()->default_value(
""),
"scene pk of the binpicking task on the mujin controller, e.g. officeboltpicking.mujin.dae.")
46 (
"robotname", bpo::value<string>()->default_value(
""),
"robot name.")
47 (
"taskparameters", bpo::value<string>()->default_value(
"{}"),
"binpicking task parameters, e.g. {'robotname': 'robot', 'robots':{'robot': {'externalCollisionIO':{}, 'gripperControlInfo':{}, 'robotControllerUri': '', robotDeviceIOUri': '', 'toolname': 'tool'}}}")
48 (
"zmq_port", bpo::value<unsigned int>()->default_value(11000),
"port of the binpicking task on the mujin controller")
49 (
"heartbeat_port", bpo::value<unsigned int>()->default_value(11001),
"port of the binpicking task's heartbeat signal on the mujin controller")
50 (
"jointmode", bpo::value<bool>()->default_value(
true),
"mode to do jogging. true indicates joint mode and tool mode otherwise")
51 (
"axis", bpo::value<unsigned int>()->default_value(0),
"axis to do jogging on. For joint mode, 0 indicates J1 and 5 indicates J6. For tool mode, 0 indicates translation in X, 5 indicates rotation in Z")
52 (
"move_in_positive", bpo::value<bool>()->default_value(
true),
"whether to move in increasing or decreasing direction")
53 (
"jog_duration", bpo::value<double>()->default_value(1.0),
"duration of jogging")
54 (
"speed", bpo::value<double>()->default_value(0.1),
"speed to move at, a value between 0 and 1.")
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:";
220 const string& robotname,
225 pTask->GetPublishedTaskState(result, robotname,
"mm", timeout);
230 const vector<int> stopjoints(dof, 0);
231 boost::shared_ptr<void> setdisconnectfn((
void*)0,
233 stopjoints, robotname,
"", speed, acc, timeout));
236 vector<int> movejointsigns(dof, 0);
237 movejointsigns[axis] = moveInPositive ? 1 : -1;
238 pTask->SetJogModeVelocities(mode, movejointsigns, robotname,
"", speed, acc, timeout);
241 unsigned long long previoustimestamp = result.
timestamp;
242 for (
unsigned int it_sleep = 0; it_sleep < duration*1000; it_sleep++) {
247 boost::this_thread::sleep(boost::posix_time::milliseconds(1));
248 pTask->GetPublishedTaskState(result, robotname,
"mm", timeout);
249 if (previoustimestamp == result.
timestamp) {
259 pTask->SetJogModeVelocities(mode, stopjoints, robotname,
"", speed, acc, timeout);
262 pTask->GetPublishedTaskState(result, robotname,
"mm", timeout);
266 int main(
int argc,
char ** argv)
269 bpo::variables_map opts;
274 const unsigned int axis = opts[
"axis"].as<
unsigned int>();
275 const bool moveinpositive = opts[
"move_in_positive"].as<
bool>();
276 const double speed = opts[
"speed"].as<
double>();
277 const double acc = speed * 0.7;
278 const double timeout = opts[
"controller_command_timeout"].as<
double>();
279 const double jogduration = opts[
"jog_duration"].as<
double>();
280 const string mode = opts[
"jointmode"].as<
bool>() ?
"joints" :
"tool";
288 cout <<
"Ctrl-C to stop jogging before jogduration passed.\n";
291 Run(pBinpickingTask, mode, axis, moveinpositive, jogduration, speed, acc,
s_robotname, timeout);
298 cout <<
"Sigint received. Stopping jogging\n";
302 signal(SIGINT, SIG_DFL);