Move a distance
move 30 cm at 50 %
Use a positive distance. A positive speed drives forward; a negative speed drives backward. The block pairs motors A+B, sets the speed, then resets both wheel counters to zero.
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
set movement speed to (speed) %
AB set relative position to 0
REPEAT UNTIL ((abs of ((B relative position) - (A relative position))) * 0.02435) > ((distance) - 1.0):
start moving (((((target heading) - (yaw angle)) * ((speed) / abs of speed) + 180) mod 360) - 180) steering
stop moving
wait 0.5 secondsA and B count in opposite directions when driving straight. Subtract B − A, then take abs so the result is positive.
Multiply that difference by 0.02435. That number is the robot’s wheel calibration: centimeters for one degree of difference.
The test is distance − 1.0. For a 30 cm command, it starts braking after the calculated distance passes 29 cm.
Try the 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) > ((distance) - 1.0):
B − Abecomes200 − (−200) = 400.abs of 400is still 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
30 − 1 = 29 cm. Since 9.74 is not greater than 29, keep moving. At 1,200° of wheel difference, the estimate is 29.22 cm, so the loop ends.
While the wheels turn, the robot steers toward target heading. When it drives backward, the steering correction must reverse too: the same wheel adjustment has the opposite effect. speed / abs of speed provides that switch. The mod 360 part then chooses a short correction across the angle wrap. The robot stops and waits half a second when it has traveled far enough.
Unpack the steering correction
start moving (((((target heading) - (yaw angle)) * ((speed) / abs of speed) + 180) mod 360) - 180) steering
- First decide whether to reverse the correction. Driving forward and backward need opposite steering to fix the same heading error.
- For any nonzero speed, dividing by its absolute value gives its sign:
50 / abs of 50 = +1, while−50 / abs of −50 = −1. Multiplying by −1 flips the correction. - Now suppose the target is 170° and yaw reads −170°.
target − yaw = 340°, although the short way around is only 20°. - At forward speed, the sign is +1:
(340 × 1 + 180) mod 360 − 180becomes520 mod 360 − 180 = 160 − 180 = −20. The robot steers toward the planned heading.
A speed of 0 would divide by zero in this block; use move signed for a safe zero check.
Move signed: choose direction with two signs
move signed -30 cm at 50 %
This wrapper lets distance be negative. It turns distance into a positive number for move, and flips the speed sign when distance is negative. Two minus signs cancel.
Click to enlarge the Word BlocksClick to shrink the Word Blocks
See the complete .bw definition
DEFINE move signed (distance) cm at (speed) %:
IF (abs of (distance)) > 0 THEN:
IF (abs of (speed)) > 0 THEN:
IF (distance) < 0 THEN:
move (abs of (distance)) cm at ((speed) * -1) %
ELSE:
move (distance) cm at (speed) %| Distance | Speed | What it sends to move | Result |
|---|---|---|---|
| 30 cm | 50% | 30 cm at 50% | Forward |
| −30 cm | 50% | 30 cm at −50% | Backward |
| −30 cm | −50% | 30 cm at 50% | Forward |
If distance or speed is zero, it does nothing. That also protects move from dividing by zero.
Unpack the two minus signs
move (abs of (distance)) cm at ((speed) * -1) %
- For
move signed −30 cm at −50%,abs of −30 = 30. The underlying move block needs that positive distance. - Because distance was negative, flip speed:
−50 × −1 = +50. - It calls
move 30 cm at 50%, so the robot goes forward. A negative distance and negative speed cancel.
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.