The kicker
kick(): aiming, and how far the ball goes.
Do this lesson in the simulatorServo 1 drives a spring-loaded lever at the front: the kicker. It is a continuous-rotation servo, the kind that spins instead of going to an angle. 90 means stop; anything else turns it, slowly near 90 and faster further away. One turn winds the spring and lets it go, and whatever is in front of the lever flies off along the direction the robot is facing. kick() does exactly one turn.
Kick
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# kick: one turn of the kicker winds the spring and lets it go
kick()
# pause 4 s (the robot keeps doing what it was told)
wait(4)
print("kicked; holding:", holding())
The ball is already in front of the kicker in this scene. Half a second into the turn the lever fires and the ball goes about 45 cm, slowing and stopping on its own. Underneath, kick() is servo(1, 91) to start the servo turning, wait(1) for one turn, and servo(1, 90) to stop it. Leave the servo turning and it fires again every turn, which is why kick() stops it after one.
One strength
A spring has one strength. Every kick travels about the same distance, around 45 cm on this mat, so there is no power to choose. The only ways to change where the ball ends up are where you kick it from and which way you face.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# camera: look for red blobs
set_cv("blob", "red")
# kick: one turn of the kicker winds the spring and lets it go
kick()
# pause 4 s (the robot keeps doing what it was told)
wait(4)
b = blobs()[0]
print("the ball is now about", round(4 * 92 / (b[5] - b[3])), "cm from the camera")
The blob's width is a distance in disguise (lesson 4.4): a 4 cm ball that is width pixels wide is about 4 * 92 / width cm away.
Aim is your heading
The kicker does not aim. It fires straight ahead, so aiming is turning the robot:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# turn 30 degrees anticlockwise, then stop
turn_left(30, angle=30)
# kick: one turn of the kicker winds the spring and lets it go
kick()
# pause 3 s (the robot keeps doing what it was told)
wait(3)
print("kicked towards heading", round(heading()))
Which means everything from Module 3 about facing a heading precisely, and everything from Module 4 about centring a target in the picture, is aiming.
Held or loose
The kicker works on a held ball too. Grip, carry, turn to aim, kick: the ball leaves the jaws.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# close the jaws (it waits while they move)
gripper("close")
print("holding:", holding())
# turn 20 degrees clockwise, then stop
turn_right(30, angle=20)
# kick: one turn of the kicker winds the spring and lets it go
kick()
print("after the kick, holding:", holding())
# pause 3 s (the robot keeps doing what it was told)
wait(3)
Task: kick it
The ball is in front of the kicker, but you are not facing the green zone. Turn to face it, then kick so the ball stops in the zone. Do not drive.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
# kick: one turn of the kicker winds the spring and lets it go
kick()
# pause 4 s (the robot keeps doing what it was told)
wait(4)
Challenges
- Turn the kicker yourself with
servo(1, 91)and leave it turning for three seconds beforeservo(1, 90): how many times does it fire? - Turn 45 degrees left, kick, and say where the ball ended up (watch the mat).
- Kick the ball, chase it, grip it, and kick it again.