00001
00010 #include <rtm/Manager.h>
00011 #include <rtm/CorbaNaming.h>
00012 #include <hrpModel/Body.h>
00013 #include <hrpModel/Link.h>
00014 #include <hrpModel/Sensor.h>
00015 #include <hrpModel/ModelLoaderUtil.h>
00016 #include <string>
00017 #include <fstream>
00018
00019
00020 class WriteOpenHRP3RobotModel {
00021 private:
00022 hrp::BodyPtr m_robot;
00023 OpenHRP::BodyInfo_var bodyinfo;
00024 std::ofstream ofs;
00025 public:
00026 WriteOpenHRP3RobotModel (hrp::BodyPtr _robot, OpenHRP::BodyInfo_var _bodyinfo, const std::string& out_fname)
00027 : m_robot(_robot), bodyinfo(_bodyinfo)
00028 {
00029 ofs.open(out_fname.c_str());
00030 };
00031 ~WriteOpenHRP3RobotModel ()
00032 {
00033 ofs.close();
00034 };
00035 void writeVector (const hrp::Vector3& vec, const std::string& print_name, const std::string& indent)
00036 {
00037 ofs << indent << print_name << " : " << vec(0) << "," << vec(1) << "," << vec(2) << std::endl;
00038 };
00039 void writeMatrix (const hrp::Matrix33 mat, const std::string& print_name, const std::string& indent)
00040 {
00041 ofs << indent << print_name << " : "
00042 << mat(0,0) << "," << mat(0,1) << "," << mat(0,2) << ","
00043 << mat(1,0) << "," << mat(1,1) << "," << mat(1,2) << ","
00044 << mat(2,0) << "," << mat(2,1) << "," << mat(2,2) << std::endl;
00045 };
00046 void writeMassProperties (hrp::Link* _link, const std::string& indent)
00047 {
00048 ofs << indent << "Mass : " << _link->m << std::endl;
00049 writeVector(_link->c, "LocalCom", indent);
00050 writeMatrix(_link->I, "LocalInertia", indent);
00051 ofs << indent << "ComNorm : " << _link->c.norm() << std::endl;
00052 ofs << indent << "InertiaNorm : " << _link->I.norm() << std::endl;
00053 writeVector(hrp::Vector3(_link->p + (_link->R * _link->c)), "GlobalCom", indent);
00054 writeMatrix(hrp::Matrix33(_link->R * _link->I * _link->R.transpose()), "GlobalInertia", indent);
00055 }
00056 void writeLinkProperties (const hrp::Vector3& localp, const hrp::Matrix33& localR, const std::string& indent)
00057 {
00058 writeVector(localp, "LocalPos", indent);
00059 writeMatrix(localR, "LocalRot", indent);
00060 };
00061 void writeLinkGrobalProperties (const hrp::Vector3& p, const hrp::Matrix33& R, const std::string& indent)
00062 {
00063 writeVector(p, "GlobalPos", indent);
00064 writeMatrix(R, "GlobalRot", indent);
00065 };
00066 void writeJointProperties (const hrp::Link* _link, const std::string& indent)
00067 {
00068 ofs << indent << "LowerLimit : " << _link->llimit << std::endl;
00069 ofs << indent << "UpperLimit : " << _link->ulimit << std::endl;
00070
00071
00072 ofs << indent << "VelocityLimit : " << _link->uvlimit << std::endl;
00073 };
00074 void writeSensorProperties (const hrp::Sensor* _sen, const std::string& indent)
00075 {
00076 writeVector(_sen->localPos, "LocalPos", indent);
00077 writeMatrix(_sen->localR, "LocalRot", indent);
00078 writeVector(hrp::Vector3(_sen->link->p + _sen->link->R *_sen->localPos), "GlobalPos", indent);
00079 writeMatrix(hrp::Matrix33(_sen->link->R *_sen->localR), "GlobalRot", indent);
00080 };
00081 void writeSensorProperties (const int sensorType, const std::string& indent)
00082 {
00083 for (size_t i = 0; i < m_robot->numSensors(sensorType); i++) {
00084 std::string name(m_robot->sensor(sensorType, i)->name);
00085 ofs << indent << name << ":" << std::endl;
00086 writeSensorProperties(m_robot->sensor(sensorType, i), std::string(indent+" "));
00087 }
00088 };
00089 hrp::Matrix33 outer_product_matrix(const hrp::Vector3 &v)
00090 {
00091 hrp::Matrix33 ret;
00092 (ret(0, 0) = 0);
00093 (ret(0, 1) = (-v(2)));
00094 (ret(0, 2) = v(1));
00095 (ret(1, 0) = v(2));
00096 (ret(1, 1) = 0);
00097 (ret(1, 2) = (-v(0)));
00098 (ret(2, 0) = (-v(1)));
00099 (ret(2, 1) = v(0));
00100 (ret(2, 2) = 0);
00101 return ret;
00102 }
00103 hrp::Matrix33 calcTotalInertia (const hrp::Vector3& total_com)
00104 {
00105 hrp::Matrix33 ret = hrp::Matrix33::Zero();
00106 for (size_t i = 0; i < m_robot->numLinks(); i++) {
00107 hrp::Link* l = m_robot->link(i);
00108 hrp::Vector3 tmpc = l->p + l->R * l->c - total_com;
00109 ret += l->R * l->I * l->R.transpose() + l->m * outer_product_matrix(tmpc).transpose() * outer_product_matrix(tmpc);
00110 }
00111 return ret;
00112 };
00113 void writeRobotModel () {
00114 ofs << "Total:" << std::endl;
00115 hrp::Vector3 tmp = m_robot->rootLink()->R.transpose() * (m_robot->calcCM() - m_robot->rootLink()->p);
00116 ofs << " Mass : " << m_robot->calcTotalMass() << std::endl;
00117 writeVector(tmp, "GlobalCom", " ");
00118 writeMatrix(calcTotalInertia(tmp), "GlobalInertia", " ");
00119 ofs << " Joints: ";
00120 for (size_t i = 0; i < m_robot->numLinks(); i++) {
00121 std::string name(m_robot->link(i)->name);
00122 if (name != std::string(m_robot->rootLink()->name))
00123 ofs << name << ", ";
00124 }
00125 ofs << std::endl;
00126 ofs << " ActualJoints: ";
00127 for (size_t i = 0; i < m_robot->numLinks(); i++) {
00128 if (m_robot->link(i)->jointId != -1)
00129 ofs << std::string(m_robot->link(i)->name) << ", ";
00130 }
00131 ofs << std::endl;
00132
00133 ofs << "Links:" << std::endl;
00134 OpenHRP::LinkInfoSequence_var links = bodyinfo->links();
00135 for (size_t i = 0; i < m_robot->numLinks(); i++) {
00136 std::string indent(" "), name, child_indent(indent+" ");
00137 for ( int k = 0; k < links->length(); k++ ) {
00138
00139 if ( std::string(links[k].name) == m_robot->link(i)->name )
00140 name = std::string(links[k].segments[0].name);
00141 }
00142 ofs << indent << name << ":" << std::endl;
00143 writeMassProperties(m_robot->link(i), child_indent);
00144 if (m_robot->link(i)->name != std::string(m_robot->rootLink()->name)) {
00145
00146 writeLinkProperties(hrp::Vector3(m_robot->link(i)->parent->R.transpose() * (m_robot->link(i)->p - m_robot->link(i)->parent->p)),
00147 hrp::Matrix33(m_robot->link(i)->parent->R.transpose() * m_robot->link(i)->R),
00148 child_indent);
00149 writeLinkGrobalProperties(m_robot->link(i)->p, m_robot->link(i)->R, child_indent);
00150 }
00151 }
00152 ofs << "Joints:" << std::endl;
00153 for (size_t i = 0; i < m_robot->numLinks(); i++) {
00154 if (m_robot->link(i)->name != std::string(m_robot->rootLink()->name)) {
00155 std::string indent(" "), child_indent(indent+" ");
00156 ofs << indent << m_robot->link(i)->name << ":" << std::endl;
00157 writeJointProperties(m_robot->link(i), child_indent);
00158 }
00159 }
00160
00161 ofs << "Sensors:" << std::endl;
00162 writeSensorProperties(hrp::Sensor::FORCE, " ");
00163 writeSensorProperties(hrp::Sensor::RATE_GYRO, " ");
00164 writeSensorProperties(hrp::Sensor::ACCELERATION, " ");
00165 }
00166 };
00167
00168 int main (int argc, char** argv)
00169 {
00170 std::string output, input;
00171
00172 for (int i = 1; i < argc; ++ i) {
00173 std::string arg(argv[i]);
00174 coil::normalize(arg);
00175 if ( arg == "--output" ) {
00176 if (++i < argc) output = argv[i];
00177 } else if ( arg == "--input" ) {
00178 if (++i < argc) input = argv[i];
00179 } else if ( arg == "-o" ) {
00180 ++i;
00181 } else if ( arg[0] == '-' || arg[0] == '_' ) {
00182 std::cerr << argv[0] << " : Unknwon arguments " << arg << std::endl;
00183 } else {
00184 }
00185 }
00186
00187 RTC::Manager* manager;
00188 manager = RTC::Manager::init(argc, argv);
00189
00190 std::string nameServer = manager->getConfig()["corba.nameservers"];
00191 int comPos = nameServer.find(",");
00192 if (comPos < 0){
00193 comPos = nameServer.length();
00194 }
00195 nameServer = nameServer.substr(0, comPos);
00196 RTC::CorbaNaming naming(manager->getORB(), nameServer.c_str());
00197 hrp::BodyPtr m_robot(new hrp::Body());
00198 if (!loadBodyFromModelLoader(m_robot, input.c_str(),
00199 CosNaming::NamingContext::_duplicate(naming.getRootContext())
00200 )){
00201 std::cerr << "failed to load model[" << input << "]" << std::endl;
00202 return RTC::RTC_ERROR;
00203 }
00204 OpenHRP::BodyInfo_var bodyinfo;
00205 bodyinfo = hrp::loadBodyInfo(input.c_str(), CosNaming::NamingContext::_duplicate(naming.getRootContext()));
00206
00207 for (size_t i = 0; i < m_robot->numJoints(); i++) {
00208 m_robot->joint(i)->q = 0;
00209 }
00210 m_robot->rootLink()->p = hrp::Vector3::Zero();
00211 m_robot->rootLink()->R = hrp::Matrix33::Identity();
00212 m_robot->calcForwardKinematics();
00213
00214 WriteOpenHRP3RobotModel rmct(m_robot, bodyinfo, output);
00215 rmct.writeRobotModel ();
00216
00217 return 0;
00218 }