Skip to content

Commit df4ba10

Browse files
authored
add keyboard control python file
1 parent 567de88 commit df4ba10

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

swarm_control.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#导入包
4+
from re import T
5+
from turtle import pu
6+
import rospy
7+
from geometry_msgs.msg import Twist
8+
import sys,select,termios,tty
9+
10+
11+
12+
#第一步:获取一个键盘值
13+
def get_one_key():
14+
tty.setraw(sys.stdin.fileno()) #通过raw标准化输入
15+
rlist, _, _ = select.select([sys.stdin], [], [], 0.1)
16+
if rlist:
17+
key = sys.stdin.read(1) #获取捕获的键盘值
18+
else:
19+
key = ''
20+
21+
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, settings) #终端参数配置
22+
return key
23+
24+
#第二步:键盘值与控制数据做匹配
25+
key_control = {
26+
'w':(0.2,0),
27+
's':(-0.2,0),
28+
'a':(0,1),
29+
'd':(0,-1),
30+
31+
'W':(0.5,0),
32+
'S':(-0.5,0),
33+
'A':(0,3),
34+
'D':(0,-3)
35+
}
36+
37+
def check_keys():
38+
global one_control_flag
39+
global speed,turn
40+
key = get_one_key()
41+
if key in key_control.keys():
42+
speed = key_control[key][0]
43+
turn = key_control[key][1]
44+
elif key == ' ' or key == 'c' :
45+
speed = 0
46+
turn = 0
47+
elif key == 'z':
48+
exit()
49+
elif key == 'e' :
50+
one_control_flag = 1
51+
elif key == "q":
52+
one_control_flag = 0
53+
print("现在是集体控制!")
54+
55+
56+
info_p = '''
57+
********这个是控制终端,请确保光标在这个脚本内********
58+
这个例程是键盘控制KKSwarm运动的节点
59+
W前进
60+
S后退
61+
A左转
62+
D右转
63+
64+
大写后提速
65+
c或者空格 暂停。
66+
e切换控制单车
67+
q切换集体控制
68+
z 退出
69+
'''
70+
71+
72+
if __name__=="__main__":
73+
settings = termios.tcgetattr(sys.stdin)
74+
pub1 = rospy.Publisher("/robot_1/cmd_vel",Twist,queue_size = 5)
75+
pub2 = rospy.Publisher("/robot_2/cmd_vel",Twist,queue_size = 5)
76+
pub3 = rospy.Publisher("/robot_3/cmd_vel",Twist,queue_size = 5)
77+
pub4 = rospy.Publisher("/robot_4/cmd_vel",Twist,queue_size = 5)
78+
pub5 = rospy.Publisher("/robot_5/cmd_vel",Twist,queue_size = 5)
79+
pub6 = rospy.Publisher("/robot_6/cmd_vel",Twist,queue_size = 5)
80+
pub7 = rospy.Publisher("/robot_7/cmd_vel",Twist,queue_size = 5)
81+
pub8 = rospy.Publisher("/robot_8/cmd_vel",Twist,queue_size = 5)
82+
pub9 = rospy.Publisher("/robot_9/cmd_vel",Twist,queue_size = 5)
83+
pub10 = rospy.Publisher("/robot_10/cmd_vel",Twist,queue_size = 5)
84+
rospy.init_node("cmd_pub",anonymous = True)
85+
speed = 0
86+
turn = 0
87+
one_control_flag = 0
88+
print(info_p)
89+
while(1):
90+
91+
check_keys()
92+
93+
if one_control_flag== 0:
94+
twist = Twist()
95+
twist.linear.x = speed; twist.linear.y = 0; twist.linear.z = 0
96+
twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = turn
97+
pub1.publish(twist)
98+
pub2.publish(twist)
99+
pub3.publish(twist)
100+
pub4.publish(twist)
101+
pub5.publish(twist)
102+
pub6.publish(twist)
103+
pub7.publish(twist)
104+
pub8.publish(twist)
105+
pub9.publish(twist)
106+
pub10.publish(twist)
107+
else:
108+
speed = 0
109+
trun = 0
110+
twist = Twist()
111+
twist.linear.x = speed; twist.linear.y = 0; twist.linear.z = 0
112+
twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = turn
113+
pub1.publish(twist)
114+
pub2.publish(twist)
115+
pub3.publish(twist)
116+
pub4.publish(twist)
117+
pub5.publish(twist)
118+
pub6.publish(twist)
119+
pub7.publish(twist)
120+
pub8.publish(twist)
121+
pub9.publish(twist)
122+
pub10.publish(twist)
123+
key = input("你想要控制几号车? >")
124+
while one_control_flag == 1:
125+
check_keys()
126+
#twist = Twist()
127+
twist.linear.x = speed; twist.linear.y = 0; twist.linear.z = 0
128+
twist.angular.x = 0; twist.angular.y = 0; twist.angular.z = turn
129+
if key == 1:
130+
pub1.publish(twist)
131+
if key == 2:
132+
pub2.publish(twist)
133+
if key == 3:
134+
pub3.publish(twist)
135+
if key == 4:
136+
pub4.publish(twist)
137+
if key == 5:
138+
pub5.publish(twist)
139+
if key == 6:
140+
pub6.publish(twist)
141+
if key == 7:
142+
pub7.publish(twist)
143+
if key == 8:
144+
pub8.publish(twist)
145+
if key == 9:
146+
pub9.publish(twist)
147+
if key == 10:
148+
pub10.publish(twist)
149+
150+
151+
152+
153+
154+
155+
156+

0 commit comments

Comments
 (0)