Skip to content
Programming Robots

Updating Targets


Learning Objectives

  • You can advance simulation time inside a finite loop.
  • You can combine elapsed time, a supplied calculation, and target commands.
  • You can finish at a requested duration by shortening the last wait.

The previous chapter calculated targets without moving the mechanism. We can now send targets to a joint as simulation time advances. The calculation and movement remain separate: wave_position returns a number, set_target records a requested angle, and wait advances the model toward it.

Suppose the clock reads 11 seconds, and the elbow’s position and target are both 0 degrees. Compare what changes after each command:

OperationClock readingElbow targetElbow position
Before either command110 degrees0 degrees
arm.set_target({"elbow": 30})1130 degrees0 degrees
arm.wait(0.1)11.130 degrees9 degrees

Setting the target changes the request. Waiting advances the clock and moves the elbow toward that target, at up to 90 degrees per second in this model.

A Loop That Advances Time

First inspect a constant target over one second. This example sends the target once and prints elapsed seconds and the current elbow angle before each wait:

def program(arm, scene):
arm.set_target({"elbow": 30})
start = arm.time()
duration = 1
interval = 0.3
finish = start + duration
while arm.time() < finish:
now = arm.time()
elapsed = now - start
print(f"{elapsed:.1f}", arm.positions()["elbow"])
remaining = finish - now
arm.wait(min(interval, remaining))

finish is a clock reading: compare the current clock with it to decide when to stop. Inside the loop, now - start gives elapsed time for a calculation or display, while finish - now gives the time remaining. The waits last 0.3, 0.3, 0.3, and approximately 0.1 seconds. min(interval, remaining) chooses the shorter final wait.

Run main.py below to inspect the four readings and waits. Right arrow completes the next command, Space plays or pauses, and R resets playback. Reading time() does not advance the model; without wait, the clock would remain unchanged and the loop would not finish.

No files opened Select a file to edit

The diagram below shows why the wait belongs inside the loop: each pass advances simulation time before the next check.

Reading the clock observes time; the wait advances the simulation.

From Clock Readings to Target Updates

To vary the requested angle, calculate and send a new target inside the loop before each wait. Use now - start as the elapsed-time argument to wave_position.

Before coding, trace a run that starts at clock reading 11, lasts one second, and updates every 0.3 seconds. The motion has center 10, amplitude 5, and period 4. Which clock readings send targets, and which elapsed values go into the calculation? How long is the last wait? Does clock reading 12 send another target?

Check your trace
Clock readingElapsed timeRemaining timeRequested targetFollowing wait
110110 degrees0.3 seconds
11.30.30.7approximately 12.27 degrees0.3 seconds
11.60.60.4approximately 14.05 degrees0.3 seconds
11.90.90.1approximately 14.94 degrees0.1 seconds

The finishing clock reading is 12. The condition arm.time() < finish is now false, so there is no further update. Each target calculation uses time since the saved start, and the final wait is shortened to reach the finish.

Predict what would happen in this run if the loop omitted wait. Then consider a different change: what if the target calculation used now instead of elapsed?

Check your predictions

Without wait, the clock stays at 11 and the loop keeps sending the same target without reaching the finish. Reading the clock does not advance it. The model’s command limit eventually stops the run.

Using now gives the calculation 11 instead of 0 on the first pass. For this wave, the first target would be 5 degrees rather than 10 degrees. Subtracting start makes the motion begin at elapsed time zero, regardless of earlier movements.

The next exercise combines the finite loop with this supplied calculation. It receives the joint, motion values, duration, and update interval as arguments, so the same helper can drive different motions.

Updating Targets for a Duration

0 / 35 points

Write wave_for(arm, joint, motion, seconds, interval) in main.py. Update the supplied joint for seconds of simulation time, starting from the arm’s current clock reading.

Before each wait, use the supplied wave_position helper to calculate a target from elapsed time and motion["center"], motion["amplitude"], and motion["period"]. Send it with arm.set_target({joint: target}). Wait for interval seconds between updates, shortening the last wait to finish at the requested duration. A zero duration makes no updates.

The caller supplies valid motion values, a nonnegative duration, and a positive interval. Keep motion unchanged and return None. The preview positions the joint at the center before calling your helper; do not repeat that positioning inside it.

Viewing Target Updates

Run your exercise’s preview to inspect its movement. The graph compares the requested and current angles after the initial positioning. The current position can lag behind a changing target because wait moves joints at a limited speed. When the loop finishes, the last target remains active; the time limit does not issue a return movement.

After attempting the exercise, inspect the worked preview below to compare its update loop with yours. It uses the same supplied model. Use Space to play or pause, Right arrow to complete the next command, and R to reset playback. Change center, amplitude, or period in scenes.py, then Run main.py again to compare the movement and graph.

No files opened Select a file to edit

Check Your Understanding

  1. With a duration of one second and an interval of 0.3 seconds, why must the final wait be shorter?
  2. Why can a changing target and the current joint position differ on the graph?
  3. At clock reading 11.6 in the trace, what do now - start and finish - now tell us?

The next chapter introduces an SO101 arm through a different supplied interface. Observing positions, checking limits, and coordinating commands remain useful ideas; we will examine the new methods before using them.