In Learn Robotics with Raspberry Pi , you'll learn how to build and code your own robot projects with just the Raspberry Pi microcomputer and a few easy-to-get components - no prior experience necessary!
Learn Robotics with Raspberry Pi will take you from inexperienced maker to robot builder. You'll start off building a two-wheeled robot powered by a Raspberry Pi minicomputer and then program it using Python, the world's most popular programming language. Gradually, you'll improve your robot by adding increasingly advanced functionality until it can follow lines, avoid obstacles, and even recognize objects of a certain size and color using computer vision.
Learn how - Control your robot remotely using only a Wii remote - Teach your robot to use sensors to avoid obstacles - Program your robot to follow a line autonomously - Customize your robot with LEDs and speakers to make it light up and play sounds - See what your robot sees with a Pi Camera
As you work through the book, you'll learn fundamental electronics skills like how to wire up parts, use resistors and regulators, and determine how much power your robot needs. By the end, you'll have learned the basics of coding in Python and know enough about working with hardware like LEDs, motors, and sensors to expand your creations beyond simple robots.
My little dude's language is all science/math/robotics/#STEM, so coding is a natural fit for him, but something I know NOTHING about! I appreciate finding cool resources to fuel his interest and learning, and these books @nostarchpress sent for review are perfect! He's been studying up for days since they arrived!
NOOBS—New Out Of the Box Software—from the Raspberry Pi Foundation
The terminal (or shell) is a way for you to give instructions directly for your computer. Specifically, it is an interface in which you can type and execute text-based commands. This is in contrast to the graphical user interface (or GUI, pronounced “gooey”) used by modern operating systems, which you probably know as a desktop. The GUI allows you to use a computer to do many advanced things in simple and visual ways. If you haven’t used a Raspberry Pi or a Linux machine before, you most likely will have only ever used a computer through its GUI.
The terminal will let you know where you are in the filesystem by listing your location before the prompt. This will turn out to be very useful.
You can think of it as moving you back “up” the directory path. So running cd ..
after the first few letters of the directory or f ilename, press the tab key. This will autocomplete the name of the directory. Try it out now. Second, if a command goes wrong or you want to execute it again or edit it, use the up and down arrow keys to scroll through your command history.
The Raspberry Pi has no power switch, and you disconnect power to it by pulling out the power cord. Every time before you do this, you need to safely shut down the operating system. Whenever you want to turn off your Pi, run this command: sudo shutdown now
To find out your Raspberry Pi’s IP address, simply open a terminal and enter the following command: ifconfig
Programming is the process of writing a set of instructions that tell a computer how to perform a task. You can program video games, apps, or even, in our case, robots. The only thing that limits what you can do with computers and programming is your imagination!
a potential difference, known as a voltage. A voltage simply pushes electrons through a conductor
Electrons are negatively charged and are therefore attracted to the positive terminal of the battery,
Although the electrons flow from negative to positive, it is convention to think of the current flowing from positive to negative.
If this voltage is increased, more electrons would be pushed around the circuit and the current would be larger.
Resistance simply reduces current.
In programming, a library is a collection of functions a program can use.
In programming, variables are names used to store information to be referenced and manipulated in a program.
If you change the direction of the voltage (sometimes referred to as reversing the voltage), the motor will spin the other way.
In both brushed and brushless motors, electricity creates electromagnetic forces that are responsible for spinning the motor shaft; however, brushless motors are more complicated and require additional expensive circuitry to function. Brushless motors are usually used for more serious tasks, like remote-controlled model airplanes and drones.
A cheap motor will usually have a high number of revolutions per minute (RPM), spinning around 1,000 to 3,000 times in a single minute, which is far too fast for a small robot. At these RPMs, each motor will create very little torque. Torque is the driving force, and the smaller it is, the more your robot will struggle to move along a surface. To fix this, your motors need a gearbox to bring the RPM down and increase the torque. The ratio of the original RPM to the new geared, lower RPM is called its reduction rate, and 48:1 is a decent reduction rate for this project.
A slow robot is usually a sign that not enough power is being provided to the motors.
Let’s give your robot some control over its speed. To do this I’ll introduce a technique called pulse-width modulation (PWM)
The Raspberry Pi is capable of providing digital outputs but not analog outputs. A digital signal can be either on or off, and nothing in between. An analog output, in contrast, is one that can be set at no voltage, full voltage, or anything in between.
PWM works by turning a GPIO pin on and off so quickly that the device (in our case, a motor) “notices” only the average voltage at any given time. This means that the state is somewhere in between 0 V and 3.3 V. This average voltage depends on the duty cycle, which is simply the amount of time the signal is on, versus the amount of time a signal is off in a given period.
In programming, each time a loop goes round and executes again is called an iteration.
Ultrasonic distance sensors are designed to sense object proximity using ultrasound reflection. A sonar system like this sends out waves that bounce off obstacles. A receiver then detects the returning sound waves. Ultrasound is accurate within short distances (around a few meters) and is inaudible to humans.
We know the speed of sound is constant at 343 m/s, and we can measure how long the sound wave takes to bounce off an object, which is the time. If you rearrange the equation to solve for distance
will send out a signal, also known as a ping
Parentheses follow a function name, and the contents of such parentheses are referred to as the function’s parameters or arguments. These parameters allow us to pass information into a function for later use.
you need to indent the code inside the function so Python knows what code belongs to the function.
the HC-SR04 is able to detect distance only in a single straight line, so your robot could miss obstacles that are directly in front of it but too low or too high for it to sense.
RGB, which stands for red green blue. computers normally represent the levels of each color as a range of decimal numbers from 0 to 255 (256 levels). You can calculate the exact range by multiplying the number of possibilities for each level: 256 × 256 × 256 = 16,777,216. That’s almost 17 million different colors!
A loudspeaker (or just plain speaker) converts an electrical audio signal into a sound that can be heard by humans. In order to translate an electrical signal into an audible sound, speakers use an electromagnet to vibrate a cone. This cone amplifies those vibrations and pumps sound waves into the surrounding air and to your ears.
We’re going to use infrared (IR) sensors to make your Raspberry Pi robot follow the black line. light is reflected more by a white surface, and almost totally absorbed by a black surface, enabling the IR sensor to detect black lines on white backgrounds.
If the left sensor doesn’t receive a reflected signal but the right sensor does, this must mean that the left sensor detects the line. This indicates that the robot must be veering right, so it should turn left to correct itself
your robot may perform better if it goes around corners slower. You could add this functionality into the program by creating another constant called CORNER_SPEED and using it in the functions for turning left and right.
You could even race against some friends and have a line-following racing tournament to see whose code is best.
Computers use RGB for displaying colors, but for processing images and the color data they contain, the HSV color format is much more appropriate. HSV stands for hue, saturation, and value, and it is just another way of digitally representing color with three parameters. This forms a mask (see Figure 821) that simply keeps the parts of the image we want and removes the parts we don’t want.
Then at z, we set a minimum and maximum area for the col ored ball. This prevents your robot from detecting and following any colored object smaller than 250 square pixels or larger than 100,000 square pixels.
In programming, an array is a collection of information where each piece of data in the array has an index, or location, associated with it.
In Python, the first piece of data in an array has an index of 0, the second piece has an index of 1, the third piece has an index of 2, and so on—in other words, Python starts counting the items in an array at 0.
How fast your robot moves does have a big effect on the quality of its image processing: usually, the faster it goes, the more likely it is to miss your colored object.
An extension of this would be to make your robot move toward certain colored balls, but run away from others. For example, it could love red balls but be terrified of yellow ones!