Marty Scratch Fun - Shaking Hands!
Marty Scratch - Shaking Hands
As a fun and fairly simple Scratch project, I've been programming Marty to shake hands!
This demonstrates few concepts - we have to move joints, show expressions - but to do it properly we also need to know whether somebody has grabbed Marty's hand - and for that we need to read the sensors!
It'll look a bit like this:
Getting started - handshake movements
There will be two main bits to this - moving and sensing. First off, let's sort out the movement bit!
The first bit of any handshake is to proffer a hand, so lets activate Marty and raise the right hand:
With the arm (and hand) raised, we need to actually shake hands. That'll mean moving the arm up and down multiple times. A repeat block will help!
In my version, I've made Marty shake its hand 4 times, but thats up to you!
Once the handshake is done, it's probably sensible to put the arm down again
Adding some emotion
That's the basic arm movements, but wouldn't it be nice if Marty's eyebrows moved too. We can do that!
Feeling more adventurous? Why not make Marty's eyebrows move each time the hand moves up and down!
We've got the movements, but really Marty needs to be able to tell when to shake hands. To do that, we're going to use the motor current sensors to detect when there is some force on the arm
Reading motor current
Let's make a variable called right arm torque
And to start we can set it to the right arm motor current
But, the motor current sensors give very small numbers - try pushing Marty's right arm a bit and double clicking the
set right arm torque block above. You should see something like this:
Let's make that variable a bit bigger:
Now when we set the
right arm torque variable, it'll be much bigger:
We're going to want to update this variable often, and we don't want to have to code the calculation more than once, so let's define a function to update the sensor values for us:
Putting it together
Ok then! Let's use our shiny new variable and
update_sensors function to make the handshake interactive!
There a few things to notice here:
- we wait for a second after moving the arm, to give the motors a chance to settle
- we call
update_sensorsbefore entering the loop, to make sure we're not using an old sensor value at the start
- we use a
repeat untilblock to loop until Marty detects an amount of force on the right arm. In this case we've set the threshold to 3, but try varying it to see what happens!
Knowing when to start
But how should Marty know when to start a handshake? That's up to you, but I've made mine so that touching either arm will make Marty offer to shake hands. To do that I made another variable:
And I added another block to the
update_sensors function we made earlier
To keep things tidy, I moved the actual handshake-y bit into its own function, and made a separate block of code to keep track of sensor readings and handshake when ready!
I've also added a cheeky wiggle at the end
Scratch project file
To get the most from this, I'd recommend copying the blocks yourself, but if you'd rather have a Scratch project file, I've uploaded one here ;-)
Making it bigger!
You don't have to stop there! I have an even better handshake with my Marty, it has high fives and sound and everything! It looks like this:
The Scratch for it looks like this:
And the Scratch file for it is here
There's something a bit more advanced here too - I wanted to move both Marty's arms at the same time, so I've turned off blocking mode. You can read more about how that works in the Scratch docs
So that's my Marty handshake!