joy2twist_pioneer_pan.cpp
Go to the documentation of this file.
1 #include "ros/ros.h"
2 #include <ros/console.h>
3 #include "sensor_msgs/Joy.h"
4 #include "geometry_msgs/Twist.h"
5 
6 
7 class Joy2twist
8 {
9 private:
16 
17  geometry_msgs::Twist in_pioneer_cmd_vel_; // input cmd_vel
18  geometry_msgs::Twist in_biclops_cmd_vel_; // input cmd_vel
19 
20  bool joystrick_drive_;
21 
22 public:
23  void joyCallback(const sensor_msgs::Joy::ConstPtr& msg);
24  void twistPioneerCallback(const geometry_msgs::Twist::ConstPtr& msg);
25  void twistBiclopsCallback(const geometry_msgs::Twist::ConstPtr& msg);
26  Joy2twist(int argc, char**argv);
27  virtual ~Joy2twist(){};
28 };
29 
30 Joy2twist::Joy2twist(int argc, char**argv)
31 {
32  joystrick_drive_ = true;
33  subJoy_ = nh_.subscribe("joy", 1000, &Joy2twist::joyCallback, this);
34  subTwistPioneer_ = nh_.subscribe("vs/pioneer/cmd_vel", 1000, &Joy2twist::twistPioneerCallback, this);
35  pubTwistPioneer_ = nh_.advertise<geometry_msgs::Twist>("RosAria/cmd_vel", 1000);
36  subTwistBiclops_ = nh_.subscribe("vs/biclops/cmd_vel", 1000, &Joy2twist::twistBiclopsCallback, this);
37  pubTwistBiclops_ = nh_.advertise<geometry_msgs::Twist>("biclops/cmd_vel", 1000);
38 }
39 
40 void Joy2twist::joyCallback(const sensor_msgs::Joy::ConstPtr& msg)
41 {
42  std::ostringstream strs;
43  strs << std::endl << "axes: [";
44  for(size_t i=0; i < msg->axes.size(); i++)
45  strs << msg->axes[i] << ",";
46  strs << "]" << std::endl;
47  strs << "button: [";
48  for(size_t i=0; i < msg->buttons.size(); i++)
49  strs << msg->buttons[i] << ",";
50  strs << "]" << std::endl;
51  std::string str;
52  str = strs.str();
53  ROS_DEBUG("%s", str.c_str());
54 
55  // si bouton 4 (devant gauche) ou 5 (devant droite) activĂ©
56  // out_cmd_vel = in_cmd_vel
57  // sinon
58  // out_cmd_vel = vitesse joy
59  // axe 3 (horizontal) = vitesse angulaire
60  // axe 4 (vertical) = vitesse lineaire sur x
61  if (msg->buttons[4] || msg->buttons[5])
62  joystrick_drive_ = false;
63  else
64  joystrick_drive_ = true;
65 
66  geometry_msgs::Twist out_pioneer_cmd_vel;
67  geometry_msgs::Twist out_biclops_cmd_vel;
68  if (! joystrick_drive_) {
69  out_pioneer_cmd_vel = in_pioneer_cmd_vel_;
70  out_biclops_cmd_vel = in_biclops_cmd_vel_;
71  }
72  else {
73  out_pioneer_cmd_vel.angular.z = msg->axes[3];
74  out_pioneer_cmd_vel.linear.x = msg->axes[4];
75  out_biclops_cmd_vel.linear.x = 0;
76  out_biclops_cmd_vel.linear.y = 0;
77  }
78  pubTwistPioneer_.publish(out_pioneer_cmd_vel);
79  pubTwistBiclops_.publish(out_biclops_cmd_vel);
80 }
81 
82 void Joy2twist::twistPioneerCallback(const geometry_msgs::Twist::ConstPtr& msg)
83 {
84  // memorize the command
85  in_pioneer_cmd_vel_.angular = msg->angular;
86  in_pioneer_cmd_vel_.linear = msg->linear;
87  // publish the command if joystick button pressed
88  if (! joystrick_drive_) {
89  geometry_msgs::Twist out_pioneer_cmd_vel;
90  out_pioneer_cmd_vel = in_pioneer_cmd_vel_;
91  pubTwistPioneer_.publish(out_pioneer_cmd_vel);
92  }
93 }
94 void Joy2twist::twistBiclopsCallback(const geometry_msgs::Twist::ConstPtr& msg)
95 {
96  // memorize the command
97  in_biclops_cmd_vel_.angular = msg->angular;
98  in_biclops_cmd_vel_.linear = msg->linear;
99  // publish the command if joystick button pressed
100  if (! joystrick_drive_) {
101  geometry_msgs::Twist out_biclops_cmd_vel;
102  out_biclops_cmd_vel = in_biclops_cmd_vel_;
103  pubTwistBiclops_.publish(out_biclops_cmd_vel);
104  }
105 }
106 
107 int main(int argc, char **argv)
108 {
109  ros::init(argc, argv, "TeleopPioneerPan");
110 
111  Joy2twist joy2twist(argc, argv);
112 
113  ros::spin();
114 }
115 
msg
void publish(const boost::shared_ptr< M > &message) const
void twistPioneerCallback(const geometry_msgs::Twist::ConstPtr &msg)
ros::Publisher pubTwistPioneer_
ros::Subscriber subTwistBiclops_
geometry_msgs::Twist in_pioneer_cmd_vel_
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void joyCallback(const sensor_msgs::Joy::ConstPtr &msg)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
Joy2twist(int argc, char **argv)
geometry_msgs::Twist in_biclops_cmd_vel_
ROSCPP_DECL void spin(Spinner &spinner)
ros::Publisher pubTwistBiclops_
ros::Subscriber subJoy_
int main(int argc, char **argv)
void twistBiclopsCallback(const geometry_msgs::Twist::ConstPtr &msg)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Subscriber subTwistPioneer_
ros::NodeHandle nh_
#define ROS_DEBUG(...)


demo_pioneer
Author(s): Fabien Spindler
autogenerated on Wed Jun 5 2019 20:53:56