-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.py
69 lines (57 loc) · 1.76 KB
/
tx.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
from crazyradio import Crazyradio
from inputs import get_gamepad #to read in controller data
#initialise crazyradio device in TX mode
def radio_tx_init():
radio = Crazyradio()
radio.set_channel(100)
radio.set_data_rate(Crazyradio.DR_250KPS)
radio.set_mode(Crazyradio.MODE_PTX)
return radio
#TX test
def radio_tx_test(radio):
for i in range(0, 100):
res = radio.send_packet([i])
print(res.data)
radio.close()
return
#send controller packet
def radio_send_pkt(radio, data):
res = radio.send_packet(data)
# print(res.data) #prints raw acknowledgement
# decodes acknowledgement
# recdlist = res.data.tolist()
# print(''.join(map(chr,recdlist)))
return
dms = False #dead man's switch flag
radio = radio_tx_init()
print("CrazyRadio Initialised.")
while 1:
events = get_gamepad()
for event in events:
#uncomment to see all controller input events
#print(event.ev_type,event.code,event.state)
#deadmanswitch L1/LB flag setting
if event.code == 'BTN_TL' and event.state == 1:
dms = True
elif event.code == 'BTN_TL' and event.state == 0:
dms = False
if dms: #dms pressed
#joystick input -> tweak depending on controller
if event.code == 'ABS_Y': #throttle
if event.state > 0:
print("back")
radio_send_pkt(radio,'b '+ str(event.state))
else:
print("forward")
radio_send_pkt(radio,'f '+ str(event.state))
elif event.code == 'ABS_RX': #steering
if event.state > 0:
print("right")
radio_send_pkt(radio,'r '+ str(event.state))
else:
print("left")
radio_send_pkt(radio,'l '+ str(event.state))
#terminate transmission with B button
if event.code == 'BTN_EAST':
radio.close()
print("Radio Transmission Closed. Ctrl-C to terminate")