-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls.py
112 lines (85 loc) · 3.38 KB
/
controls.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
102
103
104
105
106
107
108
109
110
111
112
import airsim
import win32gui
import keyboard
# parameters used to detemine the threshold when mapping to cardinal directions
xlength = 1920
ylength = 1080
margin = 0.2
leftbound = margin * xlength
rightbound = (1 - margin) * xlength
upbound = margin * ylength
downbound = (1 - margin) * ylength
# connect to the AirSim simulator
client = airsim.MultirotorClient()
client.confirmConnection()
client.enableApiControl(True)
client.armDisarm(True)
client.takeoffAsync().join()
speed = 5
def calculate_increment(caller, prev_dir, prev_cnt):
# update momentum variables
if caller != prev_dir:
prev_dir = caller
prev_cnt = 0
else:
prev_cnt += 1
# calculate position increment based on momemtum
return speed * 1.5 ** prev_cnt, prev_dir, prev_cnt
def print_command(prev_dir, increment):
switcher = {
0: "Right",
1: "Left",
2: "Backward",
3: "Forward",
4: "Up",
5: "Down"
}
print(switcher.get(prev_dir, "Invalid"), " ", increment)
def main():
print("Setup Complete")
prev_dir = -1
prev_cnt = 0
# get current drone position
position = client.getMultirotorState().kinematics_estimated.position
x_start = position.x_val
y_start = position.y_val
z_start = position.z_val
while(1):
# if key is pressed stop giving inputs until key C is pressed
# Note: key S has to be pressed (held down) until the current action has completed (this won't trigger while drone is moving)
if keyboard.is_pressed('s'):
while not keyboard.is_pressed('c'):
continue
gaze_pos = win32gui.GetCursorPos()
x = gaze_pos[0]
y = gaze_pos[1]
# look right to move right
if (x > rightbound and (upbound <= y <= downbound)):
increment, prev_dir, prev_cnt = calculate_increment(0, prev_dir, prev_cnt)
y_start = y_start + increment
# look left to move left
elif (x < leftbound and (upbound <= y <= downbound)):
increment, prev_dir, prev_cnt = calculate_increment(1, prev_dir, prev_cnt)
y_start = y_start - increment
# look down to move backwards
elif (y > downbound and (leftbound <= x <= rightbound)):
increment, prev_dir, prev_cnt = calculate_increment(2, prev_dir, prev_cnt)
x_start = x_start - increment
# look up to move forwards
elif (y < upbound and (leftbound <= x <= rightbound)):
increment, prev_dir, prev_cnt = calculate_increment(3, prev_dir, prev_cnt)
x_start = x_start + increment
# look top-right to move up
elif (y < upbound and x > rightbound):
increment, prev_dir, prev_cnt = calculate_increment(4, prev_dir, prev_cnt)
z_start = z_start - increment
# look bottom-left to move down
elif (y > downbound and x < leftbound):
increment, prev_dir, prev_cnt = calculate_increment(5, prev_dir, prev_cnt)
z_start = z_start + increment
# not a valid command, don't try to move drone
else:
continue
client.moveToPositionAsync(x_start, y_start, z_start, increment, 5).join()
print_command(prev_dir, increment)
main()