wheel_operator.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright 2016 ROBOTIS CO., LTD.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 
17 /* Authors: Taehun Lim (Darby) */
18 
19 #include <fcntl.h> // FILE control
20 #include <termios.h> // Terminal IO
21 
22 #include <ros/ros.h>
23 #include <geometry_msgs/Twist.h>
24 
25 #define ESC_ASCII_VALUE 0x1b
26 #define FORWARD 0x77
27 #define BACKWARD 0x78
28 #define LEFT 0x61
29 #define RIGHT 0x64
30 #define STOPS 0x73
31 
32 int getch(void)
33 {
34  struct termios oldt, newt;
35  int ch;
36 
37  tcgetattr( STDIN_FILENO, &oldt );
38  newt = oldt;
39  newt.c_lflag &= ~(ICANON | ECHO);
40  newt.c_cc[VMIN] = 0;
41  newt.c_cc[VTIME] = 1;
42  tcsetattr( STDIN_FILENO, TCSANOW, &newt );
43  ch = getchar();
44  tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
45 
46  return ch;
47 }
48 
49 int kbhit(void)
50 {
51  struct termios oldt, newt;
52  int ch;
53  int oldf;
54 
55  tcgetattr(STDIN_FILENO, &oldt);
56  newt = oldt;
57  newt.c_lflag &= ~(ICANON | ECHO);
58  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
59  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
60  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
61 
62  ch = getchar();
63 
64  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
65  fcntl(STDIN_FILENO, F_SETFL, oldf);
66 
67  if (ch != EOF)
68  {
69  ungetc(ch, stdin);
70  return 1;
71  }
72  return 0;
73 }
74 
75 int main(int argc, char **argv)
76 {
77  // Init ROS node
78  ros::init(argc, argv, "wheel_operator");
79  ros::NodeHandle node_handle("");
80 
81  double lin_vel_step = 0.01;
82  double ang_vel_step = 0.1;
83 
84  ROS_INFO("You can set '-lin_vel_step' and '-ang_vel_step' arguments (default is 0.01 and 0.1)");
85 
86  if (argc > 1)
87  {
88  lin_vel_step = atof(argv[1]);
89  ang_vel_step = atof(argv[2]);
90  }
91 
92  ros::Publisher cmd_vel_pub = node_handle.advertise<geometry_msgs::Twist>("cmd_vel", 10);
93  geometry_msgs::Twist twist_msg;
94 
95  std::string msg =
96  "\n\
97  Control Your Mobile Robot! \n\
98  --------------------------- \n\
99  Moving around:\n\
100  w\n\
101  a s d\n\
102  x\n\
103  \n\
104  w/x : increase/decrease linear velocity\n\
105  a/d : increase/decrease angular velocity\n\
106  \n\
107  s : force stop\n\
108  \n\
109  CTRL-C to quit\n\
110  ";
111 
112  ROS_INFO("%s", msg.c_str());
113 
114  ros::Rate loop_rate(100);
115 
116  while(ros::ok())
117  {
118  if (kbhit())
119  {
120  char c = getch();
121 
122  if (c == FORWARD)
123  {
124  twist_msg.linear.x += lin_vel_step;
125  }
126  else if (c == BACKWARD)
127  {
128  twist_msg.linear.x -= lin_vel_step;
129  }
130  else if (c == LEFT)
131  {
132  twist_msg.angular.z += ang_vel_step;
133  }
134  else if (c == RIGHT)
135  {
136  twist_msg.angular.z -= ang_vel_step;
137  }
138  else if (c == STOPS)
139  {
140  twist_msg.linear.x = 0.0f;
141  twist_msg.angular.z = 0.0f;
142  }
143  else
144  {
145  twist_msg.linear.x = twist_msg.linear.x;
146  twist_msg.angular.z = twist_msg.angular.z;
147  }
148  }
149 
150  cmd_vel_pub.publish(twist_msg);
151 
152  ros::spinOnce();
153  loop_rate.sleep();
154  }
155 
156  return 0;
157 }
#define RIGHT
#define BACKWARD
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
#define ROS_INFO(...)
int main(int argc, char **argv)
ROSCPP_DECL bool ok()
int getch(void)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int kbhit(void)
#define STOPS
ROSCPP_DECL void spinOnce()
#define FORWARD
#define LEFT


dynamixel_workbench_operators
Author(s): Darby Lim , Ryan Shim
autogenerated on Mon Sep 28 2020 03:37:04