joy_increment_node.cpp
Go to the documentation of this file.
00001 //
00002 // Created by tom on 02/05/16.
00003 //
00004 
00005 #include <ros/ros.h>
00006 #include <sensor_msgs/Joy.h>
00007 #include <sensor_msgs/JointState.h>
00008 #include <actionlib/client/simple_action_client.h>
00009 #include <control_msgs/FollowJointTrajectoryAction.h>
00010 
00011 typedef  actionlib::SimpleActionClient<control_msgs::FollowJointTrajectoryAction> ActionClient;
00012 //#define DEBUG_TORSO_INC
00013 class JoyIncrement {
00014 private:
00015     ros::NodeHandle _nodeHandle;
00016     ActionClient _actionClient;     //Action client which send the goal to the torso
00017     ros::Subscriber _joySub;        // Joystick Listener.
00018     ros::Subscriber _jointStates;   // Joint status Listener, for the position if the torso.
00019 
00020     bool _isStop;                   //True if the dead man button is release or if none of the torso button is press.
00021     float _incTorso;                 //Increment
00022     double _torsoPos;                //The torso current position.
00023     int _upButtonIndex;
00024     int _downButtonIndex;
00025     int _deadManButtonIndex;
00026 
00027     /*
00028      * This method handle the joystick reading.
00029      */
00030     void joyCallback(const sensor_msgs::Joy::ConstPtr &msg) {
00031         bool isUpActive = (bool)msg->buttons[_upButtonIndex],
00032              isDownActive = (bool)msg->buttons[_downButtonIndex],
00033              isDeadManButtonActive = (bool)msg->buttons[_deadManButtonIndex];
00034         if(isDeadManButtonActive) {
00035             if (isUpActive) {
00036                 sendGoal(_torsoPos + _incTorso);
00037                 _isStop = false;
00038             }
00039             else if (isDownActive) {
00040                 sendGoal(_torsoPos - _incTorso);
00041                 _isStop = false;
00042             }
00043             else if(!_isStop){
00044                 sendGoal(_torsoPos);
00045                 _isStop = true;
00046             }
00047         }
00048         else {
00049             if(!_isStop) {
00050                 sendGoal(_torsoPos);
00051                 _isStop = true;
00052             }
00053         }
00054 
00055     }
00056     /*
00057      * This method get the torso current position.
00058      */
00059     void jointStateCallback(const sensor_msgs::JointState::ConstPtr &msg){
00060         size_t size = msg->name.size();
00061         bool found = false;
00062         for(int i = 0; i < size && !found; ++i) {
00063             if(msg->name[i] == "torso_joint") {
00064                 _torsoPos = msg->position[i];
00065                 found = true;
00066             }
00067         }
00068         if(!found) {
00069             ROS_ERROR("[%s]: torso_joint not found", ros::this_node::getName().c_str());
00070         }
00071     }
00072 
00073     /*
00074      * This method will get call after the action server s finish.
00075      */
00076     void doneCallback(const actionlib::SimpleClientGoalState &state, const control_msgs::FollowJointTrajectoryResult::ConstPtr &result) {
00077 #ifdef DEBUG_TORSO_INC
00078         ROS_INFO("Finished in state [%s]", state.toString().c_str());
00079 #endif
00080     }
00081 
00082     /*
00083      * This method construct the goal and send it to the action server.
00084      */
00085     void sendGoal(double elevPos) {
00086 //        ROS_INFO("[%s]: %f", ros::this_node::getName().c_str(), elevPos);
00087         control_msgs::FollowJointTrajectoryGoal goal;
00088         goal.goal_time_tolerance = ros::Duration(5.0);
00089         goal.trajectory.header.frame_id = "base_link";
00090         goal.trajectory.joint_names.push_back("torso_joint");
00091         trajectory_msgs::JointTrajectoryPoint point;
00092         point.time_from_start = ros::Duration(5.0);
00093         point.positions.push_back(elevPos);
00094         goal.trajectory.points.push_back(point);
00095         _actionClient.sendGoal(goal, boost::bind(&JoyIncrement::doneCallback, this, _1, _2),
00096                                ActionClient::SimpleActiveCallback(), ActionClient::SimpleFeedbackCallback());
00097     }
00098 
00099 
00100 public:
00101     JoyIncrement() : _actionClient("torso_trajectory_controller/follow_joint_trajectory", true) {
00102         _isStop = true;
00103         if(_actionClient.waitForServer(ros::Duration(20.0))) {
00104             if(!_nodeHandle.getParam("torso_increment", _incTorso)
00105                ||!_nodeHandle.getParam("torso_up_button", _upButtonIndex)
00106                ||!_nodeHandle.getParam("torso_down_button", _downButtonIndex)
00107                ||!_nodeHandle.getParam("joy_deadman_button", _deadManButtonIndex)) {
00108                 ROS_ERROR("[%s]: Missing parameters, the requird parameters are: torso_increment,  torso_up_button, torso_down_button, joy_deadman_button", ros::this_node::getName().c_str());
00109                 ros::shutdown();
00110             }
00111             else {
00112                 _joySub = _nodeHandle.subscribe<sensor_msgs::Joy>("joy", 10, &JoyIncrement::joyCallback, this);
00113                 _jointStates = _nodeHandle.subscribe<sensor_msgs::JointState>("joint_states", 10, &JoyIncrement::jointStateCallback, this);
00114             }
00115         }
00116         else {
00117             ROS_ERROR("[%s]: Server not responding", ros::this_node::getName().c_str());
00118             ros::shutdown();
00119         }
00120     }
00121     void run() {
00122         ros::spin();
00123     }
00124 };
00125 
00126 int main(int argc, char **argv) {
00127     ros::init(argc, argv, "joy_increment_node");
00128     JoyIncrement joyIncrement;
00129     joyIncrement.run();
00130     return 0;
00131 }


robotican_armadillo
Author(s):
autogenerated on Fri Oct 27 2017 03:02:54