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 Apr 17, 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.

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)[08-Aero-Network-and-System-Administration#internet-access].
  • 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

cd
pip install dronekit

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