SimulatedController.cpp
Go to the documentation of this file.
00001 /*
00002 * SimulatedController.cpp
00003 *
00004 *  Created on: Oct 10, 2012
00005 *      Author: jnicho
00006 */
00007 
00008 /*
00009 * Software License Agreement (BSD License)
00010 *
00011 * Copyright (c) 2011, Southwest Research Institute
00012 * All rights reserved.
00013 *
00014 * Redistribution and use in source and binary forms, with or without
00015 * modification, are permitted provided that the following conditions are met:
00016 *
00017 *       * Redistributions of source code must retain the above copyright
00018 *       notice, this list of conditions and the following disclaimer.
00019 *       * Redistributions in binary form must reproduce the above copyright
00020 *       notice, this list of conditions and the following disclaimer in the
00021 *       documentation and/or other materials provided with the distribution.
00022 *       * Neither the name of the Southwest Research Institute, nor the names
00023 *       of its contributors may be used to endorse or promote products derived
00024 *       from this software without specific prior written permission.
00025 *
00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036 * POSSIBILITY OF SUCH DAMAGE.
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         // finding joint names
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                         //BOOST_FOREACH(XmlRpc::XmlRpcValue val,list.)
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         //pr2_controllers_msgs::JointTrajectoryControllerState state;
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         // the messages below contain the same information, but the are expected by different joint trajectory action servers
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         //jointState.effort = std::vector<double>(state.joint_names.size(),0.0f);
00158 
00159         // publishing controller and joint states
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                 //trajectory_msgs::JointTrajectoryPoint point = msg->points[i];
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         //_LastJointState = jointState;
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 }


object_manipulation_tools
Author(s): Jnicho
autogenerated on Mon Oct 6 2014 00:56:17