Adding a camera to the robot and enabling video streaming
In this part we will add a camera to our Raspberry PI robot and enable continuous video streaming. We will be using the camera that we have ordered in part 1 of this series of tutorials.
There is a great getting started tutorial at:
https://projects.raspberrypi.org/en/projects/getting-started-with-picamera
At some point you will want to start streaming. The process is slightly convoluted, so its better to follow the steps mentioned in the below tutorial:
https://miguelmota.com/blog/raspberry-pi-camera-board-video-streaming/
# Install dev version of libjpeg
sudo apt-get install libjpeg62-dev
# Install cmake
sudo apt-get install cmake
# Download mjpg-streamer with raspicam plugin
git clone https://github.com/jacksonliam/mjpg-streamer.git ~/mjpg-streamer
# Change directory
cd ~/mjpg-streamer/mjpg-streamer-experimental
# Compile
make clean all
# Copy to /opt
sudo mv ~/mjpg-streamer/mjpg-streamer-experimental /opt/mjpg-streamer
# Delete the package from home
sudo rm -rf ~/mjpg-streamer
# Begin 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" &
We need to provide an interface to view the video stream from our robot. To do this, we will be adding a page to our project. Clicking on the page will take us to the camera streaming window and allow us to start and stop streaming.
Create the start and stop scripts:
$ cat start_stream.sh
#!/bin/bash
if pgrep mjpg_streamer > /dev/null
then
echo "mjpg_streamer already running"
else
LD_LIBRARY_PATH=/opt/mjpg-streamer/ /opt/mjpg-streamer/mjpg_streamer -i "input_raspicam.so -fps 15 -q 50 -x 640 -y 480i -vf" -o "output_http.so -p 9000 -w /opt/mjpg-streamer/www" &
echo "mjpg_streamer started"
fi
In my case, the video from the camera was inverted, so adding the option -vf fixed the image. The vf option creates a vertical flip of the image.
The corresponding stop script:
$ cat stop_stream.sh
#!/bin/bash
if pgrep mjpg_streamer
then
kill $(pgrep mjpg_streamer) > /dev/null 2>&1
echo "mjpg_streamer stopped"
else
echo "mjpg_streamer not running"
fi
It does not really make sense to add these scripts to the start up of RPI, since we will not want the camera to be switched on all the time. Ideally, we would like the camera to be switched on when we move to the camera control page.
We will try to do this later in the project.
For the time being, we will move on to enabling the servo motors for the PAN and TILT control of the camera.
All the code is available in Bitbucket. Use the following command to download the repo:
git clone https://boseontherocks@bitbucket.org/boseontherocks/projects_1.git
The code used in this post is available in the folder robot_4
It also includes the test script for video streaming