Introduction: The Covid-19 Delivery Truck - Legos and Scratch Programming
Lego Robot With the BrickPi Using a Raspberry Pi and Scratch Programming
I have been working with children and the Lego Mindstorms kits for 18 years. I started with the RCXs, some of which still work, although I have to use my 20 year old laptop, whose screen is held on with duct tape, for it's Win98 OS. Then I went on to the NXTs, one of which just died this summer, which are still being programmed by a 13-14 year old Mac. Since I work in a school that is just under 50% underprivileged children, there's not a lot of money and I can't update my robotic hardware often, especially since these kits still work just fine for 8-10 year old kids, mostly boys.
BUT, a few years ago I discovered the Raspberry Pi. I found a plethora of examples and how-tos and really enjoyed learning with it. After playing with that a while I got a BrickPi which combines with the Raspberry Pi to replace the intelligent brick that Lego Mindstorms kits use. One of the things I liked about the BrickPi is that the children could program the robots with Scratch which they learn in school and can carry over to other projects. However, the building options provided are limited and the Scratch examples are sufficient, but bare-bones and would have been frustrating for my kids to use. (I've been spoiled by using the NXTPrograms.com website which gives a build AND a working, often very detailed, program to run the robots!) So I'm working on creating a similar set of robotic plans and programs for my class.
Enter The Time of Covid-19. As an educator, I continued working with kids, via Zoom meetings, etc, but that left me with more time than usual. I'd been looking for new builds for my kids, and I turned up some good ones! I found this pickup-truck (also here), called the Intelligent Car by Lego. Since we had to hand deliver materials to the younger kids at our school, I decided to make the Covid-19 Delivery Truck! Of course, that was in March, and it's now September, but we are making our deliveries again... :\
- An extra Lego Mindstorms Ultrasonic sensor
- Brickpi Starter Kit, which includes a Rasberry Pi, temperature, humidity and pressure sensor, although you will need to purchase more cables to run your RPi alone
- BrickPi3 Base kit PLUS
- Raspberry Pi 3 or better and all its cables
- Make sure you have the 8 battery pack that's included with the BrickPi. I'm not sure you can replace it with one from Amazon
SHORT heat sinks, 1 each, about 1/2" and 1/4" (may be included in RPi link above) They MUST be the shorter ones or they interfere with the BrickPi
Wireless mini keyboard and touch pad - for remote control at the end of the project
Nice to have
Full sized keyboard and mouse - MUCH easier to program with
Universal AC adapter with the proper size barrel connector - to cut down on batteries needed to program your truck. MAKE SURE IT'S SET TO 9 VOLTS SO YOU DON'T BLOW OUT YOUR BRICKPI!
Step 1: Set Up the BrickPi With the Raspberry Pi
For the basic setup, I'm going to send you to the websites that describe their setup because they are much clearer than I could make as well as mine just being redundant.
Raspberry Pi setup - Mostly you are going to want to use the BrickPi setup, make sure you use the BrickPi robotics image on your SD card, not NOOBs or the Raspberry Pi version of Raspbian, or your robot won't run. But there is help here with the monitor and keyboard and other useful info.
Before we go on to the BrickPi setup, you might like to add heatsinks if you need them because the BrickPi closes the RPi in and you can't get to it without taking it apart. The RPi doesn't come with heatsinks installed. The Image to the left shows without heatsinks and the image to the right shows where to place the heatsinks.
BrickPi setup - Not only is there a very detailed setup explanation, but there are also some other instructables about the BrickPi! So look for them!
Step 2: Make Your Lego Creation
I created the Lego Intelligent Car which I promptly renamed The Covid-19 Delivery truck because I envisioned contactless delivery crayons, paper, scissors and all the things elementary kids need to learn, explore and create.
But you could use any almost any Lego vehicle. I like to use NXTprograms.com for my kids because the instructions are so clear. This instructable shows you how to use a BrickPi (see image of the BrickPi and the NXT and see the differences.) The only real problems going from the NXT intelligent brick to the BrickPi are
- when the Intelligent Brick is completely enclosed because the BrickPi is taller than the Intelligent Brick and
- figuring out how to connect the BrickPi to the vehicle since the cases are different.
Step 3: Changes in the Original Instructions
I used a 2nd ultrasonic sensor rather than the front light sensor (see build instructions pp. 34-38 and the above image) because I am going to use this in a school setting and I envisioned the kids trying to run the car down the stairs! The build is almost exactly the same, but I put the ultrasonic sticking “out” of the “engine area” rather than inside like the light sensor because I am hoping the extra inch will be enough to allow the truck to come to a stop if it detects a drop-off! Also, because of the length of the ultrasonic sensor, it is too long and drags the floor, so I moved it up one hole.
I also added a pickup "bed", mostly to contain the wires, but also to contain the crayons and pencils I envisioned delivering to kids.And the cab is higer since the BrickPi is taller than the originally-designed-for NXT intelligent brick.
The last major change was to replace the 4 1/2 bushings and 2 white rubber bands of the original instructions (steps 30 and 31) with 2-24-toothed-gears.I did it because I didn't have any white bands! But I think that the 2 gears will be sturdier than the 2 bands.
Step 4: Connect the BrickPi to the Chassis
Because we are not using the NXT Intelligent Brick, we have to create another way to connect the BrickPi to the chassis of the Truck. I have found that it is easier to use the shorter connector, the darker grey one seen in the middle of the three connectors in the image, to attach the BrickPi to a Lego body. I found the more common longer black one very difficult to fit into the holes.
For this truck, I made a platform with 4 "L" shaped perpendicular pin connectors and 4 of the short connectors to hold the BrickPi in place.
Ignore the wires at this point. We'll do that soon, but this picture was taken after the truck was wired up.
For other creations, I've used the 6 holes in the sensor panels of the BrickPi, either with the short connectors or a long axle through one of the holes (see image above with the red beams).
Step 5: Connect All Your Wires
The wires are so convoluted, I can't even show you in an image, but the building instructions explain well.
These are the labels and setup for the Scratch Program to follow. If you are using a different lego build and need to change the ports on your motors or sensors, just change them in the Scratch Program as you are writing it.
MA - Drive Motor
MD - Turning Motor
S1 - Forward Looking Ultrasonic Sensor
S2 - Downward Looking Ultrasonic Sensor
S3 - Right Rear Bumper Push Button
S4 - Left Rear Bumper Push Button
The wires were hard to contain, so I added a few Lego beams and axles to provide a bed and hold the wires down, shown in the image. MAKE SURE THE WIRES DO NOT INTERFERE WITH THE GEARS because, as you can see, it's a tight fit.
Step 6: Make Your Final Changes for the BrickPi - the Battery Pack and Truck Bed
Add the battery pack however it works for you, making sure that you don't block the gears, and if you want, create a truck bed that also holds the wires in place.
I chose to make the tall version shown because I didn't like the way the lower one messed with the "windshield" area, and I really did want the bed of the truck to be able to carry things, but you can put the battery where ever you want. If your robot doesn't need to move much, you can use a universal AC adapter.
Step 7: Program With Scratch
Before you start programming, set the truck up on a box, making sure none of the moving parts, like motors, wheels or gears, touch the box. If you don't get it off the work surface, you're forever chasing down the truck as it drives away from you while you're testing!
Also, I like to use the AC adapter when I program so I don't have to keep on changing out batteries. Obviously, this would still work for a stationary robot, but for our truck, I will have to change it out for a battery box or a portable power bank of sufficient power so it can move around.
I used a number of Scratch instruction sites to figure out how to program my robots:
- Here's an instructable on How to use Scratch, just Scratch, not Scratch for Robots on the BrickPi, but if you don't know Scratch at all it's very useful.
- Here is Dexter's version of LEGO Robots with Scratch BrickPi and Scratch Programs for BrickPi.
- The MagPi, a magazine written for the Raspberry Pi, has BrickPi with Scratch in it's 18th issue.
Step 8: Program Your Motors to Move - Just the Motors!
Start with the smallest Scratch program. This is a standalone file to test your motors, one by one. It askes you which motor you want tested (A, B, C, or D) and then runs that motor. This will tell you if your motors are set up correctly.
Create these variables:
Creating variables makes it easier to test because you can change things, like the speed, more easily.
For motors, use the "broadcast" method to tell the BrickPi which motor goes which speed.
I always create the "0" method (at the bottom) so that I can stop the motors if something is wrong.
There are 2 more files that you can try. There is file is to test both motors together. You have to make the orange variables, in this file as well as in the next file, which adds turns.
Step 9: Program Your Two Ultrasonic Sensors to Sense the Proper Distance - Just the Ultrasonic Sensors!
This is a standalone file to test your ultrasonic sensors. Again, you use "broadcast" to tell the BrickPi which sensor goes to which sensor port. Sensors also need the "broadcast SETUP" and broadcast START" to work. Note that if you want continuous updating of the sensors, which you usually do, you'll have to put the "broadcast UPDATE" in a loop with the "broadcast " In this simple program, we "set" the sensor variable and it shows on the screen with the Scratch Cat.
You have to run the program thru one time so that the BrickPi can recognize the sensors so that you can set the sensor value up properly. For example, when you first write the program, the "S1 US cm" isn't available until you run the program once through.
Create these variables:
- Ultrasonic Distance
- Ultrasonic Distance 2
The ultrasonic sensors gave me trouble when I was testing it out until I pinned down the wire for the down-facing sensor with another axle as shown above.
Step 10: Program Your Two Push Buttons to Register When the Bumper Is Pushed - Just the Push Buttons!
This is a standalone file to test your bumper.
There are no variables to create. The coding of the push buttons is very similar to the coding of the ultrasonic sensors. Here, instead of having the sensor value read onto the screen, we have the Scratch Cat just tell you that the button has been pushed.
Step 11: Put the Code All Together! First Initialize and Set Up a Safety Feature:
Put the rest of the scratch file code in a single file which will drive the car, stop it, go forward and backward, turn and automatically change directions based on input from the sensors. I called it "BrickPi_Covid-19_Delivery_Truck".
These are the variables that you will eventually have to make:
This part of the code sets up the variables and sets up the "0" command which kidll the motors if you make a mistake or need to change the code. The next part of the file adds the "go forward" code.
Step 12: Code for Go Forward
Add to "BrickPi_Covid-19_Delivery_Truck" after the init code.
To drive the truck forward, use the "up arrow."
Note the "broadcast Go Forward" command and the "when I receive Go Forward" block. This is one way to creat modular code in Scratch.
This code is used for 3 things: starting up straight ahead, going faster, or starting forward again after backing up. So it checks to see if the "backing up" variable is true (1). If it is true,it sets the variable to false (0) and resets the truck to what it was before it started backing up. Otherwise it speeds up and resets the ultrasonic sensors to start checking for walls and stairs again.
Step 13: Code for Go Backwards
Add to "BrickPi_Covid-19_Delivery_Truck" after the Go Forward code.
The backup speed is set at a slow constant speed. The truck continually checks it's back bumpers when it it going backwards. If pushed, it goes forward again.
Step 14: Code for Slow Down and Turn
Add to "BrickPi_Covid-19_Delivery_Truck" after the Go Backwards code.
The "down arrow" makes the truck slow down. It just decreases the drive speed variable.
The single turn motor runs along a track so the coding is simple. +1 moves it one way, -1 moves it the other. You use the "left arrow" and the "right arrow" for turning.
Step 15: And You Are DONE!
Change your full sized keyboard for a wireless mini keyboard. Hit the green flag in Scratch, disconnect the HDMI cable and go make your Covid-19 hands-free deliveries!