-
Notifications
You must be signed in to change notification settings - Fork 17
/
keyboard_control.py
executable file
·101 lines (84 loc) · 3.15 KB
/
keyboard_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env /usr/bin/python3
import rospy
from quadrotor_msgs.msg import PositionCommand
import sys
import termios
import tty
velocity = [0.0, 0.0, 0.0] # [vx, vy, vz]
position_command = PositionCommand()
def get_key():
"""
Capture keyboard input.
"""
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
key = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return key
def update_velocity():
"""
Update velocity based on keyboard input.
"""
global velocity
key = get_key()
if key == 'w': # Increase forward velocity
velocity[0] += 0.1
elif key == 's': # Decrease forward velocity
velocity[0] -= 0.1
elif key == 'a': # Increase left velocity
velocity[1] += 0.1
elif key == 'd': # Decrease left velocity
velocity[1] -= 0.1
elif key == 'r': # Increase upward velocity
velocity[2] += 0.1
elif key == 'f': # Decrease upward velocity
velocity[2] -= 0.1
elif key == 'q': # Quit
rospy.signal_shutdown("User requested shutdown")
# prees space to stop
elif key==' ':
velocity = [0.0, 0.0, 0.0]
# clear the terminal and print help information
# including : current postion, current velocity, how to stop and how to quit
print("\033c")
print("Current Position: ({:.2f}, {:.2f}, {:.2f})".format(position_command.position.x, position_command.position.y, position_command.position.z))
print("Current Velocity: ({:.2f}, {:.2f}, {:.2f})".format(velocity[0], velocity[1], velocity[2]))
print("Press 'Space' to stop the drone")
print("Press 'Q' to quit the program")
def publish_position_command(event):
"""
Timer callback to publish PositionCommand messages.
"""
global velocity, position_command
# Check if event.last_real is None (first callback call)
if event.last_real is None:
delta_time = 0.1 # Assume the first delta time as 0.1 seconds (10 Hz)
else:
delta_time = event.current_real.to_sec() - event.last_real.to_sec()
# Update position based on velocity
position_command.position.x += velocity[0] * delta_time
position_command.position.y += velocity[1] * delta_time
position_command.position.z += velocity[2] * delta_time
# Update the message fields
position_command.header.stamp = rospy.Time.now()
position_command.velocity.x = velocity[0]
position_command.velocity.y = velocity[1]
position_command.velocity.z = velocity[2]
pub.publish(position_command)
if __name__ == '__main__':
rospy.init_node('keyboard_position_command_node')
pub = rospy.Publisher('/planning/pos_cmd', PositionCommand, queue_size=10)
position_command.position.x = 0
position_command.position.y = 0
position_command.position.z = 1.5
# Set a timer to call the publish_position_command function at 10 Hz
rospy.Timer(rospy.Duration(0.1), publish_position_command)
# Continuously check for keyboard input to update the velocity
try:
while not rospy.is_shutdown():
update_velocity()
except rospy.ROSInterruptException:
pass