Hover using cflib

Firmware/software/electronics/mechanics
Post Reply
karishma2617
Beginner
Posts: 3
Joined: Thu Mar 15, 2018 9:38 am

Hover using cflib

Post by karishma2617 » Thu Mar 22, 2018 6:14 am

Hi,
I am just using a python code from cflib to communicate to the crazyflie, but hover command does not seem to work. Also I find lot of delay while communicating to the quad, about 2 secs. Any reasons for the same? Using some bit of mechanics, isn't the hover thrust equal to the m*g of the quad? If I simply pass on this much thrust, wont the quad hover? I have attached my code. Any input is very helpful since I am new to this field!
roll = 0
pitch = 0
yawrate = 0
thrust = 39000
crazyflie.commander.send_setpoint(0, 0, 0, 0)

for i in range(1,60):
# take off
if i < 10:
crazyflie.commander.send_setpoint(roll, pitch, yawrate, thrust)
time.sleep(0.1)
# for hovering
else:
thrust = 37900
crazyflie.commander.send_setpoint(roll, pitch, yawrate, thrust)
time.sleep(0.1)
print ('Sending data..', thrust)

arnaud
Bitcraze
Posts: 1750
Joined: Tue Feb 06, 2007 12:36 pm

Re: Hover using cflib

Post by arnaud » Thu Mar 22, 2018 7:55 am

Hi, I am not sure why you are experiencing delay. Can you maybe copy a fully working example (you can create a code block in the forum post to make to copy the code, keeping spaces is very important for python code :-)).

You code will definitely make the Crazyflie take-off, but not really hover. If you send a raw thrust and 0 as roll/pitch/yaw the Crazyflie will drift away.

To be able to hover the Crazyflie needs some information about its position or velocity in the room. One way to achieve that is with the Flow deck. When the Crazyflie has a flow deck installed, or when it knows its position with other means, you can use hover setpoint to set the height and the x/y velocity. For example: https://github.com/bitcraze/crazyflie-l ... ync.py#L59.

Ps. Since this is a new question I moved your post in a new topic.

karishma2617
Beginner
Posts: 3
Joined: Thu Mar 15, 2018 9:38 am

Re: Hover using cflib

Post by karishma2617 » Thu Mar 22, 2018 11:46 am

Hi,
I am planning to use Motion Capture system for closed loop control, however, the delay is causing the quad to behave in a bizarre way. I have attached my code for open loop only. If you could just tell me why the delay comes up. Also, can I use the estimatedZ value of the Log group, "posEstimatorAlt" to do a closed loop control? Now I feel Flow deck is playing an important role though!

Code: Select all

import logging
import time
from threading import Thread

import cflib
from cflib.crazyflie import Crazyflie

logging.basicConfig(level=logging.ERROR)

link_uri = "radio://0/50/2M"

cflib.crtp.init_drivers(enable_debug_driver=False)

crazyflie = Crazyflie()
crazyflie.connected.add_callback(link_uri)
crazyflie.open_link(link_uri)
print('Connecting to %s' % link_uri)

roll    = 0
pitch   = 0
yawrate = 0
thrust  = 37000 
crazyflie.commander.send_setpoint(0, 0, 0, 0)

for i in range(1,30):
# take off
    if i < 10:
        crazyflie.commander.send_setpoint(roll, pitch, yawrate, thrust)
        time.sleep(0.1)
# increase height
    elif i > 10 and i < 15:
        thrust = 38000
        crazyflie.commander.send_setpoint(0, 0, yawrate, thrust)
        time.sleep(0.1)
# for hovering
    else:
        roll    = 0
        pitch   = 0
        thrust = 36000 # just about enough to balance weight
        crazyflie.commander.send_setpoint(roll, pitch, yawrate, thrust)
        time.sleep(0.1)            
    print ('Sending data..', thrust)
    
Fland = thrust
while Fland > 20000: 
    Fland += -500
    crazyflie.commander.send_setpoint(roll, pitch, yawrate, Fland)
    time.sleep(0.1)
    print (Fland)
crazyflie.close_link()

arnaud
Bitcraze
Posts: 1750
Joined: Tue Feb 06, 2007 12:36 pm

Re: Hover using cflib

Post by arnaud » Wed Apr 04, 2018 9:32 am

If you want to close the loop with a Mocap outside the Crazyflie you should not use any of the position estimate from within the crazyflie, then using roll/pitch/yawrate/thrust packet is indeed the way to go, you will only get position and velocity feedback from your mocap in that case.

If you have a MOCAP, you most likely do not need the flow deck, unless you want to do experiment on the flow deck using the MOCAP as ground truth.

I ran your code and I am not sure I see any delay, it does not ballance my Crazyflie though :-) (it gets to the ceiling and go back down). Though, I can see a fundamental problem: you are not waiting for the Crazyflie to be connected to start sending commands, before the connected callback is called, the lib is fetching a lot of data and that can cause latency. I modified your code to wait for connection.

What are you trying to achieve? If you want to work on a position controller, your approach is good. However if you want to work on an autonomous Crazyflie the best is to push the mocap position to the Kalman filter and let the kalman filter calculate a position estimate, you will then be able to use the on-board position control loop. You can send external position with cf.extpos.send_extpos(). You will need to recompile the Crazyflie with "ESTIMATOR=kalman" in config.mk in order to force the kalman filter as default estimator.

Code: Select all

import logging
import time
from threading import Thread

import cflib
from cflib.crazyflie import Crazyflie

logging.basicConfig(level=logging.ERROR)

link_uri = "radio://0/45/2M"

cflib.crtp.init_drivers(enable_debug_driver=False)

connected = False
def on_connected(uri):
    global connected
    connected = True

crazyflie = Crazyflie()
crazyflie.connected.add_callback(on_connected)
crazyflie.open_link(link_uri)
print('Connecting to %s' % link_uri)

while not connected:
    pass

print('Connected!')

roll    = 0
pitch   = 0
yawrate = 0
thrust  = 37000 
crazyflie.commander.send_setpoint(0, 0, 0, 0)

for i in range(1,30):
# take off
    if i < 10:
        crazyflie.commander.send_setpoint(roll, pitch, yawrate, thrust)
        time.sleep(0.1)
# increase height
    elif i > 10 and i < 15:
        thrust = 38000
        crazyflie.commander.send_setpoint(0, 0, yawrate, thrust)
        time.sleep(0.1)
# for hovering
    else:
        roll    = 0
        pitch   = 0
        thrust = 36000 # just about enough to balance weight
        crazyflie.commander.send_setpoint(roll, pitch, yawrate, thrust)
        time.sleep(0.1)            
    print ('Sending data..', thrust)
    
Fland = thrust
while Fland > 20000: 
    Fland += -500
    crazyflie.commander.send_setpoint(roll, pitch, yawrate, Fland)
    time.sleep(0.1)
    print (Fland)
crazyflie.close_link()

karishma2617
Beginner
Posts: 3
Joined: Thu Mar 15, 2018 9:38 am

Re: Hover using cflib

Post by karishma2617 » Thu Jul 26, 2018 7:09 am

Hi,
I shifted to ROS and MATLAB for the implementation of the controllers and kalman estimator. Everything works fine for a single Crazyflie. Looking forward to control multiple via a single radio. Thanks again for the support! I figured out we cannot optimally stabilize real systems with a feed forward control which was what I was trying, we definitely need a feedback. Cheers :D

Post Reply