marty's balancing act!
Marty has a new job serving food and drink at a posh restaurant! How much can be loaded onto the tray before it becomes too heavy and Marty drops it all?!
Using Scratch 3, you’ll program Marty the waiter to hold a tray and sense it’s weight. Take it in turns placing coins on the tray until it becomes too heavy and Marty drops everything on the floor!
what you will need
- A Scratch 3 compatible device
- Marty the Robot
- Sellotape/ glue
- Some coins (or any other small heavy items… eg marbles)
- Something to make the tray (we used a Pringles tube lid)
What will you learn about?
- How to detect force on individual body joints using force sensors
part 1: Getting marty ready to be a waiter!
The motors in Marty's arms can only move a certain amount and currently they do not go high enough for our waiter game! We can change this by altering the default position the arm sits at.
Open up Marty's head by loosening the two screws near the bottom of the head.
Then remove the arm that you will be holding the tray with (we chose the right arm)
Notice that the bottom gear that the arm was attached to has two slits that should currently be vertical. Remove that gear from the side panel. Then re-attach it, but this time make sure that the two slits are now horizontal.
Re-attach the arm and screw Marty's head shut. You will notice that the arm we just attached now sits horizontally instead of by Marty's side.
Part 2: Make a tray for Marty
Now that Marty has the arm movement needed, we need to create a tray to be carried. You can use anything you lying around, but it helps if it has a lip on it as otherwise the items can slide off. We used the lid from a Pringles tube.
You could just sellotape the tray to Marty's hand, but if you want to play the game a few times it is a pain to have to do this each time. Instead we rolled up a sheet of paper until it just fitted inside Marty's hand and sellotaped it to the bottom of the tray as shown below.
Now turn Marty's hand through 90 degrees so it looks like the photo below when viewed from above.
Finally slot the roll of paper through Marty's hand so that Marty holds the tray, ready to serve you!
part 3: Program The Game in Scratch
Firstly, we need to read the motor current in the arm joint which tells us how much force is being placed on the arm. The force being placed on the arm increases as the weight of the tray increases. We can do this by storing the value being read from that joint into a variable so that it makes the value easy to regularly check.
To make a new variable, select the Variable option in the code blocks drawer followed by the Make a Variable button. You will be asked what you want to call your variable - choose something meaningful so that later on you remember what it's storing! We called ours “right arm sensor”
The variable should be set to store the current from the right arm motor.
The numbers that we get back from the sensors can be very small. Try gently pushing down on Marty's right arm and clicking on the variable code blocks we just put together and see what value you get displayed on the stage section of the Scratch screen.
Hint: You will need to make sure that the checkbox beside the variable in the code blocks drawer is checked!
Lets make this number bigger by multiplying it by 1000
Now try testing out what numbers we get back from the sensors!
It's important that we regularly update the number that is being stored in our variable so that we know how much weight Marty is carrying at all times.
We can easily do this by wrapping the set variable block in a forever loop as shown.
Now that we can constantly monitor the weight Marty is holding, we need to program how Marty should behave when the weight gets too much.
This block of code will run at the same time, or in parallel, to the code sensing the weight Marty is holding. To make this happen, we use the same “when green flag clicked” block to start both sections of code running.
The first thing we want Marty to do is raise the tray so that it sits horizontally. We found that this occurred when we set Marty to move the right arm to -10. However, your Marty might have been calibrated slightly differently so you may want to experiment with values until the tray is flat enough to place the coins on.
Since moving the motors requires current to flow through them, the value in the variable will go up while Marty raises the arm. We want the current to go back to 0 (or near 0) before we start monitoring it, otherwise Marty will immediately drop the tray before we add anything to it! Adding a wait 5 seconds block will sort this out.
Now we need to program Marty to respond if the weight of the tray gets too high by lowering the arm and dropping the tray. We want Marty to constantly check whether the tray is too heavy so we need a forever loop, just like we did beforefor the variable.
We want Marty to drop the tray ONLY if it gets too heavy. We can do this using an ‘if’ statement.
You can choose the value above which you want Marty to drop the tray. We found that values between 1 and 4 tended to work best. The current through the motor changes slightly even before we add weight to Marty’s tray so if you choose a value below 1.5 sometimes Marty drops the tray randomly. Choosing a value higher than 4 on the other hand requires more weight than can be fitted on the tray! However, all motors are slightly different so do experiment a bit for yourself!
The if statement goes inside the forever loop.
Inside the if statement we want to instruct Marty to lower the right arm quickly if the weight of the tray is too high. We did this by moving the right arm to 120, but you can experiment with different values if your Marty is calibrated differently.
That’s all the coding done! Your finished code should look like this:
playing the game!
After Marty holds the tray out you and as many other players as you want can take it in turns to place a coin (or other small heavy object) on Marty’s tray. Keep doing this until someone adds the coin that causes Marty to drop the tray, ending the game.
You can repeat the game for different weights, by changing the value the ‘right arm current’ must be less than in the ‘if’ statement.