35 double curr_time = yarp::os::Time::now()-t_start;
37 if (getIterations()>100)
39 yDebug(
"Thread ran %d times, est period %lf[ms], used %lf[ms]\n",
41 1000.0*getEstimatedPeriod(),
42 1000.0*getEstimatedUsed());
45 std::lock_guard<std::mutex> lck(_mutex);
48 yarp::os::Bottle *bot = command_port.read(
false);
51 unsigned int botsize = bot->size();
52 if (botsize == control_joints)
55 for (
unsigned int i=0; i< control_joints; i++)
57 targets[i] = bot->get(i).asFloat64();
62 yError (
"Your bottle does not have the right size: module is configured to control %d joints", control_joints);
71 for (
unsigned int i=0; i< control_joints; i++)
73 if (targets[i]>max_limits[i]) targets[i]=max_limits[i];
74 if (targets[i]<min_limits[i]) targets[i]=min_limits[i];
78 for (
unsigned int i=0; i< control_joints; i++)
81 ienc->getEncoder(control_joints_list[i],&val);
86 for (
unsigned int i=0; i< control_joints; i++)
88 double diff = (targets[i]-prev_targets[i]);
94 for(
unsigned int i = 0; i<control_joints; i++)
96 double diff = (targets[i]-encoders[i]);
103 for(
unsigned int i = 0; i<control_joints; i++)
105 idir->setPosition(control_joints_list[i],targets[i]);
108 idir->setPositions(control_joints, control_joints_list, targets.data());
110 yDebug() << curr_time << targets[0] << targets[1] << targets[2];
135 sprintf(tmp,
"/%s/%s/%s/command:i", moduleName.c_str(), robotName.c_str(), partName.c_str());
136 yInfo(
"opening port for part %s\n",tmp);
137 command_port.open(tmp);
141 yError (
"Invalid device driver pointer");
152 if ( (idir==0)||(ienc==0) || (imod==0) || (ipos==0) || (ilim==0))
154 yError (
"Failed to view motor interfaces");
159 ipos->getAxes(&tmpj);
161 control_joints= jointsList->size();
162 if (control_joints>part_joints)
164 yError (
"you cannot control more of %d joints for this robot part", part_joints);
167 else if (control_joints<=0)
169 yError (
"invalid number of control joints (%d)", control_joints);
174 control_joints_list =
new int [control_joints];
175 for (
unsigned int i=0; i< control_joints; i++)
177 if (jointsList->get(i).isInt32() && jointsList->get(i).asInt32()>=0)
179 control_joints_list[i] = jointsList->get(i).asInt32();
183 yError (
"invalid list of jonts to control");
188 yInfo(
"part has %d joints, controlling %d joints\n",part_joints, control_joints);
191 speeds.resize(control_joints);
193 for (
unsigned int i=0; i<control_joints; i++)
195 ipos->setRefSpeed(control_joints_list[i],speeds[i]);
198 encoders.resize(control_joints);
199 targets.resize(control_joints);
200 prev_targets.resize(control_joints);
201 error.resize(control_joints);
207 min_limits.resize(control_joints);
208 max_limits.resize(control_joints);
209 for (
unsigned int i=0; i<control_joints; i++)
213 ilim->getLimits(control_joints_list[i],&min,&max);
218 for (
unsigned int i=0; i<control_joints; i++)
220 imod->setControlMode(control_joints_list[i],VOCAB_CM_POSITION_DIRECT);
224 for (
unsigned int i=0; i< control_joints; i++)
227 ienc->getEncoder(control_joints_list[i],&val);
228 targets[i] = encoders[i] = val;
229 prev_targets[i] = encoders[i];