Autonomous Drone Using RPi
Autonomous Drone Using RPi
by Jay_Ali
Over the last decade, the commercial UAV industry Vision algorithm to find another way out.
has grown exponentially. Nowadays UAVs are being
used for mapping, infrastructure analysis, navigation, Once you get the hang of how a drone can be
food and package delivery, photography and film controlled using external sensors, you can just go on
making, pest spraying and much more. an implement your own codes like following red color
car, reading ARUCO markers for inventory
Also during this decade we have seen Alexa and Siri management in a warehouse, using thermal camera
taking over our daily lives, providing automation to to assist firefighters regarding the best pathway inside
our day to day routine. Like dimming out the lights, the burning building….
playing songs, calling mom and even reading out
recipes while we cook. So go on and conquer the sky, cause sky isn’t the
limit.
Autonomous Drone Using RPi: Page 1
limit.
Similarly if we can fuse this concept of automation The method described in this instructables can be
with UAV, wouldn’t that be awesome? implemented on planes, VTOL, Hexacopter,
Octacopter, Quadcopter, helicopter, and basically
So here it is, an instructable that focus on how you anything being controlled with Pixhawk flight
can make a drone autonomous (level 3). This controller. The reason for using quad in this case is
instructable is only meant to get you started with the because its one of the easiest one to get started with.
concept. Consider this the “hello world” example of
autonomous drones. In order to get started with you’ll need an ArduPilot
supported flight controller (APM/Pixhawk/Pixhawk
In this tutorial, all we’ll be doing is integrating a 2.1). The reason for this is because the code/platform
LiDAR with our drone so the drone gets a capability is open-source and doesn’t need developer license.
to detect an obstruction in front of it and plan an This allows average person (like me) to grow,
action accordingly. In this tutorial we'll land once we understand, explore and educate others regarding the
see an obstacle in front of our vehicle. You might new horizon.
wanna go beyond that like increasing altitude to go
over the obstruction, scan area using Computer
So first thing you need to do is get yourself a weight of the objects that you'll be using (frame, ESC,
ArduPilot UAV. If you already have one of them then motors, propellers, battery, Flight controller, receiver).
you can skip forward. Just make sure it has enough Using the weight list verify (using motor thrust table)
room to carry Raspberry Pi (Zero/ Zero- that your combination of motor and propeller would be
Wireless/Model 3 B/ Model 3 B+). able to lift of the total weight at 50% throttle. If yes,
then you're heading in the right direction.
So you don’t have a vehicle to work on. That’s okay.
Literally just Google DIY Pixhawk quadcopter and Anyway, I will not be covering on how to select parts
you’ll find a lot of build guides and configuration and for your quadcopter. Finding a perfect combination
you’ll even find whole kit available on Amazon, Ebay can be tiring. It is an iterative process. To be honest
and AliExpress. with you guys, I have even spent almost 2 months on
a hexacopter configuration, even after having some
For those of you who are still curious on how to select experience with selection of items.
parts, just draft up a chart and start writing down the
If you need some place to start then what I went Electronic Speed Controller (ESC) - EMax BLHeli
through while selecting the parts might help you. 30A (alternate: SimonK ESC having >25A limit - with
BEC so you can power up your quad)
ecalc.ch, an online calculator which helps you select
motor-prop combination, lets you estimate battery Battery - Gens Ace 3s (11.1V) 5000mAh 45C LiPo
time and range, and much more. (alternate: capacity can vary from 3000-6000 at
~40C, remember bigger battery increases the weight
Ecalc helps you to have an idea on how to select - effecting the flight time (battery capacity is not
parts by playing around with different configurations. directly proportional to flight time!!!) You have to find
Though in real world, ecalc data generally deviates by the sweet spot.
15%. As long as you are a beginner, this would work
fine, by if you're thinking of making a drone for your Flight Controller - Pixhawk 2.1 Cube with Here
company to do a geological survey, then you might GNSS (alternate: Pixhawk 1 with M8N GPS)
wanna think again.
Transmitter and receiver (for manual flight, initial
Anyway, this is what I did for my built: calibration and taking manual control if anything
goes wrong) - Taranis FrSky X9D+ with Taranis X8R
Frame - GF 400 (alternate: F450 or any other frame (alternate: Flysky FS-i6 with FS-iA6B)
that weights around 300g and can support 10" prop)
Putting together the part is like adult version of LEGO
Motors - DJI 2212 920KV (alternate: Any BLDC 2212 blocks. Its easy and fun (as long as you don't mess
motor having KV rating from 920-1100) up the polarity of the ESC)
2 3
1. Raspberry Pi
2. Pixhawk 2.1
3. 2212 920KV motors
So now you have assembled a quadcopter with the user friendly and as a matter of fact they literally
right wiring. guide you through the whole setup, with Mission
Planner giving a more user configurable platform and
Cool, now lets begin configuring initial parameters so QGroundControl providing a cleaner user interface.
it can fly. For that download and install Mission Still if you need help, there are a lot of tutorials on
Planner on you PC. setup.
Mission Planner is open source software that can be My favourite and easiest to follow is Pixhawk series
used (and is generally used) to configure Pixhawk by Painless360.
related items. Mission Planner is available on
Windows and Linux. Mac users can use QGround If you need help then do see this video series, it'll
Control instead. help you a lot.
Okay so now you have a quad that flies using RC, So assuming you know how to use RPi (visit here for
and you want want to make it fly on its own… First how to setup RPi), download and install APSync
thing you need is a Raspberry Pi (with a PiCam for image on your RPi (download)
video (optional)). Preferably one with wireless WiFi
connectivity. APSync will let your RPi work like a WiFi hotspot.
The default WiFi name is 'ardupilot' and password:
In my setup I used Raspberry Pi Model 3 B which has 'ardupilot'. More information onhow to install here.
built in Wi-Fi. This makes it very easy to SSH into the
device. Don’t worry if you have a model which doesn’t Connect your PC to this network. Once connected,
support Wi-Fi. Using a Wi-Fi dongle will work too at open up you web browser and type 10.0.1.128:8000
the expense of a USB port. in address bar. Use this page to video streaming
on/off using this webpage (If you have attached RPi
The big idea is to transmit and receive real time flight camera)
data on your Ground Station (PC or Mobile phone)
and also receive and view live video and run python
scripts to control your drone without manual RC input.
Once connected with that WiFi acces point, use PuTT Now you’re in... so you're now in a command
y to SSH into your Raspberry Pi. Use IP address: terminal. Write 'pip list' to ensure you have dronekit,
10.0.1.128 and Port: 22 MAVProxy and pymavlink installed (It is a part of
APSync package
The default SSH login details are username: apsync
and password: apsync
The connections are quite simple. The Pixhawk 2.1 has multiple UART which we can use to interface with our Pi.
Using a custom cable to connect RPi UART (GPIO 14 and GPIO15) to TELEM2 (/dev/ttyAMA0 or
/dev/ttyS0)
Using custom cable to connect TELEM2 to RPi's USB port through a converter (/dev/ttyUSB0)
Using a micro-USB cable to connect Pixhawk directly to RPi's USB port (/dev/ttyACM0) - Not
recommended
To make sure that you're connecting to the right port, use the command ls /dev/tty*
Use the Telemetry cable that comes with the Pixhawk and splice it to add male headers to the other end.
Remember Tx on one device goes to Rx at the other end and vice versa.
Now that you have everything setup, all you have to do is run the python code you made.
First copy the python script you made on the Pi. Open the terminal and navigate to the directory where you saved
the file. Then simply type the following command
python your_file.py --connect /dev/ttyAMA0 (/dev/ttyAMA0 will be replaced with whatever comes at your end)
This command will control your vehicle, but in order to view real time in flight data and fly using python script, open
two SSH terminal windows. First type in
Here replace XX with the assigned IP of the devices where you want to view it (PC/Phone or even both). Also
10.0.1.128 is the IP of RPi in ardupilot network. If everything went good then you'll be receiving flight data. In order
to fly your quad using python, use the other terminal window to run the following command
Now you are ready to fly your drone using a custom script. But first lets make one.
This is a very simple code to initialize your drone and make it hover at a place. So in this hover and land code, all
you do is:
You can also connect a LiDAR to measure the distance and avoid obstacles. Instructions on how to connect it can
be found here. Also here is a raw code for measuring distance, if distance is below a threshold then braking and
landing.
Add functions and make your own code to suit your needs.
https://dronekit-python.readthedocs.io/en/latest/
Download
https://www.instructables.com/ORIG/FPC/RV0T/K02OX0NW/FPCRV0TK02OX0NW.py
…
Download
https://www.instructables.com/ORIG/FMF/8CM9/K02OX0O8/FMF8CM9K02OX0O8.py
…
Before you conquer the skies with your python scripts, I highly recommend that you should setup a software
simulator.
//www.youtube.com/embed/BsqCdB_2enw
I, Jawwad Zahed, have been working as full time judging panel, be we didnt quit. Our project,
researcher at National University of Sciences and Autonomous drone, got the highest marks and was
Technology (NUST) for a year. Building, flying and even nominated for gold medal.
testing UAVs is not just my hobby, its my job as a
researcher. I have been around autonomous drones During this 3-year journey, Rashad Reyaz, a junior of
for 3-years. mine, helped me a lot. He is the team leader for
NUST Airworks. NUST Airworks recently won
Accompanying me is Sohaib Ahmed Jalali, my friend IMechE UAS Challenge 2019 and were declared
and also my colleague here at NUST. Back in 2015 Grand Champions this year.
we had this dream of making flying robots, and by
the end of our degree program we were able to Its surprising that how far we have come....
achieve it. During our Final Year Project, we faced a
lot of criticism and negative comments from the