joy2twist_pioneer.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 class Joy2twist
7 {
8 private:
13 
14  geometry_msgs::Twist in_cmd_vel_; // input cmd_vel
15 
17 
18 public:
19  void joyCallback(const sensor_msgs::Joy::ConstPtr& msg);
20  void twistCallback(const geometry_msgs::Twist::ConstPtr& msg);
21  Joy2twist(int argc, char**argv);
22  virtual ~Joy2twist(){};
23 };
24 
25 Joy2twist::Joy2twist(int argc, char**argv)
26 {
27  joystrick_drive_ = true;
28  subJoy_ = nh_.subscribe("joy", 1000, &Joy2twist::joyCallback, this);
29  subTwist_ = nh_.subscribe("cmd_vel", 1000, &Joy2twist::twistCallback, this);
30  pubTwist_ = nh_.advertise<geometry_msgs::Twist>("RosAria/cmd_vel", 1000);
31 }
32 
33 void Joy2twist::joyCallback(const sensor_msgs::Joy::ConstPtr& msg)
34 {
35  std::ostringstream strs;
36  strs << std::endl << "axes: [";
37  for(size_t i=0; i < msg->axes.size(); i++)
38  strs << msg->axes[i] << ",";
39  strs << "]" << std::endl;
40  strs << "button: [";
41  for(size_t i=0; i < msg->buttons.size(); i++)
42  strs << msg->buttons[i] << ",";
43  strs << "]" << std::endl;
44  std::string str;
45  str = strs.str();
46  ROS_DEBUG("%s", str.c_str());
47 
48  // Use Logitech wireless gamepad F710
49  // if buton 4 (front left) or 5 (front right) active
50  // out_cmd_vel = in_cmd_vel
51  // else
52  // out_cmd_vel = vitesse joy
53  // axe 3 (horizontal) = angular vel
54  // axe 4 (vertical) = linera vel along x
55  if (msg->buttons[4] || msg->buttons[5])
56  joystrick_drive_ = false;
57  else
58  joystrick_drive_ = true;
59 
60  geometry_msgs::Twist out_cmd_vel;
61  if (! joystrick_drive_)
62  out_cmd_vel = in_cmd_vel_;
63  else {
64  out_cmd_vel.angular.z = msg->axes[3];
65  out_cmd_vel.linear.x = msg->axes[4];
66  }
67  pubTwist_.publish(out_cmd_vel);
68 }
69 
70 void Joy2twist::twistCallback(const geometry_msgs::Twist::ConstPtr& msg)
71 {
72  // memorize the command
73  in_cmd_vel_.angular = msg->angular;
74  in_cmd_vel_.linear = msg->linear;
75  // publish the command if joystick button pressed
76  if (! joystrick_drive_) {
77  geometry_msgs::Twist out_cmd_vel;
78  out_cmd_vel = in_cmd_vel_;
79  pubTwist_.publish(out_cmd_vel);
80  }
81 }
82 
83 int main(int argc, char **argv)
84 {
85  ros::init(argc, argv, "joy2twist");
86 
87  Joy2twist joy2twist(argc, argv);
88 
89  ros::spin();
90 }
91 
ros::Publisher pubTwist_
void publish(const boost::shared_ptr< M > &message) const
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)
int main(int argc, char **argv)
ROSCPP_DECL void spin(Spinner &spinner)
ros::Subscriber subJoy_
ros::Subscriber subTwist_
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
virtual ~Joy2twist()
geometry_msgs::Twist in_cmd_vel_
ros::NodeHandle nh_
void twistCallback(const geometry_msgs::Twist::ConstPtr &msg)
#define ROS_DEBUG(...)


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