Swarm Python not working as expected

Discussions about autonomous flight in general, regardless of positioning method
Post Reply
purvisb
Beginner
Posts: 13
Joined: Sat Apr 10, 2021 12:59 am

Swarm Python not working as expected

Post by purvisb »

I'm trying to get two CF 2.1 drones to fly forwards and backwards in the air. They are using flow decks

For some reason, it seems like each action (liftOff, hover, setDown, etc) happens twice on each device. This is not the behavior I expect.

One consequence of this is that when it goes to setDown, it ramps down the propellers until they stop (as expected), then it starts the setDown action again which brings the propellers back to hovering thrust and ramps them back down again while on the floor - causing them to crash.

Can someone take a look at this code and let me know why this is happening? And perhaps how else I should approach this?

Code: Select all

import math
import time

import cflib.crtp
from cflib.crazyflie.swarm import CachedCfFactory
from cflib.crazyflie.swarm import Swarm

# Change uris according to your setup
URI1 = 'radio://0/80/2M/E7E7E7E7E5'
URI2 = 'radio://0/83/2M/E7E7E7E7E7'

uris = {
    URI1,
    URI2
}

#MOVEMENTS
def liftOff(cf):
        print("\n+++Lift off+++" )
        for y in range(10):
            cf.commander.send_hover_setpoint(0, 0, 0, (1-(1.1**(-y)))/2.5)
            time.sleep(0.1)
            
def setDown(cf):
        print("\n---Set down---")
        for y in range(10):
            #cf.commander.send_hover_setpoint(0, 0, 0, (10 - y) / 25)
            cf.commander.send_hover_setpoint(0, 0, 0, (1.1**(-y))/2.5)
            time.sleep(0.1)
            
def hover(cf):
        print("\n===Hovering===")
        for _ in range(20):
            cf.commander.send_hover_setpoint(0, 0, 0, 0.4)
            time.sleep(0.1)
            
def moveForward(cf):
        print("\n^^^forward^^^")
        for _ in range(20):
            cf.commander.send_hover_setpoint(0.5, 0, 0, 0.4)
            time.sleep(0.1)      

def moveBackwards(cf):
        print("\n\/\/\/Backwards\/\/\/")
        for _ in range(20):
            cf.commander.send_hover_setpoint(-0.5, 0, 0, 0.4)
            time.sleep(0.1)     
            
def stop(cf):
        print("\nXXX_Stop_XXX")
        cf.commander.send_stop_setpoint()
        time.sleep(0.5) 
#END MOVEMENTS

def reset_estimator(scf):
    print("reseting state estimation")
    cf = scf.cf
    cf.param.set_value('kalman.resetEstimation', '1')
    time.sleep(0.1)
    cf.param.set_value('kalman.resetEstimation', '0')
    time.sleep(2)

def sequence(scf):
    cf = scf.cf

    liftOff(cf)
    hover(cf)
    moveForward(cf)
    hover(cf)
    setDown(cf)
    stop(cf)
    liftOff(cf)
    hover(cf)
    moveBackwards(cf)
    hover(cf)
    setDown(cf)
    stop(cf)

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

    factory = CachedCfFactory(rw_cache='./cache')
    with Swarm(uris, factory=factory) as swarm:
        swarm.parallel_safe(reset_estimator)
        print("\n############flying############")
        swarm.parallel_safe(sequence)
        
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Swarm Python not working as expected

Post by arnaud »

Hi,

just looking at the script I cannot see anything obviously wrong. One nit pick is that you will get better performance by putting both your Crazyflies on the same channel but different address. So for example by setting URI2 to radio://0/80/2M/E7E7E7E7E7.

What print-out do you get from your script, are the function only called once per Crazyflie?

What version of the lib are you using?

Unfortunately I do not have access to 2 Crazyflie right now to test.
tharunkumar_a1
Beginner
Posts: 1
Joined: Mon May 23, 2022 1:36 pm

Re: Swarm Python not working as expected

Post by tharunkumar_a1 »

How to connect a multiple crazy flie in one crazyradio and how many crazyflie can connect in a one crazyradio.

thanks in advance.
kimberly
Bitcraze
Posts: 1050
Joined: Fri Jul 06, 2018 11:13 am

Re: Swarm Python not working as expected

Post by kimberly »

@tharunkumar_a1

Please start a new thread for your problem as you are not the original poster and your problem is not related to this thread.

Just a notice, we are on our way to transfer to github discussions, so you can also start the thread there is you want to: https://github.com/orgs/bitcraze/discussions
Post Reply