simple_joy_node.cpp
Go to the documentation of this file.
00001 
00026 #include "ros/ros.h"
00027 #include "jackal_msgs/Drive.h"
00028 #include "sensor_msgs/Joy.h"
00029 
00030 #include "boost/algorithm/clamp.hpp"
00031 
00032 namespace jackal_teleop
00033 {
00034 
00035 class SimpleJoy
00036 {
00037 public:
00038   explicit SimpleJoy(ros::NodeHandle* nh);
00039 
00040 private:
00041   void joyCallback(const sensor_msgs::Joy::ConstPtr& joy);
00042 
00043   ros::NodeHandle* nh_;
00044   ros::Subscriber joy_sub_;
00045   ros::Publisher drive_pub_;
00046 
00047   int deadman_button_;
00048   int axis_linear_;
00049   int axis_angular_;
00050   float scale_linear_;
00051   float scale_angular_;
00052 
00053   bool sent_deadman_msg_;
00054 };
00055 
00056 SimpleJoy::SimpleJoy(ros::NodeHandle* nh) : nh_(nh)
00057 {
00058   joy_sub_ = nh_->subscribe<sensor_msgs::Joy>("joy", 1, &SimpleJoy::joyCallback, this);
00059   drive_pub_ = nh_->advertise<jackal_msgs::Drive>("cmd_drive", 1, true);
00060 
00061   ros::param::param("~deadman_button", deadman_button_, 0);
00062   ros::param::param("~axis_linear", axis_linear_, 1);
00063   ros::param::param("~axis_angular", axis_angular_, 0);
00064   ros::param::param("~scale_linear", scale_linear_, 1.0f);
00065   ros::param::param("~scale_angular", scale_angular_, 1.0f);
00066 
00067   sent_deadman_msg_ = false;
00068 }
00069 
00070 void SimpleJoy::joyCallback(const sensor_msgs::Joy::ConstPtr& joy_msg)
00071 {
00072   jackal_msgs::Drive drive_msg;
00073 
00074   if (joy_msg->buttons[deadman_button_])
00075   {
00076     drive_msg.mode = jackal_msgs::Drive::MODE_PWM;
00077     float linear = joy_msg->axes[axis_linear_] * scale_linear_;
00078     float angular = joy_msg->axes[axis_angular_] * scale_angular_;
00079     drive_msg.drivers[jackal_msgs::Drive::LEFT] = boost::algorithm::clamp(linear - angular, -1.0, 1.0);
00080     drive_msg.drivers[jackal_msgs::Drive::RIGHT] = boost::algorithm::clamp(linear + angular, -1.0, 1.0);
00081     drive_pub_.publish(drive_msg);
00082     sent_deadman_msg_ = false;
00083   }
00084   else
00085   {
00086     // When deadman button is released, immediately send a single no-motion command
00087     // in order to stop the robot.
00088     if (!sent_deadman_msg_)
00089     {
00090       drive_msg.mode = jackal_msgs::Drive::MODE_NONE;
00091       drive_pub_.publish(drive_msg);
00092       sent_deadman_msg_ = true;
00093     }
00094   }
00095 }
00096 
00097 }  // namespace jackal_teleop
00098 
00099 int main(int argc, char *argv[])
00100 {
00101   ros::init(argc, argv, "jackal_teleop_joy_pwm");
00102 
00103   ros::NodeHandle nh;
00104   jackal_teleop::SimpleJoy simple_joy(&nh);
00105 
00106   ros::spin();
00107 }


jackal_base
Author(s): Mike Purvis
autogenerated on Thu Jul 4 2019 19:48:56