36int main(
int argc,
char *argv[])
40 if (!
yarp.checkNetwork())
42 printf(
"YARP is not available!\n");
47 rf.configure(argc,argv);
49 string name=rf.check(
"name",Value(
"tuner")).asString();
50 string robot=rf.check(
"robot",Value(
"icub")).asString();
51 string part=rf.check(
"part",Value(
"right_arm")).asString();
52 int joint=rf.check(
"joint",Value(11)).asInt32();
53 double encoder=rf.check(
"encoder",Value(2.43)).asFloat64();
56 pOptions.put(
"device",
"remote_controlboard");
57 pOptions.put(
"remote",
"/"+robot+
"/"+part);
58 pOptions.put(
"local",
"/"+name+
"/"+part);
59 PolyDriver driver(pOptions);
60 if (!driver.isValid())
62 printf(
"Part \"%s\" is not ready!\n",
string(
"/"+robot+
"/"+part).c_str());
110 pGeneral.put(
"joint",joint);
114 pGeneral.put(
"port",
"/"+name+
"/info:o");
115 string sGeneral=
"(general ";
116 sGeneral+=pGeneral.toString();
119 Bottle bGeneral,bPlantEstimation,bStictionEstimation;
120 bGeneral.fromString(sGeneral);
123 bPlantEstimation.fromString(
"(plant_estimation (Ts 0.01) (Q 1.0) (R 1.0) (P0 100000.0) (tau 1.0) (K 1.0) (max_pwm 800.0))");
124 bStictionEstimation.fromString(
"(stiction_estimation (Ts 0.01) (T 2.0) (vel_thres 5.0) (e_thres 1.0) (gamma (10.0 10.0)) (stiction (0.0 0.0)))");
127 Bottle bConf=bGeneral;
128 bConf.append(bPlantEstimation);
129 bConf.append(bStictionEstimation);
130 pOptions.fromString(bConf.toString());
133 if (!designer.
configure(driver,pOptions))
135 printf(
"Configuration failed!\n");
145 Property pPlantEstimation;
146 pPlantEstimation.put(
"max_time",20.0);
150 pPlantEstimation.put(
"switch_timeout",2.0);
153 printf(
"Estimation experiment will last %g seconds...\n",
154 pPlantEstimation.find(
"max_time").asFloat64());
156 double t0=Time::now();
157 while (!designer.
isDone())
159 printf(
"elapsed %d [s]\n",(
int)(Time::now()-t0));
166 double tau=pResults.find(
"tau_mean").asFloat64();
167 double K=pResults.find(
"K_mean").asFloat64();
169 printf(
"plant = K/s * 1/(1+s*tau)\n");
170 printf(
"Estimated parameters...\n");
171 printf(
"tau = %g\n",tau);
172 printf(
"K = %g\n",K);
176 Property pPlantValidation;
177 pPlantValidation.put(
"max_time",10.0);
178 pPlantValidation.put(
"switch_timeout",2.0);
179 pPlantValidation.put(
"tau",tau);
180 pPlantValidation.put(
"K",K);
187 pPlantValidation.put(
"measure_update_ticks",100);
190 printf(
"Validation experiment will last %g seconds...\n",
191 pPlantValidation.find(
"max_time").asFloat64());
194 while (!designer.
isDone())
196 printf(
"elapsed %d [s]\n",(
int)(Time::now()-t0));
201 printf(
"Tuning P controller...\n");
202 Property pControllerRequirements,pController;
203 pControllerRequirements.put(
"tau",tau);
204 pControllerRequirements.put(
"K",K);
215 pControllerRequirements.put(
"f_c",0.75);
216 pControllerRequirements.put(
"type",
"P");
218 printf(
"tuning results: %s\n",pController.toString().c_str());
219 double Kp=pController.find(
"Kp").asFloat64();
220 printf(
"found Kp = %g\n",Kp);
222 double Kp_fw=Kp*encoder*(1<<scale);
223 printf(
"Kp (firmware) = %g; shift factor = %d\n",Kp_fw,scale);
226 Property pStictionEstimation;
227 pStictionEstimation.put(
"max_time",60.0);
230 pStictionEstimation.put(
"Kp",Kp);
231 pStictionEstimation.put(
"Ki",0.0);
232 pStictionEstimation.put(
"Kd",0.0);
235 printf(
"Stiction estimation experiment will last no more than %g seconds...\n",
236 pStictionEstimation.find(
"max_time").asFloat64());
239 while (!designer.
isDone())
241 printf(
"elapsed %d [s]\n",(
int)(Time::now()-t0));
248 stiction[0]=pResults.find(
"stiction").asList()->get(0).asFloat64();
249 stiction[1]=pResults.find(
"stiction").asList()->get(1).asFloat64();
250 printf(
"Stiction values: up = %g; down = %g\n",stiction[0],stiction[1]);
254 Property pControllerValidation;
255 pControllerValidation.put(
"max_time",60.0);
256 pControllerValidation.put(
"Kp",Kp_fw);
257 pControllerValidation.put(
"scale",scale);
264 Value val; val.fromString(str.str().c_str());
265 pControllerValidation.put(
"stiction",val);
268 pControllerValidation.put(
"stiction_compensation",
"middleware");
273 pControllerValidation.put(
"ref_type",
"min-jerk");
274 pControllerValidation.put(
"ref_period",2.0);
278 pControllerValidation.put(
"ref_sustain_time",1.0);
282 pControllerValidation.put(
"cycles_to_switch",4);
285 printf(
"Controller validation will last %g seconds...\n",
286 pControllerValidation.find(
"max_time").asFloat64());
289 while (!designer.
isDone())
291 printf(
"elapsed %d [s]\n",(
int)(Time::now()-t0));