joy_teleop_node.cpp
Go to the documentation of this file.
00001 //
00002 // Created by tom on 03/05/16.
00003 //
00004 
00005 #include <ros/ros.h>
00006 #include <sensor_msgs/Joy.h>
00007 #include <geometry_msgs/Twist.h>
00008 
00009 class JoyTeleop {
00010 private:
00011     ros::NodeHandle _nodeHandle;
00012     ros::Subscriber _joyListener;
00013     ros::Publisher  _cmd;
00014 
00015     float _linearScale;
00016     float _angularScale;
00017     int _deadManIndex;
00018     int _linearAxisIndex;
00019     int _angularAxisIndex;
00020 
00021     void joyCallback(const sensor_msgs::Joy::ConstPtr &msg) {
00022         bool isDeadManActive = msg->buttons[_deadManIndex] == 1;
00023         if(isDeadManActive) {
00024             geometry_msgs::Twist twist;
00025             twist.linear.x = msg->axes[_linearAxisIndex] * _linearScale;
00026             twist.angular.z = msg->axes[_angularAxisIndex] * _angularScale;
00027             _cmd.publish(twist);
00028         }
00029     }
00030 
00031 public:
00032     JoyTeleop() {
00033         if(!_nodeHandle.getParam("drive_joy_teleop_linear_axis", _linearAxisIndex)
00034            || !_nodeHandle.getParam("drive_joy_teleop_angular_axis", _angularAxisIndex)
00035            || !_nodeHandle.getParam("drive_joy_teleop_deadman_button", _deadManIndex)
00036            || !_nodeHandle.getParam("drive_joy_teleop_linear_max_vel", _linearScale)
00037            || !_nodeHandle.getParam("drive_joy_teleop_angular_max_vel", _angularScale)) {
00038             ROS_ERROR("[%s]: Missing parameter, the requird parameters are: "
00039                               "drive_joy_teleop_linear_axis, drive_joy_teleop_angular_axis"
00040                               ", drive_joy_teleop_deadman_button , drive_joy_teleop_linear_max_vel"
00041                               ", drive_joy_teleop_angular_max_vel", ros::this_node::getName().c_str());
00042             ros::shutdown();
00043         }
00044         else {
00045             _joyListener = _nodeHandle.subscribe<sensor_msgs::Joy>("joy", 10, &JoyTeleop::joyCallback, this);
00046             _cmd = _nodeHandle.advertise<geometry_msgs::Twist>("joy_vel", 10);
00047         }
00048     }
00049 
00050     void run() {
00051         ros::spin();
00052     }
00053 
00054     
00055 
00056 };
00057 
00058 int main(int argc, char **argv) {
00059     ros::init(argc, argv, "joy_teleop_node");
00060     JoyTeleop joyTeleop;
00061     joyTeleop.run();
00062     return 0;
00063 }


robotican_common
Author(s):
autogenerated on Fri Oct 27 2017 03:02:37