11 #include <yarp/os/Log.h>
12 #include <yarp/os/LogStream.h>
15 using namespace yarp::os;
16 using namespace yarp::sig;
26 yarp::os::PeriodicThread((double)rate/1000.0)
37 double t_start = yarp::os::Time::now();
39 if (getIterations()>100)
41 yDebug(
"Thread ran %d times, est period %lf[ms], used %lf[ms]\n",
43 1000.0*getEstimatedPeriod(),
44 1000.0*getEstimatedUsed());
47 std::lock_guard<std::mutex> lck(_mutex);
51 yarp::os::Bottle *bot = command_port.read(
false);
56 int size = bot->size()/2;
57 for(
int i=0;i<size;i++)
59 int ind = bot->get(2*i).asInt32();
62 targets(ind) = bot->get(2*i+1).asFloat64();
72 if(nb_void_loops > 5) {
78 yarp::os::Bottle *vec = command_port2.read(
false);
81 for(
int k=0;k<nJoints;k++)
82 targets(k)=vec->get(k).asFloat64();
88 ienc->getEncoders(encoders.data());
93 for(
int i=0;i<nJoints;i++)
94 fprintf(currentSpeedFile,
"%f ",encoders(i));
95 fprintf(currentSpeedFile,
"%f\n",t_start-time_watch);
100 for(
int k=0;k<nJoints;k++)
103 double current_err = targets(k)-encoders(k);
104 error_d(k) = (current_err - error(k))/((
double)control_rate)*1000.0;
105 error(k) = current_err;
108 command(k) = Kp(k)*error(k) + Kd(k)*error_d(k) + ffVelocities(k);
120 for(
int i=0;i<nJoints;i++)
121 fprintf(targetSpeedFile,
"%f ",command(i));
122 fprintf(targetSpeedFile,
"%f\n",t_start-time_watch);
132 for(
int i=0;i<nJoints;i++)
134 if (fabs(targets(i)-encoders(i))>6.0)
135 ipos->positionMove(i,targets(i));
137 ipid->setPidReference(yarp::dev::VOCAB_PIDTYPE_POSITION,i,targets(i));
143 while(!ivel->velocityMove(command.data()))
146 yError(
"velcontrol ERROR>> velocity move sent false\n");
149 yError(
"velcontrol ERROR>> tried 10 times to velocityMove, halting...\n");
161 ienc->getEncoders(encoders.data());
166 time_watch = Time::now();
174 for(
int k=0;k<nJoints;k++)
177 for (
int k = 0; k < nJoints; k++)
178 imod->setControlMode(k, VOCAB_CM_POSITION);
182 command_port.close();
183 command_port2.close();
198 sprintf(
tmp,
"/%s/vc/%s/fastCommand", robotName.c_str(), partName.c_str());
199 yInfo(
"opening port for part %s\n",
tmp);
200 command_port.open(
tmp);
204 tmp2+=robotName.c_str();
206 tmp2+=partName.c_str();
208 command_port2.open(tmp2.c_str());
221 if ( (ivel==0)||(ienc==0) || (imod==0) || (ipid==0) || (ipos==0))
224 ivel->getAxes(&nJoints);
225 yInfo(
"controlling %d DOFs\n",nJoints);
228 accs.resize(nJoints);
230 speeds.resize(nJoints);
231 ivel->getRefAccelerations(accs.data());
233 ivel->setRefAccelerations(accs.data());
235 ipos->setRefSpeeds(speeds.data());
237 encoders.resize(nJoints);
238 encoders_speed.resize(nJoints);
239 command.resize(nJoints);
240 targets.resize(nJoints);
241 ffVelocities.resize(nJoints);
249 error.resize(nJoints);
251 error_d.resize(nJoints);
254 maxVel.resize(nJoints);
258 sprintf(
tmp,
"%s_target_speed.dat",partName.c_str());
259 targetSpeedFile = fopen(
tmp,
"w");
260 sprintf(
tmp,
"%s_current_speed.dat",partName.c_str());
261 currentSpeedFile = fopen(
tmp,
"w");
271 yInfo(
"Suspended\n");
280 for(
int k = 0; k < nJoints; k++)
282 imod->getControlMode(k, &mode);
283 if (mode!=VOCAB_CM_MIXED && mode!=VOCAB_CM_VELOCITY)
285 std::string s = yarp::os::Vocab32::decode(mode);
286 yWarning(
"Joint (%d) is in mode (%s) and does not accepts velocty commands. You have first to set either VOCAB_CM_VELOCITY or VOCAB_CM_MIXED control mode\n", k, s.c_str());
296 yInfo(
"Setting new target %d to %lf\n", i, pos);
298 std::lock_guard<std::mutex> lck(_mutex);
305 std::lock_guard<std::mutex> lck(_mutex);
307 if((vel > 0.0) && (vel <
MAX_SPEED) && (i>=0) && (i<nJoints))
310 yInfo(
"setting max vel of joint %d to %f\n",i,maxVel(i));
313 yError(
"Impossible to set max vel higher than %f\n",
MAX_SPEED);
318 std::lock_guard<std::mutex> lck(_mutex);
323 if((gain >= 0.0) && (i>=0) && (i<nJoints))
326 yInfo(
"Setting gain for joint %d to %lf\n", i, Kp(i));
329 yError(
"Cannot set gain of joint %d\n",i);
335 for(
int k=0; k<nJoints;k++)
337 if(command(k)!=command(k))
340 yWarning(
"WARNING::Receiving NaN values\n");
342 if (fabs(command(k))>maxVel(k))
345 command(k)=maxVel(k);
347 command(k)=-maxVel(k);
void setGain(int i, double gain)
void setRef(int i, double pos)
velControlThread(int rate)
bool init(yarp::dev::PolyDriver *d, std::string partName, std::string robotName)
void limitSpeed(yarp::sig::Vector &command)
void setVel(int i, double vel)
Copyright (C) 2008 RobotCub Consortium.
const double ACCELERATIONS
const int VELOCITY_INDEX_OFFSET