Robot on RaspberryPi, Arduino and RaspiCam + OpenCV. Part 1 Review
Very often, articles about how to use the Raspberry Pi as a media center, a mobile video camera, a remote webcam and ... everything is actually appear on the desktop. It is very strange that in such a big IT community - quite a bit of information about how to program it and use a single-board computer where it is really quite useful - in any embedded systems, where there are limitations in size and cost, but there is also a need for performance . In several articles I will try to describe using the example of creating a mobile wheeled robot with a computer vision - how can the raspberry be used to create robots (pieces with intelligence on board, rather than machines controlled with android with a web camera).
Introduction
It was always interesting to program something mechanical - you feel like God (like most programmers) - you breathe a soul into a bunch of details. Probably everyone remembers that childhood joy from the first blinking of the LED, the moving servo, etc. - when did something that you can touch, that lives, moves , and not saytik on php . In many of his creations, and even more so robots - a person always tries to repeat himself, or part of his functions. We get 80% of the information about the world through vision - so computer vision, in my opinion, is one of the fundamental areas of knowledge in robotics. I began to study it by reading academic works on algorithms in parallel with mastering the OpenCV computer vision library in C ++ (in the case of Raspberry - Python) - knowledge of the principles of the algorithms will help you to assess the complexity and feasibility of the task, even before it starts, and optimize the algorithms in critical places. Even if you use mostly library functions - they are well optimized, and you are unlikely to write from scratch better - you can optimize some parameters that have little effect on the solution of your problem, but significantly affect the speed of its solution - Generally, returning to holivar - “Does a programmer need a mathematician?” In this case, it is necessary, so I advise you to exert a little gyrus and understand at least superficially in the work of the algorithms.
It will also be useful to at least superficially study the Theory of Automatic Control - instead of painting its capabilities - I suggest simply to watch the following video (BTW - half of its team are Russians)
Components of the robot
It is unlikely that you will have on hand 1 in 1 the same details as mine, if you want to repeat it - so I will describe the general concept, and you will see for yourself. ')
Mechanics
The mechanical basis of the robot - a two-wheeled with a differential drive - is classic, in general, for the first robotic experiments - it has 2 independent wheels and its movement is controlled exclusively by the speeds and the direction of their rotation (like quadrocopter screws). In addition to the wheels themselves, there is a ball / wheel support, in advanced systems - encoders for feedback and control of the current speed of the engines, which allows for more efficient control of the engines.
Motor controller
As a motor controller, you can use any microcontroller, I use Arduino nano - because it just came to hand. Perhaps the question arises - why not manage directly with Raspberry? The fact is that the operating system has a quantum of time much more than a microcontroller, besides, there is no hardware PWM s, plus if we want to improve the control of motors using feedback and Control Theory, this will require computational costs and a faster reaction - therefore the engine controlling part and the brain of the robot are separated - the arduino simply receives a command from the UART - with what speeds and directions the brain would like the engines to spin - how it will be achieved - simply switching on the PWM with the required duty cycle or tricky control, when at first we apply more voltage than the setpoint, spinning the engine, and then we align - thus speeding up the engine to the desired speed - all this is the concern of the motor controller, not Raspberry - since this is generally a much tighter time task - on order - two smaller than allows Raspberry, and indeed similar systems.
Engine driver
Arduinka alone is not enough for the motors to spin - the current given by the leg is too small - if we put a motor winding on the small output transistor of the controller, which requires an ampere current, then we simply short-circuit it and close the key building - therefore, we need a more powerful key that allows us to pass a large current through ourselves - if we need to turn the motor in one direction - in general, we only need one transistor, but if we want to turn in different directions - we already have Dreghood 4 - such a scheme is called H - bridge - closing the diagonal keys closed other diagonal - we can change the direction of the current in the motor. And such a scheme is necessary for each wheel. Fortunately, in our time there is no need to assemble it - it is implemented in the form of integrated circuits, of which there are a great many - so that anyone who can control the current needed by your engine will do. I use this two-channel pololu from here: There is also a great variety of all kinds of shields for arduins - with the help of Google, you can easily find them on request “arduino motor driver”. Connection scheme is also usually provided by the manufacturer or users of various forums - the seeker will find it. Chip 2 power supply - one - which is supplied to the engines from a powerful current source - for example, 7.2V Li-Pol batteries, the other - the power supply of the logic input stage - Arduyn's 5V, also has inputs that control the direction of rotation of each channel and the Enable input - supplying PWM signal, we can adjust the speed of rotation of the engine. There may be different configurations depending on the shield, but the main conclusions are.
In general, having thus connected the Arduino, the driver of the engines, the motors and the battery (or just some kind of current source on a long wire), you can already start playing with the control of the motors. To receive commands from Raspberry, you will need to implement the reception of a UART line and its parsing - here you can think of a protocol which your heart desires. The above - the main parts of almost any wheeled robot - then the options are already starting - you can generally score on computer vision and make the robot purely on the Arduino, which, for example, travels along a line, avoids obstacles using distance sensors, etc.
Main controller
My task is to make a somewhat more intelligent platform for studying computer vision and control theory - so the next element of the system will be the single-board computer Raspberry Pi B + due to its low price, prevalence and accessibility of information. The Python interpreter is included in the Raspbian assembly - so I wrote a program for the robot
Camera
As a camera, you can generally use any webcam (which I did the first time) - I use Raspicam - it is small, light, there is a separate port for connecting it, a wide viewing angle is a good driver and 90 fps in VGA resolution.
Debug tool
For debugging, I use a USB Wifi whistle, connecting to Raspberry via remote desktop via SSH. Also, in general, you can use any, for the initial configuration, you can generally use an Ethernet cable and SSH
Supply system
Battery - lithium polymer from 2Ah to 7.2V nominal voltage + charge. A step-down DC-DC converter - our battery supplies from 8.4 to 6V - we can directly apply this voltage to the motors through the driver chip, but a 5V power supply is required to power the Raspberry and Arduino, according to the Raspberry Pi documentation, a source of at least 800mA is required - you can of course lower the voltage from the battery to 5V using a linear regulator, but at such currents it will warm up and use the battery charge inefficiently, so I recommend using a pulsed step-down DC-DC converter - from it I have nourished and Arduino and Raspberry
Actually, a photo of my nanotech robot and a couple of videos of his ride on various competitive tracks as a demonstration: Pro Line (Intermittent)
Thin line with sharp turns (Euros)
In general, the review article is completed - he spoke about the main tools used, then it will be more specific, namely: 1. Introduction 2. Initial setup of Raspberry Pi + debugging on Wifi 3. Getting started with OpenCV, Raspicam on Python 4. Robot control, PID controller, examples Questions, comments, additions and suggestions for the following articles leave in the comments, ochepyatki and more - in a personal