I use the Crazyflie 2.0. here is my
Code: Select all
# -*- coding: utf-8 -*-
#
# || ____ _ __
# +------+ / __ )(_) /_______________ _____ ___
# | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \
# +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/
# || || /_____/_/\__/\___/_/ \__,_/ /___/\___/
#
# Copyright (C) 2018 Bitcraze AB
#
# 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 uses the high level commander
to send setpoints and trajectory to fly a figure 8.
This example is intended to work with any positioning system (including LPS).
It aims at documenting how to set the Crazyflie in position control mode
and how to send setpoints using the high level commander.
"""
import time
import cflib.crtp
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.log import LogConfig
from cflib.crazyflie.mem import MemoryElement
from cflib.crazyflie.mem import Poly4D
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
from cflib.crazyflie.syncLogger import SyncLogger
# URI to the Crazyflie to connect to
uri = 'radio://0/80/250K'
# The trajectory to fly
# See https://github.com/whoenig/uav_trajectories for a tool to generate
# trajectories
# Duration,x^0,x^1,x^2,x^3,x^4,x^5,x^6,x^7,y^0,y^1,y^2,y^3,y^4,y^5,y^6,y^7,z^0,z^1,z^2,z^3,z^4,z^5,z^6,z^7,yaw^0,yaw^1,yaw^2,yaw^3,yaw^4,yaw^5,yaw^6,yaw^7
figure8 = [
[1.94407,0.35,0,0,0,0,0,0,0,0,0,0,0,0.249185,-0.210906,0.066447,-0.00752572,0,0,0,0,0.245519,-0.207575,0.065594,-0.00746944,0,0,0,0,0,0,0,0
, ],
# noqa
[1.81561,0.35,1.53354e-17,2.51224e-18,9.0972e-18,0,0,0,0,0.5,0.487702,0.00269216,-0.0313613,-0.0965227,0.046441,0.00411718,-0.00340606,0.5,0.497023,0.014375,-0.0311825,-0.136679,0.0937604,-0.0178844,0.000197844,0,0,0,0,0,0,0,0
, ],
# noqa
[1.5796,0.35,-3.61885e-18,-1.11751e-18,-1.60786e-18,0,0,0,0,1,0.0331445,-0.0377929,-0.00404071,0.0244386,-0.0141881,0.00335248,-0.000278285,1,-0.00450496,-0.0803347,0.00141505,-0.430467,0.479214,-0.194482,0.0281352,0,0,0,0,0,0,0,0
, ],
# noqa
[1.68851,0.35,4.11491e-18,3.12437e-18,-1.06481e-17,0,0,0,0,1,-0.00531817,0.0053301,-1.44305e-05,0.00343343,-0.00732799,0.00412526,-0.000743177,0.5,-0.532882,0.017878,0.0389627,-0.0940468,0.266293,-0.177903,0.0356159,0,0,0,0,0,0,0,0
, ],
# noqa
]
class Uploader:
def __init__(self):
self._is_done = False
def upload(self, trajectory_mem):
print('Uploading data')
trajectory_mem.write_data(self._upload_done)
while not self._is_done:
time.sleep(0.2)
def _upload_done(self, mem, addr):
print('Data uploaded')
self._is_done = True
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(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 activate_high_level_commander(cf):
cf.param.set_value('commander.enHighLevel', '1')
def activate_mellinger_controller(cf):
cf.param.set_value('stabilizer.controller', '2')
def upload_trajectory(cf, trajectory_id, trajectory):
trajectory_mem = cf.mem.get_mems(MemoryElement.TYPE_TRAJ)[0]
total_duration = 0
for row in trajectory:
duration = row[0]
x = Poly4D.Poly(row[1:9])
y = Poly4D.Poly(row[9:17])
z = Poly4D.Poly(row[17:25])
yaw = Poly4D.Poly(row[25:33])
trajectory_mem.poly4Ds.append(Poly4D(duration, x, y, z, yaw))
total_duration += duration
Uploader().upload(trajectory_mem)
cf.high_level_commander.define_trajectory(trajectory_id, 0,
len(trajectory_mem.poly4Ds))
return total_duration
def run_sequence(cf, trajectory_id, duration):
commander = cf.high_level_commander
commander.takeoff(1.0, 2.0)
time.sleep(3.0)
relative = True
commander.start_trajectory(trajectory_id, 1.0, relative)
time.sleep(duration)
commander.land(0.0, 2.0)
time.sleep(2)
commander.stop()
if __name__ == '__main__':
cflib.crtp.init_drivers(enable_debug_driver=False)
with SyncCrazyflie(uri, cf=Crazyflie(rw_cache='./cache')) as scf:
cf = scf.cf
trajectory_id = 1
activate_high_level_commander(cf)
#activate_mellinger_controller(cf)
duration = upload_trajectory(cf, trajectory_id, figure8)
print('The sequence is {:.1f} seconds long'.format(duration))
reset_estimator(cf)
run_sequence(cf, trajectory_id, duration)
For the configuration I don't know which configuration you are talking about, the only configuration i did for the anchors is this :