RobotHardware.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include <rtm/CorbaNaming.h>
00011 #include "hrpsys/util/VectorConvert.h"
00012 #include "RobotHardware.h"
00013 #include "robot.h"
00014 
00015 #include <hrpModel/Sensor.h>
00016 #include <hrpModel/ModelLoaderUtil.h>
00017 
00018 using namespace OpenHRP;
00019 using namespace hrp;
00020 
00021 // Module specification
00022 // <rtc-template block="module_spec">
00023 static const char* robothardware_spec[] =
00024   {
00025     "implementation_id", "RobotHardware",
00026     "type_name",         "RobotHardware",
00027     "description",       "RobotHardware",
00028     "version",           HRPSYS_PACKAGE_VERSION,
00029     "vendor",            "AIST",
00030     "category",          "example",
00031     "activity_type",     "DataFlowComponent",
00032     "max_instance",      "1",
00033     "language",          "C++",
00034     "lang_type",         "compile",
00035     // Configuration variables
00036     "conf.default.isDemoMode", "0",
00037     "conf.default.fzLimitRatio", "2.0",
00038     "conf.default.servoErrorLimit", ",",
00039     "conf.default.jointAccelerationLimit", "0",
00040     "conf.default.servoOnDelay", "0",
00041 
00042     ""
00043   };
00044 // </rtc-template>
00045 
00046 RobotHardware::RobotHardware(RTC::Manager* manager)
00047   : RTC::DataFlowComponentBase(manager),
00048     // <rtc-template block="initializer">
00049     m_isDemoMode(0),
00050     m_qRefIn("qRef", m_qRef),
00051     m_dqRefIn("dqRef", m_dqRef),
00052     m_ddqRefIn("ddqRef", m_ddqRef),
00053     m_tauRefIn("tauRef", m_tauRef),
00054     m_qOut("q", m_q),
00055     m_dqOut("dq", m_dq),
00056     m_tauOut("tau", m_tau),
00057     m_ctauOut("ctau", m_ctau),
00058     m_pdtauOut("pdtau", m_pdtau),
00059     m_servoStateOut("servoState", m_servoState),
00060     m_emergencySignalOut("emergencySignal", m_emergencySignal),
00061     m_rstate2Out("rstate2", m_rstate2),
00062     m_RobotHardwareServicePort("RobotHardwareService"),
00063     // </rtc-template>
00064         dummy(0)
00065 {
00066 }
00067 
00068 RobotHardware::~RobotHardware()
00069 {
00070 }
00071 
00072 
00073 RTC::ReturnCode_t RobotHardware::onInitialize()
00074 {
00075   // Registration: InPort/OutPort/Service
00076   // <rtc-template block="registration">
00077   
00078   addInPort("qRef", m_qRefIn);
00079   addInPort("dqRef", m_dqRefIn);
00080   addInPort("ddqRef", m_ddqRefIn);
00081   addInPort("tauRef", m_tauRefIn);
00082 
00083   addOutPort("q", m_qOut);
00084   addOutPort("dq", m_dqOut);
00085   addOutPort("tau", m_tauOut);
00086   addOutPort("ctau", m_ctauOut);
00087   addOutPort("pdtau", m_pdtauOut);
00088   addOutPort("servoState", m_servoStateOut);
00089   addOutPort("emergencySignal", m_emergencySignalOut);
00090   addOutPort("rstate2", m_rstate2Out);
00091 
00092   // Set service provider to Ports
00093     m_RobotHardwareServicePort.registerProvider("service0", "RobotHardwareService", m_service0);
00094   
00095   // Set service consumers to Ports
00096   
00097   // Set CORBA Service Ports
00098   addPort(m_RobotHardwareServicePort);
00099   
00100   // </rtc-template>
00101 
00102   RTC::Properties& prop = getProperties();
00103   double dt = 0.0;
00104   coil::stringTo(dt, prop["dt"].c_str());
00105   if (!dt) {
00106       std::cerr << m_profile.instance_name << ": joint command velocity check is disabled" << std::endl;
00107   }
00108   m_robot = boost::shared_ptr<robot>(new robot(dt));
00109 
00110 
00111   RTC::Manager& rtcManager = RTC::Manager::instance();
00112   std::string nameServer = rtcManager.getConfig()["corba.nameservers"];
00113   int comPos = nameServer.find(",");
00114   if (comPos < 0){
00115       comPos = nameServer.length();
00116   }
00117   nameServer = nameServer.substr(0, comPos);
00118   RTC::CorbaNaming naming(rtcManager.getORB(), nameServer.c_str());
00119   if (!loadBodyFromModelLoader(m_robot, prop["model"].c_str(), 
00120                                CosNaming::NamingContext::_duplicate(naming.getRootContext())
00121           )){
00122       if (prop["model"] == ""){
00123           std::cerr << "[" << m_profile.instance_name << "] path to the model file is not defined. Please check the configuration file" << std::endl;
00124       }else{
00125           std::cerr << "[" << m_profile.instance_name << "] failed to load model[" << prop["model"] << "]" << std::endl;
00126       }
00127   }
00128 
00129   std::vector<std::string> keys = prop.propertyNames();
00130   for (unsigned int i=0; i<keys.size(); i++){
00131       m_robot->setProperty(keys[i].c_str(), prop[keys[i]].c_str());
00132   } 
00133   std::cout << "dof = " << m_robot->numJoints() << std::endl;
00134   if (!m_robot->init()) return RTC::RTC_ERROR;
00135 
00136   m_service0.setRobot(m_robot);
00137 
00138   m_q.data.length(m_robot->numJoints());
00139   m_dq.data.length(m_robot->numJoints());
00140   m_tau.data.length(m_robot->numJoints());
00141   m_ctau.data.length(m_robot->numJoints());
00142   m_pdtau.data.length(m_robot->numJoints());
00143   m_servoState.data.length(m_robot->numJoints());
00144   m_qRef.data.length(m_robot->numJoints());
00145   m_dqRef.data.length(m_robot->numJoints());
00146   m_ddqRef.data.length(m_robot->numJoints());
00147   m_tauRef.data.length(m_robot->numJoints());
00148 
00149   int ngyro = m_robot->numSensors(Sensor::RATE_GYRO);
00150   std::cout << "the number of gyros = " << ngyro << std::endl;
00151   m_rate.resize(ngyro);
00152   m_rateOut.resize(ngyro);
00153   for (unsigned int i=0; i<m_rate.size(); i++){
00154       Sensor *s = m_robot->sensor(Sensor::RATE_GYRO, i);
00155       std::cout << s->name << std::endl;
00156       m_rateOut[i] = new OutPort<TimedAngularVelocity3D>(s->name.c_str(), m_rate[i]);
00157       registerOutPort(s->name.c_str(), *m_rateOut[i]);
00158   }
00159 
00160   int nacc = m_robot->numSensors(Sensor::ACCELERATION);
00161   std::cout << "the number of accelerometers = " << nacc << std::endl;
00162   m_acc.resize(nacc);
00163   m_accOut.resize(nacc);
00164   for (unsigned int i=0; i<m_acc.size(); i++){
00165       Sensor *s = m_robot->sensor(Sensor::ACCELERATION, i);
00166       std::cout << s->name << std::endl;
00167       m_accOut[i] = new OutPort<TimedAcceleration3D>(s->name.c_str(), m_acc[i]);
00168       registerOutPort(s->name.c_str(), *m_accOut[i]);
00169   }
00170 
00171   int nforce = m_robot->numSensors(Sensor::FORCE);
00172   std::cout << "the number of force sensors = " << nforce << std::endl;
00173   m_force.resize(nforce);
00174   m_forceOut.resize(nforce);
00175   for (unsigned int i=0; i<m_force.size(); i++){
00176       Sensor *s = m_robot->sensor(Sensor::FORCE, i);
00177       std::cout << s->name << std::endl;
00178       m_forceOut[i] = new OutPort<TimedDoubleSeq>(s->name.c_str(), m_force[i]);
00179       m_force[i].data.length(6);
00180       registerOutPort(s->name.c_str(), *m_forceOut[i]);
00181   }
00182 
00183   // <rtc-template block="bind_config">
00184   // Bind variables and configuration variable
00185   bindParameter("isDemoMode", m_isDemoMode, "0");  
00186   bindParameter("servoErrorLimit", m_robot->m_servoErrorLimit, ",");
00187   bindParameter("fzLimitRatio", m_robot->m_fzLimitRatio, "2");
00188   bindParameter("jointAccelerationLimit", m_robot->m_accLimit, "0");
00189   bindParameter("servoOnDelay", m_robot->m_servoOnDelay, "0");
00190 
00191   // </rtc-template>
00192 
00193   return RTC::RTC_OK;
00194 }
00195 
00196 
00197 /*
00198 RTC::ReturnCode_t RobotHardware::onFinalize()
00199 {
00200   return RTC::RTC_OK;
00201 }
00202 */
00203 
00204 /*
00205 RTC::ReturnCode_t RobotHardware::onStartup(RTC::UniqueId ec_id)
00206 {
00207   return RTC::RTC_OK;
00208 }
00209 */
00210 
00211 /*
00212 RTC::ReturnCode_t RobotHardware::onShutdown(RTC::UniqueId ec_id)
00213 {
00214   return RTC::RTC_OK;
00215 }
00216 */
00217 
00218 /*
00219 RTC::ReturnCode_t RobotHardware::onActivated(RTC::UniqueId ec_id)
00220 {
00221   return RTC::RTC_OK;
00222 }
00223 */
00224 
00225 /*
00226 RTC::ReturnCode_t RobotHardware::onDeactivated(RTC::UniqueId ec_id)
00227 {
00228   return RTC::RTC_OK;
00229 }
00230 */
00231 
00232 RTC::ReturnCode_t RobotHardware::onExecute(RTC::UniqueId ec_id)
00233 {
00234     //std::cout << "RobotHardware:onExecute(" << ec_id << ")" << std::endl;
00235   Time tm;
00236   this->getTimeNow(tm);
00237 
00238   if (!m_isDemoMode){
00239       robot::emg_reason reason;
00240       int id;
00241       if (m_robot->checkEmergency(reason, id)){
00242           if (reason == robot::EMG_SERVO_ERROR || reason == robot::EMG_POWER_OFF){
00243               m_robot->servo("all", false);
00244               m_emergencySignal.data = reason;
00245               m_emergencySignalOut.write();
00246           } else if (reason == robot::EMG_SERVO_ALARM) {
00247               m_emergencySignal.data = reason;
00248               m_emergencySignalOut.write();
00249           }
00250       }
00251   }    
00252 
00253   if (m_qRefIn.isNew()){
00254       m_qRefIn.read();
00255       //std::cout << "RobotHardware: qRef[21] = " << m_qRef.data[21] << std::endl;
00256       if (!m_isDemoMode 
00257           && m_robot->checkJointCommands(m_qRef.data.get_buffer())){
00258           m_robot->servo("all", false);
00259           m_emergencySignal.data = robot::EMG_SERVO_ERROR;
00260           m_emergencySignalOut.write();
00261       }else{
00262           // output to iob
00263           m_robot->writeJointCommands(m_qRef.data.get_buffer());
00264       }
00265   }
00266   if (m_dqRefIn.isNew()){
00267       m_dqRefIn.read();
00268       //std::cout << "RobotHardware: dqRef[21] = " << m_dqRef.data[21] << std::endl;
00269       // output to iob
00270       m_robot->writeVelocityCommands(m_dqRef.data.get_buffer());
00271   }
00272   if (m_ddqRefIn.isNew()){
00273       m_ddqRefIn.read();
00274       //std::cout << "RobotHardware: dqRef[21] = " << m_dqRef.data[21] << std::endl;
00275       // output to iob
00276       m_robot->writeAccelerationCommands(m_ddqRef.data.get_buffer());
00277   }
00278   if (m_tauRefIn.isNew()){
00279       m_tauRefIn.read();
00280       //std::cout << "RobotHardware: tauRef[21] = " << m_tauRef.data[21] << std::endl;
00281       // output to iob
00282       m_robot->writeTorqueCommands(m_tauRef.data.get_buffer());
00283   }
00284 
00285   // read from iob
00286   m_robot->readJointAngles(m_q.data.get_buffer());  
00287   m_q.tm = tm;
00288   m_robot->readJointVelocities(m_dq.data.get_buffer());  
00289   m_dq.tm = tm;
00290   m_robot->readJointTorques(m_tau.data.get_buffer());
00291   m_tau.tm = tm;
00292   m_robot->readJointCommandTorques(m_ctau.data.get_buffer());
00293   m_ctau.tm = tm;
00294   m_robot->readPDControllerTorques(m_pdtau.data.get_buffer());
00295   m_pdtau.tm = tm;
00296   for (unsigned int i=0; i<m_rate.size(); i++){
00297       double rate[3];
00298       m_robot->readGyroSensor(i, rate);
00299       m_rate[i].data.avx = rate[0];
00300       m_rate[i].data.avy = rate[1];
00301       m_rate[i].data.avz = rate[2];
00302       m_rate[i].tm = tm;
00303   }
00304 
00305   for (unsigned int i=0; i<m_acc.size(); i++){
00306       double acc[3];
00307       m_robot->readAccelerometer(i, acc);
00308       m_acc[i].data.ax = acc[0];
00309       m_acc[i].data.ay = acc[1];
00310       m_acc[i].data.az = acc[2];
00311       m_acc[i].tm = tm;
00312   }
00313 
00314   for (unsigned int i=0; i<m_force.size(); i++){
00315       m_robot->readForceSensor(i, m_force[i].data.get_buffer());
00316       m_force[i].tm = tm;
00317   }
00318   
00319   for (unsigned int i=0; i<m_servoState.data.length(); i++){
00320       size_t len = m_robot->lengthOfExtraServoState(i)+1;
00321       m_servoState.data[i].length(len);
00322       int status = 0, v;
00323       v = m_robot->readCalibState(i);
00324       status |= v<< OpenHRP::RobotHardwareService::CALIB_STATE_SHIFT;
00325       v = m_robot->readPowerState(i);
00326       status |= v<< OpenHRP::RobotHardwareService::POWER_STATE_SHIFT;
00327       v = m_robot->readServoState(i);
00328       status |= v<< OpenHRP::RobotHardwareService::SERVO_STATE_SHIFT;
00329       v = m_robot->readServoAlarm(i);
00330       status |= v<< OpenHRP::RobotHardwareService::SERVO_ALARM_SHIFT;
00331       v = m_robot->readDriverTemperature(i);
00332       status |= v<< OpenHRP::RobotHardwareService::DRIVER_TEMP_SHIFT;
00333       m_servoState.data[i][0] = status;
00334       m_robot->readExtraServoState(i, (int *)(m_servoState.data[i].get_buffer()+1));
00335   }
00336   m_servoState.tm = tm;
00337 
00338   getStatus2(m_rstate2.data);
00339   m_rstate2.tm = tm;
00340   
00341   m_robot->oneStep();
00342 
00343   m_qOut.write();
00344   m_dqOut.write();
00345   m_tauOut.write();
00346   m_ctauOut.write();
00347   m_pdtauOut.write();
00348   m_servoStateOut.write();
00349   for (unsigned int i=0; i<m_rateOut.size(); i++){
00350       m_rateOut[i]->write();
00351   }
00352   for (unsigned int i=0; i<m_accOut.size(); i++){
00353       m_accOut[i]->write();
00354   }
00355   for (unsigned int i=0; i<m_forceOut.size(); i++){
00356       m_forceOut[i]->write();
00357   }
00358   m_rstate2Out.write();
00359 
00360   return RTC::RTC_OK;
00361 }
00362 
00363 template<class T>
00364 void getStatus(boost::shared_ptr<robot> robot, T& rstate)
00365 {
00366   rstate.angle.length(robot->numJoints());
00367   robot->readJointAngles(rstate.angle.get_buffer());
00368 
00369   rstate.command.length(robot->numJoints());
00370   robot->readJointCommands(rstate.command.get_buffer());
00371 
00372   rstate.torque.length(robot->numJoints());
00373   if (!robot->readJointTorques(rstate.torque.get_buffer())){
00374     for (unsigned int i=0; i<rstate.torque.length(); i++){
00375       rstate.torque[i] = 0.0;
00376     }
00377   }
00378 
00379   rstate.servoState.length(robot->numJoints());
00380   int v, status;
00381   for(unsigned int i=0; i < rstate.servoState.length(); ++i){
00382     size_t len = robot->lengthOfExtraServoState(i)+1;
00383     rstate.servoState[i].length(len);
00384     status = 0;
00385     v = robot->readCalibState(i);
00386     status |= v<< OpenHRP::RobotHardwareService::CALIB_STATE_SHIFT;
00387     v = robot->readPowerState(i);
00388     status |= v<< OpenHRP::RobotHardwareService::POWER_STATE_SHIFT;
00389     v = robot->readServoState(i);
00390     status |= v<< OpenHRP::RobotHardwareService::SERVO_STATE_SHIFT;
00391     v = robot->readServoAlarm(i);
00392     status |= v<< OpenHRP::RobotHardwareService::SERVO_ALARM_SHIFT;
00393     v = robot->readDriverTemperature(i);
00394     status |= v<< OpenHRP::RobotHardwareService::DRIVER_TEMP_SHIFT;
00395     rstate.servoState[i][0] = status;
00396     robot->readExtraServoState(i, (int *)(rstate.servoState[i].get_buffer()+1));
00397   }
00398 
00399   rstate.rateGyro.length(robot->numSensors(Sensor::RATE_GYRO));
00400   for (unsigned int i=0; i<rstate.rateGyro.length(); i++){
00401     rstate.rateGyro[i].length(3);
00402     robot->readGyroSensor(i, rstate.rateGyro[i].get_buffer());
00403   }
00404 
00405   rstate.accel.length(robot->numSensors(Sensor::ACCELERATION));
00406   for (unsigned int i=0; i<rstate.accel.length(); i++){
00407     rstate.accel[i].length(3);
00408     robot->readAccelerometer(i, rstate.accel[i].get_buffer());
00409   }
00410 
00411   rstate.force.length(robot->numSensors(Sensor::FORCE));
00412   for (unsigned int i=0; i<rstate.force.length(); i++){
00413     rstate.force[i].length(6);
00414     robot->readForceSensor(i, rstate.force[i].get_buffer());
00415   }
00416 
00417   robot->readPowerStatus(rstate.voltage, rstate.current);
00418 }
00419  
00420 void RobotHardware::getStatus2(OpenHRP::RobotHardwareService::RobotState2 &rstate2)
00421 {
00422   getStatus(m_robot, rstate2);
00423 #if defined(ROBOT_IOB_VERSION) && ROBOT_IOB_VERSION >= 2
00424   rstate2.batteries.length(m_robot->numBatteries());
00425   for(unsigned int i=0; i<rstate2.batteries.length(); i++){
00426       m_robot->readBatteryState(i, 
00427                                 rstate2.batteries[i].voltage,
00428                                 rstate2.batteries[i].current,
00429                                 rstate2.batteries[i].soc);
00430   }
00431   rstate2.temperature.length(m_robot->numThermometers());
00432   for (unsigned int i=0; i<rstate2.temperature.length(); i++){
00433       m_robot->readThermometer(i, rstate2.temperature[i]);
00434   }
00435 #endif
00436 }
00437 
00438 /*
00439 RTC::ReturnCode_t RobotHardware::onAborting(RTC::UniqueId ec_id)
00440 {
00441   return RTC::RTC_OK;
00442 }
00443 */
00444 
00445 /*
00446 RTC::ReturnCode_t RobotHardware::onError(RTC::UniqueId ec_id)
00447 {
00448   return RTC::RTC_OK;
00449 }
00450 */
00451 
00452 /*
00453 RTC::ReturnCode_t RobotHardware::onReset(RTC::UniqueId ec_id)
00454 {
00455   return RTC::RTC_OK;
00456 }
00457 */
00458 
00459 /*
00460 RTC::ReturnCode_t RobotHardware::onStateUpdate(RTC::UniqueId ec_id)
00461 {
00462   return RTC::RTC_OK;
00463 }
00464 */
00465 
00466 /*
00467 RTC::ReturnCode_t RobotHardware::onRateChanged(RTC::UniqueId ec_id)
00468 {
00469   return RTC::RTC_OK;
00470 }
00471 */
00472 
00473 
00474 
00475 extern "C"
00476 {
00477 
00478   void RobotHardwareInit(RTC::Manager* manager)
00479   {
00480     RTC::Properties profile(robothardware_spec);
00481     manager->registerFactory(profile,
00482                              RTC::Create<RobotHardware>,
00483                              RTC::Delete<RobotHardware>);
00484   }
00485 
00486 };
00487 
00488 


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed May 15 2019 05:02:18