00001 #include <robodyn_ros/R2RosTreeId.hpp>
00002
00003 R2RosTreeId::R2RosTreeId(std::string urdf, ros::NodeHandle& nh):
00004 gravityFrame("r2/robot_world"),
00005 argos(false),
00006 useImu(false),
00007 gravityBase("r2/robot_world"),
00008 idMode("full"),
00009 yankLimBFF(0.5),
00010 loopRate(0.0333),
00011 baseFrame("r2/robot_world"),
00012 bfCmd("r2/robot_world"),
00013 bfFactor(1.0),
00014 newBase(false)
00015 {
00016 id.loadFromFile(urdf);
00017 jd.InitializeMaps(id.getTree());
00018
00019 torqueOut = nh.advertise<sensor_msgs::JointState>("/joint_torque_commands", 1);
00020 inertiaOut = nh.advertise<sensor_msgs::JointState>("/treeid_inertia", 1);
00021 segForceOut = nh.advertise<r2_msgs::WrenchState>("/treeid_segment_forces", 1);
00022 completionOut = nh.advertise<r2_msgs::StringArray>("/completion", 1);
00023
00024 baseFrameIn = nh.subscribe("/base_frames", 1, &R2RosTreeId::baseFrameCallback, this);
00025 forceIn = nh.subscribe("/treeid_external_forces", 1, &R2RosTreeId::forceCallback, this);
00026 jointStateIn = nh.subscribe("/joint_states", 1, &R2RosTreeId::jointStateCallback, this);
00027 trajIn = nh.subscribe("/joint_desired", 1, &R2RosTreeId::trajCallback, this);
00028 imuIn = nh.subscribe("imu", 1, &R2RosTreeId::imuCallback, this);
00029 }
00030
00031 bool R2RosTreeId::setGravity(double x, double y, double z, std::string frame, std::string mode)
00032 {
00033 if (!id.isBaseFrameInTree(frame))
00034 {
00035 ROS_ERROR("Unable to setGravity in R2RosTreeId, no such frame");
00036 return false;
00037 }
00038
00039 idMode = mode;
00040 gravityFrame = frame;
00041 gravity.resize(3);
00042 gravity[0] = x;
00043 gravity[1] = y;
00044 gravity[2] = z;
00045 baseMsg.data.resize(1);
00046 baseMsg.data[0] = frame;
00047 rmt.setBaseFrame(baseMsg, frame);
00048 completionMsg.header.stamp = ros::Time::now();
00049 completionMsg.data.clear();
00050 completionMsg.data.push_back("base_frame");
00051 completionOut.publish(completionMsg);
00052 return true;
00053 }
00054
00055 void R2RosTreeId::baseFrameCallback(const r2_msgs::StringArray& msg)
00056 {
00057 baseMsg = msg;
00058
00059 if (rmt.setBaseFrame(baseMsg, bfCmd) > 0)
00060 {
00061 newBase = true;
00062 ROS_INFO("TreeId Received base frame");
00063 }
00064 else
00065 {
00066 ROS_ERROR("No base frames specified");
00067 }
00068 }
00069
00070 void R2RosTreeId::forceCallback(const r2_msgs::WrenchState& msg)
00071 {
00072 forceMsg = msg;
00073 }
00074
00075 void R2RosTreeId::trajCallback(const sensor_msgs::JointState& msg)
00076 {
00077 trajStateMsg = msg;
00078 }
00079
00080 void R2RosTreeId::imuCallback(const sensor_msgs::Imu& msg)
00081 {
00082 imuInMsg = msg;
00083 }
00084
00085 void R2RosTreeId::jointStateCallback(const sensor_msgs::JointState& msg)
00086 {
00087 if (idMode == r2_msgs::JointCommand::FULL || idMode == r2_msgs::JointCommand::INERTIA)
00088 {
00089 rmt.setJointData(msg, trajStateMsg, forceMsg, jd);
00090 }
00091 else
00092 {
00093 rmt.setJointData(msg, emptyTrajMsg, emptyForceMsg, jd);
00094 }
00095
00096 if (idMode == r2_msgs::JointCommand::FULL || idMode == r2_msgs::JointCommand::GRAVITY)
00097 {
00098
00099 if (gravity[0] != 0 || gravity[1] != 0 || gravity[2] != 0)
00100 {
00101 if (baseFrame != bfCmd)
00102 {
00103 bfFactor -= yankLimBFF * loopRate;
00104 }
00105
00106 if (baseFrame == bfCmd && bfFactor < 1.0)
00107 {
00108 bfFactor += yankLimBFF * loopRate;
00109 }
00110
00111 if (bfFactor <= 0.0 )
00112 {
00113 baseFrame = bfCmd;
00114 bfFactor = 0.0;
00115 }
00116
00117 if (bfFactor > 1.0)
00118 {
00119 bfFactor = 1.0;
00120 }
00121 }
00122
00123
00124 gravity_kdl = bfFactor * KDL::Vector(-gravity[0], -gravity[1], -gravity[2]);
00125 id.getAccelInBaseFrame(gravity_kdl, gravityFrame, jd, baseFrame, a_in);
00126
00127 id.treeRecursiveNewtonEuler(jd, baseFrame, "null", v_in, a_in, f_out, I_out);
00128 }
00129 else
00130 {
00131
00132 id.treeRecursiveNewtonEuler(jd, baseFrame, "null", zeroTwist, zeroTwist, f_out, I_out);
00133 }
00134
00135
00136 if (idMode != r2_msgs::JointCommand::NONE)
00137 {
00138 rmt.getJointCommand(jd, jointTorqueMsg);
00139 }
00140 else
00141 {
00142 rmt.setEmptyTorqueMsg(jd, jointTorqueMsg);
00143 }
00144
00145
00146 rmt.getJointInertias(jd, jointInertiaMsg);
00147 rmt.getSegmentForces(jd, segForceMsg);
00148
00149 torqueOut.publish(jointTorqueMsg);
00150 inertiaOut.publish(jointInertiaMsg);
00151 segForceOut.publish(segForceMsg);
00152
00153 if (bfFactor >= 1.0 && bfCmd == baseFrame && newBase)
00154 {
00155 completionMsg.header.stamp = ros::Time::now();
00156 completionOut.publish(completionMsg);
00157 newBase = false;
00158 }
00159 }
00160