teleop.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <termios.h>
3 #include <unistd.h>
4 #include <map>
5 
6 #include <ros/ros.h>
7 #include <geometry_msgs/Twist.h>
8 
9 // Global Vars
10 
11 
12 
13 // -------------------------------
14 
15 std::map<char, std::vector<float>> moveBindings
16 {
17  // lin_x, lin_y, ang_z
18  {'w', {0.5, 0, 0}},
19  {'x', {-0.5, 0, 0}},
20  {'a', {0, 0.5, 0}},
21  {'d', {0, -0.5, 0}},
22  {'s', {0, 0, 0}},
23  {'q', {0, 0, -0.5}},
24  {'e', {0, 0, 0.5}},
25 };
26 
28  std::cout << "Reading from keyboard and publishing on cmd_vel..." << std::endl;
29  std::cout << "--------------------------------------------------" << std::endl;
30 
31  std::cout << "To move around use:" << std::endl;
32  std::cout << " w " << std::endl;
33  std::cout << " a s d" << std::endl;
34  std::cout << " x " << std::endl;
35 
36  std::cout << "To rotate the bot use:" << std::endl;
37  std::cout << "q/e for anti-clockwise/clockwise rotation" << std::endl;
38 
39  std::cout << "\nPress the key again to increase the velocity..." << std::endl;
40 }
41 
42 
43 // For non-blocking keyboard inputs
44 int getch()
45 {
46  int ch;
47  struct termios oldt;
48  struct termios newt;
49 
50  // Store old settings, and copy to new settings
51  tcgetattr(STDIN_FILENO, &oldt);
52  newt = oldt;
53 
54  // Make required changes and apply the settings
55  newt.c_lflag &= ~(ICANON | ECHO);
56  newt.c_iflag |= IGNBRK;
57  newt.c_iflag &= ~(INLCR | ICRNL | IXON | IXOFF);
58  newt.c_lflag &= ~(ICANON | ECHO | ECHOK | ECHOE | ECHONL | ISIG | IEXTEN);
59  newt.c_cc[VMIN] = 1;
60  newt.c_cc[VTIME] = 0;
61  tcsetattr(fileno(stdin), TCSANOW, &newt);
62 
63  // Get the current character
64  ch = getchar();
65 
66  // Reapply old settings
67  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
68 
69  return ch;
70 }
71 
72 void print_vel(const geometry_msgs::Twist &vel)
73 {
74  std::cout << "Currently: lin_x is " << vel.linear.x << " lin_y is " << vel.linear.y << " and ang_z is " << vel.angular.z << std::endl;
75 }
76 
77 int main(int argc, char** argv)
78 {
79  ros::init(argc, argv, "teleop_keyboard");
80  ros::NodeHandle nh;
81  ros::NodeHandle nh_private("~");
82 
83  ROS_INFO("Starting Teleop Node\n");
84 
85  ros::Publisher vel_pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1);
86  geometry_msgs::Twist vel;
87 
88  print_message();
89 
90  char key(' ');
91  float x(0), y(0), ang_z(0);
92 
93 
94  while(ros::ok()){
95  key = getch();
96 
97  if (moveBindings.count(key) > 0) // key exists in the map
98  {
99  if (key != 's')
100  {
101  x = x + moveBindings[key][0];
102  y = y + moveBindings[key][1];
103  ang_z = ang_z + moveBindings[key][2];
104  }
105  else{
106  x = moveBindings[key][0];
107  y = moveBindings[key][1];
108  ang_z = moveBindings[key][2];
109 
110  std::cout << "\n\n" << std::endl;
111  print_message();
112  }
113  }
114  if (key == '\x03') // Ctrl+C
115  {
116  ROS_INFO("CLOSING TELEOP NODE\n");
117  break;
118  }
119 
120  vel.linear.x = x;
121  vel.linear.y = y;
122  vel.linear.z = 0;
123  vel.angular.x = 0;
124  vel.angular.y = 0;
125  vel.angular.z = ang_z;
126 
127  vel_pub.publish(vel);
128  print_vel(vel);
129  ros::spinOnce();
130  }
131 }
int main(int argc, char **argv)
Definition: teleop.cpp:77
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
int getch()
Definition: teleop.cpp:44
void print_message()
Definition: teleop.cpp:27
void publish(const boost::shared_ptr< M > &message) const
#define ROS_INFO(...)
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void print_vel(const geometry_msgs::Twist &vel)
Definition: teleop.cpp:72
std::map< char, std::vector< float > > moveBindings
Definition: teleop.cpp:16
ROSCPP_DECL void spinOnce()


omnibase_control
Author(s): Harshal Deshpande , Mihir Kulkarni
autogenerated on Mon Feb 28 2022 23:01:32