xie186 / py4kid

0 stars 0 forks source link

Robotics With Raspberry Pi: Build and Program Your First Robot Buggy #1

Open xie186 opened 3 years ago

xie186 commented 3 years ago

https://www.futurelearn.com/courses/robotics-with-raspberry-pi

What you will learn

What you will need

xie186 commented 3 years ago

Parts and tools list for a robot buggy

To build a Raspberry Pi robot buggy, you will need the components and tools listed below. The model and type of components are recommendations that have been tested with a Raspberry Pi, although alternatives are also available. You can view most of the required components on this AliExpress Wish List (https://my.aliexpress.com/wishlist/shared.htm?groupId=100000018016415), but they can be ordered and bought from many other suppliers.

image

image

Tools and other parts ❏ Black tape and white paper or card (to make a track for the line-following robot) ❏ Soldering iron and solder ❏ Wire strippers ❏ Scissors ❏ Screwdriver ❏ Electrical tape ❏ Cable ties or Velcro-type straps

xie186 commented 3 years ago

Software The latest version of the Raspbian operating system

An IDE of your choice

Using a soldering iron can be a bit daunting at first, but whether you are new to soldering or a veteran, this Getting started with soldering resource (https://projects.raspberrypi.org/en/projects/getting-started-with-soldering) will give you some quick tips and tricks.

The Ultrasonic Distance Sensor (UDS) and line-following sensors are required for weeks two and three respectively.

If the UDS is 5V, you will need two resistors to split the voltage so that it can operate safely with the Raspberry Pi at 3.3V e.g. by using a 1200 and a 2200 Ohm resistor. Full instructions on how to do this are in week two of the course.

A ball caster is necessary if your robot has only two wheels; it will act as a third wheel so that the robot is balanced and can move freely.

xie186 commented 3 years ago

Choosing a programming environment

I recommend using an Integrated Development Environment (IDE) during this course to create, run, and test your Python programs.

The IDE I will be using is Mu, which is pre-installed on most Raspberry Pis. Mu is also available at the website Code with Mu along with instructions on how to install Mu on a Raspberry Pi.

If you experience problems or would just like to know more about programming environments, have a look at Raspberry Pi’s Choosing a programming environment project guide.

xie186 commented 3 years ago

Week one

In week one of the course you will attach the motors and get your robot buggy moving.

What part of the course are you most looking forward to? Share your thoughts in the comments.

Important note for teachers in England: In order to get free upgraded access to this course, and to use it towards your National Centre for Computing Education certification, you must have joined the course through a link from the Teach Computing website. If you have not, please join the course on this page and accept the invite on the next page. Failure to do this will mean that you will lose access to the course and it will not be counted towards the certification. Your eligibility for bursary payments may also be affected.

xie186 commented 3 years ago

Using the course content

The contents of this course are free for you to reuse. Unless otherwise specified, you can copy, and adapt the text, images and videos to use in your classes under the Open Government Licence v3.0., so long as you attribute the Raspberry Pi Foundation as follows: This text/image/video was created by the Raspberry Pi Foundation and is licensed under the Open Government Licence v3.0.

xie186 commented 3 years ago

We are a charity that works to put the power of computing and digital making into the hands of people all over the world. We do this so that more people are able to harness the power of digital technologies for work, to solve problems that matter to them, and to express themselves creatively.

Here are some of the things we currently offer to educators:

xie186 commented 3 years ago

The world of robotics

Robots are an integral part of modern life and are set to be even more prominent in the future. They come in all shapes and sizes and are able to perform a wide range of tasks, from building cars to landing on Mars. But defining what a robot actually is can be tricky; ask five roboticists and you are likely to get five different answers!

Robots can vary massively, from fixed-location robots making tiny, precise movements to manufacture microchips, to humanoid robots that can walk around different environments.

What nearly all robots can do, though, is these three things:

Making sense of the environment

Robots are able to sense the physical world, so they can perform actions accordingly. They often use sensors to detect the movement of their own components, as well as detecting other objects nearby. For example, a robotic arm in a smartphone factory may use a motion sensor to keep track of its position, and may use a pressure sensor to ensure it holds components with the correct force, to avoid dropping or crushing them.

A robotic arm used for fitting together an electronic device such as a smartphone.

Between 1966 and 1972, researchers at Stanford University developed Shakey, the first truly mobile robot with the ability to sense its environment. It was capable of navigating around obstacles by using a TV camera and an infrared rangefinder to scan items in front of it. Shakey also had whisker-like feelers to determine if it was touching another object. Instead of relying on a preprogrammed set of instructions, Shakey could piece together simple commands to perform more complex tasks, as well as remembering and adjusting these actions for future tasks. This was integral to Shakey’s ability to navigate spaces effectively, which in turn helped to advance artificial intelligence and pathfinding.

The Shakey mobile robot, with its TV camera, range finder and bump detectors labelled.

Over the years, new and improved sensors have increased robots’ abilities to understand the environment. For example, self-driving cars use light detection and ranging (LIDAR) sensors, which use pulses of laser light to measure the distance to other objects in order to build a 3D model of the world. All this extra information means that robots require more processing power so that they can process data in a quick and timely manner.

The power of processing

Nearly all robots need a device, such as a computer or microcontroller, to receive data from sensors and other components. This device will need to process the data into meaningful information that can then be used to communicate instructions.

In this course your buggy will be based around a Raspberry Pi, a single-board computer that allows a wide range of components to be connected to it. You’ll attach the different components of the buggy to the Raspberry Pi and program it to run autonomously.

Automation in action

The ability to process data and act on this information almost instantly, with minimal human interaction, is one of the major advancements of robotics, and indeed technology, in the last few decades. Most robots will be able to perform some actions autonomously, from a robotic arm that twists the lids onto jars of jam in an assembly line to a drone that is able to return to the location it took off from.

The level of autonomy needed depends on how the robot is designed to interact with the world. Some are closely controlled by human operators, such as medical robots that assist surgeons with performing operations. These robots help surgeons to make very precise incisions that would be difficult without this assistance, and can sometimes even allow surgeons to operate remotely.

A surgeon using a robotic assistant to perform surgery on a patient.

Other robots are much more autonomous and rely on minimal human interaction. A robot vacuum cleaner will use sensors to move around the room while avoiding the furniture, and will map the areas it has visited to ensure it has covered the entire room. Some even have the ability to find a charging station and empty themselves automatically into a designated bin.

Many robots are somewhere in the middle, having the ability to automate some tasks, but also requiring a small amount of human interaction. These semi-autonomous robots are optimal for activities such as navigating contaminated places that would be dangerous for people. It is possible for operators to take over the decision-making from the robot and perform certain actions remotely that the robot may not be programmed to perform.

Discussion

Share your thoughts in the comments section below.

xie186 commented 3 years ago

Raspberry Pi and General-Purpose Input/Output

Robots are able to sense and interact with the environment through a wide range of components.

So that a robot can make decisions and perform actions, it uses a processor to receive information from and send signals back to the other components.

A standard interface for connecting a single-board computer or microprocessor to other components is through General-Purpose Input/Output (GPIO) pins.

Controlling robots using the Raspberry Pi

Raspberry Pi computers are wonderfully diverse in what they can do. Among the many applications they can be used for, robotics is one of the most exciting and powerful.

Each model of the Raspberry Pi has a set of General-Purpose Input/Output (GPIO) pins along the top edge of the board. These can be used for connecting and communicating with all manner of electronic components, acting as a physical interface between the Raspberry Pi and the outside world. Using the pins, you can program the Raspberry Pi to switch components on and off (output), or receive data from sensors and switches (input).

image

The GPIO pins on a Raspberry Pi 4 with a 40 pin header

Raspberry Pi models

Most models of the Raspberry Pi have a 40-pin header, as shown in the image above. Of the 40 pins, 26 are GPIO pins and the others are power or ground pins (plus two ID EEPROM pins, which you should not play with unless you know your stuff!). Any of the GPIO pins can be designated (in software) as an input or output pin and used for a wide range of purposes; whether it is turning on an LED, driving a motor, or sending data to another device, the possibilities are almost endless.

Early models of the Raspberry Pi A and B have a shorter header with 26 pins, as shown below. image

The GPIO pins on a Raspberry Pi 1 with a 26 pin header

The Raspberry Pi Zero models have unpopulated pins (apart from the Raspberry Pi Zero WH) so there are holes where the GPIO header is located instead of physical pins. This means you need to add a header that includes the pins yourself. image

A Raspberry Pi Zero with a solder-less header

Although it is possible to create a robot buggy with most models of Raspberry Pi, I recommend using a Raspberry Pi 3B, 3B+, or 4. These models allow you to program the Raspberry Pi easily and connect it to another computer or even a smartphone by using the inbuilt WiFi or Bluetooth, rather than needing to plug the Pi physically into a screen or a keyboard and mouse.

GPIO pin numbering

When programming the GPIO pins, there are two different ways to refer to them: GPIO numbering and physical numbering. Throughout this course (and in all our resources) we will refer to the pins using the GPIO numbering scheme. These are the GPIO pins as the computer sees them.

image

The layout of the GPIO pins on a 40-pin Raspberry Pi using GPIO numbering, which can be used as a reference guide.

The numbering of the GPIO pins is not in numerical order, instead relating to the numbering on the CPU of the Raspberry Pi, so there is no easy way to remember them. However, you can use a reference board that fits over the pins, a printed reference (like the image above), or a website guide to the GPIO pins to help you.

Voltages The voltage of a pin is labelled on the reference guide. There are two 5V pins and two 3V3 pins, as well as a number of ground pins (0V), which are unconfigurable. The remaining pins are all general-purpose 3V3 pins, meaning that the outputs are set to 3.3 volts and the inputs are tolerant of 3.3 volts.

A GPIO pin designated as an output pin can be set to high (3.3V) or low (0V). Components are usually attached so that setting the output to high will allow current to flow to them, while setting the output to low won’t.

A GPIO pin that is designated as an input will allow a signal to be received by the Raspberry Pi. The threshold between a high and a low signal is around 1.8V. A voltage between 1.8V and 3.3V will be read by the Raspberry Pi as high; anything lower than 1.8V will be read as low. Do not allow an input voltage above 3.3V, or else you will fry your Pi!

A word of caution While connecting most components to the GPIO pins is perfectly safe, it’s important to be careful how you wire things up, or you could damage the Raspberry Pi or the components.

A few pieces of general advice:

Do not attach 3V3 components directly to a 5V pin on the Raspberry Pi, or you may damage the component or your device Certain components, such as LEDs, should have resistors to limit the current passing through them Do not connect motors directly to the GPIO pins; instead, use a motor controller board or an H-bridge circuit In the next step, you will look at how to connect the motors to the motor controller board.

xie186 commented 3 years ago

Connecting a motor to the motor controller

In this step you will start by connecting the motors to the motor controller board, before connecting the battery holder to the motor controller.

The instructions are for a L298N dual H-bridge DC stepper motor driver controller board, and they will be similar for most motor controller boards. Check the documentation for your board if you are using a different one.

What you will need

For this step you will need the following items:

You may also need:

Soldering iron and solder Wire strippers Scissors Tape

Using a soldering iron can be a bit daunting at first, but whether you are new to soldering or a veteran, this Getting started with soldering resource will give you some quick tips and tricks.

image

The DC motors

Most DC motors do not come with wires attached to them, which means you will need to attach your own, using solder.

Preparing the wires

You will need two wires for each DC motor, to connect it to the motor controller board. Alternatively, you can use wire strippers to strip both ends of the jumper leads, to expose the bare wire for attaching to each of the motors. A demonstration of how to strip a wire with wire strippers can be seen in this video: https://www.youtube.com/watch?v=TZFTKbT4XFs.

image

Remove the soft plastic clip from the motors so that the wires can be attached. You can use a flathead screwdriver to help remove the clip.

Thread the bare wire through the contact on the motor. Top tip: It may be easier to solder the wire to the contact on the motor if you bend the wire once it has passed through the contact.

image

Soldering the wires

Turn on the soldering iron and wait for it to heat up. Clean the soldering iron tip before using it; you can use a damp sponge or damp cloth to remove any residue while the iron is hot.

Heat the contact on the motor with the soldering iron for a second or two. Keeping the soldering iron on the contact, touch the end of the solder to the soldering iron tip until the solder melts. Stop applying solder once the contact and wire are connected by the solder.

image

Wait for a minute or two for the solder to cool and then gently try to move the wire, to test whether it is securely attached to the contact. If the wire moves, you can either reheat the applied solder with the soldering iron and realign the wire, or apply more solder to the connection.

Try not to touch with the soldering iron the plastic coating of the wires or any plastic between the two contacts, or the plastic will melt and start to smoke. More tips and guidance can be found in this soldering tutorial: https://projects.raspberrypi.org/en/projects/getting-started-with-soldering

Once the wires have been soldered to the motors securely, trim the end of the wires with scissors. If the wires accidentally touch the metal casing while power is running, this can short the circuit and stop the motor from receiving consistent power.

Reattach the plastic clips to the motors. It’s also a good idea to wrap the end of the motors where you attached the wire in tape, to protect the connection and help keep the solder in good condition.

Connect the motors to the motor controller board

A motor controller board will usually have screw terminals for connecting a motor to it. A DC motor needs two screw terminals for it to work, and a servo motor needs four terminals. The four OUT terminals on the board I am using are circled in green below.

image

A DC motor needs to use two wires so that it can turn forwards and backwards. Sending a high signal to one wire and a low signal to the other wire will turn the motor in one direction, and swapping the signals around will turn the motor in the other direction.

Using a screwdriver, loosen the screws in the terminal blocks labelled OUT1, OUT2, OUT3, and OUT4. Have a look at the documentation for your board if your labels are different.

Strip the ends of the wires; you can snip off the ends if you need to. Insert the stripped ends of one motor into the OUT1 and OUT2 terminals and the stripped ends of the second motor into the OUT3 and OUT4 terminals. Tighten the screws so the wires are secured firmly in the terminal blocks.

image

xie186 commented 3 years ago

Connecting the Raspberry Pi and battery pack to the motor board

Your next step is to connect the motorboard to the GPIO pins on the Raspberry Pi, and to the battery pack. This will allow you to power the motorboard and control the motors using the Raspberry Pi.

What you will need

You may also need:

GPIO reference card

image

Connect the battery pack to the motorboard

The motors require more power than the Raspberry Pi can provide. Therefore, you will use four AA batteries to power them.

The battery pack should include two wires: one red and one black. The red wire is for the positive terminal and needs to be inserted into the VCC terminal block if it is present, or the ‘voltage in’ terminal block labelled +12V otherwise.

image

The black wire is the ground wire and must be inserted into the GND terminal to complete the circuit. Make sure all the screws of the terminal blocks are tightened securely.

image

Insert the four AA batteries into the battery pack and if it has a switch, turn it on. Most motor controllers have a red LED that lights up to show that it is powered on. If the LED is not lit up, the wires may not be connected properly. Also check that your battery pack is full, and the batteries are the right way around.

Receiving input

These instructions are for a L298N dual H-bridge DC stepper motor driver controller board, and they will be similar for most motor controller boards. Other boards may connect differently to the one that I’m using, and some boards can simply be placed onto the Raspberry Pi GPIO pins as a HAT. Check the documentation for your board if you are using a different one.

On this motor controller board there are pins labelled IN1, IN2, IN3, and IN4. Some motorboards also have one or two GND (ground) pins next to the IN pins, but this board does not.

image

Which GPIO pins you use on your Pi is up to you; I have used GPIO 7, 8, 9, and 10. However, if you use different GPIO pins, make sure you remember which ones, as you will need to refer to them in the code.

Use five female-to-female jumper leads to connect the Raspberry Pi GPIO pins to the pins on the motor controller board. Each DC motor will need to need to use two IN pins to connect to the Raspberry Pi, so that the motor can turn in both directions. You will also need one GND pin to complete the circuits; this can be the same pin for both motors.

GPIO pin Connects to Board pin
7 <–> IN1
8 <–> IN2
9 <–> IN3
10 <–> IN4
GND <–> GND

If your motorboard does not have a GND pin, use the terminal block that the battery pack also uses. Strip the end of the wire for the GND pin and secure it into the GND terminal block that your battery pack feeds into. There will now be two wires fed into the GND block: one from the battery pack and one from the Raspberry Pi.

image

Powering the Raspberry Pi The Raspberry Pi will need its own source of power. For testing, the Raspberry Pi can be plugged directly into the mains power supply. However, when you come to building the body of the buggy and making it move, the Raspberry Pi will need a mobile source of power. A USB power bank is a good choice, as it will be fairly light for the chassis to carry. Most USB power banks are capable of powering a Raspberry Pi, since they usually have an output voltage of 5 V. Make sure the USB power bank will not shut down the power output after some time, or interrupt the power output when you connect the power bank to mains.

Setting up your Raspberry Pi If you are not familiar with setting up a Raspberry Pi and using it, follow this guide to Getting started with Raspberry Pi: https://projects.raspberrypi.org/en/projects/raspberry-pi-getting-started

The programming language I will be using in this course is Python together with a library called GPIO Zero, which provides an accessible way to control components through the GPIO pins.

Python and GPIO Zero will be installed by default if your Raspberry Pi is running the Raspbian operating system. Follow this guide to Installing GPIO Zero (https://gpiozero.readthedocs.io/en/stable/index.html) if you are using Raspbian Lite or another operating system.

If you would like to control your Raspberry Pi remotely from another computer, check out our guide to Creating a virtual desktop using VNC Server: https://www.raspberrypi.org/documentation/remote-access/vnc/

xie186 commented 3 years ago

Testing the motors

In this step you will label and test the motors, to help ensure that your robot will correctly go forwards, backwards, left, or right as commanded.

Labelling the motors

The easiest way to keep track of which motor is left and which is right is to label them.

xie186 commented 2 months ago

https://projects.raspberrypi.org/en/projects/build-a-buggy

https://projects.raspberrypi.org/en/projects/rpi-python-line-following/1