By the end of this lesson you will be able to
- use the programming console on these pages: write a program, run it and watch the replay
- move Bramble's neck, shoulder and elbow with
robot.move()and let time pass withrobot.wait() - read the angle conventions for each joint, so you can predict where a limb will go
- print messages with
console.log()androbot.log() - read Bramble's error messages and fix a simple mistake in a program
Alice Bennett would like Bramble to wave to visitors as they come into the museum. Henry Wren opens the programming console on the workshop laptop. “Before we let it near the public,” he says, “we write the wave here and try it on the simulated Bramble. The simulator is safe to get wrong. The real robot is not.”
The console
Every lesson from here on has boxes like the one below. Each box holds a short program written in JavaScript, the programming language built into every web browser. When you press Run, the program is sent to a practice version of Bramble that lives inside this page. Nothing is sent over the internet, and you cannot damage anything.
When the program finishes, you see three things:
- a replay: a side view of Bramble, facing right, doing what your program told it to do. You can pause it, play it again, or drag the slider to any moment;
- the output: any messages your program printed;
- a list of what happened to Bramble, if anything worth knowing happened, such as hitting the end of a joint's travel.
Press Run now. You can also change the numbers and run it again: the box is yours to experiment with, and Reset puts the original program back.
Bramble lifted its arm until it pointed straight forwards. The program has two lines, and each ends with a semicolon (;), which marks the end of an instruction, much as a full stop ends a sentence.
Two commands: move and wait
A command is an instruction that Bramble understands. You write it as robot., the command's name, and then round brackets holding the details. The details are called arguments, and they are separated by commas.
| Command | What it does |
|---|---|
| robot.move("shoulder", 0) | Sets a new target for a joint, in degrees. The joint starts moving towards it at once, at its normal speed. |
| robot.wait(1) | Lets time pass on Bramble's clock, in seconds, while the joints keep moving. |
The joint's name goes in double quotes, "shoulder", because it is a piece of text. The angle and the time are numbers, so they have no quotes. In this course Bramble's program can move three joints of its right arm and head: "neck", "shoulder" and "elbow". The legs are worked by their own commands, which you will meet in Modules 7 and 8.
The most important idea in this lesson is that robot.move() does not wait for the movement to finish. It only sets the target. The movement happens while time passes, and time passes only when your program calls robot.wait() (or, later, robot.step()). Try the program below, which has no wait at all.
The arm did not move. The program set the target and then ended at time 0.00 s, before any time had passed. When a program ends, Bramble's joints hold still where they are, so the shoulder never started its swing. Add robot.wait(1); on a second line and run it again.
Common mistake: expecting a move to finish on its own
If a joint does not seem to move, check that the program lets time pass after the move. And if you give two moves to the same joint with no wait between them, only the second counts: the first target is replaced before the joint has had any time to reach it.
Which way is which: joint angles
Every joint angle is in degrees. To predict where a limb will go, you need to know what 0 means for each joint and which way the numbers grow.
Bramble starts each program standing with its arm hanging down (shoulder −90°) and its elbow slightly bent (10°), and its head level (neck 0°). Each joint also has limits: the shoulder can go from −95° to 180°, the elbow from 0° to 145°, and the neck from −40° to 40°. Lesson 1.3 explains why a program should never drive a joint all the way to those ends.
A program is a list of steps
The computer carries out a program one line at a time, from the top down. So to make the arm go up and then come back down, you write the steps in that order, with a wait after each move so that it has time to happen.
How long should a wait be? Each joint has a top speed. The shoulder and elbow can turn at up to 225° a second, so a 180° swing takes a little under a second. A wait that is too short cuts the movement off when the next move begins; a wait that is too long only wastes time.
A friendly wave, seen from the side: lift the upper arm, then bend and straighten the elbow a few times.
1. Lift the arm: robot.move("shoulder", 60), and wait 1 second.
2. Bend the elbow to 70°, wait 0.4 s; straighten it to 20°, wait 0.4 s. Repeat.
3. Lower the arm again at the end.
The same four lines appear twice. In Lesson 2.3 you will learn to write them once and have the computer repeat them.
Messages: console.log and robot.log
A program can also print text in the Output area. There are two ways, and both are useful.
console.log("text")prints the text exactly as written. It is the standard way to print in JavaScript.robot.log("text")adds the text to Bramble's own log, a record kept by the robot, with the time on Bramble's clock in front of it. It is printed in green. Technicians read logs like this when they find faults.
The log line shows [1.00 s], because one second of Bramble's time had passed when it was written.
When Bramble cannot do what you asked
If a program asks for something impossible, Bramble stops the program and explains why, in the Output. The most common causes at this stage are a misspelt joint name, a missing quote, or a missing bracket. The program below has a spelling mistake. Run it, read the message, then fix it.
Messages that start Bramble says come from the robot: the program was written correctly, but asked for something Bramble cannot do. Other messages come from JavaScript itself, when it cannot understand how the program is written. In both cases the message and the line number point you to the problem, and the note below explains it in plain words.
JavaScript needs straight quotes ("), not the curly ones (“ ”) that phones and word processors often type. If you paste code from a document and it will not run, this is often why. The console notices curly quotes and offers to fix them.
Exercises
Each exercise has a starting program. Change it to do what the task asks, run it to watch Bramble, and press Check my answer when you are happy. The checker runs your program on the simulated Bramble and looks at what the robot actually did.
Exercise 1 · Point forwards
Change the program so that Bramble lifts its arm to point straight forwards, and gives the movement one second to finish.
Exercise 2 · A nod
Make Bramble nod: tip its head down to 20°, wait half a second, then bring it back to level (0°) and wait another half second.
Exercise 3 · Finish the wave
This wave is incomplete. Add lines so that, after the arm is lifted, the elbow bends to 70° and straightens to 20° twice, with a wait of 0.4 seconds after each move, and then the arm goes back down to −90°.
Exercise 4 · Say hello
Print the greeting Hello, I am Bramble with console.log(), lift the arm to 60° (and wait one second), and then add Waving to Bramble's log with robot.log().
Expected output from console.log
Exercise 5 · Fix the program
Oliver Grant typed this program to make Bramble hold its forearm upright, but it will not run. Find and fix the mistakes. At the end the shoulder should be at 0° and the elbow at 90°, and the program must give them time to get there.
Quick check
Four questions to confirm the main ideas. Choose an answer to see the explanation.
A program contains only robot.move("elbow", 90); and nothing else. What happens?
robot.move() only sets a target. Time passes on Bramble's clock only during robot.wait() (or robot.step()), so the program ends before the joint has had any time to move, and the joints then hold still.
Bramble faces right. Which shoulder angle makes the upper arm point straight up?
The shoulder angle is measured from straight forwards (0°), and larger numbers lift the arm, so straight up is 90°. −90° is hanging down.
What is the difference between console.log() and robot.log() on these pages?
Both print text, but robot.log() writes into the robot's record with a time stamp, which is what a technician reads when looking for a fault.
A program says robot.move("Elbow", 45) and Bramble replies that there is no such joint. Why?
The joint is called "elbow", all in small letters. JavaScript text must match exactly, so "Elbow" is a different name. 45° is well inside the elbow's range.
Summary
- Write a program in the box, press Run, and watch the replay, the output and the list of what happened.
robot.move(joint, angle)only sets a target; time passes, and the joint moves, duringrobot.wait(seconds).- The shoulder is 0° pointing forwards and 90° pointing up; the elbow is 0° straight and bends to 145°; the neck tips the head down for positive angles.
- A program runs from the top down, one line at a time.
console.log()prints text;robot.log()adds a time-stamped line to Bramble's log.- Messages starting “Bramble says” explain a command the robot cannot carry out, such as a misspelt joint name.
Found a mistake on this page, or something unclear? Report a problem and mention “Control Lesson 1.2”.
