Introduction: Autonomous Line Follower Drone With Raspberry Pi
This tutorial shows that how you can make line follower drone eventually.
This drone will have an "autonomous mode" switch that will enter the drone to mode. So, you can still fly your drone as before.
Please be aware of that it is going to take time to build and even more time to adjust. But the final... is makes you think worth it.
To begin creating your own autonomus line tracker drone, make sure you have;
- Rasberry Pi 3 or Raspberry Pi Zero W with SSH access
- Ready-To-Fly drone with APM or Pixhawk flight controller
- Arduino Leonardo or another Arduino with fast clock speed
- At least 6 CH transmitter
- USB Webcam that Raspberry Pi and OpenCV supports
- A PC
- 6 general purpose transistors
- Wiring cables
Step 1: The Idea and Connections
APM, aka ArduPilot, is an flight controller based on Arduino Mega. This means we can modify it to be best for our case. But since I do not have the information to do so, I am going to go to follow another way.
Raspberry Pi's are, unfortunately, not time-sensitive which means cannot deal with PPM signals.
That's why we need the additional Arduino board.
In this way, Raspberry Pi will process the images and calculate the flight instructions and send it to Arduino via Serial UART interface. Arduino card will stand here as a PPM encoder/decoder, which encode the flight instructions to PPM signals that APM wants. To have an idea, you can examine the symbolic circuit diagram.
Raspberry Pi will behave as telemetry transmitter alongside detecting line.
The essential circuit is shown in the images. I will continue explaining in next steps.
Step 2: Raspberry Pi Connections and Configuration
Raspberry Pi will be connected to Wi-Fi Adapter (optional), USB Webcam, Arduino Leonardo via USB, APM via built-in serial interface. APM - RPI connection shown with details in the images.
To configurate, you have two options: pure Raspbian with needed packages or special image for MAVLink connection called APSync.
If you are going to use Raspbian, make sure you have installed these packages:
sudo apt-get update sudo apt-get install -y screen python-wxgtk3.0 python-matplotlib sudo apt-get install -y python-opencv python-pip python-numpy python-dev sudo apt-get install -y libxml2-dev libxslt-dev python-lxml sudo pip install future pymavlink mavproxy<strong> </strong>pyserial<strong> <br></strong>
In order to use Raspberry Pi's built-in serial interface, you must tell OS to not use it. To do so, type
and follow Interfacing options > Serial interface
You must disable serial interface but enable serial hardware.
At this point, the rest is suitable for both Raspbian and APSync.
In home directory, create three files: reboot script and image processor scriptt. Second line makes reboot script executable.
touch reboot.sh image_processor.py chmod +x reboot.sh<br>
Copy the all lines in files given below to your home directory ( /home/pi ) in Raspberry Pi.
The reboot script will contain triggers that's going to trigger image processor and telemetry scripts. Also few settings. Note that if you don't want telemetry feature, add # before that line.
#!/bin/bash python3 /home/pi/image_processor.py
Save it with CTRL+O and exit with CTRL+X. Last step about it is registering it to OS startup file, rc.local
sudo nano /etc/rc.local
Append this line above exit 0:
Our reboot script will be executed on every boot.
We want Raspberry Pi to record live video, process it on-the-fly, calculate flight instructions, send it to flight controller and be telemetry. But since Raspberry Pi is not able to generate PPM signal that APM wants, we need another way to accomplish it.
Raspberry Pi will send its image processing output to Arduino (in my case Arduino Leonardo) via Serial Port. Arduino will generate PPM signal from that input and send it to Flight Controller via jumper wires. This is all for Raspberry Pi.
Let's move on to next step.
Step 3: APM Connections and Configuration
The things about APM is simple since it is already ready to fly. We need to know serial ports' baudrates, and make sure that TELEM port is enabled.
In your ground software, in my case Mission Planner, check the flight controller's parameters list and findout the baudrates. For example, SERIAL_BAUD is USB baudrate and SERIAL_BAUD1 is TELEM port baudrate for APM. Note that values.
The most important part is INPUT pins' connections. As shown in the image, connect Arduino's digital pins 4 thorough 9. You may want to use a breadbord for this, since we are going to add some transistors and receiver outputs. (See images) (Transistors will be working in case that you want to take control of your drone)
ARD 4 ↔ APM INPUT 1
ARD 5 ↔ APM INPUT 2
ARD 6 ↔ APM INPUT 3
ARD 7 ↔ APM INPUT 4
ARD 8 ↔ APM INPUT 5
ARD 9 ↔ APM INPUT 6
Connect all 5V pins on APM Input to Arduino Leonardo 5V pin. Likewise connect all APM Input GND pins to Arduino Leonardo GND pin.
Step 4: Arduino Leonardo Configuration
We have connected all the wires for Leonardo so only the code left.
Upload the given code below to your Arduino Leonardo. Pay attention to baudrates.
Step 5: First Flight
When you are done with all previous steps, it means you are ready.
Power up all cards and connect with SSH to Raspberry Pi. Type in terminal:
sudo su mavproxy.py --master=/dev/[SERIAL INTERFACE] --baudrate [TELEM PORT BAUDRATE] --aircraft [CUSTOM NAME<br>
Default Raspberry Pi built-in serial interface is ttyS0 ( /dev/ttyS0 )
Default APM TELEM port baudrate is 57600
Default APM USB port baudrate is 115200
You can give any name to your aircraft, choose it wisely, so you can recognize it later.
If everything is okay, now connect to your Raspberry Pi via VNC, so you can watch what drone sees in real time.
Now, you can arm your drone. Exciting, right?
Take your drone off, and fly above the line track. Now, you can activate line tracking mode by using CH6 switch.
Participated in the
Make It Fly Challenge
Question 4 months ago
how will we power the RPi form APM and how its circuit look
3 years ago
I quite like the idea of using CH6 as a switch between the flight modes. Maybe l will implement that too in one of my projects. Nice project!
Reply 3 years ago