The gripper
grip, release, holding: knowing when you have something.
Do this lesson in the simulatorServo 0 opens and closes a pair of jaws at the front of the robot. Closed, they hold a ping-pong ball. gripper("close") closes them, gripper("open") opens them, and holding() tells you whether the jaws actually caught something.
Open and close
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
print("holding at the start:", holding())
# close the jaws (it waits while they move)
gripper("close")
print("holding with nothing there:", holding())
# open the jaws and let go
gripper("open")
gripper("close") waits while the jaws move, about 0.4 s, so the next line runs once they are shut. gripper(1) and gripper(0) do the same as "open" and "close". holding() is the colour of the ball in the jaws, or None: nothing was in reach, so it is None.
Underneath, the gripper is servo 0 from lesson 7.1: gripper("close") is servo(0, 90) then wait(0.4), and gripper("open") is servo(0, 0) then wait(0.4).
Get close enough
The jaws reach about 3 cm past the front of the robot, and the ball is a ping-pong ball, 4 cm across. The ball has to be there:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# as long as the thing ahead is further than 6 cm
while distance() > 6:
# drive forward at 40 (keeps going until the next command)
forward(40)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()
print("stopped", distance(), "cm from the ball, measured from my centre")
# close the jaws (it waits while they move)
gripper("close")
print("holding:", holding())
distance() measures from the robot's centre, and the front is 3.5 cm out, so a reading of 5 or 6 means the ball is 2 or 3 cm in front of the jaws. Close enough, and holding() says red.
It comes with you
Once held, the ball rides in front of the robot wherever it goes:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# as long as the thing ahead is further than 6 cm
while distance() > 6:
# drive forward at 40 (keeps going until the next command)
forward(40)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()
# close the jaws (it waits while they move)
gripper("close")
# slide left at 50 for 15 cm, then stop
left(50, distance=15)
# turn 90 degrees clockwise, then stop
turn_right(30, angle=90)
# drive backward at 50 for 10 cm, then stop
backward(50, distance=10)
print("still holding:", holding(), "at", position())
# open the jaws and let go
gripper("open")
print("after release:", holding())
Turning, strafing, reversing: the ball stays in the jaws until they open.
Knowing what you hold
On the real robot the gripper has no sensor of its own. holding() comes from the camera: a ball filling the bottom of the picture right in front of the jaws after a grip. If you want a second opinion, distance() reads very short while a ball is in the jaws.
Task: grab the ball
Drive up to the ball, grip it, and print holding: red.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# drive forward at 50 for 20 cm, then stop
forward(50, distance=20)
print("holding:", holding())
Challenges
- Try gripping from 8 cm and from 4 cm. Where does it stop working?
- Grip the ball, carry it 20 cm, release it, back away 10 cm, and grip it again.
- Print
distance()before and after the grip. - Close the jaws slowly with
servo(0, angle), in steps of 10 degrees with a short wait between, and printholding()at each step: at what angle does the ball get caught?