pan_tilt_joy_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 <std_msgs/Float64MultiArray.h>
00008 #include <urdf/model.h>
00009 #include <trajectory_msgs/JointTrajectory.h>
00010 
00011 //#define DEBUG_JOY
00012 
00013 typedef boost::shared_ptr<const urdf::Joint> UrdfJointConstPtr;
00014 
00015 class PanTiltJoy {
00016 private:
00017     int _panAxisIndex;
00018     int _tiltAxisIndex;
00019     float _deadManButtonIndex;
00020     float _zeroButtonIndex;
00021 
00022     float _incTilt;                 //The increment value for tilt.
00023     float _incPan;                  //The increment value for pan.
00024 
00025     float _tiltPos;                 //The tilt current position.
00026     float _panPos;                  //The pan current position.
00027     bool _deadManButtonActive;      //True if the dead man button is press.
00028     bool _zeroButtonActive;         //True if the zero button is press.
00029 
00030     UrdfJointConstPtr tiltLim;
00031     UrdfJointConstPtr panLim;
00032 
00033     ros::NodeHandle _nodeHandle;
00034     ros::Publisher _panTiltCommand; //This object is for sending command to the pan and tilt.
00035     ros::Subscriber _joySub;        //Listener to the joystick state.
00036 
00037     /*
00038      * This method is for the joystick state.
00039      */
00040     void joyCallback(const sensor_msgs::Joy::ConstPtr &msg) {
00041         _deadManButtonActive = msg->buttons[_deadManButtonIndex] == 1;
00042         _zeroButtonActive = msg->buttons[_zeroButtonIndex] == 1;
00043         if (_deadManButtonActive) {
00044             if (msg->axes[_tiltAxisIndex] > 0) {
00045                 if (tiltLim->limits->lower <= (_tiltPos - _incTilt)) _tiltPos -= _incTilt;
00046             }
00047             else if (msg->axes[_tiltAxisIndex] < 0) {
00048                 if (tiltLim->limits->upper >= (_tiltPos + _incTilt)) _tiltPos += _incTilt;
00049             }
00050             if (msg->axes[_panAxisIndex] > 0) {
00051                 if (panLim->limits->upper >= (_panPos + _incPan)) _panPos += _incPan;
00052 
00053             }
00054             else if (msg->axes[_panAxisIndex] < 0) {
00055                 if (panLim->limits->lower <= (_panPos - _incPan)) _panPos -= _incPan;
00056             }
00057             if (_zeroButtonActive) {
00058                 _panPos = _tiltPos = 0.0;
00059             }
00060 
00061         }
00062     }
00063 
00064 public:
00065     PanTiltJoy() {
00066         std::string panTiltTopic, joySubTopic;
00067         _tiltPos = _panPos = 0;
00068         _deadManButtonActive = _zeroButtonActive = false;
00069         boost::shared_ptr<urdf::Model> urdf(new urdf::Model);
00070         if (!urdf->initParam("robot_description")) {
00071             ROS_ERROR("[%s]: Need to have urdf model.", ros::this_node::getName().c_str());
00072             ros::shutdown();
00073         }
00074         tiltLim = urdf->getJoint("head_tilt_joint");
00075         panLim = urdf->getJoint("head_pan_joint");
00076 
00077         if (!tiltLim) {
00078             ROS_ERROR("[%s]: Could not find head_tilt_joint.", ros::this_node::getName().c_str());
00079             ros::shutdown();
00080         }
00081 
00082         if (!panLim) {
00083             ROS_ERROR("[%s]: Could not find head_pan_joint.", ros::this_node::getName().c_str());
00084             ros::shutdown();
00085         }
00086 
00087         // Validation check.
00088         if (!_nodeHandle.getParam("pan_tilt_topic", panTiltTopic)
00089             || !_nodeHandle.getParam("joy_sub_topic", joySubTopic)
00090             || !_nodeHandle.getParam("joy_pan_axis", _panAxisIndex)
00091             || !_nodeHandle.getParam("joy_tilt_axis", _tiltAxisIndex)
00092             || !_nodeHandle.getParam("increament_tilt", _incTilt)
00093             || !_nodeHandle.getParam("increament_pan", _incPan)
00094             || !_nodeHandle.getParam("joy_deadman_button", _deadManButtonIndex)
00095             || !_nodeHandle.getParam("zero_button", _zeroButtonIndex)) {
00096             ROS_ERROR(
00097                     "[%s]: Requird: pan_tilt_topic,  joy_sub_topic, joy_pan_axis, joy_tilt_axis, increament_tilt, increament_pan , zero_button, joy_deadman_button parameters",
00098                     ros::this_node::getName().c_str());
00099             ros::shutdown();
00100         }
00101         else {
00102             _panTiltCommand = _nodeHandle.advertise<trajectory_msgs::JointTrajectory>(panTiltTopic, 10);
00103             _joySub = _nodeHandle.subscribe<sensor_msgs::Joy>(joySubTopic, 10, &PanTiltJoy::joyCallback, this);
00104         }
00105     }
00106 
00107     // Method which publish position to the pan_tilt.
00108     void run() {
00109         ros::Rate loopRate(50);
00110         trajectory_msgs::JointTrajectory positions;
00111         positions.joint_names.push_back("head_pan_joint");
00112         positions.joint_names.push_back("head_tilt_joint");
00113         positions.points.resize(1);
00114         positions.points[0].positions.resize(2);
00115         positions.points[0].velocities.push_back(0);
00116         positions.points[0].velocities.push_back(0);
00117         while (ros::ok()) {
00118 
00119             if (_deadManButtonActive) {
00120                 positions.points[0].time_from_start = ros::Duration(0.1);
00121                 positions.points[0].positions[0] = _panPos;
00122                 positions.points[0].positions[1] = _tiltPos;
00123 #ifdef DEBUG_JOY
00124                 ROS_INFO_STREAM("[" << ros::this_node::getName() << "]: " << positions);
00125 #endif
00126                 _panTiltCommand.publish(positions);
00127             }
00128             ros::spinOnce();
00129             loopRate.sleep();
00130         }
00131     }
00132 
00133 };
00134 
00135 int main(int argc, char **argv) {
00136     ros::init(argc, argv, "pan_tilt_joy_node");
00137     PanTiltJoy panTiltJoy;
00138     panTiltJoy.run();
00139     return 0;
00140 }


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