Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <object_manipulation_tools/controller_utils/SimulatedController.h>
00040
00041 SimulatedController::SimulatedController()
00042 :_CntrlStatePubTopic(CONTROLLER_STATE_TOPIC_NAME),
00043 _ControlFeedbackPubTopic(CONTROLLER_FEEDBACK_TOPIC_NAME),
00044 _JointStatePubTopic(JOINT_STATE_TOPIC_NAME),
00045 _JointTrajSubsTopic(JOINT_TRAJECTORY_TOPIC_NAME),
00046 _JointNames(),
00047 _LastJointState(),
00048 _ProcessingRequest(false)
00049 {
00050
00051 }
00052
00053 SimulatedController::~SimulatedController()
00054 {
00055
00056 }
00057
00058 void SimulatedController::init()
00059 {
00060 ros::NodeHandle nh;
00061 std::string nodeName = ros::this_node::getName();
00062
00063
00064 std::string matchFound;
00065 XmlRpc::XmlRpcValue list;
00066 _JointNames.clear();
00067 _LastJointState.name.clear();
00068 _LastJointState.position.clear();
00069 _LastJointState.velocity.clear();
00070 _LastJointState.effort.clear();
00071
00072 if(nh.getParam(JOINT_NAMES_PARAM_NAME,list) ||
00073 nh.getParam(ros::names::parentNamespace(ros::this_node::getName()) + "/" +JOINT_NAMES_PARAM_NAME,list))
00074 {
00075 if(list.getType() == XmlRpc::XmlRpcValue::TypeArray)
00076 {
00077 ROS_INFO_STREAM(nodeName + ": Found joints under parameter " + JOINT_NAMES_PARAM_NAME);
00078
00079 for(int i = 0; i < list.size(); i++)
00080 {
00081 XmlRpc::XmlRpcValue val = list[i];
00082 if(val.getType() == XmlRpc::XmlRpcValue::TypeString)
00083 {
00084 std::string name = static_cast<std::string>(val);
00085 _JointNames.push_back(name);
00086 _LastJointState.name.push_back(name);
00087 _LastJointState.position.push_back(0.0f);
00088 _LastJointState.velocity.push_back(0.0f);
00089 _LastJointState.effort.push_back(0.0f);
00090 }
00091 }
00092 }
00093 }
00094 else
00095 {
00096 ROS_ERROR_STREAM(nodeName + ": Could not retrieve parameter " + JOINT_NAMES_PARAM_NAME);
00097 }
00098
00099 sensor_msgs::JointState st = _LastJointState;
00100 updateLastStateFeedbackMessages(st);
00101
00102 _JointTrajSubscriber = nh.subscribe(_JointTrajSubsTopic,1,&SimulatedController::callbackJointTrajectory,this);
00103 _ControllerStatePublisher = nh.advertise<pr2_controllers_msgs::JointTrajectoryControllerState>(_CntrlStatePubTopic,
00104 1);
00105 _ControllerFeedbackPublisher = nh.advertise<control_msgs::FollowJointTrajectoryFeedback>(_ControlFeedbackPubTopic,1);
00106 _JointStatePublisher = nh.advertise<sensor_msgs::JointState>(_JointStatePubTopic,1);
00107 _StatusUpdateTimer = nh.createTimer(ros::Duration(1.0f),&SimulatedController::broadcastState,this);
00108 ros::spin();
00109 }
00110
00111 void SimulatedController::broadcastState(const ros::TimerEvent &evnt)
00112 {
00113
00114 if(_ProcessingRequest)
00115 {
00116 return;
00117 }
00118 else
00119 {
00120 _LastControllerJointState.header.stamp = _LastControllerTrajState.header.stamp = ros::Time::now();
00121 _LastJointState.header.stamp = ros::Time::now();
00122 _ControllerStatePublisher.publish(_LastControllerJointState);
00123 _ControllerFeedbackPublisher.publish(_LastControllerTrajState);
00124 _JointStatePublisher.publish(_LastJointState);
00125 }
00126 }
00127
00128 void SimulatedController::callbackJointTrajectory(const trajectory_msgs::JointTrajectory::ConstPtr &msg)
00129 {
00130 std::string nodeName = ros::this_node::getName();
00131
00132 if(msg->points.empty())
00133 {
00134 _ProcessingRequest = false;
00135 return;
00136 }
00137 else
00138 {
00139 _ProcessingRequest = true;
00140 }
00141
00142 ROS_INFO_STREAM(nodeName<<": Starting joint trajectory execution, "
00143 <<msg->points.size()<<" points in requested trajectory");
00144
00145
00146 pr2_controllers_msgs::JointTrajectoryControllerState state;
00147 control_msgs::FollowJointTrajectoryFeedback feedback;
00148
00149 trajectory_msgs::JointTrajectoryPoint error;
00150 state.joint_names = feedback.joint_names = _JointNames;
00151 error.positions = std::vector<double>(state.joint_names.size(),0.0f);
00152 state.error = feedback.error = error;
00153
00154 sensor_msgs::JointState jointState;
00155 jointState.name = state.joint_names;
00156
00157
00158
00159
00160 ros::Duration duration(0.0f);
00161 const ros::Duration printInterval(2.0f);
00162 ros::Time printStartTime = ros::Time::now();
00163 int counter = 0;
00164 BOOST_FOREACH(trajectory_msgs::JointTrajectoryPoint point,msg->points)
00165 {
00166
00167 state.desired = feedback.desired = point;
00168 state.actual = feedback.actual = point;
00169 jointState.position = state.desired.positions;
00170 jointState.velocity = state.desired.velocities;
00171
00172 (point.time_from_start - duration).sleep();
00173 duration = point.time_from_start;
00174
00175 state.header.stamp = feedback.header.stamp = ros::Time::now();
00176 jointState.header.stamp = ros::Time::now();
00177
00178 _ControllerStatePublisher.publish(state);
00179 _ControllerFeedbackPublisher.publish(feedback);
00180 _JointStatePublisher.publish(jointState);
00181
00182 if(ros::Time::now() - printStartTime > printInterval)
00183 {
00184 ROS_INFO_STREAM(nodeName<<": Executed "<<
00185 counter<<" joint trajectory points after "<<point.time_from_start.sec<<" seconds");
00186 printStartTime = ros::Time::now();
00187 }
00188 counter++;
00189 }
00190
00191 ROS_INFO_STREAM(nodeName<<": Finished joint trajectory execution, ");
00192
00193 updateLastStateFeedbackMessages(jointState);
00194
00195 _ProcessingRequest = false;
00196 }
00197
00198 void SimulatedController::updateLastStateFeedbackMessages(const sensor_msgs::JointState &st)
00199 {
00200 _LastJointState = st;
00201
00202 trajectory_msgs::JointTrajectoryPoint error;
00203 error.positions = std::vector<double>(st.name.size(),0.0f);
00204 error.velocities = std::vector<double>(st.name.size(),0.0f);
00205
00206 _LastControllerTrajState.joint_names = st.name;
00207 _LastControllerTrajState.actual.positions = _LastControllerTrajState.desired.positions = st.position;
00208 _LastControllerTrajState.actual.velocities = _LastControllerTrajState.desired.velocities = st.velocity;
00209 _LastControllerTrajState.actual.time_from_start = _LastControllerTrajState.desired.time_from_start = ros::Duration(0.0f);
00210 _LastControllerTrajState.error = error;
00211
00212 _LastControllerJointState.joint_names = st.name;
00213 _LastControllerJointState.actual.positions = _LastControllerJointState.desired.positions = st.position;
00214 _LastControllerJointState.actual.velocities = _LastControllerJointState.desired.velocities = st.velocity;
00215 _LastControllerJointState.actual.time_from_start = _LastControllerJointState.desired.time_from_start = ros::Duration(0.0f);
00216 _LastControllerJointState.error = error;
00217 }