Move a distance
move -30 cm at 50 %
Give the block a distance in centimeters and a speed in percent. A negative distance reverses direction; a negative speed reverses it again. The block pairs motors A+B and resets their wheel counters before moving.
Click to enlarge the Word BlocksClick to shrink the Word Blocks
See the complete .bw definition
DEFINE move (distance) cm at (speed) %:
set movement motors to A+B
IF ((abs of (distance)) > "1.0") and ((abs of (speed)) > "0") THEN:
set movement speed to ((speed) * ((distance) / (abs of (distance)))) %
AB set relative position to 0
REPEAT UNTIL ((abs of ((B relative position) - (A relative position))) * 0.02435) > ((abs of (distance)) - 1.0):
start moving ((((((target heading - (yaw angle)) * ((speed) / (abs of (speed)))) * ((distance) / (abs of (distance)))) + 180) mod 360) - 180) steering
stop moving
wait 0.5 secondsdistance / abs of distance is +1 for a positive distance and −1 for a negative one. Multiplying the speed by that sign tells the motors which way to go.
A and B count in opposite directions when driving straight. Subtract B − A, take abs, then multiply by 0.02435 to estimate centimeters traveled.
Compare wheel travel with abs of distance − 1.0. For a 30 cm request, the block starts braking after the estimate passes 29 cm.
| Distance | Speed | Motor speed | Direction |
|---|---|---|---|
| 30 cm | 50% | 50% | Forward |
| −30 cm | 50% | −50% | Backward |
| −30 cm | −50% | 50% | Forward |
Unpack the two signs
set movement speed to ((speed) * ((distance) / (abs of (distance)))) %
- For
move −30 cm at −50%,−30 / abs of −30 = −1. - The motor speed is
−50 × −1 = +50%. Two minus signs make forward movement. - The same distance sign also reverses the steering correction. Backward driving needs the opposite wheel adjustment to fix a heading error.
Try the distance math: A reads −200°, B reads +200°. The difference is 200 − (−200) = 400°. Then 400 × 0.02435 = 9.74 cm.
Unpack the distance test
REPEAT UNTIL ((abs of ((B relative position) - (A relative position))) * 0.02435) > ((abs of (distance)) - 1.0):
B − Abecomes200 − (−200) = 400.abs of 400is 400.absmeans distance from zero, so it also makes a negative result positive.400 × 0.02435 = 9.74 cm.- For a −30 cm request, compare with
abs of −30 − 1 = 29 cm. Since 9.74 is below 29, keep moving.
While the wheels turn, the robot steers toward target heading. The speed sign and distance sign reverse the correction when needed. mod 360 keeps the angle correction short across the wrap. Then the robot stops and waits half a second.
Unpack the steering correction
start moving ((((((target heading - (yaw angle)) * ((speed) / (abs of (speed)))) * ((distance) / (abs of (distance)))) + 180) mod 360) - 180) steering
- Find the heading error:
target heading − yaw angle. - Multiply by
speed / abs of speedanddistance / abs of distance. Each is +1 or −1, so each can reverse the correction. - Suppose the target is 170° and yaw reads −170°. The raw error is 340°. At positive speed and distance,
(340 + 180) mod 360 − 180 = −20°: the short correction across the wrap.
The block skips the move when the requested distance is 1 cm or less in magnitude, or when speed is 0%. That keeps both sign calculations from dividing by zero. A skipped move does not issue a stop command.
Turn to a heading
turn to heading 170 at 10 %
Imagine the hub starts at −170°. A heading is a direction, like a compass bearing. Give this block a positive speed. It saves 170° as the new target heading, then spins the wheels in opposite directions to get there.
Click to enlarge the Word BlocksClick to shrink the Word Blocks
See the complete .bw definition
DEFINE turn to heading (heading) at (speed) %:
set movement motors to A+B
stop moving
set target heading to (heading)
IF (abs of (((((target heading) - (yaw angle)) + 180) mod 360) - 180)) > 8 THEN:
IF (((((target heading) - (yaw angle)) + 180) mod 360) - 180) < 0 THEN:
start moving at ((speed) * -1) left (speed) right % speed
ELSE:
start moving at (speed) left ((speed) * -1) right % speed
wait until (abs of (((((target heading) - (yaw angle)) + 180) mod 360) - 180)) < 8
stop moving
wait 0.5 secondsSubtract current yaw from the target. +180, mod 360, then −180 folds the answer into a short turn between −180° and 180°.
A negative error spins one way; a positive error spins the other. One wheel goes forward while the other goes backward.
It turns only if the error is more than 8°. Once the error is under 8°, it stops and waits half a second.
Across the wrap: target 170°, current yaw −170° gives 170 − (−170) = 340°. The wrap math changes 340° to −20°: a short turn, not nearly a full circle. After turning roughly −12°, about 8° remains. The block stops once the remaining error is under 8°—so just past −12°.
New math word: mod means “the remainder after division.” 520 mod 360 = 160 because 520 is one full 360 plus 160. Here it lets angles loop around a circle.
Unpack the turn calculation
IF (abs of (((((target heading) - (yaw angle)) + 180) mod 360) - 180)) > 8 THEN:
wait until (abs of (((((target heading) - (yaw angle)) + 180) mod 360) - 180)) < 8
- Find the raw difference:
170 − (−170) = 340°. - Shift by 180:
340 + 180 = 520°. Keep the remainder after a full circle:520 mod 360 = 160°. - Shift back:
160 − 180 = −20°. The minus sign tells the robot which way to turn. abs of −20 = 20, which is greater than 8, so it turns. After roughly −12° of turning, only 8° remains; the< 8test becomes true just past that point.
The planned heading stays at 170° even if the robot stops a few degrees away. A later move can correct toward the plan.
Move both arms to positions
arm position 4000 10000 wait 0
Motor C tips the scoop. Motor D raises and lowers the lift arm that carries it. Give each motor a target relative position in motor degrees. The block compares each target with the motor’s current reading, then chooses a speed for each motor.
Click to enlarge the Word BlocksClick to shrink the Word Blocks
See the complete .bw definition and its two receivers
DEFINE arm position (C position) (D position) wait (wait):
set C position to (C position)
set D position to (D position)
set C change to (abs of ((C position) - (C relative position)))
set D change to (abs of ((D position) - (D relative position)))
IF C change > D change THEN:
set max change to C change
ELSE:
set max change to D change
IF max change > "0" THEN:
set C moving to "1"
set D moving to "1"
IF (wait) = "0" THEN:
broadcast "arm position"
ELSE:
broadcast "arm position" and wait
WHEN I receive "arm position":
IF C change > "0" THEN:
C go to relative position C position at ((C change / max change) * 100) % speed
set C moving to "0"
WHEN I receive "arm position":
IF D change > "0" THEN:
D go to relative position D position at ((D change / max change) * 100) % speed
set D moving to "0"C change and D change are the distances each motor needs to travel. abs keeps each distance positive, even when an arm moves backward.
max change stores whichever distance is larger. That motor gets 100% speed.
Each motor uses its share of the larger distance: change ÷ max change × 100. The block then broadcasts a message so both motor commands can begin.
Try the math: If both counters start at 0°, C needs 4,000° and D needs 10,000°. The larger move is 10,000°, so D gets 100% speed and C gets 4,000 ÷ 10,000 × 100 = 40%.
Unpack the two speed expressions
C go to relative position C position at ((C change / max change) * 100) % speed
D go to relative position D position at ((D change / max change) * 100) % speed
- Both receivers use the same
max change, so their speeds have the same scale. - For C,
4,000 ÷ 10,000 × 100 = 40%. - For D,
10,000 ÷ 10,000 × 100 = 100%. The slower, shorter move is meant to finish near the longer one.
With wait 0, this block starts both arms and lets the program continue. Any other value chooses broadcast "arm position" and wait, which waits for both receivers. In our SPIKE test, leaving the input blank also behaved like 0. Writing 0 makes that choice clear to someone reading the program.
If both motors are already at their targets, max change is 0, so the block skips the broadcast. Each receiver also skips its motor command when that motor has no distance to travel. Set the motor counters to a known zero before using target positions measured from zero.
Wait for both arms
arm wait
Use this after an arm-position call with wait 0 when later steps need the scoop and lift arm to finish first. The two receivers each clear their own moving flag when done; this block waits until both flags are 0.
Click to enlarge the Word BlocksClick to shrink the Word Blocks
See the complete .bw definition
DEFINE arm wait:
wait until (C moving = "0") and (D moving = "0")