joystick_driver.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <ros/subscriber.h>
3 #include <ros/publisher.h>
4 #include <igvc_msgs/velocity_pair.h>
5 #include <sensor_msgs/Joy.h>
6 
7 using namespace std;
8 
9 ros::Publisher cmd_pub;
10 
11 ros::NodeHandle *nhp;
12 
13 void joyCallback(const sensor_msgs::Joy::ConstPtr& msg)
14 {
15  double absoluteMaxVel, maxVel, maxVelIncr;
16  nhp->param(string("absoluteMaxVel"), absoluteMaxVel, 1.0);
17  nhp->param(string("maxVel"), maxVel, 1.0);
18  nhp->param(string("maxVelIncr"), maxVelIncr, 0.1);
19 
20  if(msg->buttons[1])
21  maxVel -= maxVelIncr;
22  else if(msg->buttons[3])
23  maxVel += maxVelIncr;
24  maxVel = min(maxVel, absoluteMaxVel);
25  maxVel = max(maxVel, 0.0);
26 
27  nhp->setParam("maxVel", maxVel);
28 
29  int leftJoyAxis, rightJoyAxis;
30  bool leftInverted, rightInverted;
31  nhp->param(string("leftAxis"), leftJoyAxis, 1);
32  nhp->param(string("rightAxis"), rightJoyAxis, 3);
33  nhp->param(string("leftInverted"), leftInverted, false);
34  nhp->param(string("rightInverted"), rightInverted, false);
35 
36  igvc_msgs::velocity_pair cmd;
37  cmd.left_velocity = msg->axes[leftJoyAxis]*maxVel * (leftInverted ? -1.0 : 1.0);
38  cmd.right_velocity = msg->axes[rightJoyAxis]*maxVel * (rightInverted ? -1.0 : 1.0);
39 
40  cmd_pub.publish(cmd);
41 }
42 
43 int main(int argc, char** argv)
44 {
45  ros::init(argc, argv, "joystick_driver");
46  ros::NodeHandle nh;
47  nhp = new ros::NodeHandle("~");
48 
49  cmd_pub = nh.advertise<igvc_msgs::velocity_pair>("/motors", 1);
50 
51  ros::Subscriber joy_sub = nh.subscribe("/joy", 1, joyCallback);
52 
53  ros::spin();
54 
55  delete nhp;
56 
57  return 0;
58 }
ros::Publisher cmd_pub
void joyCallback(const sensor_msgs::Joy::ConstPtr &msg)
ros::NodeHandle * nhp
int main(int argc, char **argv)


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45