Controlling thrust using crazyflie-lib-python

Post here to get support
Post Reply
thecountoftuscany
Beginner
Posts: 4
Joined: Tue Dec 08, 2020 2:52 am

Controlling thrust using crazyflie-lib-python

Post by thecountoftuscany »

Hi,

I want to control crazyflie's thrust using crazyflie-lib-python and the bluetooth dongle. I am using this minimal example from the user guide:

Code: Select all

import cflib.crtp
from cflib.crazyflie import Crazyflie
import os
import time
import logging


## Only output errors from the logging framework
logging.basicConfig(level=logging.ERROR)


if __name__ == '__main__':
    cflib.crtp.init_drivers(enable_debug_driver=False)

    cf = Crazyflie(rw_cache=os.path.expanduser("~") + "/.cache")
    cf.open_link('radio://0/13/2M/E7E7E7E7E7')
    time.sleep(3)  # give some time to establish connection; 3 sec seems to work

    # Send thrust of 30000 for 3 seconds
    for i in range(300):
        cf.commander.send_setpoint(0.0, 0.0, 0, 30000)
        time.sleep(0.01)  # send the setpoint once every 10 ms as per the user guide
    cf.commander.send_stop_setpoint()

    cf.close_link()
I can connect to the crazyflie (cf.is_connected() returns True) and I can also see the green/red (Tx/Rx) LED on the crazyflie go on as expected, but the propellers do not spin, no matter what thrust value I send for whatever amount of time. I did get the high level MotionCommander working (with syncCrazyflie) as expected but cannot get the thrust control (from Commander) to work. Please let me know if I am doing something wrong.

Thanks!
thecountoftuscany
Beginner
Posts: 4
Joined: Tue Dec 08, 2020 2:52 am

Re: Controlling thrust using crazyflie-lib-python

Post by thecountoftuscany »

Just found the solution from reading a reply in another thread that seemed relevant.
Apparently, I just needed to send a thrust value of 0 first before sending any non-zero value as arnaud mentioned in that thread.
There is a protection in the Crazyflie that requires you to send thrust=0 before the thrust commands are accepted.
Just made this change to my code:

Code: Select all

    ....
    cf.open_link('radio://0/13/2M/E7E7E7E7E7')
    time.sleep(3)  # give some time to establish connection; 3 sec seems to work

    # Send thrust=0 first so that the crazyflie-firmware's safety protection requirements are met
    cf.commander.send_setpoint(0.0, 0.0, 0, 0)
    time.sleep(0.01)

    for i in range(300):
    ....
Seems to be working now. Just saving this here in case someone else encounters the same problem.

A suggestion: it would be helpful to add this information to https://github.com/bitcraze/crazyflie-l ... l-commands so that people trying to read the docs know what to do immediately. I can open a pull request if necessary.

Thanks!
thecountoftuscany
Beginner
Posts: 4
Joined: Tue Dec 08, 2020 2:52 am

Re: Controlling thrust using crazyflie-lib-python

Post by thecountoftuscany »

The speeds of the individual motors still seem to vary based on the roll/pitch angles using this method of thrust control. Is there a way to just command fixed speeds for the motors irrespective of the roll/pitch angles (using crazyflie-lib-python)?
arnaud
Bitcraze
Posts: 2391
Joined: Tue Feb 06, 2007 12:36 pm

Re: Controlling thrust using crazyflie-lib-python

Post by arnaud »

Thanks for updating the thread with the solution! Where did you find this example code?

Sending thrust with this command is sending a thrust and attitude setpoint to the Crazyflie controller, so this activates the controller that will actively try to maintain the requested attitude.

Individual motors PWM can be controlled using parameters. The variable "MotorPowerSet.enable" has to be set to 1 and then "MotorPowerSet.m1" to "MotorPowerSet.m4" can be set to the raw PWM value from 0 to 65535.

I updated the documentation to talk about the different watchdog and locking mechanisms.
thecountoftuscany
Beginner
Posts: 4
Joined: Tue Dec 08, 2020 2:52 am

Re: Controlling thrust using crazyflie-lib-python

Post by thecountoftuscany »

I just found the functions to be used from the user guide and then wrote the bare minimum code.

Setting those parameters seems to do exactly what I want. Thanks a lot!
Post Reply