void idlingCoupledJoints(const int i, const bool sw) { IControlMode *imod; driver->view(imod); PidData &pid=pids[i]; for (size_t j=0; j<pid.idling_joints.size(); j++) imod->setControlMode(pid.idling_joints[j], sw?VOCAB_CM_IDLE:VOCAB_CM_POSITION); }
bool tune(const int i) { PidData &pid=pids[i]; Property pGeneral; pGeneral.put("joint",i); string sGeneral="(general "; sGeneral+=pGeneral.toString(); sGeneral+=')'; Bottle bGeneral,bPlantEstimation,bStictionEstimation; bGeneral.fromString(sGeneral); 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))"); 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)))"); Bottle bConf=bGeneral; bConf.append(bPlantEstimation); bConf.append(bStictionEstimation); Property pOptions(bConf.toString().c_str()); OnlineCompensatorDesign designer; if (!designer.configure(*driver,pOptions)) { yError("designer configuration failed!"); return false; } idlingCoupledJoints(i,true); Property pPlantEstimation; pPlantEstimation.put("max_time",20.0); pPlantEstimation.put("switch_timeout",2.0); designer.startPlantEstimation(pPlantEstimation); yInfo("Estimating plant for joint %d: max duration = %g seconds", i,pPlantEstimation.find("max_time").asDouble()); double t0=Time::now(); while (!designer.isDone()) { yInfo("elapsed %d [s]",(int)(Time::now()-t0)); Time::delay(1.0); if (interrupting) { idlingCoupledJoints(i,false); return false; } } Property pResults; designer.getResults(pResults); double tau=pResults.find("tau_mean").asDouble(); double K=pResults.find("K_mean").asDouble(); yInfo("plant = %g/s * 1/(1+s*%g)",K,tau); Property pControllerRequirements,pController; pControllerRequirements.put("tau",tau); pControllerRequirements.put("K",K); pControllerRequirements.put("f_c",0.75); if (i!=15) { pControllerRequirements.put("T_dr",1.0); pControllerRequirements.put("type","PI"); } else pControllerRequirements.put("type","P"); designer.tuneController(pControllerRequirements,pController); yInfo("tuning results: %s",pController.toString().c_str()); double Kp=pController.find("Kp").asDouble(); double Ki=pController.find("Ki").asDouble(); pid.scale=4.0; int scale=(int)pid.scale; int shift=1<<scale; double fwKp=floor(Kp*pid.encs_ratio*shift); double fwKi=floor(Ki*pid.encs_ratio*shift/1000.0); pid.Kp=yarp::math::sign(pid.Kp*fwKp)>0.0?fwKp:-fwKp; pid.Ki=yarp::math::sign(pid.Ki*fwKi)>0.0?fwKi:-fwKi; pid.Kd=0.0; yInfo("Kp (FW) = %g; Ki (FW) = %g; Kd (FW) = %g; shift factor = %d",pid.Kp,pid.Ki,pid.Kd,scale); Property pStictionEstimation; pStictionEstimation.put("max_time",60.0); pStictionEstimation.put("Kp",Kp); pStictionEstimation.put("Ki",0.0); pStictionEstimation.put("Kd",0.0); designer.startStictionEstimation(pStictionEstimation); yInfo("Estimating stiction for joint %d: max duration = %g seconds", i,pStictionEstimation.find("max_time").asDouble()); t0=Time::now(); while (!designer.isDone()) { yInfo("elapsed %d [s]",(int)(Time::now()-t0)); Time::delay(1.0); if (interrupting) { idlingCoupledJoints(i,false); return false; } } designer.getResults(pResults); pid.st_up=floor(pResults.find("stiction").asList()->get(0).asDouble()); pid.st_down=floor(pResults.find("stiction").asList()->get(1).asDouble()); yInfo("Stiction values: up = %g; down = %g",pid.st_up,pid.st_down); IControlMode *imod; IPositionControl *ipos; IEncoders *ienc; driver->view(imod); driver->view(ipos); driver->view(ienc); imod->setControlMode(i,VOCAB_CM_POSITION); ipos->setRefSpeed(i,50.0); ipos->positionMove(i,0.0); yInfo("Driving the joint back to rest... "); t0=Time::now(); while (Time::now()-t0<5.0) { double enc; ienc->getEncoder(i,&enc); if (fabs(enc)<1.0) break; if (interrupting) { idlingCoupledJoints(i,false); return false; } Time::delay(0.2); } yInfo("done!"); idlingCoupledJoints(i,false); return true; }