Shows how to jog robot in joint and tool spaces. example1: mujinjog –controller_hostname=yourhost –robotname=yourrobot example2: mujinjog –controller_hostname=yourhost –robotname=yourrobot –task_scenepk=yourscene –jointmode=false –axis=1 –move_in_positive=true –speed=0.2
#include <boost/program_options.hpp>
#include <signal.h>
#include <iostream>
#if defined(_WIN32) || defined(_WIN64)
#undef GetUserName // clashes with ControllerClient::GetUserName
#endif // defined(_WIN32) || defined(_WIN64)
using namespace mujinclient;
namespace bpo = boost::program_options;
using namespace std;
bool ParseOptions(
int argc,
char ** argv, bpo::variables_map& opts)
{
bpo::options_description desc("Options");
desc.add_options()
("help,h", "produce help message")
("controller_hostname", bpo::value<string>()->required(), "hostname or ip of the mujin controller, e.g. controllerXX or 192.168.0.1")
("controller_port", bpo::value<unsigned int>()->default_value(80), "port of the mujin controller")
("slave_request_id", bpo::value<string>()->default_value(""), "request id of the mujin slave, e.g. controller20_slave0. If empty, uses ")
("controller_username_password", bpo::value<string>()->default_value("testuser:pass"), "username and password to the mujin controller, e.g. username:password")
("controller_command_timeout", bpo::value<double>()->default_value(10), "command timeout in seconds, e.g. 10")
("locale", bpo::value<string>()->default_value("en_US"), "locale to use for the mujin controller client")
("task_scenepk", bpo::value<string>()->default_value(""), "scene pk of the binpicking task on the mujin controller, e.g. officeboltpicking.mujin.dae.")
("robotname", bpo::value<string>()->default_value(""), "robot name.")
("taskparameters", bpo::value<string>()->default_value("{}"), "binpicking task parameters, e.g. {'robotname': 'robot', 'robots':{'robot': {'externalCollisionIO':{}, 'gripperControlInfo':{}, 'robotControllerUri': '', robotDeviceIOUri': '', 'toolname': 'tool'}}}")
("zmq_port", bpo::value<unsigned int>()->default_value(11000), "port of the binpicking task on the mujin controller")
("heartbeat_port", bpo::value<unsigned int>()->default_value(11001), "port of the binpicking task's heartbeat signal on the mujin controller")
("jointmode", bpo::value<bool>()->default_value(true), "mode to do jogging. true indicates joint mode and tool mode otherwise")
("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")
("move_in_positive", bpo::value<bool>()->default_value(true), "whether to move in increasing or decreasing direction")
("jog_duration", bpo::value<double>()->default_value(1.0), "duration of jogging")
("speed", bpo::value<double>()->default_value(0.1), "speed to move at, a value between 0 and 1.")
;
try {
bpo::store(bpo::parse_command_line(argc, argv, desc, bpo::command_line_style::unix_style ^ bpo::command_line_style::allow_short), opts);
}
catch (const exception& ex) {
stringstream errss;
errss << "Caught exception " << ex.what();
cerr << errss.str() << endl;
return false;
}
bool badargs = false;
try {
bpo::notify(opts);
}
catch(const exception& ex) {
stringstream errss;
errss << "Caught exception " << ex.what();
cerr << errss.str() << endl;
badargs = true;
}
if(opts.count("help") || badargs) {
cout << "Usage: " << argv[0] << " [OPTS]" << endl;
cout << endl;
cout << desc << endl;
return false;
}
return true;
}
{
const string controllerUsernamePass = opts["controller_username_password"].as<string>();
const double controllerCommandTimeout = opts["controller_command_timeout"].as<double>();
const string taskparameters = opts["taskparameters"].as<string>();
const string locale = opts["locale"].as<string>();
const unsigned int taskZmqPort = opts["zmq_port"].as<unsigned int>();
const string hostname = opts["controller_hostname"].as<string>();
const unsigned int controllerPort = opts["controller_port"].as<unsigned int>();
stringstream urlss;
urlss << "http://" << hostname << ":" << controllerPort;
const unsigned int heartbeatPort = opts["heartbeat_port"].as<unsigned int>();
string slaverequestid = opts["slave_request_id"].as<string>();
string taskScenePk = opts["task_scenepk"].as<string>();
const bool needtoobtainfromheatbeat = taskScenePk.empty() || slaverequestid.empty();
if (needtoobtainfromheatbeat) {
stringstream endpoint;
endpoint << "tcp:\/\/" << hostname << ":" << heartbeatPort;
cout << "connecting to heartbeat at " << endpoint.str() << endl;
string heartbeat;
const size_t num_try_heartbeat(5);
for (size_t it_try_heartbeat = 0; it_try_heartbeat < num_try_heartbeat; ++it_try_heartbeat) {
heartbeat = utils::GetHeartbeat(endpoint.str());
if (!heartbeat.empty()) {
break;
}
cout << "Failed to get heart beat " << it_try_heartbeat << "/" << num_try_heartbeat << "\n";
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
if (heartbeat.empty()) {
throw MujinException(boost::str(boost::format(
"Failed to obtain heartbeat from %s. Is controller running?")%endpoint.str()));
}
if (taskScenePk.empty()) {
taskScenePk = utils::GetScenePkFromHeatbeat(heartbeat);
cout << "task_scenepk: " << taskScenePk << " is obtained from heatbeat\n";
}
if (slaverequestid.empty()) {
slaverequestid = utils::GetSlaveRequestIdFromHeatbeat(heartbeat);
cout << "slave_request_id: " << slaverequestid << " is obtained from heatbeat\n";
}
}
const string tasktype = "realtimeitlplanning";
cout << "connected to mujin controller at " << urlss.str() << endl;
pBinpickingTask = scene->GetOrCreateBinPickingTaskFromName_UTF8(tasktype+
string(
"task1"), tasktype,
TRO_EnableZMQ);
const string userinfo = "{\"username\": \"" + controllerclient->GetUserName() + "\", ""\"locale\": \"" + locale + "\"}";
cout << "initialzing binpickingtask with userinfo=" + userinfo << " taskparameters=" << taskparameters << endl;
vector<SceneResource::InstObjectPtr> instobjects;
scene->GetInstObjects(instobjects);
for (vector<SceneResource::InstObjectPtr>::const_iterator it = instobjects.begin();
it != instobjects.end(); ++it) {
if (!(**it).dofvalues.empty()) {
cout <<
"robot name: " <<
s_robotname <<
" is obtained from scene\n";
break;
}
}
throw MujinException(
"Robot name was not given by command line option. Also, failed to obtain robot name from scene.\n");
}
}
pBinpickingTask->Initialize(taskparameters, taskZmqPort, heartbeatPort, zmqcontext, false, 10, controllerCommandTimeout, userinfo, slaverequestid);
}
{
stringstream ss;
ss << "Failed to obtain robot state: joint values have "
throw std::runtime_error(ss.str());
}
stringstream ss;
}
return ss.str();
}
const string& mode,
unsigned int axis,
bool moveInPositive,
double duration,
double speed,
double acc,
const string& robotname,
double timeout)
{
pTask->GetPublishedTaskState(result, robotname, "mm", timeout);
const vector<int> stopjoints(dof, 0);
boost::shared_ptr<void> setdisconnectfn((void*)0,
stopjoints, robotname, "", speed, acc, timeout));
vector<int> movejointsigns(dof, 0);
movejointsigns[axis] = moveInPositive ? 1 : -1;
pTask->SetJogModeVelocities(mode, movejointsigns, robotname, "", speed, acc, timeout);
unsigned long long previoustimestamp = result.
timestamp;
for (unsigned int it_sleep = 0; it_sleep < duration*1000; it_sleep++) {
break;
}
boost::this_thread::sleep(boost::posix_time::milliseconds(1));
pTask->GetPublishedTaskState(result, robotname, "mm", timeout);
continue;
}
}
pTask->SetJogModeVelocities(mode, stopjoints, robotname, "", speed, acc, timeout);
pTask->GetPublishedTaskState(result, robotname, "mm", timeout);
}
int main(
int argc,
char ** argv)
{
bpo::variables_map opts;
return 1;
}
const unsigned int axis = opts["axis"].as<unsigned int>();
const bool moveinpositive = opts["move_in_positive"].as<bool>();
const double speed = opts["speed"].as<double>();
const double acc = speed * 0.7;
const double timeout = opts["controller_command_timeout"].as<double>();
const double jogduration = opts["jog_duration"].as<double>();
const string mode = opts["jointmode"].as<bool>() ? "joints" : "tool";
cout << "Ctrl-C to stop jogging before jogduration passed.\n";
Run(pBinpickingTask, mode, axis, moveinpositive, jogduration, speed, acc,
s_robotname, timeout);
return 0;
}
{
cout << "Sigint received. Stopping jogging\n";
#ifndef _WIN32
signal(SIGINT, SIG_DFL);
kill(0 , SIGINT);
#endif
}