You are already one step closer to building your own robot as you already know the basics of electronics and have developed few applications using Raspberry Pi. To take further steps in this direction, in this chapter, you will learn how to develop a remote-controlled robot that will be able to live stream video over the Internet and also send distance of the nearest object in real time. Before you move ahead, you need to understand a few more electronics components:
As mentioned earlier, in this chapter, you will develop a remote-controlled robot with some add-on features, so the first thing that you need to make a robot movable are wheels. Now, once you got the wheels, you need some mechanism that can control the speed of the wheels. Motors are used for this purpose; they convert electrical energy into mechanical energy. A world without electric motors is difficult to imagine. From the tiniest motor found in a quartz watch to a million-plus horsepower motor powering a ship, motors are used in many diverse applications. The following screenshot is an example of a motor:
There are basically two types of conventional electrical motor available: AC type motors and DC type motors. AC motors are generally used in high power single or multi-phase industrial applications, where a constant rotational torque and speed is required to control large loads such as fans or pumps. DC motors are used in many electronics, positional control, microprocessor, PIC, and robotic circuits. Another DC motors that is commonly used in robotics is the stepper motor. This is particularly well suited to applications that require accurate positioning and a fast response to starting, stopping, reversing, and speed control. Another key feature of the stepper motor is its ability to hold the load steady once the required position is achieved. For example, in a normal DC motor, once you stopped the power to motor, it will still go to some distance because of the continuous motion, while in a stepper motor, once you stop the power, it will stop instantaneously.
A multimeter is an electronic measuring instrument that combines several measurement functions in one unit. A typical multimeter can include features such as the ability to measure voltage, current, and resistance. It is also known as a volt-ohm meter (VOM). It can be of two types: analog multimeter and digital multimeter based on the circuit used in the construction. An analog multimeter usually shows results using a pointer that moves over a scale calibrated for all the different measurements that can be made, while on the digital multimeter digits are displayed on the screen. A typical digital multimeter is shown in the following image:
A typical analog multimeter is shown in the following image:
Most of the time, you will be using digital multimeter, so you need to understand the capabilities and usage of digital multimeter. On the bottom-right corner of the multimeter, you will see three holes where you can connect a measuring cable. If your circuit operates on higher currents, connect the anode (typically a red probe) to the first hole and connect the cathode (typically, a black probe) to the last hole. The second hole is for circuits operating at low current. Most of the time, you will be using the second hole, where you will connect the anode (typically, a red probe) and the third hole, where you will connect the cathode (typically, a black probe). Here are some features that you will be using while developing a robot in this chapter:
Once you have the motors for the movement, you need some kind of base that can hold all the motors at a certain distance for the proper movement of the robot. Apart from holding the motor, you need some firm base on which you can place all the electronics components. In this chapter, you will build a 4-wheel robot, so you will require a 4-wheel robotic base. You will be able to get it from a nearby electronics shop. Have a look at the following image to get a clear idea of how it will look in the real world:
Because most of the electronics power supply provide 5V and most modern sensors displays are 3.3V only, you need to perform level shifting/conversion to protect the 3.3V device from 5V. Although you can use resistors to make a simple divider, for high-speed transfers, the resistors cause a havoc that is tough to debug. For this reason, you have to use a level converter. Those converters can be of two types: unidirectional and bi-directional. As the name suggests, a unidirectional converter can only convert in one direction, that is, either from 5V to 3.3V or 3.3V to 5V. While on the other end, a bi-directional converter can convert 5V to 3.3V as well as 3.3V to 5V.
Depending on the DC motor used, it requires voltage varying from 5V to 36V. Raspberry Pi GPIO can't provide such high range of voltage, so you need some kind of level converter. Moreover, you need to drive the motor in both directions, so some special circuitry is required for this purpose. That special circuit is embedded in a typically used motor driver L293D IC. It works on the concept of H-bridge. Before you understand the functionality of L293D, you need to understand what is H-bridge and the basic functionality of the H-bridge circuit.
H-Bridge is an electronic circuit that enables the voltage to be applied across a motor (in general, any load) in both directions. It can be built using four switches. The following diagram shows the basic structure of an H-bridge:
Generally, it is used to change the direction of the motor, but it can also be used for other two purposes:
S1 |
S2 |
S3 |
S4 |
Result |
---|---|---|---|---|
Close |
Open |
Open |
Close |
The motor moves right |
Open |
Close |
Close |
Open |
The motor moves left |
Open |
Open |
Open |
Open |
The motor free runs |
Open |
Close |
Open |
Close |
The motor brakes |
Close |
Open |
Close |
Open |
The motor brakes |
Close |
Close |
Open |
Open |
Shoot-through |
Open |
Open |
Close |
Close |
Shoot-through |
Close |
Close |
Close |
Close |
Shoot-through |
In the preceding table, you can see there is one more condition "Shoot-through". When the power supply gets directly shorted, it is called "Shoot-through". You should never use these conditions in your circuits.
Motor driver IC L293D has two H-bridge circuits, so you can control a set of two DC motors simultaneously in any direction. This is a 16-pin dual H-bridge motor driver integrated circuit. Due to its small size, it is widely used in robotics applications to control the DC motors. The pin diagram of the L293D IC is shown in the following diagram:
The following table shows the inputs provided and the corresponding result:
Pin 2 / Pin 15 |
Pin 7 / Pin 10 |
Result |
---|---|---|
HIGH |
LOW |
The motor moves in clockwise direction |
LOW |
HIGH |
The motor moves in anticlockwise direction |
LOW |
LOW |
No rotation |
HIGH |
HIGH |
No rotation |
Since the launch of Raspberry Pi board A, people all around the world are exploring it in numerous ways. Computer scientists and researchers have explored the USB camera with Raspberry Pi. However, not all the USB cameras are supported with Raspberry Pi, so Raspberry Pi foundation decided to build a camera module that can directly connect with the Raspberry Pi CSI connector. The 5MP camera module is capable of capturing 1080p video and still images. The module is around 25mm*20mm*9mm and weighs just over 3 gm, which makes it perfect for mobile or other applications where size and weight play an important role.
As mentioned earlier, before powering up the Raspberry Pi, connect the Raspberry Pi camera module to the board using CSI connector. The following steps will help you set up the camera:
sudo apt-get update sudo apt-get install python-picamera
python-picamera
is a pure Python interface to the Raspberry Pi camera module for Python 2.7 and above. The python3
package can be installed using the following command:
sudo apt-get update sudo apt-get install python3-picamera
Open the raspi-config
tool from the terminal:
sudo raspi-config
Select the Enable camera and hit Enter, then go to Finish. Reboot the Raspberry Pi. Now, you can go ahead and start using your camera module.
In this section, you will learn how to use the module by taking a picture and capturing a video using the Raspberry Pi camera module.
Create a file cameraImage.py
with the following script:
import picamera camera = picamera.PiCamera() camera.capture('image.jpg')
The first line makes the picamera
library available to the script. The second line will create the instance of the PiCamera
class. The third line will take the picture and store it in the image.jpg
file.
You have used the Python script to capture an image using the camera module. You can also use the command-line tool that is available to capture an image. raspistill
is the command-line tool for capturing still images with the camera module. Once you have connected and enabled the camera, run the following command in a terminal to take a picture:
raspistill -o cam.jpg
The raspistill
command will take a picture from the camera module, the -o
flag will store data to the output file and the cam.jpg
filename, where you want to save the data.
There are many other options using which you can control the properties of the image taken from a camera module. Some of these options will be discussed in Chapter 6, Image Processing Algorithms.
Similar to the previous section, you can use either the command-line tool or can use the Python script to take a video from the camera module. First off, you will take a video using the Python script.
Create a file cameraVideo.py
with the following script:
import picamera from time import sleep camera = picamera.PiCamera() camera.start_recording('video.h264') sleep(5) camera.stop_recording()
The first two lines make the picamera
and time.sleep
libraries available to the script. Line 3 will create the instance of the PiCamera
class. The fourth line will start recording the video and store it as video.h264
. The fifth line will wait for 5
seconds using the sleep
command. The sixth line will stop the recording and the video clip will get saved as video.h264
.
The
raspivid
is the command-line tool for capturing videos with the camera module. Once you have connected the camera, run the following command in terminal to take a video:
raspivid -o video.h264 -t 10000
This will record a video for 10 seconds.
The raspivid
command will capture a video from the camera module, the -o
flag will store the data to the output file and the video.h264
filename where you want to save the data.
If you do not give any -t
flag with the length of the video, by default, raspivid
will capture a video for 5 seconds.
Ultrasonic sensors work on a principle similar to radar or sonar, which calculates properties of a target by interpreting the echoes from radio or sound waves respectively. This sensor will generate the high frequency sound waves and evaluate the echo, which is received back by the sensor by measuring the time interval between sending the signal and receiving the echo to calculate the distance to an object. In this chapter, you will be using an HC-SR04 ultrasonic sensor Arduino module. It uses sonar to determine the distance to an object. It gives distance with the nearest object with high accuracy and stable reading in an easy-to-use package for distance ranging from 2 cm to 400 cm. Unlike a sharp sensor, which can also be used for measuring the distance, its operation is not affected by sunlight. The following image is an example of ultrasonic sensors:
As you can see in the preceding image, there are four pins on HC-SR04:
Vcc: +5VDC
Trig: Trigger input for sensor
Echo: Echo output of the sensor
GND: Ground
First, you need to send the trigger pulse of 10 µs (micro Second) to the Trig Pin, which will enable the ultrasonic sensor. The ultrasonic sensor module will send the output to the Echo pin.
There are many applications where you might want to use Raspberry Pi as a standalone device. The Internet connection and power supply are the only two things that will hinder the use of Raspberry Pi as a standalone device. The Internet connectivity issue can be resolved using the Raspberry Pi WiFi module. To resolve the power supply issue, a rechargeable battery can be used.
As mentioned in the previous section, to make your Raspberry Pi a standalone device, you need to get rid of the Ethernet cable. For this reason, you will be using the Raspberry Pi WiFi module to use the WiFi network. As you know Raspberry Pi doesn't have the capability to connect to the WiFi network, you will be using $5 WiFi module. To make it work, you need to update the current Internet configuration. Here are a few steps that you need to follow to make the WiFi module work properly:
sudo nano /etc/network/interfaces
nano
text editor:auto lo iface lo inet loopback iface eth0 inet dhcp
This is a very basic configuration that is used to connect Raspberry Pi to the Internet using an Ethernet cable. You need to modify the interface file to enable the Wi-Fi module. Add the following lines to the interface file:
allow-hotplug wlan0 iface wlan0 inet dhcp wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcp
Once you have edited the file, press CTRL + X to save the file and exit nano
editor.
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Delete all the content of the file and add the following lines to the file:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="YOURSSID" #It is same as your WiFi network name psk="YOURPASSWORD" # This is your secret network password # Protocol type can be: RSN (for WP2) and WPA (for WPA1) proto=WPA # Key management type can be: WPA-PSK or WPA-EAP (Pre- Shared or Enterprise) key_mgmt=WPA-PSK # Pairwise can be CCMP or TKIP (for WPA2 or WPA1) pairwise=TKIP #Authorization option should be OPEN for both WPA1/WPA2 (in less commonly used are SHARED and LEAP) auth_alg=OPEN }
Once you have edited the file , press CTRL + X to save and close the file.
sudo reboot
In this section, you were introduced to a few more electronics components that will be used in the next section for building the remote-controlled robot.
18.119.157.62