Tetrapod Project
joystick_controller.cpp
Go to the documentation of this file.
2 
4 {
5  twist_command_message.linear.x = 0.0;
6  twist_command_message.linear.y = 0.0;
7  twist_command_message.linear.z = 0.0;
8 
9  twist_command_message.angular.x = 0.0;
10  twist_command_message.angular.y = 0.0;
11  twist_command_message.angular.z = 0.0;
12 }
13 
15 {
16  nodeHandle->shutdown();
17 
18  //this->listenerThread.clear();
19  //this->listenerThread.disable();
20  //nodeHandle->keyboardListenerThread.join();
21 }
22 
24 {
25  if(!ros::isInitialized())
26  {
27  int argc = 0;
28  char **argv = NULL;
29  ros::init
30  (
31  argc,
32  argv,
33  "joystick_controller_node",
34  ros::init_options::NoSigintHandler
35  );
36  }
37 
38  nodeHandle.reset(new ros::NodeHandle("joystick_controller_node"));
39 
40  joystick_subscriber = nodeHandle->subscribe
41  (
42  "/joy",
43  10,
45  this
46  );
47 
49  (
50  "/keyboard/keydown",
51  10,
53  this
54  );
55 
57  (
58  "/keyboard/keyup",
59  10,
61  this
62  );
63 
64  twist_command_publisher = nodeHandle->advertise<geometry_msgs::Twist>("/twist_command", 1);
65 }
66 
67 
69 {
71  {
72  return true;
73  }
74  else
75  {
76  return false;
77  }
78 }
79 
81 {
83  {
84  return true;
85  }
86  else
87  {
88  return false;
89  }
90 }
91 
92 void JoystickController::joystickCallback(const sensor_msgs::JoyConstPtr &_msg)
93 {
94  double t = ros::Time::now().toSec();
95 
96  if(_msg->buttons[START] == 1)
97  {
98  if(pause == false)
99  {
100  pause = true;
101  }
102  else
103  {
104  pause = false;
105  }
106  }
107 
108  if((_msg->buttons[TRIANGLE] == 1) && (_msg->buttons[CROSS] == 0))
109  {
111  {
112  time_of_last_linear_limit_change = ros::Time::now().toSec();
114  }
115  }
116  else if((_msg->buttons[TRIANGLE] == 0) && (_msg->buttons[CROSS] == 1))
117  {
119  {
120  time_of_last_linear_limit_change = ros::Time::now().toSec();
121  changeLinearMultiplier(false);
122  }
123  }
124 
125  if((_msg->buttons[SQUARE] == 1) && (_msg->buttons[CIRCLE] == 0))
126  {
128  {
129  time_of_last_rotational_limit_change = ros::Time::now().toSec();
131  }
132  }
133  else if((_msg->buttons[SQUARE] == 0) && (_msg->buttons[CIRCLE] == 1))
134  {
136  {
137  time_of_last_rotational_limit_change = ros::Time::now().toSec();
139  }
140  }
141 
144 
145  if((_msg->buttons[L1] == 1) && (_msg->buttons[R1] == 0))
146  {
148  }
149  else if((_msg->buttons[R1] == 1) && (_msg->buttons[L1] == 0))
150  {
152  }
153  else
154  {
155  twist_command_message.angular.z = 0.0;
156  }
157 
158  if(_msg->buttons[DPAD_UP] == 1)
159  {
160  std_srvs::Empty readyToProceedSrv;
161  ros::service::call("/my_robot/controller/ready_to_proceed", readyToProceedSrv);
162  }
163 
164  if(_msg->buttons[SELECT] == 1)
165  {
166  std_srvs::Empty shutdownSrv;
167  ros::service::call("/my_robot/controller/shutdown", shutdownSrv);
168  ros::service::call("/my_robot/motor_interface/shutdown", shutdownSrv);
169  }
170 }
171 
172 void JoystickController::keyboardButtonPressedCallback(const keyboard::KeyConstPtr &_msg)
173 {
174  switch (_msg->code)
175  {
176  case KEY_UP:
177  FORWARD = PRESSED;
179  break;
180  case KEY_DOWN:
181  BACKWARD = PRESSED;
183  break;
184  case KEY_RIGHT:
185  RIGHT = PRESSED;
187  break;
188  case KEY_LEFT:
189  LEFT = PRESSED;
191  break;
195  break;
199  break;
203  if(FORWARD == PRESSED)
204  {
206  }
207  else if(BACKWARD == PRESSED)
208  {
210  }
211 
212  if(LEFT == PRESSED)
213  {
215  }
216  else if(RIGHT == PRESSED)
217  {
219  }
220  break;
223  changeLinearMultiplier(false);
224  if(FORWARD == PRESSED)
225  {
227  }
228  else if(BACKWARD == PRESSED)
229  {
231  }
232 
233  if(LEFT == PRESSED)
234  {
236  }
237  else if(RIGHT == PRESSED)
238  {
240  }
241  break;
246  {
248  }
249  else if(CLOCKWISE_ROTATION)
250  {
252  }
253  break;
258  {
260  }
261  else if(CLOCKWISE_ROTATION)
262  {
264  }
265  break;
266  case KEY_PAUSE:
267  PAUSE = RELEASED;
268  if(pause == false)
269  {
270  pause = true;
271  }
272  else
273  {
274  pause = false;
275  }
276  break;
277  default:
278  break;
279  }
280 }
281 
282 void JoystickController::keyboardButtonReleasedCallback(const keyboard::KeyConstPtr &_msg)
283 {
284  switch (_msg->code)
285  {
286  case KEY_UP:
287  FORWARD = RELEASED;
288  twist_command_message.linear.x = 0.0;
289  break;
290  case KEY_DOWN:
291  BACKWARD = RELEASED;
292  twist_command_message.linear.x = 0.0;
293  break;
294  case KEY_RIGHT:
295  RIGHT = RELEASED;
296  twist_command_message.linear.y = 0.0;
297  break;
298  case KEY_LEFT:
299  LEFT = RELEASED;
300  twist_command_message.linear.y = 0.0;
301  break;
304  twist_command_message.angular.z = 0.0;
305  break;
308  twist_command_message.angular.z = 0.0;
309  break;
310  default:
311  break;
312  }
313 }
314 
316 {
318  {
320  }
322  {
324  }
325 }
326 
328 {
330  {
332  }
334  {
336  }
337 }
338 
340 {
342 }
ButtonStatus COUNTER_CLOCKWISE_ROTATION
void keyboardButtonReleasedCallback(const keyboard::KeyConstPtr &_msg)
ButtonStatus LINEAR_SPEED_INCREASE
std::unique_ptr< ros::NodeHandle > nodeHandle
void changeRotationalMultiplier(bool _increase)
geometry_msgs::Twist twist_command_message
ButtonStatus CLOCKWISE_ROTATION
ButtonStatus ANGULAR_RATE_INCREASE
ros::Subscriber joystick_subscriber
void keyboardButtonPressedCallback(const keyboard::KeyConstPtr &_msg)
ros::Publisher twist_command_publisher
void joystickCallback(const sensor_msgs::JoyConstPtr &_msg)
ButtonStatus ANGULAR_RATE_DECREASE
double minimum_seconds_between_limit_change
void changeLinearMultiplier(bool _increase)
double time_of_last_rotational_limit_change
ros::Subscriber keyboard_button_pressed_subscriber
ButtonStatus LINEAR_SPEED_DECREASE
ros::Subscriber keyboard_button_released_subscriber
@ LEFT_STICK_UD
@ RIGHT_STICK_LR
@ SELECT
@ TRIANGLE
@ SQUARE
@ CIRCLE
@ DPAD_UP
#define BUTTON_ERROR
Eigen::Matrix< double, 6, 1 > Twist
Definition: kinematics.h:48