-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeyboardControl.py
116 lines (91 loc) · 3.11 KB
/
keyboardControl.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
113
114
115
116
import time, sys
from struct import *
from serial import *
from xbee import XBee
from lib import network_const
from lib.dictionaries import *
from lib.command_interface import CommandInterface
from lib.network_coordinator import NetworkCoordinator
THRUST_INCREMENT = 0.05
YAW_INCREMENT = -0.5
ELEVATOR_INCREMENT = 0.2
THRUST_UPPER_LIMIT = 1.0
THRUST_LOWER_LIMIT = 0.0
YAW_UPPER_LIMIT = 1.0
YAW_LOWER_LIMIT = -1.0
ELEVATOR_UPPER_LIMIT = 1.0
ELEVATOR_LOWER_LIMIT = -1.0
def txCallback(dest, packet):
global xb
xb.tx(dest_addr = dest, data = packet)
def loop():
DEFAULT_COM_PORT = 'COM7'
DEFAULT_BAUD_RATE = 57600
DEFAULT_ADDRESS = '\x10\x21'
DEFAULT_PAN = '\x10\x01'
if len(sys.argv) == 1:
com = DEFAULT_COM_PORT
baud = DEFAULT_BAUD_RATE
addr = DEFAULT_ADDRESS
elif len(sys.argv) == 4:
com = sys.argv[1]
baud = int(sys.argv[2])
addr = pack('>H', int(sys.argv[3], 16))
else:
print "Wrong number of arguments. Must be: COM BAUD ADDR"
sys.exit(1)
coord = CommandInterface(addr, txCallback)
coord.enableDebug()
ser = Serial(port = com, baudrate = baud)
xb = XBee(ser)
print "Setting PAN ID to " + hex(DEFAULT_PAN)
xb.at(command = 'ID', parameter = pack('>H', DEFAULT_PAN))
thrust = 0.0
yaw = 0.0
elevator = 0.0
while True:
try:
c = msvcrt.getch()
if c == 'w':
thrust = thrust + THRUST_INCREMENT
elif c == 'a':
yaw = yaw + YAW_INCREMENT
elif c == 's':
thrust = thrust - THRUST_INCREMENT
elif c == 'd':
yaw = yaw - YAW_INCREMENT
elif c == 'r':
elevator = elevator + ELEVATOR_INCREMENT
elif c == 'f':
elevator = elevator - ELEVATOR_INCREMENT
elif c == 'q':
thrust = 0.0
yaw = 0.0
elevator = 0.0
elif c == 'e':
break
elif c == 't':
coord.setRegulatorMode(RegulatorStates['Remote Control'])
if thrust > THRUST_UPPER_LIMIT:
thrust = THRUST_UPPER_LIMIT
elif thrust < THRUST_LOWER_LIMIT:
thrust = THRUST_LOWER_LIMIT
if yaw > YAW_UPPER_LIMIT:
yaw = YAW_UPPER_LIMIT
elif yaw < YAW_LOWER_LIMIT:
yaw = YAW_LOWER_LIMIT
if elevator > ELEVATOR_UPPER_LIMIT:
elevator = ELEVATOR_UPPER_LIMIT
elif elevator < ELEVATOR_LOWER_LIMIT:
elevator = ELEVATOR_LOWER_LIMIT
coord.setRemoteControlValues(thrust, yaw, elevator)
except:
print "Exception: ", sys.exc_info()[0]
break
xb.halt()
ser.close()
if __name__ == '__main__':
try:
loop()
except:
pass