Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

04 Autonomous drone programming in Python

Paul Guermonprez edited this page Jul 8, 2017 · 37 revisions

Python is a scripting language highly appreciated in the world of scientific and robotics software developers. Examples of Python libraries of interest:

  • 3DR released DroneKit-Python, to manage the Flight Controller over MAVLink protocol.
  • PyOpenCV, to handle simple computer vision.
  • PyVISP, to facilitate advanced computer vision.
  • ROSPy, to manage the ROS robotics framework from Python.

That's why we think it is a good solution to prototype quickly and efficiently.

But the choice is yours. You have access to the board as root and can code in the language of your choice. We also document C/C++ programming following the Yocto Project methods, as it may be interesting for production embedded engineers.

Here's a simple python script using the basic pymavlink wrapper to arm the motors for 3 seconds. Arming the motors is the simplest action we can test to show everything is connected. Note: we're using tcp:127.0.0.1:5760 to connect to the flight controller, as we'll do for all the following examples.

UNPLUG THE PROPELLERS BEFORE RUNNING THIS CODE. WE INSIST.

#!/usr/bin/python
from __future__ import print_function

import pymavlink.mavutil as mavutil
import sys

mav = mavutil.mavlink_connection('tcp:127.0.0.1:5760')
mav.wait_heartbeat()
mav.mav.command_long_send(mav.target_system, mav.target_component,
                          mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, 0, 1,
                          0, 0, 0, 0, 0, 0)
sleep(3)
mav.mav.command_long_send(mav.target_system, mav.target_component,
                          mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, 0, 0,
                          0, 0, 0, 0, 0, 0)

It’s important to know the basics of MAVLINK, as it the base of all communications with the Flight Controllers. But coding frames with python-mavlink is not developer friendly. DroneKit, developed by (3D Robotics)[http://3drobotics.com], is one of the friendly python abstractions available under Apache v2 Licence : (python.dronekit.io)[http://python.dronekit.io] To install on Intel Aero:

pip install dronekit

UNPLUG THE PROPELLERS BEFORE RUNNING THIS CODE. WE INSIST.

Here's the code, still arming the motors for 5 seconds:

#!/usr/bin/python
from dronekit import connect, VehicleMode, LocationGlobalRelative
import time

vehicle = connect('tcp:127.0.0.1:5760', wait_ready=True)
print "Arming motors:"
vehicle.mode    = VehicleMode("GUIDED")
vehicle.armed   = True
while not vehicle.armed:
        print "  Waiting for arming to be finished"
        time.sleep(1)
print "Keeping motors armed for 5s"
time.sleep(5)
print "Disarming"
vehicle.armed   = False

We'll present samples for two setups:

  • Intel Aero Ready To Fly Drone, running Yocto as provided and supported by Intel. The RTF kit includes the Intel Aero Flight controller and the R200 Intel Real Sense 3D Camera.
  • Intel Aero Compute board, where you could install Ubuntu/Debian/... (but it comes with Yocto, like the RTF kit). Ubuntu is not currently supported by Intel on Aero and is only partially working on this platform. The Compute Board does not include a flight controller, but you can use a simulated software "SITL" flight controller, and plug the FC of your choice. We'll add various USB devices using the USB OTG port, like RealSense cameras and USB modem.

Flight controller connectivity:

  • On the RTF kit, the FC is accessible with /dev/ttyS1 and a linux service mavlink-router is exposing this serial port as a network port 5760 (mavlink-router is also sensing data to QGroundControl over wifi). From DroneKit or the mavlink library, connect to "tcp:127.0.0.1:5760".
  • On the Compute Board, you could run a SITL FC. This software would typically listen on port 5760.

Login:

  • Yocto: The default setup on Aero is to propose a minimal terminal on the HDMI output, and have aero working as a wifi acces point. It's good enough for initial setup but not convenient for programming: you may want to work from your workstation and have internet access to install packages on aero. We suggest you connect your board to internet using your wifi router using this procedure.
  • Ubuntu: you can login graphically on aero itself, and use the desktop interface of ubuntu to configure, setup the internet access and start coding with the editor of your choice. Or you could login with ssh from your workstation after having configured the network.

Yocto on the RTF Drone

It is already installed (check /usr/lib/python2.7/site-packages). You can install additional packages using pip, if you have internet access.

Ubuntu on the Compute Board

sudo apt-get install python-pip python-dev python-numpy python-opencv python-serial python-pyparsing python-wxgtk3.0
git clone https://github.com/dronekit/dronekit-python.git
sudo pip install dronekit
sudo pip install dronekit-sitl
sudo pip install MAVProxy

Flight Controller Hello World

Intel Aero RTF Kit : Intel Aero Flight Controller on Yocto

On the Yocto build, the flight controller is accessible from /dev/ttyS1 and the malink-router is exposing this serial port as a network service on port 5760. That's why (on the RTF Drone with Yocto) your code should connect to the FC using "tcp:127.0.0.1:5760".

from dronekit import connect, VehicleMode

connection_string = "tcp:127.0.0.1:5760"
print("Connecting to vehicle on: %s" % (connection_string,))
vehicle = connect(connection_string, wait_ready=False)
# Please note: wait_ready=False

print "Get some vehicle attribute values:"
print " GPS: %s" % vehicle.gps_0
print " Battery: %s" % vehicle.battery
print " Last Heartbeat: %s" % vehicle.last_heartbeat
print " Is Armable?: %s" % vehicle.is_armable
print " System status: %s" % vehicle.system_status.state
print " Mode: %s" % vehicle.mode.name    # settable

vehicle.close()
print("Done")

Intel Aero Compute Board : SITL on Ubuntu

The Intel Aero Compute Board does not have a Flight Controller included, unlike the Intel Aero RTF drone. But we can first use a simulated flight controller "SITL", and you'll integrate the flight controller of your choice later. Here's how to run the (DroneKit demo)[http://python.dronekit.io/examples/vehicle_state.html] script:

cd dronekit-python/examples/vehicle_state
python vehicle_state.py

Intel Aero board LEDs

There's a multicolor LED on top of the board (if the board is in the enclosure, you can see the light from the white cable hole), and an orange LED under the board. And here is a sample code to test all the colors:

import time
from periphery import GPIO

print "Top LED Blue"
gpio = GPIO(403, "out")
gpio.write(bool(1))
time.sleep(1)
gpio.write(bool(0))
gpio.close()

print "Top LED Green"
gpio = GPIO(397, "out")
gpio.write(bool(1))
time.sleep(1)
gpio.write(bool(0))
gpio.close()

print "Top LED Red"
gpio = GPIO(437, "out")
gpio.write(bool(1))
time.sleep(1)
gpio.write(bool(0))
gpio.close()

print "Bottom LED Orange"
gpio = GPIO(507, "out")
gpio.write(bool(1))
time.sleep(1)
gpio.write(bool(0))
gpio.close()

CAN

The Intel Aero Compute Board includes a MCP2515 CAN controller and MCP2562 CAN transceiver. The controller is connected to the Atom processor via the SPI interface on bus 1 (SPI1) chip select 0 (CS0). It can be accessed via spidev as /dev/spidev1.0. Python spidev libraries