Page 1 of 1

Controlling my crazyflie

Posted: Wed May 15, 2019 11:07 am
by RyanMco
Hi , How can I tell my crazyflie to just go up, then left and right, and then down (taking off) .. any clue?
I didn't find on the forum any reference that I can tell my crazyflie by python script to go up, left, right, down .. any help?

Re: Controlling my crazyflie

Posted: Wed May 15, 2019 12:03 pm
by arnaud
Hi, the motion commander api is what you are looking for. There is an example for it in the lib example folder: ...

Re: Controlling my crazyflie

Posted: Wed May 15, 2019 9:46 pm
by RyanMco
So how can I modify that with my multiranger? I mean I have attached my multiranger to my crazyflie and it's working all fine!, my problem now I would use this motion commander but how can I modify to it to let my multi ranger work through motion? thanks alot.

** multiranger is working all fine, I mean how can I let this script that you attached to recognize on my multiranger which can avoid obstacles while motion on that script

thanks alot

Re: Controlling my crazyflie

Posted: Thu May 16, 2019 6:02 am
by arnaud
Yes you can use the motion commander with the multiranger, it is what the multiranger_push demo is doing: ... py#L78-L79.

You can just add the line "with Multiranger(scf) as multiranger" and then you will be able to detect the distance to front facing obstacle with "multiranger.front" for example.

Re: Controlling my crazyflie

Posted: Sat May 18, 2019 1:15 pm
by RyanMco

Code: Select all

# -*- coding: utf-8 -*-
#     ||          ____  _ __
#  +------+      / __ )(_) /_______________ _____  ___
#  | 0xBC |     / __  / / __/ ___/ ___/ __ `/_  / / _ \
#  +------+    / /_/ / / /_/ /__/ /  / /_/ / / /_/  __/
#   ||  ||    /_____/_/\__/\___/_/   \__,_/ /___/\___/
#  Copyright (C) 2017 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
#  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.
This script shows the basic use of the MotionCommander class.
Simple example that connects to the crazyflie at `URI` and runs a
sequence. This script requires some kind of location system, it has been
tested with (and designed for) the flow deck.
The MotionCommander uses velocity setpoints.
Change the URI variable to your Crazyflie configuration.
import logging
import time

import cflib.crtp
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.syncCrazyflie import SyncCrazyflie
from cflib.positioning.motion_commander import MotionCommander

URI = 'radio://0/70/2M'

# Only output errors from the logging framework

if __name__ == '__main__':
    # Initialize the low-level drivers (don't list the debug drivers)

    with SyncCrazyflie(URI, cf=Crazyflie(rw_cache='./cache')) as scf:
	with Multiranger(scf) as multiranger:
		keep_flying = True
        # We take off when the commander is created
        with MotionCommander(scf) as mc:

            # There is a set of functions that move a specific distance
            # We can move in all directions


            # We can also set the velocity
            mc.right(0.5, velocity=0.8)
            mc.left(0.5, velocity=0.4)

            # We can do circles or parts of circles
            mc.circle_right(0.5, velocity=0.5, angle_degrees=180)

            # Or turn

            # We can move along a line in 3D space
            mc.move_distance(-1, 0.0, 0.5, velocity=0.6)

            # There is also a set of functions that start a motion. The
            # Crazyflie will keep on going until it gets a new command.

            # The motion is started and we can do other stuff, printing for
            # instance
            for _ in range(5):
                print('Doing other work')

            # And we can stop

            # We land when the MotionCommander goes out of scope
I used the previous script(in your second comment) updated with multiranger motion .. but my crazyflie isn't avoiding the obstacles .. although multiranger is working .. any help? in brief I want my crazyflie to do and travel with the previous script but to aviod also obstacles .. any help? I used "with Multiranger(scf) as multiranger:" command but it's not working on my crazyflie

Re: Controlling my crazyflie

Posted: Sat May 18, 2019 8:11 pm
by arnaud

There is not automatic obstacle avoidance implemented anywhere in the Crazyflie or in the lib, the multiranger deck and API allows you to get the distance to object and so to implement obstacle avoidance.

While you are running the trajectory you need to check the multiranger sensor values. You most likely will need to change a little bit the way the trajectory is run: send velocity setpoint and in a loop keep track of time as well as if there is any obstacle in sight. This is very much what the push demo is doing.

To go straight for about 50cm while stopping for an obstacle it would look something like that:

Code: Select all

start = time.time()
while multiranger.front > 20 and (time.time() - start) < 5:
    mc.start_linear_motion(0.1, 0, 0) # Go forward at 0.1m/s
mc.start_linear_motion(0, 0, 0) # Stop

Re: Controlling my crazyflie

Posted: Sun May 19, 2019 6:48 pm
by RyanMco
And I have another problem if my crazyflie is flying lets assume straight up to 0.4m from ground and I put something on the ground in its path on the ground , then my crazyflie fly and all gound until it reaches where I put something on the ground then it starts going crazy.

for example, if my crazyflyie was traveling/hovering above the ground 2m and in its way on the ground I put chair then my crazyflie since pass over the chair (pass it on the air) it starts flying crazy and falling down immediately like crazy movement .. so I guess my crazyflie isn't configured to the obstacles that he faces underneath it .. so what I should do in that case?! I put the multiranger but it monitors the left/right/above regions and now the underneath region of the crazyflie ..

any help please?

Re: Controlling my crazyflie

Posted: Tue May 28, 2019 4:48 am
by arnaud
There is a ranging sensor facing down on the flow deck and it is used for height control. In the current implementation the distance measured by the down facing sensor is considered as the absolute height, this means that if you add an obstacle on the bottom, your Crazyflie will jump up when you reach it. I am not sure why you are observing crashes though, maybe the obstacle is too big or you are hitting the ceiling?

There has been discussion on the forum of ways to use the up-facing sensor to track the ceiling instead of tracking the floor, as long as the ceiling is flat this is a solution for that problem, it currently requires to change the firmware, you can see the thread there: viewtopic.php?f=6&t=3444&hilit=range+up.

In theory you should be able to use other sensor to solve this problem too, looking at accelerometer and barometer to understand that the difference in down-range is an obstacle and not the absolute height of the Crazyflie changing. Though this is a non-trivial problem, and I am not sure how to implement it properly.