[SOLVED] Simple Hover Program

Post here to get support
Post Reply
mrmks
Beginner
Posts: 13
Joined: Fri Jun 03, 2016 7:08 pm

[SOLVED] Simple Hover Program

Post by mrmks »

Hi,

I am trying to run the simple hover program from another thread, but the parameter 'flightmode.althold' is not being set.
From 'basicparam.py', I can see the 'flightmode' parameter with althold as the first option. How can I make the hover program work?

Here is the hover program output:

Code: Select all

hold
sending initial thrust of 0
putting in althold
Unable to find variable [flightmode.althold]
Cannot set value for [flightmode.althold], it's not in the TOC!
Traceback (most recent call last):
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/miles/projects/crazyflie-lib-python/examples/alt_hold.py", line 13, in <module>
    cf.param.set_value("flightmode.althold","True")
  File "/home/miles/projects/crazyflie-lib-python/cflib/crazyflie/param.py", line 265, in set_value
    raise KeyError('{} not in param TOC'.format(complete_name))
KeyError: 'flightmode.althold not in param TOC'
and here is the code for the hover program:

Code: Select all

import time

from cflib.crazyflie import Crazyflie
cf = Crazyflie()

print("sending initial thrust of 0")

cf.commander.send_setpoint(0,0,0,0);
time.sleep(0.5);

print("putting in althold")
cf.param.set_value("flightmode.althold","True")

print("Stay in althold for 7s")

it=0

while it<700:
    cf.commander.send_setpoint(0,0,0,32767)
    cf.param.set_value("flightmode.althold","True")
    time.sleep(0.01)
    it+=1

print("Close connection")
cf.commander.send_setpoint(0,0,0,0)
cf.close_link()
Last edited by mrmks on Tue Sep 27, 2016 5:33 pm, edited 1 time in total.
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Simple Hover Program

Post by arnaud »

Hi,

You are not connected to a crazyflie, this is why you are getting this error. You should connect first before being able to send setpoint and set parameters. You can look at the ramp.py example to see how to connect and send commands to the Crazyflie: https://github.com/bitcraze/crazyflie-l ... es/ramp.py
mrmks
Beginner
Posts: 13
Joined: Fri Jun 03, 2016 7:08 pm

[SOLVED] Re: Simple Hover Program

Post by mrmks »

Thanks! Altitude hold is beautiful now, takes off from a bench and stays about 15 cm above.

Here's the code I used:

Code: Select all

import logging
import time
from threading import Thread

import cflib
from cflib.crazyflie import Crazyflie
from cflib.crazyflie.log import LogConfig

logging.basicConfig(level=logging.ERROR)

class AltHoldExample:

    def __init__(self, link_uri):
        """ Initialize and run the example with the specified link_uri """

        self._cf = Crazyflie()

        self._cf.connected.add_callback(self._connected)
        self._cf.disconnected.add_callback(self._disconnected)
        self._cf.connection_failed.add_callback(self._connection_failed)
        self._cf.connection_lost.add_callback(self._connection_lost)

        self._cf.open_link(link_uri)
        self.is_connected = True
        # Variable used to keep main loop occupied until disconnect
        print('Connecting to %s' % link_uri)

    def _connected(self, link_uri):
        """ This callback is called form the Crazyflie API when a Crazyflie
        has been connected and the TOCs have been downloaded."""
        print('Connected to %s' % link_uri)
        # Start a separate thread to do the motor test.
        # Do not hijack the calling thread!
        Thread(target=self._hover_test).start()

    def _connection_failed(self, link_uri, msg):
        """Callback when connection initial connection fails (i.e no Crazyflie
        at the specified address)"""
        print('Connection to %s failed: %s' % (link_uri, msg))

    def _connection_lost(self, link_uri, msg):
        """Callback when disconnected after a connection has been made (i.e
        Crazyflie moves out of range)"""
        print('Connection to %s lost: %s' % (link_uri, msg))

    def _disconnected(self, link_uri):
        """Callback when the Crazyflie is disconnected (called in all cases)"""
        print('Disconnected from %s' % link_uri)
        self.is_connected = False

    def _hover_test(self):
        print("sending initial thrust of 0")

        self._cf.commander.send_setpoint(0,0,0,0);
        time.sleep(0.5);

        print("putting in althold")
        self._cf.param.set_value("flightmode.althold","True")

        print("Stay in althold for 7s")

        it=0

        while it<700:
            self._cf.commander.send_setpoint(0,0,0,32767)
            self._cf.param.set_value("flightmode.althold","True")
            time.sleep(0.01)
            it+=1

        print("Close connection")
        self._cf.commander.send_setpoint(0,0,0,0)
        self._cf.close_link()


if __name__ == '__main__':
    # Initialize the low-level drivers (don't list the debug drivers)
    cflib.crtp.init_drivers(enable_debug_driver=False)
    # Scan for Crazyflies and use the first one found
    print('Scanning interfaces for Crazyflies...')
    available = cflib.crtp.scan_interfaces()
    print('Crazyflies found:')
    for i in available:
        print(i[0])

    if len(available) > 0:
        le = AltHoldExample(available[0][0])
    else:
        print('No Crazyflies found, cannot run example')
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: [SOLVED] Simple Hover Program

Post by arnaud »

Great that it worked and thanks for sharing your code!
/Arnaud
weiyuh3
Beginner
Posts: 1
Joined: Tue Nov 19, 2019 9:29 pm

Re: [SOLVED] Simple Hover Program

Post by weiyuh3 »

Hi!

I tried the codes attached, my my crazyflie keeps shifting during flying. How did you solve it? Thanks for answering.
Post Reply