Sunday 27 December 2015

K9.1 Arduino / Raspberry Pi Web Controlled / Autonomous Robot





Introduction (Updated 26/12/15)
This is my first ever attempt at building a web controlled robot with built in autonomous mode. The project started out as an Arduino Nano based collision avoidance robot built to inspire Primary School children and develop an interest in computing and electronics.
At first the robot was a simple collision avoidance robot to which I then added a Raspberry Pi. Attached to the Pi was a PIR motion detector and a Logitech C270 Web Cam, all mounted on top. I put together a small python program using fswebcam to take pictures using the Web Cam whenever the PIR motion detector detected motion, which was all the time that the robot was moving. The still pictures were later replaced with a live video stream, accomplished using a python program called MJPG-Streamer.
After a lot of research the Pi was connected to the Nano over an i2c serial (two wire) interface to serve a Web Page that could be used to send and receive commands to control the movement of the robot and view the video stream.
This is still a work in progress and is far from finished but I thought it was about time I created a blog on the build. If I get time at a later date I will do a step by step, in the mean time feel free to ask me questions and if I can i'll help.

Parts list:
1. Raspberry Pi Model B
2. Logitech C270 Web Cam
3. Arduino Nano AT-Mega328P (Chinese copy)
4. PIR Detector (Redundant in the latest build)
5. EasyAcc 10000mAh USB Battery Pack (To power the Raspberry Pi)
6. LiPo 7.4v Rechargeable Battery
7. WiFi Dongle
8. HC-SR04 Ultrasonic Sensor Module
9. L298N Dual 'H' Bridge Motor Driver Board
10. 9g Micro Servo
10. Perspex Chassis (with 2 x DC Motors and Caster Wheel)
11. Foxes biscuit and G Shock watch tin
12. Male to Male and Male to Female Jumper Cables
13. Solderless Breadboard

The Build
Initially, as I mentioned above, the robot started out in life as a result of the need to build a collision avoidance robot for a school robotics club. The idea was to identify parts cheap enough to make the building of a robot affordable for club members who were in Y5 and Y6 at the Primary School at which I am currently training. The initial robot was based on an Arduino Nano AT-Mega328P, to keep the costs down. It also incorporated a Perspex Chassis Kit that included 2 x DC Motors and a Castor Wheel. Collision detection was established using an Ultrasonic Sensor and a Dual H Bridge Motor Driver used to control the motors. Power was initially provided from 1 x PP3 9v battery (no research done on batteries and power requirements at this point ).
This initial plan was for the Ultrasonic Sensor to be mounted on the 9g Micro Servo however the poor and un-researched battery choice proved inadequate to drive everything with the servo attached.
With the servo omitted the robot functioned admirably and proved to be a favourite and remained the version purchased and built by the children.

Power Requirements
As mentioned above the robot was initially powered by a 9v PP3 battery. This was fine for powering just the motors and ultrasonic sensor however, in order for this version of the robot to have a ultrasonic sensor mounted on a rotating servo it would need a better power supply. After researching power requirements on Let's Make Robots, I was advised to ditch the 9v PP3 (which incidentally are designed for very low power consumption items like Smoke Detectors) and opt for AA batteries, preferably rechargeable with around a 2000mAh rating. I was told to use 6 as rechargeable AA output around 1.2v as opposed to 1.5v output by the non- rechargeable type.
Another option that I was told to pursue was to use Lithium Polymer (LiPo) batteries, commonly used amongst the robotics fraternity. Whilst the batteries are reasonably priced (about £11 for a 2 Cell Pack - 7.4v) you will also need a Balance Charger which are expensive. This is a high initial outlay but is a one off cost.

In the end I purchased a 7.4v (Turnigy) LiPo Rechargeable Battery:

And an Imax B6ACBalance Charger:

It is also recommended that you get a LiPo charging bag which is fire proof, these batteries are pretty safe but if one is damaged and is charged they can be akin to light a firework in your house!!
To use this battery with this charger I had to solder on a different connector on the end of the battery.
Details of how the battery is connected is covered below.
The Raspberry Pi is powered using a USB Rechargeable Power Pack, in my case I used an EasyAcc 10000mAh pack which when fully charged last for ages (not a technical unit of measure but you get my drift).

Chassis Upgrade
To give it a body and change the visual aspect of the robot I mounted a Foxes biscuit tin on top of the perspex chassis and housed all of the electronics inside of of it. The Ultrasonic Sensor was mounted inside a tin that came with a new Casio G Shock. The tin was cut using a mix of Tin Snips, Scissors and a Stanley Knife, once cut be careful as the edges are razor sharp, I sanded them down but also covered them with PVC tape.

Microprocessors
At the time of writing this robot has two microprocessors, a Raspberry Pi and an Arduino Nano. The Arduino Nano drives the DC Motors and the Ultrasonic Sensor and the RPi provides the WiFi / Web Control, runs a web camera and has ability to further enhance / expand this robot.
The Nano and the RPi are connected using an ic2 two wire connection, the Nano is setup as a slave device and the RPi is the master. Using the Nano to drive the motors and sensors in this way avoids tying up the Pis GPIO pins which ensures expandability.
As a beginner, setting up the i2c took a great deal of research and although it works the motor control still needs fine tuning.

Electronics
The 2 x DC Motors are connected to an L298N H Bridge Motor Driver Board, these boards enable the control of DC Motors to drive them forwards and backwards. The L298 also comes with an onboard 5v voltage regulator. The L298 is an inexpensive option but does suffer from a large voltage drop so make sure your battery is more than big enough.
The Battery is connected to the L298 via a simple Single Pole Single Throw (SPST) on / off switch. The L298 then provides 5v Power to the solderless breadboard which in turn provides power to the HC-SR04 Ultrasonic Sensor, the 9g Micro Servo on which the sensor is mounted and the Arduino Nano.

How it Works
The RPi is setup with remote access and once powered up I log into it using SSH (Secure Shell) and start the Python program which acts as the i2c master program to control the Arduino. From within the Python program a bash script is run which starts another Python program to serve a Web Page using WebIOPi a Python program for Web Control, this also calls another Python Program, mjpg-streamer which switches on the Web Cam and streams mjpg frames that are viewed on the web pages served by WebIOPi. The web page that I created contains a simply control panel with buttons marked Forward, Backward, Left, Right and Stop. I also added a couple of buttons to turn the servo that the head is mounted on.
When the robot itself is turned on the Nano fires up and starts to listen for commands coming in over the i2c interface from the Pi (when a button is pressed on the web page control panel). I have also setup an 'Autonomous Mode' which waits for a specified time period and if no commands are received the robot starts to auto navigate around its environment.

The Code
There are three programs associated to this project, HTML, Python and Sketch.
The HTML file is the web page served by the Pi using WebIOpi, the Python code starts the i2c master program, starts WebIOPi and calls mjpg-streamer to stream the video feed. The sketch code is uploaded to the Arduino to set it up as a slave and await and action commands received from the Pi as well as provide the autonomous mode.

Youtube
Here is a link to a short YouTube video of the robot in action (autonomous mode only), he did crash a few time but I edited those out :-)


Current Issues
At the time of this update there are two issues with the current version:

1. Movement (especially left and right). Control over the web interface needs further work, clicking on a control button on the web page control panel (a .html mouse down event) causes the robot to move, there is too much delay between sending the stop command (the mouse up event when the user releases the mouse button) and the robot stopping the current command. Whilst this is not particularly noticeable when moving forward and backwards, when turning the robot turns about 270 degrees every time which makes fine control impossible. More work required?

2. Autonomous Mode. Autonomous mode kicks in after about a minute of inactivity however when the user tries to take control again rather that autonomous mode ceasing, it carries on resulting in a fight between the user and the robots AI for control!! Probably a simple fix however once again more work required.
I have setup a GitHub account to host the code however I have never used Git Hub before so I am struggling to work it all out as soon as I do I'll post up the link to the various code used in this project.

I hope you like this blog / project and if you need any help with it please feel free to leave a comment or find me on LMR.