19#include <yarp/os/Network.h>
20#include <yarp/os/RFModule.h>
21#include <yarp/os/Time.h>
22#include <yarp/os/Log.h>
23#include <yarp/os/LogStream.h>
24#include <yarp/os/BufferedPort.h>
25#include <yarp/sig/Vector.h>
26#include <yarp/math/Math.h>
45 BufferedPort<Bottle> port_data_out;
50 port_data_out.open(
"/trajectoryPlayer/all_joints_data_out:o");
55 port_data_out.interrupt();
56 port_data_out.close();
80 if (driver && driver->ienc_ll)
82 driver->ienc_ll->getEncoders(encs);
91 if (driver && driver->ipid_ll)
93 driver->ipid_ll->getPidOutputs(VOCAB_PIDTYPE_POSITION,outs);
102 if (driver && driver->ipid_ll)
104 driver->ipid_ll->getPidErrors(VOCAB_PIDTYPE_POSITION,errs);
113 if (driver && driver->imotenc_ll)
115 driver->imotenc_ll->getMotorEncoders(opts);
124 Bottle& bot2 = this->port_data_out.prepare();
132 bot2.addString(
"commands:");
133 for (
int ix=0;ix<size;ix++)
135 bot2.addFloat64(ll[ix]);
137 bot2.addString(
"encoders:");
138 for (
int ix=0;ix<size;ix++)
140 bot2.addFloat64(encs[ix]);
142 bot2.addString(
"outputs:");
143 for (
int ix=0;ix<size;ix++)
145 bot2.addFloat64(outs[ix]);
147 bot2.addString(
"optical:");
148 for (
int ix=0;ix<size;ix++)
150 bot2.addFloat64(opts[ix]);
152 bot2.addString(
"errors:");
153 for (
int ix=0;ix<size;ix++)
155 bot2.addFloat64(errs[ix]);
157 bot2.addString(
"timestamp:");
158 bot2.addFloat64(yarp::os::Time::now());
159 this->port_data_out.write();
206 if (!
driver)
return false;
212 for (
int j = 0; j < nj; j++)
233 yError(
"failed to execute command");
241 for (
int j = 0; j < nj; j++)
249 yError(
"Critical error: invalid driver");
254 Bottle& bot2 = this->port_command_joints.prepare();
258 int size = this->actions.
action_vector[action_id].get_n_joints();
259 bot2.addString(
"commands:");
260 for (
int ix=0;ix<size;ix++)
262 bot2.addFloat64(ll[ix]);
264 bot2.addString(
"encoders:");
265 for (
int ix=0;ix<size;ix++)
267 bot2.addFloat64(encs[ix]);
269 this->port_command_joints.write();
274 lock_guard<mutex> lck(
mtx);
275 double current_time = yarp::os::Time::now();
288 for (
int j = 0; j < nj; j++)
297 if (last_action == 0)
299 yError(
"sequence empty!");
329 yInfo(
"sequence completed in: %f s",yarp::os::Time::now()-
start_time);
334 yInfo(
"sequence completed in: %f s, restarting", yarp::os::Time::now() -
start_time);
346 for (
int j = 0; j < nj; j++)
350 yarp::os::Time::delay(0.1);
351 for (
int j = 0; j < nj; j++)
356 yInfo() <<
"going to home position";
363 for (
int j = 0; j < nj; j++)
366 double err = fabs(enc[j] - ll[j]);
367 check &= (err < 2.0);
369 yarp::os::Time::delay(0.1);
371 }
while (!check && loops>0);
377 for (
int j = 0; j <nj; j++)
381 yarp::os::Time::delay(0.1);
389 yError() <<
"unable to reach start position!";
397 for (
int j = 0; j <nj; j++)
401 yarp::os::Time::delay(0.1);
411 yWarning(
"no sequence in memory");
417 yError() <<
"unknown current_status";
441 if (
rf.check(
"name"))
442 name=string(
"/")+
rf.find(
"name").asString().c_str();
444 name=
"/trajectoryPlayer";
450 portProp.put(
"robot",
rf.find(
"robot"));
451 portProp.put(
"part",
rf.find(
"part"));
456 yError() <<
"Error configuring position controller, check parameters";
462 yError() <<
"Error cannot connect to remote ports" ;
471 yError() <<
"Working thread did not start, queue will not work";
475 yInfo() <<
"Working thread started";
478 if (
rf.check(
"execute")==
true)
480 yInfo() <<
"Enabling iPid->setReference() controller";
485 yInfo() <<
"Not using iPid->setReference() controller";
489 if (
rf.check(
"period")==
true)
491 int period =
rf.find(
"period").asInt32();
492 yInfo() <<
"Thread period set to " << period <<
"ms";
493 w_thread.setPeriod((
double)period/1000.0);
495 yInfo() <<
"Using parameters:" <<
rf.toString();
498 yInfo() <<
"opening file...";
499 if (
rf.check(
"filename")==
true)
501 string filename =
rf.find(
"filename").asString().c_str();
503 if (
rf.check(
"joints"))
505 req_joints =
rf.find(
"joints").asInt32();}
508 yError() <<
"Missing parameter 'joints' (number of joints to control)";
514 yError() <<
"Requested number of joints exceeds the number of available joints on the robot!";
520 yWarning() <<
"changing n joints from" <<
robot.
n_joints <<
"to" << req_joints;
524 if (
rf.check(
"jointsMap"))
526 Bottle* b =
rf.find(
"jointsMap").asList();
527 if (!b) { yError() <<
"Error parsing parameter jointsMap";
return false; }
528 if (b->size() != req_joints) { yError() <<
"invalid size of jointsMap parameter";
return false; }
529 for (
int i = 0; i < b->size(); i++)
537 for (
int i = 0; i < req_joints; i++)
545 yError() <<
"Unable to parse file " << filename;
551 yWarning() <<
"no sequence files load.";
554 yInfo() <<
"module successfully configured. ready.";
558 virtual bool respond(
const Bottle &command, Bottle &reply)
560 lock_guard<mutex> lck(this->w_thread.
mtx);
563 if (command.size()!=0)
565 string cmdstring = command.get(0).asString().c_str();
567 if (cmdstring ==
"help")
569 cout <<
"Available commands:" << endl;
570 cout <<
"start" << endl;
571 cout <<
"stop" << endl;
572 cout <<
"reset" << endl;
573 cout <<
"clear" << endl;
574 cout <<
"add" << endl;
575 cout <<
"load" << endl;
576 cout <<
"forever" << endl;
577 cout <<
"list" << endl;
578 reply.addVocab32(
"many");
579 reply.addVocab32(
"ack");
580 reply.addString(
"Available commands:");
581 reply.addString(
"start");
582 reply.addString(
"stop");
583 reply.addString(
"reset");
584 reply.addString(
"clear");
585 reply.addString(
"add");
586 reply.addString(
"load");
587 reply.addString(
"forever");
588 reply.addString(
"list");
590 else if (cmdstring ==
"start")
599 if (this->b_thread.isRunning()==
false)
b_thread.start();
601 reply.addVocab32(
"ack");
603 else if (cmdstring ==
"forever")
610 reply.addVocab32(
"ack");
612 else if (cmdstring ==
"stop")
615 if (this->b_thread.isRunning()==
true)
b_thread.askToStop();
617 reply.addVocab32(
"ack");
619 else if (cmdstring ==
"clear")
622 reply.addVocab32(
"ack");
624 else if (cmdstring ==
"add")
628 yError() <<
"Unable to parse command";
629 reply.addVocab32(
"error");
633 yInfo() <<
"Command added";
634 reply.addVocab32(
"ack");
637 else if (cmdstring ==
"load")
639 string filename = command.get(1).asString().c_str();
642 yError() <<
"Unable to parse file";
643 reply.addVocab32(
"error");
647 yInfo() <<
"File opened";
648 reply.addVocab32(
"ack");
651 else if (cmdstring ==
"reset")
656 if (this->b_thread.isRunning()==
true)
b_thread.askToStop();
658 reply.addVocab32(
"ack");
660 else if (cmdstring ==
"list")
663 reply.addVocab32(
"ack");
667 reply.addVocab32(
"nack");
674 reply.addVocab32(
"nack");
694int main(
int argc,
char *argv[])
697 rf.setDefaultContext(
"ctpService");
698 rf.configure(argc,argv);
700 if (rf.check(
"help"))
702 yInfo() <<
"Options:";
703 yInfo() <<
"\t--joints <n>: number of joints, default 6";
704 yInfo() <<
"\t--jointsMap (1 2 3...) map of the joints to be controller. Size must be equal to --joints";
705 yInfo() <<
"\t--name <moduleName>: set new module name";
706 yInfo() <<
"\t--robot <robotname>: robot name";
707 yInfo() <<
"\t--part <robotname>: part name";
708 yInfo() <<
"\t--filename <filename>: the positions file";
709 yInfo() <<
"\t--execute activate the iPid->setReference() control";
710 yInfo() <<
"\t--period <period>: the period in ms of the internal thread (default 5)";
711 yInfo() <<
"\t--verbose to display additional infos";
717 if (!
yarp.checkNetwork())
719 yError() <<
"yarp.checkNetwork() failed.";
725 return mod.runModule(rf);
void attachActions(action_class *a)
void attachRobotDriver(robotDriver *p)
BroadcastingThread(int period=1)
bool enable_execute_joint_command
bool execute_joint_command(int action_id)
void compute_and_send_command(int action_id)
BufferedPort< Bottle > port_command_out
BufferedPort< Bottle > port_command_joints
void attachRobotDriver(robotDriver *p)
WorkingThread(double period=5)
bool parseCommandLine(const char *command_line, int line, int n_joints)
std::deque< action_struct > action_vector
bool openFile(string filename, int n_joints)
std::map< int, int > joints_map
bool setPosition(int j, double ref)
bool setControlMode(const int j, const int mode)
bool getEncoder(int j, double *v)
bool configure(const Property &copt)
bool positionMove(int j, double ref)
virtual bool updateModule()
virtual double getPeriod()
virtual bool respond(const Bottle &command, Bottle &reply)
BroadcastingThread b_thread
virtual bool configure(ResourceFinder &rf)
Copyright (C) 2008 RobotCub Consortium.