93#include <yarp/os/Network.h>
94#include <yarp/os/RFModule.h>
95#include <yarp/os/Time.h>
96#include <yarp/os/BufferedPort.h>
97#include <yarp/sig/Vector.h>
98#include <yarp/math/Math.h>
100#include <yarp/dev/ControlBoardInterfaces.h>
101#include <yarp/dev/PolyDriver.h>
102#include <yarp/os/PeriodicThread.h>
103#include <yarp/os/Thread.h>
104#include <yarp/os/LogStream.h>
109#include <condition_variable>
118using namespace yarp::os;
119using namespace yarp::sig;
121using namespace yarp::math;
124#define VCTP_TIME yarp::os::createVocab32('t','i','m','e')
125#define VCTP_OFFSET yarp::os::createVocab32('o','f','f')
126#define VCTP_CMD_NOW yarp::os::createVocab32('c','t','p','n')
127#define VCTP_CMD_QUEUE yarp::os::createVocab32('c','t','p','q')
128#define VCTP_CMD_FILE yarp::os::createVocab32('c','t','p','f')
129#define VCTP_POSITION yarp::os::createVocab32('p','o','s')
130#define VCTP_WAIT yarp::os::createVocab32('w','a','i','t')
132#define VEL_FILT_SIZE 10
133#define VEL_FILT_THRES 1.0
146 const double &
getTime()
const {
return time; }
149 const Vector &
getCmd()
const {
return cmd; }
166 return actions.size();
171 for(
unsigned int k=0; k<actions.size();k++)
191 actions.push_back(v);
214 cerr<<
"Error: not connected to control board skipping"<<endl;
218 int size=
x->getCmd().size();
220 double time=
x->getTime();
223 enc->getAxes(nJoints);
224 if ((
offset+size)>nJoints)
226 cerr<<
"Error: detected possible overflow, skipping"<<endl;
227 cerr<<
"For debug --> joints: "<<nJoints<<
" off: "<<
offset<<
" cmd length: "<<size<<endl;
238 for (
size_t i=0; i<disp.length(); i++)
245 cerr<<
"Error: encoders timed out, cannot rely on encoder feedback, aborted"<<endl;
249 disp[i]=
x->getCmd()[i]-q;
258 std::vector<int> joints;
259 std::vector<yarp::dev::ControlModeEnum> modes;
260 std::vector<double> speeds;
261 std::vector<double> positions;
263 for (
size_t i=0; i<disp.length(); i++)
265 joints.push_back(
offset+i);
266 speeds.push_back(disp[i]/
time);
267 modes.push_back(yarp::dev::ControlModeEnum::VOCAB_CM_POSITION);
268 positions.push_back(
x->getCmd()[i]);
273 std::vector<yarp::dev::SelectableControlModeEnum> setModes(disp.length(), yarp::dev::SelectableControlModeEnum::VOCAB_CM_POSITION);
274 mode->setControlModes(joints, setModes);
277 yarp::os::Time::delay(0.01);
278 mode->getControlModes(joints, modes);
279 for (
size_t i=0; i<disp.length(); i++)
281 if(modes[i] != yarp::dev::ControlModeEnum::VOCAB_CM_POSITION)
283 yError() <<
"Joint " << i <<
" not in position mode";
286 pos->setTrajSpeeds(disp.length(), joints.data(), speeds.data());
287 pos->positionMove(disp.length(), joints.data(), positions.data());
289 cout <<
"Script port: " <<
x->getCmd().toString() << endl;
303 lock_guard<mutex> lck(
mtx);
310 if (options.check(
"device"))
311 drvOptions.put(
"device", options.find(
"device").asString());
313 drvOptions.put(
"device",
"remote_controlboard");
315 string name=string(options.find(
"name").asString());
317 string remote=string(
"/")+string(options.find(
"robot").asString());
318 remote+=string(
"/")+string(options.find(
"part").asString());
321 local+=string(
"/local/")+string(options.find(
"part").asString());
328 cout <<
"Driver options:\n" <<
drvOptions.toString();
336 lock_guard<mutex> lck(
mtx);
342 lock_guard<mutex> lck(
mtx);
349 lock_guard<mutex> lck(
mtx);
408 double time=tmp->getTime();
419 condition_variable cv;
432 unsigned int filtElemsCnt;
434 bool checkInitConnection(Port *
p)
437 if(
p->getOutputCount() > 0)
439 cout <<
"***** got a connection" << endl;
448 bool initVelCtrl(Port *
p, ResourceFinder *rf)
457 string gainStr =
"gain";
458 sprintf(numberId,
"%d", i);
459 gainStr = gainStr + numberId;
464 if (rf->check(gainStr))
466 c.addFloat64(rf->find(gainStr).asFloat64());
476 cout <<
" velCtrl: missing 'gain' parameters" << endl;
483 string svelStr =
"svel";
484 sprintf(numberId,
"%d", i);
485 svelStr = svelStr + numberId;
490 if (rf->check(svelStr))
492 c.addFloat64(rf->find(svelStr).asFloat64());
502 cout <<
" velCtrl: missing 'svel' parameters" << endl;
508 bool readLine(Vector &v,
double &
time)
510 fin.getline(&line[0],
sizeof(line),
'\n');
520 time=b.get(1).asFloat64();
521 v.resize(b.size()-2);
523 for (
size_t i=0; i<v.length(); i++)
524 v[i]=b.get(i+2).asFloat64();
535 Vector formCommand(
const Vector &
p,
const double t)
539 for (
size_t i=0; i<
p.length(); i++)
549 Vector v=linEst.estimate(el);
553 for (
size_t i=0; i<
p.length(); i++)
555 res.push_back(1000.0+i);
590 bool go(
const string &fileName)
598 fin.open(fileName.c_str());
601 fin.seekg(0,ios_base::beg);
603 fin.getline(&line[0],
sizeof(line),
'\n');
604 fin.getline(&line[0],
sizeof(line),
'\n');
606 cout<<
"File loaded"<<endl;
612 cout<<
"Unable to load file: "<<fileName<<endl;
632 double time1,time2,t;
635 while (!isStopping())
639 if (checkInitConnection(velInitPort))
641 bool b = initVelCtrl(velInitPort, pRF);
644 cout <<
"velocity control initialized. gain, svel parameters loaded" << endl;
648 cout <<
"*** velocity control missing init parameters! (gain, svel)" << endl;
659 unique_lock<mutex> lck(mtx);
667 velInitPort->write(c,r);
668 send=readLine(p1,time1);
677 Vector
cmd=formCommand(p1,t);
682 if (readLine(p2,time2))
684 double dt=time2-time1;
697 velInitPort->write(c,r);
730 reply.addVocab32(
"ack");
743 reply.addVocab32(
"ack");
753 string fileName=
rf->findFile(
cmd.get(1).asString());
757 reply.addVocab32(
"ack");
761 reply.addVocab32(
"nack");
762 reply.addString(
"Unable to load file");
769 cerr<<
"Warning command not implemented yet"<<endl;
770 reply.addVocab32(
"ack");
780 const int expectedMsgSize=7;
782 if (
cmd.size()!=expectedMsgSize)
789 double time=
cmd.get(2).asFloat64();
797 posCmd=
cmd.get(6).asList();
805 cout<<
"Received command:"<<endl;
806 cout<<
"Time: " <<
time <<
"offset: " <<
offset <<
" Cmd: " << posCmd->toString()<<endl;
809 (*action)->
getCmd().resize(posCmd->size());
810 for(
int k=0;k<posCmd->size();k++)
811 (*action)->getCmd()[k]=posCmd->get(k).asFloat64();
813 (*action)->getOffset()=
offset;
814 (*action)->getTime()=
time;
826 if (
rf.check(
"name"))
827 name=string(
"/")+
rf.find(
"name").asString();
831 rpcPort.open(
name+
string(
"/")+
rf.find(
"part").asString()+
"/rpc");
835 portProp.put(
"name",
name);
836 portProp.put(
"robot",
rf.find(
"robot"));
837 portProp.put(
"part",
rf.find(
"part"));
839 if (!
posPort.configure(portProp))
841 cerr<<
"Error configuring position controller, check parameters"<<endl;
847 cerr<<
"Error cannot connect to remote ports"<<endl;
851 cout <<
"***** connect to rpc port and type \"help\" for commands list" << endl;
856 cerr<<
"Thread did not start, queue will not work"<<endl;
859 if (
rf.check(
"autochange_control_mode_enable"))
864 posPort.autochange_control_mode_enable=
true;
866 velPort.open(
name+
string(
"/")+
rf.find(
"part").asString()+
"/vc:o");
870 cout <<
"Using parameters:" << endl <<
rf.toString() << endl;
872 cout <<
"***** starting the thread" << endl;
878 virtual bool respond(
const Bottle &command, Bottle &reply)
881 if (command.size()!=0)
883 switch (command.get(0).asVocab32())
885 case yarp::os::createVocab32(
'h',
'e',
'l',
'p'):
887 cout <<
"Available commands:" << endl;
888 cout<<
"Queue command:\n";
889 cout<<
"[ctpq] [time] seconds [off] j [pos] (list)\n";
890 cout<<
"New command, execute now (erase queue):\n";
891 cout<<
"[ctpn] [time] seconds [off] j [pos] (list)\n";
892 cout<<
"Load sequence from file:\n";
893 cout<<
"[ctpf] filename\n";
894 reply.addVocab32(
"ack");
917 return RFModule::respond(command,reply);
922 reply.addVocab32(
"nack");
948int main(
int argc,
char *argv[])
951 rf.setDefaultContext(
"ctpService");
952 rf.configure(argc,argv);
954 if (rf.check(
"help"))
956 cout <<
"Options:" << endl << endl;
957 cout <<
"\t--name moduleName: set new module name" << endl;
958 cout <<
"\t--robot robotname: robot name" << endl;
959 cout <<
"\t--part partname: robot part name" << endl;
960 cout <<
"\t--from fileName: input configuration file" << endl;
961 cout <<
"\t--context dirName: resource finder context" << endl;
962 cout <<
"\t--autochange_control_mode_enable: sets the joints to position mode when trying to execute the command (default off)" << endl;
968 if (!
yarp.checkNetwork())
972 return mod.runModule(rf);
const double & getTime() const
const Vector & getCmd() const
const int & getOffset() const
void push_back(ActionItem *v)
bool go(const string &fileName)
void attachVelPort(Port *p)
void attachVelInitPort(Port *p)
void attachRF(ResourceFinder *attachedRF)
void attachPosPort(scriptPosPort *p)
WorkingThread(int period=100)
Adaptive window linear fitting to estimate the first derivative.
Basic element for adaptive polynomial fitting.
bool parsePosCmd(const Bottle &cmd, Bottle &reply, ActionItem **action)
virtual bool updateModule()
bool handlectpm(const Bottle &cmd, Bottle &reply)
virtual double getPeriod()
bool handle_ctp_file(const Bottle &cmd, Bottle &reply)
bool handle_wait(const Bottle &cmd, Bottle &reply)
virtual bool respond(const Bottle &command, Bottle &reply)
virtual bool configure(ResourceFinder &rf)
bool handle_ctp_queue(const Bottle &cmd, Bottle &reply)
void _send(const ActionItem *x)
bool autochange_control_mode_enable
void queue(ActionItem *item)
void send(ActionItem *tmp)
bool configure(const Property &options)
void sendNow(ActionItem *item)
deque< ActionItem * > ActionList
Copyright (C) 2008 RobotCub Consortium.