-
Notifications
You must be signed in to change notification settings - Fork 0
/
hover_with_observer.py
164 lines (136 loc) · 5.65 KB
/
hover_with_observer.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import logging
from operator import pos
import time
import json
import numpy as np
import cflib.crtp
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.log import LogConfig
# Specify the uri of the drone to which we want to connect (if your radio
# channel is X, the uri should be 'radio://0/X/2M/E7E7E7E7E7')
uri = 'radio://0/19/2M/E7E7E7E7E7' # <-- Drone 1
# Specify the variables we want to log (all at 100 Hz)
variables = [
]
class SimpleClient:
def __init__(self, uri, use_controller=False, use_observer=False):
self.init_time = time.time()
self.use_controller = use_controller
self.use_observer = use_observer
self.cf = Crazyflie(rw_cache='./cache')
self.cf.connected.add_callback(self.connected)
self.cf.connection_failed.add_callback(self.connection_failed)
self.cf.connection_lost.add_callback(self.connection_lost)
self.cf.disconnected.add_callback(self.disconnected)
print(f'Connecting to {uri}')
self.cf.open_link(uri)
self.is_connected = False
self.data = {}
def connected(self, uri):
print(f'Connected to {uri}')
self.is_connected = True
# Start logging
self.logconfs = []
self.logconfs.append(LogConfig(name=f'LogConf0', period_in_ms=10))
num_variables = 0
for v in variables:
num_variables += 1
if num_variables > 5: # <-- could increase if you paid attention to types / sizes (max 30 bytes per packet)
num_variables = 0
self.logconfs.append(LogConfig(name=f'LogConf{len(self.logconfs)}', period_in_ms=10))
self.data[v] = {'time': [], 'data': []}
self.logconfs[-1].add_variable(v)
for logconf in self.logconfs:
try:
self.cf.log.add_config(logconf)
logconf.data_received_cb.add_callback(self.log_data)
logconf.error_cb.add_callback(self.log_error)
logconf.start()
except KeyError as e:
print(f'Could not start {logconf.name} because {e}')
for v in logconf.variables:
print(f' - {v.name}')
except AttributeError:
print(f'Could not start {logconf.name} because of bad configuration')
for v in logconf.variables:
print(f' - {v.name}')
# Reset the stock EKF
self.cf.param.set_value('kalman.resetEstimation', 1)
# Enable the controller (1 for stock controller, 4 for ae483 controller)
if self.use_controller:
self.cf.param.set_value('stabilizer.controller', 4)
else:
self.cf.param.set_value('stabilizer.controller', 1)
# Enable the observer (0 for disable, 1 for enable)
if self.use_observer:
self.cf.param.set_value('ae483par.use_observer', 1)
self.cf.param.set_value('ae483par.reset_observer', 1)
else:
self.cf.param.set_value('ae483par.use_observer', 0)
# self.cf.param.set_value('stabilizer.estimator', 1) # <------------------- FIXME
def connection_failed(self, uri, msg):
print(f'Connection to {uri} failed: {msg}')
def connection_lost(self, uri, msg):
print(f'Connection to {uri} lost: {msg}')
def disconnected(self, uri):
print(f'Disconnected from {uri}')
self.is_connected = False
def log_data(self, timestamp, data, logconf):
for v in logconf.variables:
self.data[v.name]['time'].append(timestamp)
self.data[v.name]['data'].append(data[v.name])
def log_error(self, logconf, msg):
print(f'Error when logging {logconf}: {msg}')
def move(self, x, y, z, yaw, dt):
print(f'Move to {x}, {y}, {z} with yaw {yaw} degrees for {dt} seconds')
start_time = time.time()
while time.time() - start_time < dt:
self.cf.commander.send_position_setpoint(x, y, z, yaw)
time.sleep(0.1)
def move_smooth(self, p1, p2, yaw, dt):
print(f'Move smoothly from {p1} to {p2} with yaw {yaw} degrees in {dt} seconds')
p1 = np.array(p1)
p2 = np.array(p2)
start_time = time.time()
while True:
current_time = time.time()
s = (current_time - start_time) / dt
p = (1 - s) * p1 + (s * p2)
self.cf.commander.send_position_setpoint(p[0], p[1], p[2], yaw)
if s >= 1:
return
else:
time.sleep(0.1)
def stop(self, dt):
print(f'Stop for {dt} seconds')
self.cf.commander.send_stop_setpoint()
start_time = time.time()
while time.time() - start_time < dt:
time.sleep(0.1)
def disconnect(self):
self.cf.close_link()
def write_data(self, filename='logged_data.json'):
with open(filename, 'w') as outfile:
json.dump(self.data, outfile, indent=4, sort_keys=False)
if __name__ == '__main__':
# Initialize everything
logging.basicConfig(level=logging.ERROR)
cflib.crtp.init_drivers()
# Create and start the client that will connect to the drone
client = SimpleClient(uri, use_controller=False, use_observer=False) # <-- FIXME
while not client.is_connected:
print(f' ... connecting ...')
time.sleep(1.0)
# Leave time at the start to initialize
client.stop(1.0)
# Take off
client.move(0., 0., 0.15, 0., 1.)
client.move(0., 0., 0.5, 0., 8.0)
# Prepare to land
client.move(0., 0., 0.15, 0., 1.)
# Land
client.stop(1.0)
# Disconnect from drone
client.disconnect()
# Write data from flight
client.write_data('hardware_data.json')