Motors
Plug a LEGO motor into one of the Build HAT's four ports, A to D, and your program can run it at any speed, in either direction, or turn it to an exact angle.
BuildHat comes from a library, not from the language, so this code won't build without it. In
IntelliJ, choose Tools | Nylon | Add Nylon Library… and pick build-hat. It brings the Raspberry
Pi library with it, and adds one line to your project's build.markup.code:
(+dependencies : RepositoryDependency coordinate={"com.grandtheorem:build-hat:0.3.0"} /)
The first run after you power up the Build HAT loads its firmware. That takes about 20 seconds, once per power-up, and the run window shows "Build HAT: loading its firmware…". The HAT's light turns green when it's ready, so wait for green before you drive anything. Later runs start at once.
Run one motor
Plug a motor into port A.
Pi is the board, and BuildHat is the HAT on top of it.
# Turn a motor on port A one way, then the other.
namespace spin
import grandtheorem.rpi.Pi
import grandtheorem.buildhat.BuildHat
import grandtheorem.buildhat.Port
import namespace nylon.time
BuildHat.connect starts the HAT on the Pi it's seated on. A port is named as the HAT is labelled, so port A is
Port.A.
# The Build HAT on the Pi, and the motor in port A.
entrypoint.default main() {
let hat = BuildHat.connect(Pi)
let motor = hat.motor(Port.A)
run turns the motor at a speed for a while, then stops it. Speed goes from -100 to 100, and the
sign sets the direction. This is half speed one way, then the other.
# Half speed for two seconds each
# way. run waits until it's done.
motor.run(50, 2.seconds())
motor.run(-50, 2.seconds())
}
The motor turns for two seconds, then back for two, and stops.
Run two motors together
run waits until it's finished, so two motors at once need start, which sets a motor going and
moves straight on. Plug a second motor into port B.
The start is the same as before.
# Drive two motors at once, as a robot's wheels.
namespace drive
import grandtheorem.rpi.Pi
import grandtheorem.buildhat.BuildHat
import grandtheorem.buildhat.Port
import namespace nylon.time
One motor on each side of a robot.
# Ports A and B.
entrypoint.default main() {
let hat = BuildHat.connect(Pi)
let left = hat.motor(Port.A)
let right = hat.motor(Port.B)
Speed can also go from -1 to 1, the same range as a gamepad's stick. On a two-wheeled robot the motors usually face opposite ways, so driving straight takes opposite signs.
# start sets a motor going and
# returns, so both turn together.
# Opposite signs, since the two motors face opposite ways.
left.start(0.5)
right.start(-0.5)
sleep(2.seconds())
hat.stop() stops every motor on the HAT at once.
# A started motor keeps turning, so stop
# the HAT's motors before the program ends.
hat.stop()
}
Drive two motors from a gamepad
A gamepad's sticks go from -1 to 1, as a motor's speed can, so each stick can drive a motor directly. This program is
Joyride: the left stick's up and down drives the motor in port A, and the right stick's the motor in port B. It needs
the gamepad library as well, which Joystick shows how to add, and a gamepad
plugged into the Pi's USB. A USB gamepad on the Zero 2 W is coming soon.
Many USB cables only carry power, for charging. With one of those the gamepad never connects: its light bar stays dark and the program never sees it.
Gamepad comes from the gamepad library, and Motor names a motor's type, for the class below.
# Joyride: a gamepad drives the Build HAT's two motors, one per stick.
namespace joyride
import grandtheorem.rpi.Pi
import grandtheorem.gamepad.Gamepad
import grandtheorem.buildhat.BuildHat
import grandtheorem.buildhat.Motor
import grandtheorem.buildhat.Port
import namespace nylon.time
Gamepad.resolve(0) gives the first gamepad plugged in, or nothing while there isn't one. The program drives while it
has one, and looks again when it stops answering.
entrypoint.default joyride() {
let hat = BuildHat.connect(Pi)
let wheels = Wheels(hat)
while (true) {
if (let pad? = Gamepad.resolve(0)) {
# Drive until the pad stops answering, then look again.
var driving = true
while (driving) {
driving = wheels.drive(pad)
}
} else {
sleep(100.milliseconds()) # no pad plugged in yet
}
}
}
A stick is read in twentieths, so a motor is only sent a new speed when its stick has really moved. A gamepad reports many times a second, and sending each one would fill the HAT's serial line.
# A stick's position in twentieths, or OFF once stopped.
let OFF: i32 = 99
let level(speed: f32): i32 = i32(speed * f32(20))
pad.read waits up to 100 milliseconds for the pad's next state. With one, each stick sets its motor going with
start. With none, both motors stop, so a robot whose gamepad is unplugged doesn't drive on.
class Wheels(hat: BuildHat) {
let left: Motor = hat.motor(Port.A)
let right: Motor = hat.motor(Port.B)
var leftLevel: i32 = 99
var rightLevel: i32 = 99
# One turn of the loop: answers whether the pad answered.
let drive(pad: Gamepad): boolean {
if (let state? = pad.read(100.milliseconds())) {
let l = level(state.leftY)
if (l != leftLevel) {
# The motors face each other, so the left one runs backward to drive the robot forward.
left.start(f32(l) / f32(-20))
leftLevel = l
}
let r = level(state.rightY)
if (r != rightLevel) {
right.start(f32(r) / f32(20))
rightLevel = r
}
return true
}
if (leftLevel != OFF) {
left.stop()
leftLevel = OFF
}
if (rightLevel != OFF) {
right.stop()
rightLevel = OFF
}
return false
}
}
Push a stick up and its motor turns one way, pull it down and it turns the other, faster the further it goes. Let go, or unplug the gamepad, and the motors stop.
Turn to an angle
A motor knows where it is, so it can turn an exact number of degrees, or to a place on its dial: what an arm or a steering wheel needs. Plug a motor into port A.
The start is the same as before.
# Turn a motor by degrees, then to an angle.
namespace turn
import grandtheorem.rpi.Pi
import grandtheorem.buildhat.BuildHat
import grandtheorem.buildhat.Port
The motor in port A, as in the first program.
# The motor in port A.
entrypoint.default main() {
let hat = BuildHat.connect(Pi)
let motor = hat.motor(Port.A)
runForDegrees turns the motor a number of degrees from where it is, at a speed from -100 to 100, and waits until
it's there. A negative speed turns it the other way. At 100 it makes five turns a second.
# A quarter turn one way at half
# speed, then back the other way.
motor.runForDegrees(90, 50)
motor.runForDegrees(90, -50)
runToPosition turns the motor to a place on its dial, from -180 to 180 degrees, the shortest way round, and waits
until it's there. Its speed goes from 0 to 100.
# Turn the dial to 0 degrees, at half speed.
motor.runToPosition(0, 50)
}
The motor turns a quarter turn and back, then turns to 0 on its dial. After each of these runs, the motor is free to turn by hand.
Hold a position
An arm or a lift fights gravity. After a turn to an angle a motor goes limp, so a lifted arm drops back down. A motor can hold where its runs end instead. Plug the arm's motor into port A.
The start is the same as before.
# Lift an arm, hold it up, then let it go.
namespace lift
import grandtheorem.rpi.Pi
import grandtheorem.buildhat.BuildHat
import grandtheorem.buildhat.Port
import namespace nylon.time
The arm's motor, in port A.
# The arm's motor in port A.
entrypoint.default main() {
let hat = BuildHat.connect(Pi)
let arm = hat.motor(Port.A)
holdAfterRuns makes every run after it end holding where it got to, so the arm stays up once the run is done.
# Hold after each run, then lift
# the arm to 90 on its dial.
arm.holdAfterRuns()
arm.runToPosition(90, 30)
coast() lets it go.
# Stay up for five seconds, then let go.
sleep(5.seconds())
arm.coast()
}
The arm lifts to 90 on its dial and stays there. Push it away and it springs back. After five seconds it goes limp.
Everything a motor can do
| Call | What it does |
|---|---|
BuildHat.connect(Pi) | Starts the HAT on the Pi's header |
hat.motor(port) | The motor on a port: Port.A to Port.D |
motor.run(speed, duration) | Runs at a speed for a while, then stops |
motor.start(speed) | Runs at a speed until it's stopped |
motor.runForDegrees(degrees, speed) | Turns that many degrees from where it is, then waits until it's there |
motor.runForRotations(rotations, speed) | Turns that many turns, or part of one, as runForDegrees does |
motor.runToPosition(degrees, speed) | Turns its dial to degrees, -180 to 180, the shortest way round |
motor.runToPosition(degrees, speed, turn) | The same, the way turn says: Turn.Shortest, Turn.Clockwise or Turn.Anticlockwise |
motor.holdAfterRuns() | From now on, a turn to an angle ends holding where it got to |
motor.coastAfterRuns() | From now on, a turn to an angle ends free to turn by hand, as it does at first |
motor.position() | Where it is, in degrees, counted since the HAT powered up |
motor.absolutePosition() | Where its dial points, -180 to 180 degrees |
motor.stop() | Stops this motor |
motor.coast() | Lets it turn freely by hand |
hat.stop() | Stops every motor on the HAT |
run and start each take a speed as a whole number from -100 to 100, or as a fraction from -1 to
- Anything past either end is full speed.
runForDegreesandrunForRotationstake a whole number from -100 to 100, andrunToPositionone from 0 to 100.
position and absolutePosition give nothing when the motor doesn't answer within half a second, so each gives an
i32?. Turn comes from grandtheorem.buildhat, as Port does.
Good to know
Motors run at up to 70% power. The library limits each motor to 0.7 of the supply, as the HAT's own library does. On the 8 V supply, that keeps a motor within what it's built for.
On 5 V, motors are weak. Without its own supply, the HAT only gets the Pi's 5 V, so a motor gets 70% of a lower voltage. It turns slowly and stalls easily. Use the 8 V supply.
A turn to an angle needs a motor that knows where it is. SPIKE and Technic motors with a dial do. A Medium Linear
Motor knows how far it has turned but has no dial, so runForDegrees works on it and runToPosition ends the run. A
motor that doesn't know where it is ends the run on either.
After a turn to an angle, the motor is free to turn by hand, unless it holds. Hold a position keeps a lifted arm up.
Holding draws power the whole time. A held motor keeps working against whatever pushes it, so let it go with
coast() once it no longer needs to hold.
coast(), stop() and hat.stop() let a held motor go. coastAfterRuns() changes only how the next runs end: a
motor holding now goes on holding until one of those, or until its next run ends.
A started motor keeps turning, even after your program ends. Pressing Stop in IntelliJ doesn't
stop it either. It turns until hat.stop() or the next program that uses the HAT, so end any
program that uses start with hat.stop().
Boards: every board on Boards.