controller.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # coding: UTF-8
3 # The MIT License (MIT)
4 #
5 # Copyright (c) 2018 Bluewhale Robot
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in all
15 # copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 # SOFTWARE.
24 #
25 # Author: Randoms
26 
27 import fcntl
28 import os
29 import signal
30 import sys
31 import termios
32 import thread
33 
34 import rospy
35 from geometry_msgs.msg import Twist
36 from nav_msgs.msg import Odometry
37 
38 
39 def signal_handler(signal, frame):
40  os.system('reset')
41  rospy.loginfo("Bye.")
42  rospy.signal_shutdown("exit")
43 
44 
45 
46 CURRENT_KEY = ""
47 CURRENT_ODOM = Odometry()
48 KEY_CACHE = []
49 UP_KEY = [27, 91, 65]
50 DOWN_KEY = [27, 91, 66]
51 LEFT_KEY = [27, 91, 68]
52 RIGHT_KEY = [27, 91, 67]
53 
54 
55 def update_speed(odom):
56  global CURRENT_ODOM
57  CURRENT_ODOM = odom
58 
59 def send_cmd(key):
60  cmd = Twist()
61  cmd.linear.x = CURRENT_ODOM.twist.twist.linear.x
62  cmd.angular.z = CURRENT_ODOM.twist.twist.angular.z
63  cmd.linear.x = 0
64  cmd.angular.z = 0
65  if key == "up":
66  cmd.linear.x = 0.2
67  elif key == "down":
68  cmd.linear.x = -0.2
69  elif key == "left":
70  cmd.angular.z = 0.8
71  elif key == "right":
72  cmd.angular.z = -0.8
73  cmd_pub.publish(cmd)
74 
75 
77  global CURRENT_KEY, KEY_CACHE
78  while True:
79  try:
80  c = sys.stdin.read(1)
81  if ord(c) == 27:
82  # somestrange pressed
83  KEY_CACHE = [27]
84  continue
85  elif len(KEY_CACHE) != 0:
86  KEY_CACHE.append(ord(c))
87  if KEY_CACHE == LEFT_KEY:
88  CURRENT_KEY = "left"
89  if KEY_CACHE == RIGHT_KEY:
90  CURRENT_KEY = "right"
91  if KEY_CACHE == UP_KEY:
92  CURRENT_KEY = "up"
93  if KEY_CACHE == DOWN_KEY:
94  CURRENT_KEY = "down"
95  if len(KEY_CACHE) == 3:
96  KEY_CACHE = []
97  else:
98  CURRENT_KEY = c
99  except:
100  pass
101 
102 
103 
104 if __name__ == "__main__":
105 
106  rospy.init_node('xiaoqiang_controller', anonymous=True)
107  rospy.loginfo("Welcome to use xiaoqiang controller.")
108  rospy.loginfo("Use arrow keys to move robot around.")
109  rospy.loginfo("Use space to stop robot.")
110  rospy.loginfo("Use Ctrl + C to exit.")
111 
112  # init terminal
113  signal.signal(signal.SIGINT, signal_handler)
114 
115  fd = sys.stdin.fileno()
116 
117  old_term = termios.tcgetattr(fd)
118  new_attr = termios.tcgetattr(fd)
119  new_attr[3] = new_attr[3] & ~termios.ICANON & ~termios.ECHO
120  termios.tcsetattr(fd, termios.TCSANOW, new_attr)
121 
122  old_flags = fcntl.fcntl(fd, fcntl.F_GETFL)
123  fcntl.fcntl(fd, fcntl.F_SETFL, old_flags | os.O_NONBLOCK)
124 
125  cmd_pub = rospy.Publisher('/cmd_vel', Twist, queue_size=0)
126  rospy.Subscriber("/xiaoqiang_driver/odom", Odometry, update_speed)
127 
128  thread.start_new_thread(get_current_key, ())
129 
130  rate = rospy.Rate(5)
131  while not rospy.is_shutdown():
132  send_cmd(CURRENT_KEY)
133  rate.sleep()
def update_speed(odom)
Definition: controller.py:55
def get_current_key()
Definition: controller.py:76
def signal_handler(signal, frame)
Definition: controller.py:39
def send_cmd(key)
Definition: controller.py:59


xiaoqiang_controller
Author(s):
autogenerated on Mon Jun 10 2019 15:53:16