(Sorry, Automatic Google Translation)

Raspberry PI + Arduino = TwichDrone

Introduction

English readers note: This blog entry in English, but I provide a " README_ENGLISH.txt " with the installation procedure in english. Also see the section " For the Impatient " to learn how to boot the Raspberry with the custom image. For any question, feel free to contact me. See the video of the drone working at the bottom of the page.

If you want to skip the whole roll and have the drone running as soon as possible go to the section " For the Impatient "It is in English, but it is well understood.Although I recommend that you read everything and do an installation from scratch.You can see the video of the drone running at the end of the entrance.

Well I've been trying to integrate a raspberry pi with an arduino ... and since I've been playing RainbowSix for a while, I thought ... and why do not we make a drone of those that use the characters to look for the position? So I decided to start the project ... TwichDrone !. For those who have not played RainbowSix, I explain briefly: players when they attack have an initial phase to locate the target, and they use a radio controlled cars to locate it. To handle it join a mobile device that could be a phone or a tablet. Twich (one of the characters) has a somewhat special one that launches downloads with a taser. This is the look you have in the game:


Obviously, the drone is not going to give downloads (because I have not put, things as they are, hehe). The pint that has it physically is this, so that you have an idea of what I want to implement:

The Drone of Twich "really"


As you see in the model, the drone is powered by two engines and has wheels that allow it to move sideways. It also has "infrared" leds and a camera system. In my implementation I will not use this type of wheels, because they are very expensive and the idea is that it is cheap to build. On the other hand, we are not going to use the infrared illuminators either (although it would have been easy to have a few LEDs). And of course, it does not give electric shocks.

Our TwichDrone. Close enough!


For the Impatient

This section explains how you get your TwichDrone up and running quickly. I recommend that you read all the documentation, if you are english reader, start with README_ENGLISH.txt file. If you're brave enough, let's start. Remember you need: Arduino UNO, Raspberry Pi 3 Model B, 2 DC motors, and a battery. Optional to GoPro3.

1. Set ARDUINO.

You need to install Nanpy Firmware in your arduino. Follow these steps:

  1. Download ARDUINO IDE for your platform. Install it.
  2. Download the Nanny Code and prepare the sketch. Issue these commands.
  3. Git clone https://github.com/nanpy/nanpy-firmware.git
    Cd nanpy-firmware
    Cp sample_cfg.h Nanpy / cfg.sh
  4. Connect the Arduino UNO via USB to your computer. If using a clone with a CH340 / CH341 usb2serial, see the blog for the required drivers for your platform (Linux works, mac & windows need drivers).
  5. Copy Nanpy directory under your "sketchbook" Arduino IDE directory, start your Arduino IDE, open Sketchbook / Nanpy, select the right board (Arduino UNO) and "Upload". Done.

2. Mount the hardware.

  1. See below " Block Diagram "
  2. See below " Part III Hardware Assembly "

You should mount the motors with the right polarity (see photos) and build a custom wiring to power the raspberry. Then, mount everything into the desired platform, connect all the things.



3. Download raspberry image.

I prepared the raspberry, with everything working. Just download the TwichDrone_Raspberry_Image to your disk, and burn it into the SD. Yo will ned at least 4.4 GB free, only the 8GB card will do the trick.


Run the following command (check directories and sd device)


Gzip -dc TwichDrone_Raspberry.img.gz | Dd of = / dev / yoursdcardhere

4. Configure your GOPRO (if you are planning to use it)


The image comes with the following settings:


AP_SSID: TwichDrone01
AP_IP: 192.168.2.1
AP_PASSWD: rainbow6
GOPROWIFI_SSID: GOPROR6
GOPROWIFI_PASSWD: rainbow6


You need to configure the GOPRO3. Go Here and choose a method (via Capture APP or via Web Update). If you do not set the SSID and PASSWD to the given values, the GOPRO3 will not work.


5. Start & Enjoy

Start the raspberry PI3, wait 15-35 seconds to load, and then look for the wifi TwichDrone01 with your portable device (phone / tablet). Login with the given password, and point your browser to the 192.168.2.1. You should be there. Enjoy.



Analysis and Objectives

Well, let's build a gossip of these, see if we can. Before we start buying things and programming like crazy, it is best to define goals and establish a plan of action, to make things more or less clear. The objective of this science project is to build a drone that meets the following:

Well, there are not that many things either. Now we will approach each one of them, to see how we solve it. The idea is not to spend a lot of money on the project , so let's pull a simple platform (nothing to buy an RC kit) and adapt it for our needs. The reuse comes in the thread that I have a raspberry pi and some arduino, and I feel like integrating them together. Also I have a motor shield, so honey over flakes. At the time of controlling the device I feel like trying to make an HTML interface for the control, so that we can control the drone from almost any device. Which brings us to that the link has to be WIFI (could be bluetooth, but this is simpler). To have a real-time view , we're going to use the raspberry camera, and as a bonus pack, we're going to mount a gopro with which to make cool recordings. Make it simple. If we have to do a complex development, this loses grace. So let's try to program everything in python and HTML (with some JS). And of course, doing everything from scratch having to implement it , nothing to buy a kit.

Let's do it. HW & SW diagrams.

Broadly speaking, I'm going to use the following: a raspberry pi 3 that will take care of the control and interface of the camera. An arduino with a motor shield to take care of the motors. A 7.4v lithium battery (2S) along with a BEC to get clean 5V (main power), an inexpensive arduino amazon engine kit, and a custom development in Python for backend control, , And another in HTML + JS for the graphical control interface (frontend). To test it all I will develop a simulator in python to adjust some things.

Blocks diagram

In the previous image we have the hardware block diagram of the Drone. As we can see, the power is taken from a LIPO 2S battery, which provides us 7.4V. As this voltage is too high to feed the Raspberry, which is also very sensitive, we use a BEC (uBEC) to transform those 7.4V into 5V. These devices are widely used in RC and support a fairly high voltage input range (also with enough intensity). In our case, we used a Zippy Flightmax 1600mAh battery (2S1P 20C). The BEC used is a 5V X5Pro with an output load of 5A (enough for this). To switch on / off we have used a switch that switches the positive line. From here a direct line is drawn to the motorshield feed (to move the motors). The 5V output feeds the Raspberry, which in turn will power the arduino (5V) via USB. It is necessary to CUT the "Vin Connect" bridge of the motorshield plate ( see this link ). In this way, the arduino power is independent of the motor shield (5V vs 7.4V).



Finally, the motors are connected to the motor shield using the corresponding pins, as we will see later. The raspberry camera connects using the native girdle to the CSI port, as we will see. The GoPro is controlled by Wifi, so it appears without any wiring.

Software Block Diagram

On the other hand it is clear that we have to put some software to make all this work. The idea is simple; Use the Raspberry PI 3 as the main computer, and the arduino for motor control. To connect we will use WiFi, taking advantage of a feature that has the Raspberry, and is acting as AP (Access Point).

Choosing the TwichDrone Access Point



So to connect to it, just look for the WiFi of the Drone ( TwichDrone01 ) on our tablet / phone and connect. This will assign us a dynamic IP (DHCP) and allow us to connect to the drone using the configured IP ( 192.168.2.1 ). Do not get nervous that there will come a detailed document on how to assemble all this step by step.

Putting the drone ip into the mobile browser

Well, once connected to the Drone network, just point the browser to the web server (192.168.2.1) from which will load the HTML interface that allows us to control the drone.

The web interface loaded and working. Yes that's me.


This interface is quite simple, on the one hand, has a joystick on the bottom right that allows us to handle the drone (forward, back, rotate). A button at the bottom left allows us to enable / disable recording of the goPro. In the upper left we have a "console" that shows us the status of the drone (consumption of the motors, battery status of the gopro and whether or not recording) and finally, full screen is the image of the camera Of the raspberry, updated in real time.

Console log detail



The interface performs two-port communication. On the one hand, the HTTP standard (80) by which the webpage receives, the "stream" of the image and the JavaScript. On the other hand, we use port 8000 to open a webSocket connection to the control server (controlserver.py) which is in charge of controlling the drone (through controlling the arduino for the engines, and the gopro to activate / deactivate the recording ). This server is implemented in Python, and uses a couple of libraries, as we will see later.

Part I. Setting the Raspberry

The first thing we have to do is configure the Raspberry. For the more prisillas I have generated an image of the raspberry of the drone that I have currently running, so it should be worth to boot and play with the drone. But to document everything, it is best to make the configuration step by step to understand how the issue works. To find the necessary products see the Shopping List below


The first thing we need is a Raspberry Pi 3 Model B, with an SD card with at least 8 GB. To install and configure it, you need these things:


If you plan to do it all at once and you want to prick the camera, you need the Raspberry PI CAM 2.1 (the second version is necessary because it has another sensor (I am IMX219 of 8MP) and it is managed differently.

1. BURN THE IMAGE, START THE RASPY

The distribution we are going to use is the DEBIAN JESSIE (RASPBIAN) that you can download from here , of course. I have used the one that brings the GUI (Pixel) because it is more comfortable, although later we are going to disable it. You download the image to your PC, and burn it to the card. I am in MacOS so I have used dd . In linux it is similar.


How to toast:

  1. Look for the disk ( df -h ) (/ dev / disk6s1 3.7Gi 4.1Mi 3.7Gi 1% 0 0 100% / Volumes / TWICHDRONE)
  2. Unmount: sudo diskutil unmount / dev / disk6s1
  3. Decompress the image
  4. You put it with dd: s udo dd bs = 1m if = 2017-03-02-raspbian-jessie.img of = / dev / rdisk6
  5. We connect the HDMI, the keyboard and the mouse.
  6. We bounce and wait until the prompt exits (the partition of the card is reset)


Default user: pi (passwd raspberry)
Superserver (ROOT): root (no password, use sudo su -)


At this point you should have the raspberry ripped to the PIXEL (GUI) desktop without any problems.

2 INITIAL SETTINGS (NETWORK, ETC).

The image is compressed to fit a small card (occupies 4.4 GB) if you want to use all the available space on the partition (for example, because you put the image on a 16GB card) you have to expand the filesystem. You throw these two commands as root, and outside, after bouncing you will have.


Raspi-config --expand-rootfs
Shutdown -r now


The first thing we need is to have an operational internet outlet. Use the Ethernet cable to prick raspberry to your router, or connect via WiFi. The thing is to be able to go online. Better the cable, because we are going to be messing with WiFi.


We install the following packages:


Now we update the firmware of the raspberry. It takes about 10 minutes, do not get nervous.


We launch this command as root.


After updating the firmware and bouncing, if we launch this command, we will see that the camera module is loaded correctly.


Once the firwmare is updated, we can configure the network. Remember, we want the Raspberry to function as an AP (Access Point) to allow us to connect to the drone from the mobile / tablet and a WiFi client to control the GoPro (versions 3, 4 works differently).


For this, we have to configure dnsmasq, the network interfaces, and the firewall to do masquerading. The network configuration of the drone is as follows:


We started to configure. Uncomment and edit these lines in /etc/dnsmasq.conf


Interface = lo, uap0
No-dhcp-interface = lo, wlan0
Dhcp-range = 192.168.2.100,192,168.2.200,12h


Edit /etc/hostapd/hostapd.conf and where you put the variable below, put these parameters:


SSID_OF_AP = TwichDrone01
SAME_CHANNEL_NUMBER_AS_WIFI_CLIENT = 1
WIFI_AP_PASS = rainbow6



SAME_CHANNEL_NUMBER_AS_WIFI_CLIENT has to be THE SAME as the WiFi channel you want to connect to, because otherwise it does not work. Look at the WIFI channel with WifiAnalyzer, for example, to check the channel. In the case of the gopro it is easy, because it is the 1. (if you want to connect to the Wi-Fi from your house instead of to the gopro, you have to put the same number)


Interface = uap0
Ssid = [SSID_OF_AP]
Hw_mode = g
Channel = [SAME_CHANNEL_NUMBER_AS_WIFI_CLIENT]
Macaddr_acl = 0
Auth_algs = 1
Ignore_broadcast_ssid = 0
Wpa = 2
Wpa_passphrase = [WIFI_AP_PASS]
Wpa_key_mgmt = WPA-PSK
Wpa_pairwise = TKIP
Rsn_pairwise = CCMP


Edit and add this to the / etc / network / interfaces


Auto uap0
Iface uap0 inet static
Address 192.168.2.1
Netmask 255.255.255.0


Create a new file / usr / local / bin / hostapdstart and add:

Iw dev wlan0 interface add uap0 type __ap
Service dnsmasq restart
Sysctl net.ipv4.ip_forward = 1
Iptables - t nat - A POSTROUTING -s 192.168.2.0/24! -d 192.168.2.0/24 -j MASQUERADE
Ifup uap0
Hostapd /etc/hostapd/hostapd.conf


Change the permissions of / usr / local / bin / hostapdstart


Chmod 667 / usr / local / bin / hostapdstart


Edit and add this line to /etc/rc.local


Hostapdstart> 1 &


Add this to the / etc / network / interfaces file (remove all references to the wlan0 adapter) and change [SSID_OF_NETWORK_TO_CONNECT], [PSK_KEY_OF_NETWORK_TO_CONNECT] to the values of the network you are interested in. (You can connect to more networks, but remember that the channel has to be the same).


Auto wlan0
Iface wlan0 inet dhcp
Wpa-ssid [SSID_OF_NETWORK_TO_CONNECT]
Wpa-psk [PSK_KEY_OF_NETWORK_TO_CONNECT]


Replace PSK_KEY_OF_NETWORK_TO_CONNECT with this:


SSID_OF_NETWORK_TO_CONNECT: SSID network to connect (eg GOPROWIFI)
PSK_KEY_OF_NETWORK_TO_CONNECT: wpa_passphrase GOPROWIFI PasswordEnTextoPlano


The wpa_passphrase command returns three fields. Stay with the longest. Something like this:
A03133ea3333471b0d33dbd1b2b19233294649968537c35904eb3389a7df65ba (eg wpa_passphrase GOPROR6 rainbow6)


We bounced the Raspberry, and at this point, you should be able to connect to the TwichDrone through your WiFi network. Check it with a mobile phone or tablet. What we see in the GoPro right now:

  1. Start the GoPro with the selected WIFI configuration, and turn on WiFi (remote app mode)
  2. Restart the Raspberry (so check in the log that boots and connects well)
  3. After booting, you should have the network well configured with this pint:


# Ifconfig -a
# En0 -> cable [Adapter IP: if plugged, DHCP assigned]
# Wlan0 -> GoPro Wifi [Adapter IP: 10.5.5.x]
# Uap0 -> Ap Wifi [IP Adapter: 192.168.2.1]

  1. Now we ping the gopro, which always has the same IP: ping 10.5.5.9
  2. We can verify that we can access your HTTP port http://10.5.5.9:8080
  3. In fact, we can see the live stream with a browser: http://10.5.5.9:8080/live/amba.m3u8


And with this we have verified that we can access the raspberry as an access point, and that the raspberry correctly access as a client to the WiFi of the GOPRO. Note about GOPRO: only works with GOPROs that have this type of WIFI interface, ie, 3 and 3+. The 4 works in a different way, so this only works with GOPRO 3 and 3+.


Now, they are going to configure the PI for DEVELOPING. Turn off the gopro for
Now, change the WLAN to your WIFI AP, or use a cable to connect (better).

2. CONFIGURATION OF SYSTEM PARAMETERS, PASSWORDS, SSH AND RSYNC.

In this section we configure some parameters to go smoother the Raspberry, and set up a couple of services to be able to connect to raspberry download files in a more comfortable way. Needless to say if you do not plan to touch anything, SSH and RSYNC you can skip.


Edit /etc/sysctrl.conf and add:


Vm.swappiness = 15
Vm.vfs_cache_pressure = 50
Vm.dirty_background_ratio = 15
Vm.dirty_ratio = 20
Vm.min_free_kbytes = 16384


Edit /lib/systemd/system/networking.service.d/network-pre.conf
(If you do not find it, believe it, but it should be) and add:


[Service]
TimeoutStartSec = 15


This way you do not wait 60 seconds to search for a WiFi network in case you do not find it.


Now launch the raspi-config script and configure the following options:

  1. Starting the CLI
  2. Set the keyboard in your language (ES)
  3. Autologin to Pi User disabled (request login)
  4. Set the time zone (Europe / Madrid, GMT + 1)
  5. Enable these services (disable the rest): camera, ssh, serial
  6. Rename host to TwichDrone01
  7. It establishes the country of the Wifi (for the channel)


Now go to the shell and set the user password pi:


#sudo su -
#passwd pi [pon rainbow6]


Edit / etc / ssh / sshd_config and add "UseDNS no". This way you do not get "hung" waiting for the login. (Solving DNS). Bounce the raspberry, and you should be able to get into the Raspberry.


From here, this is optional and is only used in case you are going to walk up and down files to the raspberry. Basically this is used to configure ssh access with certificate, and not to ask for password (which facilitates rsync). SSH access must work, so try it from your computer before. Once it works:


1) In the raspberry we do this:
#ssh -l pi 192.168.2.1
Log in (this creates the .ssh directory)


2) In your development team:
# Ssh-keygen -t rsa (passphrase and empty password)
#cat .ssh / id_rsa.pub (copy content)


3) We turn to the raspberry and do this:
# Cd .ssh (/ home / pi)
# Vi authorized_keys [paste the contents of the .ssh / id_rsa.pub]


4) If we test on the development team:
# Ssh -l pi 192.168.0.163 (you should not ask for a password)


3. Other Software.


3.1 Installation of the MJPEG-STREAMER


After testing with NINGX-RTMP, UVL4, FFMPEG, AVCONV, VLC and GStreamer, I found that the best solution is MJPEG-Streamer. Why? Because we need very low latency and not too high image quality. Pure streaming video solutions do not work because:

  1. The adroid video needs coding in WebM or MP4 to see it inside the video tag
  2. The latency is very high.

So after multiple tests, we ended up using MJPEG-STREAMER, which is an original MJPEG-STREAMER fork that serves JPEGS images continuously. So we in HTML what we do is use a tag Instead of the tag.


To install it:


# Apt-get install cmake libjpeg8-dev
# Git clone https://github.com/jacksonliam/mjpg-streamer.git
# Cd mjpg-streamer-experimental
Make


After compiling it launches easy:


# Export LD_LIBRARY_PATH =.
# ./mjpg_streamer -o "output_http.so -w ./www" -i "input_raspicam.so -x 768-y 432 -fps 25"


3.2. Python WebSockets


In order to implement the dron control services, I have used a WebSockets implementation for python 2.7. Python3.0 brings it native, but since we were using 2.7 ... well.
I use simple-websocket-server , a quick, lightweight and easy-to-use implementation. ( See your github for more details ). The installation is simple:


#pip install git + https: //github.com/dpallot/simple-websocket-server.git


3.3 ControlServer and Simulator


These two programs in python is what I have developed for the control of arduino engines. They are written in Python2.7 and have some nice things, for example supports nonlinear mapping curves (power and direction), angle of rotation adjustment, etc. In addition the controlServer works decoupled from the input, which allows to check the status with a certain frequency, asynchronously.


The simulator is basically a console in pygame that shows the various physical parameters of the drone, and allows us to control a simulation from the web interface. Thanks to this simulator it was possible to optimize the turning angles, the applied power, etc. In addition it allowed to write the code of control before having mounted the drone.

Simulator.py running.

The web interface is quite simple. It presents a web page that has a background img tag and through a bit of javascript and using the NippleJS library the commands are captured and mapped to the server through WebSockets.


The best thing is that if you are curious you give a return for the code of controlserver.py and simulator.py .


Part II. Interface with the Arduino

To control the motors, we will use an Arduino Uno with its Motor Shield, which is what I had at home.

Arduino Uno "clone" with the CH341


In addition, the arduino that we are going to use, is a "clone" that is characterized to be very cheap, to use a serial driver based on the chip CH341, which means that we have to install a driver in our operating system for Connect to the arduino. The good news is that in linux and raspberry it's natively supported, but in MacOS and Windows we need the driver. C'est la vie

Arduino MotorShield R3


The Arduino Motor shield that we are going to use is the original, revision 3. This shield is mounted on top of the Arduino and allows us to control two DC motors, or else one step by step. It does not have much mystery. The motors are connected in the blue terminals that are seen in the bottom left of the photo, along with its feeding. It is necessary to CUT the "Vin Connect" bridge of the motorshield plate ( see this link ). In this way, the arduino power is independent of the motor shield (5V vs 7.4V). If we do not cut the bridge, we fry the arduino, because we are going to feed it externally.


Once the shield is mounted on top of the arduino, we have to install the NanPy on the raspberry Y on the Arduino. Nanpy allows to control via python all the components of the arduino (via serial port on the USB). For this reason, we have to download the firwmare of NanPy in the Arduino.


The first thing is that if you are in MacOS, you need the drivers; The Captain , Sierra . If you are in Windows, then these are yours. Downloads, and install them.


In the raspberry we do not have to do anything, because the preinstalled packages come. What we have to do is put the firmware in the Arduino, and we can do this from the development team.


The first thing is to install the Arduino IDE for your platform. We need to compile the code and download it to the arduino. Download and install it. Do not open it yet.


From a shell, we execute the following:

Git clone https://github.com/nanpy/nanpy-firmware.git
Cd nanpy-firmware
Cp sample_cfg.h Nanpy / cfg.sh

Now we connect the Arduino UNO via USB to the development team. We check that the IDE works and we can download a simple sketch (eg blink). When we have made sure that this works, we close the IDE and:


Part III. Mounting Hardware.

To mount the drone, the first thing that we have to do is a suitable container to put all the electronics. I have used a tupper of the mercadona, in particular of ice cream. You can use the base on which the wheels come (see below) or a sealed suitcase ... I did not want to complicate it because it was a test. That yes, the Tupper painted it in black, to make it more apparent.

Box

Actually, the assembly of the drone is quite simple, and the only thing we have to do is to enter all the components. In my case I had to drill the box to remove the wheels and screw the motors since I did not use the plate that comes with the kit. Here everyone who does a little bit what will fit him. The kit I bought in amazon includes a battery holder, engines, wheels and a jockey wheel. As you can see in the photo, the plate can be used to carry out the assembly. It will not be so cool, but it's faster. The battery holder and the cables do not use them.

Car kit


Mounting the motors is important to find the correct polarity. In my case, I have put them so that the terminals go into the box, and can thus correctly identify the positive and negative poles. In our assembly, engine B is left and engine A is right. It is important to establish this correctly because if not then the control code will not correctly identify the polarity and the motors, and it will not work.

Right motor A

As you can see in the photos the top terminal is positive (RED wire) and the lower terminal is negative (BLACK wire).

Left Motor B

These terminals are connected to the terminals of the Arduino SHIELD marked for this purpose in the photo. Let's go, B to the top (marked B) and A to the middle (marked A) respecting the polarity. The one at the bottom of the whole (lower left) is the current input.

Motor connections to arduino shield

The next step is to connect the arduino with the raspberry, this is easy, because it is done through the USB cable. In this way the arduino is fed, and the control commands are passed to him by series. And there is little left.



Now is the time to solder another time and create the power system. I have used a LIPO 2S (7.4V) battery. To connect it I have used a standard connector XT60 (the orange that the photos are seen). As these connectors are very hard, I have inserted a switch that switches the positive not to walk removing and putting the connector. The wiring diagram is as follows:

Electric scheme.



Remember that the shield power is taken BEFORE the UBEC since we need 7.4V. The UBEC converts the voltage to 5V, which is the one we use to power the Raspberry. To do this, we weld the UBEC output to a male MiniUSB connector that in theory can then be inserted into its connector. I was not able to, so I put heat shrink and hot glue. Be careful it is delicate.

How to connect the power pins to the MiniUSB connector.

And with that we have the electrical connections. We have to connect the MiniUSB to the raspberry, it is to the camera (which is made with the CSI belt, it is simple but with care) and the USB that connects with the arduino. In the end it has to be something like this:

The Raspberry with the Power Switch, USB, Camera and Power


And this is the cable nightmare you see inside. As you will see is not so much. You see elUBEC (lost sticker) and connector XT60. What looks blue below the UBEC is the LIPO battery.

Internal Wiring

Just turn it on, connect the phone / tablet to the WiFi of the Drone, and point the browser to the address http://192.168.2.1 This should be running smoothly.


If you want more detailed information on the installation process at hand, you can consult the file README_ENGLISH.txt

In Github you can find the whole code along with the corresponding installation image.



Shopping list


These are the main components and where to buy them.


http://blog.capitanpenurias.com/2017/04/raspberry-pi-arduino-twichdrone.html