joy2twist_pioneer_pan.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 
00007 class Joy2twist
00008 {
00009 private:
00010   ros::NodeHandle nh_;
00011   ros::Publisher  pubTwistPioneer_;
00012   ros::Publisher  pubTwistBiclops_;
00013   ros::Subscriber subJoy_;
00014   ros::Subscriber subTwistPioneer_;
00015   ros::Subscriber subTwistBiclops_;
00016 
00017   geometry_msgs::Twist in_pioneer_cmd_vel_; // input cmd_vel
00018   geometry_msgs::Twist in_biclops_cmd_vel_; // input cmd_vel
00019 
00020   bool joystrick_drive_;
00021 
00022 public:
00023   void joyCallback(const sensor_msgs::Joy::ConstPtr& msg);
00024   void twistPioneerCallback(const geometry_msgs::Twist::ConstPtr& msg);
00025   void twistBiclopsCallback(const geometry_msgs::Twist::ConstPtr& msg);
00026   Joy2twist(int argc, char**argv);
00027   virtual ~Joy2twist(){};
00028 };
00029 
00030 Joy2twist::Joy2twist(int argc, char**argv)
00031 {
00032   joystrick_drive_ = true;
00033   subJoy_   = nh_.subscribe("joy", 1000, &Joy2twist::joyCallback, this);
00034   subTwistPioneer_ = nh_.subscribe("vs/pioneer/cmd_vel", 1000, &Joy2twist::twistPioneerCallback, this);
00035   pubTwistPioneer_ = nh_.advertise<geometry_msgs::Twist>("RosAria/cmd_vel", 1000);
00036   subTwistBiclops_ = nh_.subscribe("vs/biclops/cmd_vel", 1000, &Joy2twist::twistBiclopsCallback, this);
00037   pubTwistBiclops_ = nh_.advertise<geometry_msgs::Twist>("biclops/cmd_vel", 1000);
00038 }
00039 
00040 void Joy2twist::joyCallback(const sensor_msgs::Joy::ConstPtr& msg)
00041 {
00042   std::ostringstream strs;
00043   strs << std::endl << "axes: [";
00044   for(size_t i=0; i < msg->axes.size(); i++)
00045       strs << msg->axes[i] << ",";
00046   strs << "]" << std::endl;
00047   strs << "button: [";
00048   for(size_t i=0; i < msg->buttons.size(); i++)
00049       strs << msg->buttons[i] << ",";
00050   strs << "]" << std::endl;
00051   std::string str;
00052   str = strs.str();
00053   ROS_DEBUG("%s", str.c_str());
00054 
00055   // si bouton 4 (devant gauche) ou 5 (devant droite) activé
00056   //   out_cmd_vel = in_cmd_vel
00057   // sinon
00058   //   out_cmd_vel = vitesse joy
00059   //   axe 3 (horizontal) = vitesse angulaire
00060   //   axe 4 (vertical)   = vitesse lineaire sur x
00061   if (msg->buttons[4] || msg->buttons[5])
00062     joystrick_drive_ = false;
00063   else
00064     joystrick_drive_ = true;
00065 
00066   geometry_msgs::Twist out_pioneer_cmd_vel;
00067   geometry_msgs::Twist out_biclops_cmd_vel;
00068   if (! joystrick_drive_) {
00069     out_pioneer_cmd_vel = in_pioneer_cmd_vel_;
00070     out_biclops_cmd_vel = in_biclops_cmd_vel_;
00071   }
00072   else {
00073     out_pioneer_cmd_vel.angular.z = msg->axes[3];
00074     out_pioneer_cmd_vel.linear.x = msg->axes[4];
00075     out_biclops_cmd_vel.linear.x = 0;
00076     out_biclops_cmd_vel.linear.y = 0;
00077   }
00078   pubTwistPioneer_.publish(out_pioneer_cmd_vel);
00079   pubTwistBiclops_.publish(out_biclops_cmd_vel);
00080 }
00081 
00082 void Joy2twist::twistPioneerCallback(const geometry_msgs::Twist::ConstPtr& msg)
00083 {
00084   // memorize the command
00085   in_pioneer_cmd_vel_.angular = msg->angular;
00086   in_pioneer_cmd_vel_.linear  = msg->linear;
00087   // publish the command if joystick button pressed
00088   if (! joystrick_drive_) {
00089     geometry_msgs::Twist out_pioneer_cmd_vel;
00090     out_pioneer_cmd_vel = in_pioneer_cmd_vel_;
00091     pubTwistPioneer_.publish(out_pioneer_cmd_vel);
00092   }
00093 }
00094 void Joy2twist::twistBiclopsCallback(const geometry_msgs::Twist::ConstPtr& msg)
00095 {
00096   // memorize the command
00097   in_biclops_cmd_vel_.angular = msg->angular;
00098   in_biclops_cmd_vel_.linear  = msg->linear;
00099   // publish the command if joystick button pressed
00100   if (! joystrick_drive_) {
00101     geometry_msgs::Twist out_biclops_cmd_vel;
00102     out_biclops_cmd_vel = in_biclops_cmd_vel_;
00103     pubTwistBiclops_.publish(out_biclops_cmd_vel);
00104   }
00105 }
00106 
00107 int main(int argc, char **argv)
00108 {
00109   ros::init(argc, argv, "TeleopPioneerPan");
00110 
00111   Joy2twist joy2twist(argc, argv);
00112 
00113   ros::spin();
00114 }
00115 


demo_pioneer
Author(s): Fabien Spindler
autogenerated on Fri Aug 19 2016 04:09:46