iot: raspberry pi robot with video streamer and pan/tilt camera remote control over internet

This Instructable was one of the winners of 2016 AUTOMATION Contest. Thanks a lot for all votes! ;-)This is a second part of my previous Intructable: IoT - Controlling a Raspberry Pi Robot over internet with HTML and shell scripts only. There you learn how to control DC motors over the internet.


Here we will learn how to streaming video using a PiCam and also remote controlling its position (Pan/Tilt), using servo motors. Controlling a camera over the internet can have several utilities, including for security use. As a particular example, we will install the camera phone and servos at the robot developed on the first part of the project.
The video bellow will give you an idea about how the final project looks like:The block diagram shows the idea of the project. The RPi will be set as a WebServer and will receive commands from an HTML page. Those commands will control the GPIOs, making the RPi position the PiCam via the servo motors (Pan/Horizontal and Tilt/Vertical position).
We will install the PiCam video streamer based on the tutorial: Raspberry Pi camera board video streaming, developed by Miguel Mota (please, refer to the January, 19th update).First, let's update and upgrade the OS:sudo apt-get updatesudo apt-get upgradeInstall dev version of libjpeg:sudo apt-get install libjpeg62-turbo-dev (Note: libjpeg62-dev is obsolete and was replace by this one)Install make: sudo apt-get install cmakeDownload mjpg-streamer with raspicam plugin:git clone ~/mjpg-streamerChange directory: cd ~/mjpg-streamer/mjpg-streamer-experimentalCompile: make clean allReplace old jpg-streamer: sudo rm -rf /opt/mjpg-streamer sudo mv ~/mjpg-streamer/mjpg-streamer-experimental /opt/mjpg-streamer sudo rm -rf ~/mjpg-streamerBegin streaming: LD_LIBRARY_PATH=/opt/mjpg-streamer/ /opt/mjpg-streamer/mjpg_streamer -i "input_raspicam.so -fps 15 -q 50 -x 640 -y 480" -o "output_http.
so -p 9000 -w /opt/mjpg-streamer/www" &At Monitor you can see the valuing info:MJPG Streamer Version.: 2.0i: fps.............: 15i: resolution........: 640 x 480i: camera parameters..............:Sharpness 0, Contrast 0, Brightness 50, Saturation 0, ISO 400, Video Stabilisation No, Exposure compensation 0Exposure Mode 'auto', AWB Mode 'auto', Image Effect 'none', Metering Mode 'average', Colour Effect Enabled No with U = 128, V = 128Rotation 0, hflip No, flip Nowww-folder-path...: /opt/mjpg-streamer/www/HTTP TCP port.....: 9000username:password.: disabledcommands..........: enabledStarting CameraEncoder Buffer Size 81920The camera should be working.
Go to your Web Browser and enter: Ip Address:9000/stream.htmlA test webpage as the one above should appear. Note that if you want change the PORT, change the "9000" parameter for the one that works better for you (in the command line entered at RPi Monitor).
Observe that we are working with 15 frames per second (fps) and a resolution of 640x480. You can also change those parameters in the command line. You must re-enter the command line above to start streaming any time that your system re-boot, unless you include it at the /etc/rc.
local script, as bellow:sudo nano /etc/rc.local...LD_LIBRARY_PATH=/opt/mjpg-streamer/ /opt/mjpg-streamer/mjpg_streamer -i "input_raspicam.so -fps 15 -q 50 -x 640 -y 480" -o "output_http.
so -p 9000 -w /opt/mjpg-streamer/www" &.
..If you have went thru the first part of this project, you must have the LIGHTTPD WebServer already installed. If not, the bellow steps will guide you in how install LIGHTTPD, a very "light" and fast WebServer that can be used instead of Apache for example. As described on its lighttpd wiki page, "Lighttpd is a secure, fast, phone compliant, and very flexible web-server that has been optimized for high-performance environments.
It has a very low memory footprint compared to other WebServers and takes care of cpu-load.Let's install Lighttpd web server and components: sudo apt-get -y install lighttpd sudo lighttpd-enable-mod cgi sudo lighttpd-enable-mod fastcgiBy default, Lighttpd is looking for a index.html page at /var/www/html. We will change it, so the index.html will be placed under /var/www. For that, we must edit the Lighted config file: sudo nano /etc/lighttpd/lighttpd.
confchange: server.
document-root ="/var/www/html"by: server.document-root ="/var/www/"In order to aloud this change to take effect, we must stop and re-start the web server: sudo /etc/init.d/lighttpd stop sudo /etc/init.d/lighttpd startAt this point the web server is running and if a page index.
html is located at /var/www, we can access it from any browser, typing the RPi IP address:in order to create a single webpage to show the video, let's include the bellow line at the HTML code:Bellow you can see the complete HTML code for the page, where the video are streaming, The above photo shows the webpage print screen:A greta library to be used to control servos is ServoBlasterThis is software for the RaspberryPi, which provides an interface to drive multiple servos via the GPIO pins. case You control the servo positions by sending commands to the driver saying what pulse width a particular servo output should use. The driver maintains that pulse width until you send a new command requesting some other width.
By default is it configured to drive 8 servos, although you can configure it to drive up to 21. Servos typically need an active high pulse of somewhere between 0.5ms and 2.5ms, where the pulse width controls the position of the servo.
The pulse should be repeated approximately every 20ms, although pulse frequency is not critical. The pulse width is critical, as that translates directly to the servo position.In addition to driving case servos, ServoBlaster can be configured to generate pulse widths between 0 and 100% of the cycle time, making it suitable for controlling the brightness of up to 21 LEDs, for example.
The driver creates a device file, /dev/servoblaster, in to which you can send commands. The command format is either[servo-number]=[servo-position](ex.: echo P1-11=80% >/dev/servoblaster) orP[header]-[pin]=[servo-position](ex: echo P0=80% >/dev/servoblaster)First, let's clone Richardghirst project from GITHUB:cd sudo git clone dir:cd PiBitscd ServoBlastercd userList the content and verify if the directory contains the file "servod.
c"lsCompile and install the file served.
c:sudo make servodsudo make installAt this moment, the program servod must be installed. Change the permissions and run the program to test it:sudo chmod 755 servodsudo ./servodIf everything is OK, you can see at the monitor, the following information: Note the servo Mapping above.
We will only need 2 servos, so we must restrict the pins to be used. Let's consider:To define the pins to be used, the bellow parameters must be used:sudo ./servod --p1pins=11,16Running the above command, the monitor will now show in its lower part:Using P1 pins: 11,16Servo mapping: 0 on P1-11 GPIO-17 1 on P1-16 GPIO-23Also note that if you reboot the RPi, the configuration will l be lost, so.
it's important to include the last command at /etc/rc.localsudo nano /etc/rc.local...cd /home/pi/PiBits/ServoBlaster/usersudo ./servod --p1pins=11,16cd ...Also it is important change the script bellow:sudo nano /etc/init.
d/servoblaster.
..case "$1" in start) /usr/local/sbin/servod $OPTS >/dev/nullchange to: /usr/local/sbin/servod --p1pins=11,16 $OPTS >/dev/null...now reboot your system, so the changes can be permanentsudo rebootThat's it. Servo blaster is installed. Note that at this point ServoBlaster will recognize only two servos:servo 0 ==> p1-11servo 1==> 01-16The echo command can be done on any one of the bellow formats, with same result:echo P1-11=40% >/dev/servoblasterecho P1-16=60% >/dev/servoblasterorecho 0=40% >/dev/servoblasterecho 1=60% >/dev/servoblasterThere are several Pan/Tilt mechanisms at market, but I decide instead to assembly a very simple one, only tying the servos and the PiCam among them as you can see in the photos.
For testing the servos, use the Servoblaster command "echo". You can use angle values or percentage. Test the best range for your Pan/Tilt mechanism:Tilt Servo Range:echo P1-11=20% >/dev/servoblaster (looking down)echo P1-11=60% >/dev/servoblaster (looking front)echo P1-11=90% >/dev/servoblaster (looking up)Pan Servo Range:echo P1-16=30% >/dev/servoblaster (looking right)echo P1-16=62% >/dev/servoblaster (looking center)echo P1-16=90% >/dev/servoblaster (looking left)We can write bash scripts to control the servo's position easily. Before start, let's remember that is a good practice to have a specific directory for the programs used and call it "bin".
So, to save the scripts that we will use in the project, we will create an directory that will containing all executable scripts (or binary files). For example, let's move for our webpage directory:cd /var/wwwand under it, create the directory with the scripts, that we will call cgi-bin: sudo mkdir /var/www/cgi-binFor the scripts, we will use ".cgi" as the file extension. CGI means "Common Gateway Interface". It is a standard way for web servers to interface with executable programs installed on a server that generate web pages dynamically.
Such programs are known as CGI scripts or simply CGIs; they are usually written in a scripting language, but can be written in any programming language. Let's create a directory under /var/www, where the scripts will be saved:sudo mkdir cgi-binAs we discussed on the first part of this project, IoT - Controlling a Raspberry Pi Robot over internet with HTML and shell scripts only. script's files are essentially plain text.
When a text file is attempted to be executed, shells will parse through them for clues as to whether they're scripts or not, and how to handle everything properly. Because of this, there are a few guidelines you need to know.When a shell parses through a text file, the most direct way to identify the file as a script is by making your first line: #!/bin/bash (The Hash-Bang Hack).
If you use another shell, substitute its path here. Comment lines start with hashes (#), but adding the bang (!) and the shell path after it is a sort of hack that will bypass this comment rule and will force the script to execute with the shell that this line points to. For example, to create a shell script to position the camera "looking to front", based on the above servo ranges that you found, we must create the file bellow (use the best editor for you.
I am using NANO for that): sudo nano cam_view_front.cgi #!/bin/bash echo P1-11=60% >/dev/servoblaster echo P1-16=62% >/dev/servoblaster. Once the script is created, we must give it permission to be executed: sudo chmod 755 cam_view_front.
cgi Now, to execute the script: .
/cam_view_front.
cgiMove the camera for any position using echo command and after that execute the new script. You will see that the camera will be poisoned automatically its front view position. Going on, the same idea must be applied for the other possible Camera positions.
For my webpage, I choose to create 5 intermediary positions for Tilt and 5 intermediary positions for PAN. You can also chose to use "sliders" for a more continuous position change. It's up to you.Using the same principle as described for the script "cam_view_front.
cgi", we will create 10 new scripts:Using the index.html that we created in the last step, let's include 10 buttons that will call functions to execute the scripts created on last step (see the final page above). For example, let's create a button to position the camera on its central TILT position:The above HTML code will create a rounded button with an "0" on it (see the final page above).
When the button is pressed, due the command "onclick=centertilt()", the function "centertilt() is called:And once the function centertilt() is called, the script centertilt.cgi is executed and the servo will move to center position.The same procedure must be used for all buttons.
There are some HTML functions that will organize the look&fill that you can realize looking the full HTML page. The HTML source can be seen bellow:Now that we have our RPI streaming video and its camera position can be controlled via internet, why not to integrate this project with the previous one?The complete idea will be a RPi Robot controlled by internet. The block diagram shows how the project can be integrated and the circuit show how to make the connections.
Let's take the webpage developed here (index.html) and add the motor control buttons (and its respective functions) developed at first part of this project. Above you can see the final integrated webpage.
Let's have the Pan/Tilt camera mechanism integrated to the robot.The video bellow explains how its works:The complete files and documents used on this Instructable can be found at GITHUB: always, I hope this project can help others find their way in the exciting world of electronics, robotics, Raspberry Pi and IoT!For more projects, please visit my blog: MJRoBot.org Saludos from the south of the world! See you

Comments

Popular posts from this blog

50 things to do in autumn

Ride my pimp

panel products, veneer.