background image

52 

MJPG-streamer (Operation on Raspberry Pi) 

A.

 

MJPG-streamer Installation 

Introduction   

The acquisition and transmission of video data by the SunFounder Smart Video Car is fulfilled 
based on MJPG-streamer.   

MJPG-streamer  is  a  command  line  application  that  copies  JPG-frame  from a  single  input 
plugin to multiple output plugins. It can be used to stream JPEG files over an IP-based network 
from  the  webcam  to  a  viewer  like  Firefox,  Cambozola  and  Videolanclient  or  even  to  a 
Windows mobile device running the TCPMP-Player. 

It was written for embedded devices with very limited resources in terms of RAM and CPU. Its 
origin,  the  "uvc_streamer"  was  written,  because  Linux-UVC  compatible  cameras  directly 
produce JPEG-data, allowing fast and performant M-JPEG streams even from an embedded 
device running OpenWRT. The input module "input_uvc.so" captures such JPG frames from a 
connected webcam.   

Installation   

Plug the USB camera into Raspberry Pi, and run the command lsusb. The GEMBIRD represents 
the USB camera; since it is printed on the screen, it indicates the system has recognized the 
camera. 

 

lsusb

 

You'll see:   

pi

@

raspberrypi

:~

 

$ lsusb

 

Bus

 

001

 

Device

 

004

:

 

ID

 

1908

:

2310

 

GEMBIRD

 

Bus

 

001

 

Device

 

003

:

 

ID

 

0424

:

ec00

 

Standard

 

Microsystems

 

Corp.

 

SMSC9512

/

9514

 

Fast

 

Ethernet

 

Adapter

 

Bus

 

001

 

Device

 

002

:

 

ID

 

0424

:

9514

 

Standard

 

Microsystems

 

Corp.

 

Bus

 

001

 

Device

 

001

:

 

ID

 

1d6b

:

0002

 

Linux

 

Foundation

 

2

.

0

 

root

 

hub

 

Check whether the driver for the camera works normally:   

ls

 

/

dev

/

vid

You'll see:   

pi

@

raspberrypi

:~

 

$ ls

 

/

dev

/

vid

*

 

/

dev

/

video0

 

If /

dev/video0

 is printed, the driver is in the normal state.   

Then, install the following software needed:   

sudo

 

apt

-

get

 

install

 

subversion

 

sudo

 

apt

-

get

 

install

 

libv4l

-

dev

 

SunFounder

Summary of Contents for Smart Video Car Kit B+

Page 1: ...Raspberry Pi model B 2 model B and 3 model B In this book we will show you how to build the smart car via description illustrations of physical components and schematic diagrams of circuits in both hardware and software respects You may visit our website www sunfounder com to download the related code and view the user manual on LEARN Get Tutorials and watch related videos under VIDEO or clone the...

Page 2: ... 13 B Back Half Chassis Copper Standoffs 15 C Upper Plate Copper Standoffs 15 D Battery Holder 17 E Back Chassis Upper Plate 18 F Electrical Module Assembly 19 Circuit Connecting 20 Servo Calibration Operation on Raspberry Pi 29 A Burn the image 29 B Car Power Supply 30 C Log into Raspberry Pi 30 For Linux or Mac OS X Users 30 For Windows Users 32 D Get Source Code 33 E Install python dev python s...

Page 3: ...ient 48 D Start Calibration 50 Motor Adjustment 50 Turning Adjustment 51 Mount Adjustment 51 MJPG streamer Operation on Raspberry Pi 52 A MJPG streamer Installation 52 B Testing 53 Get on the Road 55 A Run TCP server Operation on Raspberry Pi 55 B Run the Client operation on PC 55 Program Analysis and Explanation 58 Abstract 58 Introduction of Socket 58 1 Server 59 Process Diagram of Server Progra...

Page 4: ... is run on Raspberry Pi for direct control of the car And the video data are acquired and delivered via the open source software MJPG streamer in a real time manner The TCP client program is run on PC to send the control command Both the client and server programs are developed in Python The smart car is developed based on the open source hardware Raspberry Pi and integrates the knowledge of mecha...

Page 5: ...2 Components A Acrylic Plates S u n F o u n d e r ...

Page 6: ... screw 8 M2 8 screw 6 M2 5 6 screw 16 M3 10 countersunk screw 2 M3 8 screw 8 M3 10 screw 6 M3 30 screw 4 M4 25 screw 2 M2 5 8 copper pillar 16 M3 24 copper pillar 8 M2 nut 6 M2 5 nut 16 M3 nut 20 M4 self locking nut 2 F694ZZ flange bearing 2 S u n F o u n d e r ...

Page 7: ...4 C Drive Parts Parts Name Qty Tower Pro Micro Servo SG90 3 Gear reducer 2 Driven wheel 2 Active wheel 2 S u n F o u n d e r ...

Page 8: ...5 D Electrical Components Parts Name Qty Raspberry Pi Model B 1 16 Channel 12 bit PWM driver servo controller 1 L298N DC motor driver 1 Step down DC DC converter module 1 S u n F o u n d e r ...

Page 9: ...6 USB Wi Fi adapter 1 USB camera 1 18650 2 Battery holder 1 Ribbon 1 USB cable 1 S u n F o u n d e r ...

Page 10: ...7 Cross socket wrench 1 Cross screwdriver 1 Cable Spiral Wrap 1 20cm jumper wire F to F 4 10cm jumper wire F to F 5 10cm jumper wire M to F 2 20cm jumper wire M to M 2 S u n F o u n d e r ...

Page 11: ...8 E Self provided Parts The following parts are not included in the set Parts Name Qty Needed 18650 3 7V rechargeable Li ion battery 2 TF Card 1 S u n F o u n d e r ...

Page 12: ... browsing the internet and playing high definition video to making spreadsheets doing word processing and playing games What s more the Raspberry Pi has the ability to interact with the outside world and has been used in a wide array of digital maker projects from music machines and parent detectors to weather stations and tweeting birdhouses with infra red cameras In this kit we use Raspberry Pi ...

Page 13: ...t can be supported C Servo In this smart car one servo controls the direction of the car and the other two the movement of the camera between X axis and Y axis thus defining the coverage of the camera A servo is an automatic control system composed of DC motor reduction gear set sensor and control circuit It defines the rotation angle of the output shaft via delivering specific PWM signals General...

Page 14: ...sts the module is used to drive DC motors The driver is built based on L298N As a high voltage and large current chip for motor driving encapsulated with 15 pins the chip has a maximum operating voltage of 46V and an instant peak current of as high as 3A with an operating current of 2A and rated power of 25W Thus it is completely capable of driving two low power DC motors E USB Wi Fi Adaptor The a...

Page 15: ...servo and controls the rotation angle of the servo Meanwhile the module controls the duty cycle of the square waves output from channel 14 and 15 to regulate the rotational speed of the DC motor so as to control the speed of the car G 18650 2 Battery Holder The battery is to use two 18650 batteries to power the modules and servos on the car Note Please pay attention to the cathode and anode marks ...

Page 16: ...o acrylic plates b When completed the assembly should look like the figure below c Assemble the gear reducer the active wheel and following acrylic plates with two M3 30 screws and M3 nuts d When completed the assembly should look like the figure below S u n F o u n d e r ...

Page 17: ... completed the assembly should look like the figure below g Assemble the gear reducer the active wheel and the previously assembled part with two M3 30 screws and M3 nuts h When completed the assembly should look like the figure below S u n F o u n d e r ...

Page 18: ...w b When completed the assembly should look like the figure below C Upper Plate Copper Standoffs a Assemble 16 M2 5 8 copper standoffs and 16 M2 5 nuts into the acrylic plate as shown below Pay attention to the face of the plate Refer to the small hole in the plate as pointed by the arrow in the following figure S u n F o u n d e r ...

Page 19: ...16 b When completed the assembly should look like the figure below c The view from the back of the plate S u n F o u n d e r ...

Page 20: ...th 2 M3 10 countersunk screws and 2 M3 nuts You can thread a ribbon through the plate below so it will be easy to remove the battery which is up to you b When completed the assembly should look like the figure below c The view from the top is as follows S u n F o u n d e r ...

Page 21: ...18 E Back Chassis Upper Plate a Connect the two assembled parts with 4 M3 8 screws b When completed the assembly should look like the figure below S u n F o u n d e r ...

Page 22: ...19 c The view from the back of the plate F Electrical Module Assembly Assemble the electrical components to the car with M2 5 6 screws See the figure below S u n F o u n d e r ...

Page 23: ...20 Circuit Connecting Preview Looks complicated but don t worry The detailed procedures will be given below step by step S u n F o u n d e r ...

Page 24: ...ector and plug it back after wiring Note It doesn t matter how to wire the motors After all the assembly is done if the car moves in an opposite direction of what you control just swap the wiring of the two motors and it will work normally After wiring it should be like this S u n F o u n d e r ...

Page 25: ...22 Step 2 Connect the motor driver with the Raspberry Pi GPIO port based on the following table Raspberry Pi GPIO Port DC Motor Driver Pin11 IN1 Pin12 IN2 Pin13 IN3 Pin15 IN4 S u n F o u n d e r ...

Page 26: ...23 Step 3 Connect the servo controller with the Raspberry Pi GPIO port as follows Raspberry Pi GPIO Port Servo Controller Pin 2 5V VCC Pin 3 SDA SDA Pin 5 SCL SCL GND GND S u n F o u n d e r ...

Page 27: ...ontrols the car s direction to CH0 of the servo controller and the two servos that control the view of the camera to CH14 and CH15 respectively as shown below Step 5 Connect the motor driver with the servo controller S u n F o u n d e r ...

Page 28: ...ect the servo controller with the step down DC DC converter module For the connector loosen the screws insert the wire and then tighten the screws with a screwdriver The connection should be like S u n F o u n d e r ...

Page 29: ...e care that the red wire is the power and should be connected to the anode of the holder when the black wire i e the ground to cathode Insert the four wires into two jacks of the connector two in each Pay attention the red wires to the anode of the step down module when the black ones to the cathode Also DO NOT plug a red wire with a black one into the same jack or it ll cause a short circuit S u ...

Page 30: ...27 The whole picture of wiring should be like this S u n F o u n d e r ...

Page 31: ...28 Step 8 Connect the Raspberry Pi with the step down DC DC converter module the USB Wi Fi adapter and the USB camera S u n F o u n d e r ...

Page 32: ... we need to configure the servo via software First you need to finish some software installation before the configuration A Burn the image Take the following steps before inserting the TF card into the Raspberry Pi 1 Prepare the tool of image burning Such as win32DiskImager 2 Download the complete image on the official website at this link https www raspberrypi org downloads raspbian Both the RASP...

Page 33: ...n needs a long time and the battery won t be able to support 2 Also keep the power on by battery the servos and motors are powered by the battery so ensure the power is on for them when the RPi is supplied separately Plug in the USB Wi Fi adapter skip this if you use a Raspberry Pi 3 with built in WiFi device and complete the setting in the way you re comfortable with After the blinking indicator ...

Page 34: ...e name suggests your Pi s IP address For example ssh pi 192 168 0 101 Press Enter to confirm If you get a prompt that no ssh is found you need to install an ssh tool like Ubuntu and Debian by yourself sudo apt get install ssh For other Linux platforms please contact your supplier S u n F o u n d e r ...

Page 35: ...under Port by default it is 22 3 Click Open Note that when you first log in to the Raspberry Pi with the IP address you ll be prompted with a security reminder Just click Yes When the PuTTY window prompts login as type in the user name of the RPi pi and password raspberry the default one if you haven t changed it Note When you re typing the password in the window shows nothing just null but you re...

Page 36: ...onfig sudo raspi config Enable I2C Select Advanced Options I2C Yes Ok Yes Select Finish Close the window If a message of rebooting appears click No Before reboot we still need to complete some configurations G Start calibration Run the calibration server on the Raspberry Pi and wait for client to connect Make sure that the circuit is connected properly Power the smart car open a terminal in Linux ...

Page 37: ...python cali_server py Then the contents of config are printed and the last line Waiting for connection At this time the servo might move a bit this is the original position set Note During the subsequent assembly cali_server py should keep running S u n F o u n d e r ...

Page 38: ...5 screw and an M4 self locking nut in the way as shown below Use the cross socket wrench to fasten the M4 self locking nut b When completed the assembly should look like the figure below c Bear in mind that DO NOT over tighten the nut or else the wheel cannot turn flexibly Neither too loose in case the gap between the parts is too large Pay attention to the direction of this plate S u n F o u n d ...

Page 39: ...ocking nut in the way shown as below e When completed the assembly should look like the figure below f Bear in mind that DO NOT over tighten the nut or else the wheel cannot turn flexibly Neither too loose in case the gap between the parts is too large Pay attention to the direction of this plate S u n F o u n d e r ...

Page 40: ... M2 8 self tapping screw is contained in the package of the servo it is one of the two longest screws Also the rocker arm is packaged together with the servo Note Be careful with the screw of pricking your fingers b Tighten the screw first then loosen it a little to ensure the Steering Linkage s movement the assembly should look like the figure below S u n F o u n d e r ...

Page 41: ... to the acrylic plate below with two M2 8 screws and M2 nuts b When completed the assembly should look like the figure below Note The servo should be placed with its output shaft toward the front of the plate see the following figure S u n F o u n d e r ...

Page 42: ... the shortest of the screws in the package Keep the Steering Linkage perpendicular with the middle of the car otherwise remove it and mount again DO NOT directly turn it in case of damaging the servo Minor errors are allowed which can corrected later by programming b When completed the assembly should look like the figure below S u n F o u n d e r ...

Page 43: ...nt Chassis Upper Plate a Connect the following parts and wheels with M3 8 screws M3 24 copper standoffs and M3 nuts 4 for each b When completed the assembly should look like the figure below S u n F o u n d e r ...

Page 44: ...er arm is packaged together with the servo b When completed the assembly should be like the figure below c Connect the rocker arm of the servo to the acrylic plate below with four M1 2 4 screws The rocker arm is packaged together with the servo d When completed the assembly should be like the figure below S u n F o u n d e r ...

Page 45: ...ws and M2 nuts and we name it pan servo b When completed the assembly should look like the figure below c Connect the servo to the acrylic plate below with two M2 8 screws and M2 nuts and we name it tilt servo d When completed the assembly should look like the figure below S u n F o u n d e r ...

Page 46: ...screws and M3 nuts The two plates should be perpendicular to each other b When completed the assembly should look like the figure below c Connect the two parts at a right angle with two M3 10 screws and M3 nuts d When completed the assembly should look like the figure below S u n F o u n d e r ...

Page 47: ...ow The two plates as indicated with arrows below should be assembled in parallel otherwise remove and mount again DO NOT directly rotate them in case of damages to the servo Top view Front view c Then fasten them with an M2 4 screw The M2 4 screw is contained in the package of the servo and is the shortest of the screws S u n F o u n d e r ...

Page 48: ...45 J Mount Car Assemble the mount part and the car with two M3 10 screws and M3 nuts When completed the assembly should look like the figure below S u n F o u n d e r ...

Page 49: ...46 Now the car is completed Congratulations The car should be assembled successfully as shown below To organize the wires you can use the cable spiral wrap Bottom view S u n F o u n d e r ...

Page 50: ...e sidebar of the page as shown below After download unzip the file Find the file whose name containing master and delete its suffix The file name then should be Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi B Install Python software For Linux users Install python tk for a remote interface open a Terminal and type in the command sudo install apt get install python tk For Windows users Go to the Py...

Page 51: ...ow will pop up Click File Open Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi master client cali_client py to open this file modify the value of HOST for the IP address of the Raspberry Pi After modification save the file and click the Run menu and select Run Module For Linux users Open another terminal in Linux not via ssh on your Pi Find the sketch downloaded and edit client cali_client py cd cl...

Page 52: ...n cali_client py sudo python cali_client py No matter what system your computer is running on Linux or Windows when you run cali_client py a window Raspberry Pi Smart Video Car Calibration will pop up In the terminal remotely connected with the Raspberry Pi the IP address of the PC will be printed S u n F o u n d e r ...

Page 53: ...be face up no matter what directions the pan servo is pointed at D Start Calibration On the calibration UI there are three sections Motor Turning and Mount Motor Adjustment Click Run The car will walk forward Check whether both the back wheels move forward together If either fails to do so your wiring may be wrong But don t worry You don t need to rewire just click the corresponding Reverse in Mot...

Page 54: ...Then you may place the car on the table and click Run to verify whether it runs in a straight line If not perform the adjustment again till it does Mount Adjustment Now the pan servo on the car should be pointed at the exact front direction and the camera face up But if not you may need to adjust them similarly In the Mount section there are the adjustments for the pan servo and tilt one Also you ...

Page 55: ...t M JPEG streams even from an embedded device running OpenWRT The input module input_uvc so captures such JPG frames from a connected webcam Installation Plug the USB camera into Raspberry Pi and run the command lsusb The GEMBIRD represents the USB camera since it is printed on the screen it indicates the system has recognized the camera lsusb You ll see pi raspberrypi lsusb Bus 001 Device 004 ID ...

Page 56: ...IR usr install B Testing Operation on Raspberry Pi Run the program sudo sh start sh Then the video data acquisition will start like this Type in the following address replace 192 168 0 126 with your Raspberry Pi IP address at the address bar of your browser Firefox is recommended operation on PC http 192 168 0 126 8080 stream html Press Enter and you will see the view captured by the camera displa...

Page 57: ...54 S u n F o u n d e r ...

Page 58: ..._Kit_for_RaspberryPi server Then run tcp_server py sudo python tcp_server py The server program on the Raspberry Pi will be running and waiting for the client to connect to the Raspberry Pi B Run the Client operation on PC Run the client on PC to connect the server on the Raspberry Pi For Windows users Open the IDLE Click File Open Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi client client App p...

Page 59: ... and edit the file client py Similar to changes in calibration change the IP address in client program in your PC After the alteration press Ctrl O and save and Ctrl X to exit Then run the client program sudo python client_App py No matter what system your computer is running on Linux or Windows when you run the client_App py the following window will appear on your screen S u n F o u n d e r ...

Page 60: ...e client program Some settings must be completed for the server before the service is done A communication endpoint needs to be created for the server to listen to requests from the client Take the server as a receptionist or an operator of the bus phone in a company Once the phone and device installation is completed and the receptionist or operator is in place the service begins S u n F o u n d ...

Page 61: ...client run on your PC datasheet contains some PDF files about the chip you need to view them on PC html_server the web server run on the Raspberry Pi for Android app client i2cHelper a simple script to help you set up i2c on the Raspberry Pi mjpg streamer the camera driver to acquire and upload images README md an introduction file with update information server the server run on the Raspberry Pi ...

Page 62: ...with IP address and port by the function bind Since TCP is a connection oriented communication system some settings need to be completed before the TCP server starts operation The TCP server must listen to connections from the client After the settings are done the server will enter an indefinite loop A simple like single thread server will call the function accept to wait for the coming connectio...

Page 63: ... is built the dialog between the client and the server is enabled When the dialog ends the client can close the socket and the connection Introduction of Tkinter Developed based on Tkinter our client program carries graphical interfaces Tkinter is a GUI widget set for Python We can develop application programs with graphical interfaces fast by Python language based on it It is quite easy to use Tk...

Page 64: ...ble privilege to the file chmod x Tk_test py Open the file vim Tk_test py Type in the following code usr bin env python from Tkinter import top Tk Create a top window top title Sunfounder com label Label top text Hello Geeks fg blue Create a label and set its foreground color as blue label pack layout top mainloop main loop Save the code and exit Run Tk_test py Then the following picture will appe...

Page 65: ...62 Process Diagram of Client Program S u n F o u n d e r ...

Page 66: ...parts like servo Wi Fi adapter etc Also you ve got a lot of software and coding which lays a solid foundation for your future journey of exploring open source field The SunFounder Smart Video Car for Raspberry Pi is not only a toy but more a meaningful development kit for Raspberry Pi After all the study and hands on practice of the kit you should have a better understanding of Raspberry Pi Now ge...

Page 67: ...al study investigation enjoyment or other non commercial or nonprofit purposes under the related regulations and copyrights laws without infringing the legal rights of the author and relevant right holders For any individual or organization that uses these for commercial profit without permission the Company reserves the right to take legal action S u n F o u n d e r ...

Reviews: