-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow_coords.py
189 lines (160 loc) · 6.56 KB
/
follow_coords.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding: utf-8 -*-
#
# || ____ _ __
# +------+ / __ )(_) /_______________ _____ ___
# | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
# +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
# || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
#
# Copyright (C) 2016 Bitcraze AB
#
# Crazyflie Nano Quadcopter Client
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
"""
Simple example that connects to one crazyflie (check the address at the top
and update it to your crazyflie address) and send a sequence of setpoints,
one every second.
This example is intended to work with the Loco Positioning System in TWR TOA
mode. It aims at documenting how to set the Crazyflie in position control mode
and how to send setpoints.
"""
import logging
import time
import cflib.crtp
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.log import LogConfig
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
from cflib.crazyflie.syncLogger import SyncLogger
from cflib.utils import uri_helper
def wait_for_position_estimator(scf):
print('Waiting for estimator to find position...')
log_config = LogConfig(name='Kalman Variance', period_in_ms=500)
log_config.add_variable('kalman.varPX', 'float')
log_config.add_variable('kalman.varPY', 'float')
log_config.add_variable('kalman.varPZ', 'float')
var_y_history = [1000] * 10
var_x_history = [1000] * 10
var_z_history = [1000] * 10
threshold = 0.001
with SyncLogger(scf, log_config) as logger:
for log_entry in logger:
data = log_entry[1]
var_x_history.append(data['kalman.varPX'])
var_x_history.pop(0)
var_y_history.append(data['kalman.varPY'])
var_y_history.pop(0)
var_z_history.append(data['kalman.varPZ'])
var_z_history.pop(0)
min_x = min(var_x_history)
max_x = max(var_x_history)
min_y = min(var_y_history)
max_y = max(var_y_history)
min_z = min(var_z_history)
max_z = max(var_z_history)
# print("{} {} {}".
# format(max_x - min_x, max_y - min_y, max_z - min_z))
if (max_x - min_x) < threshold and (
max_y - min_y) < threshold and (
max_z - min_z) < threshold:
break
def reset_estimator(scf):
cf = scf.cf
cf.param.set_value('kalman.resetEstimation', '1')
time.sleep(0.1)
cf.param.set_value('kalman.resetEstimation', '0')
wait_for_position_estimator(cf)
def position_callback(timestamp, data, logconf):
x = data['kalman.stateX']
y = data['kalman.stateY']
z = data['kalman.stateZ']
bat = data["pm.vbat"]
#print('pos: ({}, {}, {}, {})'.format(x, y, z, bat))
try:
with open("temp/last_v.txt", "w") as file:
file.write(str(bat))
except:
print("Busy")
def start_position_printing(scf):
log_conf = LogConfig(name='Position', period_in_ms=100)
log_conf.add_variable('kalman.stateX', 'float')
log_conf.add_variable('kalman.stateY', 'float')
log_conf.add_variable('kalman.stateZ', 'float')
log_conf.add_variable("pm.vbat", "float")
scf.cf.log.add_config(log_conf)
log_conf.data_received_cb.add_callback(position_callback)
log_conf.start()
def run_sequence(scf, sequence, path_name):
cf = scf.cf
for position in range(len(sequence)):
print('Setting position {}'.format(sequence[position]))
if position == 3:
ended = False
while not ended:
try:
with open("temp/last_v.txt","r") as file:
voltage = float(file.readline())
with open("data/"+path_name + ".txt", "a") as file:
file.write(str(voltage))
ended = True
except:
continue
if position == len(sequence)-2:
ended = False
while not ended:
try:
with open("temp/last_v.txt","r") as file:
voltage = float(file.readline())
with open("data/"+path_name + ".txt", "a") as file:
file.write(" " + str(voltage) + "\n")
ended = True
except:
continue
for i in range(13):
cf.commander.send_position_setpoint(sequence[position][0],
sequence[position][1],
sequence[position][2],
sequence[position][3])
time.sleep(0.1)
cf.commander.send_stop_setpoint()
# Make sure that the last packet leaves before the link is closed
# since the message queue is not flushed before closing
if __name__ == '__main__':
# URI to the Crazyflie to connect to
uri = uri_helper.uri_from_env(default='radio://0/100/2M/E7E7E7E7E7')
path_name = "path_1"
# Change the sequence according to your setup
# x y z YAW
with open("actions/"+ path_name + ".txt","r")as path_file:
path = path_file.read()
sequence = eval(path)
print("Loading drivers")
cflib.crtp.init_drivers(enable_debug_driver=False)
print("Drivers loaded")
with SyncCrazyflie(uri, cf=Crazyflie(rw_cache='./cache')) as scf:
print("Resetting estimator")
reset_estimator(scf)
start_position_printing(scf)
print("Running sequence")
with open("data/"+path_name + ".txt", "a") as file:
file.write("\n")
while True:
run_sequence(scf, sequence, path_name)
for i in range(0,20):
scf.cf.commander.send_position_setpoint(2,1,1.5,0)
time.sleep(0.1)
for i in range(0,20):
scf.cf.commander.send_position_setpoint(0.5,0.5,1.5,0)
time.sleep(0.1)