joy2twist_pioneer.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <ros/console.h>
00003 #include <sensor_msgs/Joy.h>
00004 #include <geometry_msgs/Twist.h>
00005 
00006 class Joy2twist
00007 {
00008 private:
00009   ros::NodeHandle nh_;
00010   ros::Publisher  pubTwist_;
00011   ros::Subscriber subJoy_;
00012   ros::Subscriber subTwist_;
00013 
00014   geometry_msgs::Twist in_cmd_vel_; // input cmd_vel
00015 
00016   bool joystrick_drive_;
00017 
00018 public:
00019   void joyCallback(const sensor_msgs::Joy::ConstPtr& msg);
00020   void twistCallback(const geometry_msgs::Twist::ConstPtr& msg);
00021   Joy2twist(int argc, char**argv);
00022   virtual ~Joy2twist(){};
00023 };
00024 
00025 Joy2twist::Joy2twist(int argc, char**argv)
00026 {
00027   joystrick_drive_ = true;
00028   subJoy_   = nh_.subscribe("joy", 1000, &Joy2twist::joyCallback, this);
00029   subTwist_ = nh_.subscribe("cmd_vel", 1000, &Joy2twist::twistCallback, this);
00030   pubTwist_ = nh_.advertise<geometry_msgs::Twist>("RosAria/cmd_vel", 1000);
00031 }
00032 
00033 void Joy2twist::joyCallback(const sensor_msgs::Joy::ConstPtr& msg)
00034 {
00035   std::ostringstream strs;
00036   strs << std::endl << "axes: [";
00037   for(size_t i=0; i < msg->axes.size(); i++)
00038       strs << msg->axes[i] << ",";
00039   strs << "]" << std::endl;
00040   strs << "button: [";
00041   for(size_t i=0; i < msg->buttons.size(); i++)
00042       strs << msg->buttons[i] << ",";
00043   strs << "]" << std::endl;
00044   std::string str;
00045   str = strs.str();
00046   ROS_DEBUG("%s", str.c_str());
00047 
00048   // Use Logitech wireless gamepad F710
00049   // if buton 4 (front left) or 5 (front right) active
00050   //   out_cmd_vel = in_cmd_vel
00051   // else
00052   //   out_cmd_vel = vitesse joy
00053   //   axe 3 (horizontal) = angular vel
00054   //   axe 4 (vertical)   = linera  vel along x
00055   if (msg->buttons[4] || msg->buttons[5])
00056     joystrick_drive_ = false;
00057   else
00058     joystrick_drive_ = true;
00059 
00060   geometry_msgs::Twist out_cmd_vel;
00061   if (! joystrick_drive_)
00062     out_cmd_vel = in_cmd_vel_;
00063   else {
00064     out_cmd_vel.angular.z = msg->axes[3];
00065     out_cmd_vel.linear.x = msg->axes[4];
00066   }
00067   pubTwist_.publish(out_cmd_vel);
00068 }
00069 
00070 void Joy2twist::twistCallback(const geometry_msgs::Twist::ConstPtr& msg)
00071 {
00072   // memorize the command
00073   in_cmd_vel_.angular = msg->angular;
00074   in_cmd_vel_.linear  = msg->linear;
00075   // publish the command if joystick button pressed
00076   if (! joystrick_drive_) {
00077     geometry_msgs::Twist out_cmd_vel;
00078     out_cmd_vel = in_cmd_vel_;
00079     pubTwist_.publish(out_cmd_vel);
00080   }
00081 }
00082 
00083 int main(int argc, char **argv)
00084 {
00085   ros::init(argc, argv, "joy2twist");
00086 
00087   Joy2twist joy2twist(argc, argv);
00088 
00089   ros::spin();
00090 }
00091 


demo_pioneer
Author(s): Fabien Spindler
autogenerated on Thu Aug 27 2015 12:56:37