Python API

start_joystick
link_joy_button
take_off
stop
emergency
land
set_mode
set_formation
next_formation
prev_formation
inc_scale
dec_scale
toggle_ctrl_mode
go_to
get_positions
rotate_formation

Python API to control the swarm.

This module maked it possible to easily send command to the swarm through a Python script.

Example

# Formation exemple
swarm = SwarmAPI()

# Link joystick buttons to commands
swarm.start_joystick("ds4")
swarm.link_joy_button("S", swarm.take_off)
swarm.link_joy_button("X", swarm.land)
swarm.link_joy_button("O", swarm.emergency)
swarm.link_joy_button("T", swarm.toggle_ctrl_mode)

# Start swarm
swarm.set_mode("formation")
swarm.set_formation("v")

swarm.take_off()
rospy.sleep(10)

# Change formation
swarm.set_formation("pyramid")

SwarmAPI class

class swarm_api.api.SwarmAPI[source]

Python API class

start_joystick(joy_type, joy_dev='js0')[source]

Initialize joystick node. See here for a tutorial on how to add new joystick types.

Possible types:

  • ds4
Parameters:
  • joy_type (str) – Controller type.
  • joy_dev (str, Optional) – Specify joystick port. Defaults to js0
set_joy_control(to_control)[source]

To enable/disable control of formation position with joystick axes.

Parameters:to_control (bool) – If True, formation will be moved by joystick axes

Link a button to a function call

Parameters:
  • button_name (str) – Name of button, as written in joy_conf.yaml.
  • func (Callable) – Function to call
  • args (optional) – Function args. Defaults to None. Can be a single arg or a list of args
  • kwargs (dict, optional) – Function kwargs. Defaults to None.
Raises:

KeyError – Invalid button name

Example:

swarm.start_joystick("ds4")
swarm.link_joy_button("S", swarm.take_off)
swarm.link_joy_button("X", swarm.land)
take_off()[source]

Take off all landed CFs.

Modify take_off_height in swarm_conf.yaml to change take off height

Note

Will only take off landed CFs

stop()[source]

Stop all CFs

emergency()[source]

Call emergency srv of all CFs

land()[source]

Land all CFs at their starting position.

set_mode(new_mode)[source]

Set SwarmController control mode.

Possible modes are:
  • Automatic: CF will plot trajectory to new goals. Send go_to commands from python script
  • Formation: Swarm moves in formation. Formation position can be moved /w joystick.
Not implemented:
  • Pilot: Like CF client. No formation
  • Assisted: Control change of position /w joystick. No formation.

Note

Modes are not case sensitive

Parameters:new_mode (str) – New control mode
set_formation(formation_name)[source]

Set swarn formation

Parameters:formation_name (str) – New formation name
next_formation()[source]

Go to next swarm formation

prev_formation()[source]

Go to prev swarm formation

inc_scale()[source]

Increase formation scale by 0.5

dec_scale()[source]

Decrease formation scale by 0.5

toggle_ctrl_mode()[source]

Toggle control mode between absolute and relative.

In absolute: x, y, z are world axis

In relative: x, y, z depends on swarm orientation

go_to(goals)[source]

Move formation and/or cf to a position using the trajectory planner.

Dict format: "goal_name": [x, y, z, yaw] where "goal_name" is either "formation" or "cf_x"

X, Y, Z in meters, Yaw in rad.

Example::

# To move formation to [2, 2, 0.5, 1.57] swarm.go_to({‘formation’: [2, 2, 0.5, 1.57]})

# To move cf_0 to [0, 0, 0.5] and cf_1 to [1, 1, 1] goals = {} goals[“cf_0”] = [0, 0, 0.5, 0] goals[“cf_1”] = [1, 1, 1, 0] swarm.go_to(goals)

Parameters:goals (dict) – New goals
get_positions(cf_list=None)[source]

Get current position of crazyflies

If cf_list is None, will return position of all Cfs.

Parameters:cf_list (list, optional) – List of cf to read positions. Defaults to None.
Returns:Positions of CFs read. "{cf_id": [x, y, z, yaw], ...}
Return type:dict
rotate_formation(angle, duration)[source]

Rotate formation around it’s center

Note

Formation control with joystick must be False to use this function swarm.set_joy_control(False)

Parameters:
  • angle (float) – Angle to turn [deg]
  • duration (float) – Rotation duration [sec]