Hover mode using code

Firmware/software/electronics/mechanics
Post Reply
islamoc
Beginner
Posts: 23
Joined: Sat Nov 05, 2016 5:59 pm

Hover mode using code

Post by islamoc »

Code: Select all

import sys
sys.path.append("../lib")

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

# Initialize the low-level drivers (don't list the debug drivers)
cflib.crtp.init_drivers(enable_debug_driver=False)

print("Scanning interfaces for Crazyflies...")

if True:
    # Create a Crazyflie object without specifying any cache dirs
    cf = Crazyflie()

    def handle_connected(link_uri):
        print("Connected to %s" % link_uri)

        print("Sending thrust 45000")
        cf.commander.send_setpoint(0, 0, 0, 45000)
        #time.sleep(0.75)
        print("Stopping thrust; hovering")
        cf.commander.send_setpoint(0, 0, 0, 32767)
        cf.param.set_value("flightmode.althold", "True")

    def close_link():
        print('Closing')
        cf.commander.send_setpoint(0, 0, 0, 0)
        time.sleep(0.1)
        cf.close_link()

    # Connect some callbacks from the Crazyflie API
    cf.connected.add_callback(handle_connected)

    link_uri = "radio://0/80/250K"
    print("Connecting to %s" % link_uri)

    # Try to connect to the Crazyflie
    cf.open_link(link_uri)

    # Variable used to keep main loop occupied until disconnect
    is_connected = True

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        close_link()
else:
    print("No Crazyflies found, cannot run example")
This is a little code for activating althold mode but it is not working any updates on this ?
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Hover mode using code

Post by arnaud »

Hi,

Just tested with the latest firmware from master branch and this example is working! Maybe you where experiencing a bug we had recently in altitude hold in the firmware.

/Arnaud
islamoc
Beginner
Posts: 23
Joined: Sat Nov 05, 2016 5:59 pm

Re: Hover mode using code

Post by islamoc »

I compiled the latest firmware but still it is not working I wanted to hover for 10 seconds and it does not it keeps falling
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Hover mode using code

Post by arnaud »

The altitude-hold is working for me, but your code is not setting it to fly for 10 second.

I suggest you start from the ramp.py example: it starts a thread on connection that runs a sequence of actions. In the thread you can set alt-hold, rise for a while, stay stable for 10 seconds, go low for a while, and then disconnect.
islamoc
Beginner
Posts: 23
Joined: Sat Nov 05, 2016 5:59 pm

Re: Hover mode using code

Post by islamoc »

I have put this code inside ramp_motors method in the ramp example

Code: Select all

self._cf.param.set_value("flightmode.althold", "True")
        self._cf.commander.send_setpoint(0, 0, 0, 45000)
        time.sleep(0.75)
        print("Stopping thrust; hovering")
        self._cf.commander.send_setpoint(0, 0, 0, 32767)
        #self._cf.param.set_value("flightmode.althold", "True")
        time.sleep(10)
        self._cf.close_link()
      
Still it is not hovering for 10sec
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Hover mode using code

Post by arnaud »

Thanks for the example code, I played a bit with it and got various results.

I ended up with this:

Code: Select all

        # Unlock startup thrust protection
        self._cf.commander.send_setpoint(0, 0, 0, 0)

        self._cf.param.set_value("flightmode.althold", "True")
        for i in range(20):
            self._cf.commander.send_setpoint(0, 0, 0, 45000)
            time.sleep(0.1)
        print("Stopping thrust; hovering")
        for i in range(50):
            self._cf.commander.send_setpoint(0, 0, 0, 32767)
            time.sleep(0.1)

	print("Landing ...")
        for i in range(30):
            self._cf.commander.send_setpoint(0, 0, 0, 20000)
            time.sleep(0.1)

        self._cf.param.set_value("flightmode.althold", "False")
        self._cf.commander.send_setpoint(0, 0, 0, 0)
        # Make sure that the last packet leaves before the link is closed
        # since the message queue is not flushed before closing
        time.sleep(0.1)
        self._cf.close_link()
One important things is to send commander packets at regular intervals, otherwise the commander watchdog in the Crazyflie will turn off the motors.

For some reason this code works almost only after restaring the Crazyfie, this looks like a bug.

Also, the pressure tends to move a lot, so it does not work all the times. Altitude hold has almost only been used as an assistance for manual flight, not for autonomous. There seems to need some improvement to use it reliably that way.

/Arnaud
Post Reply