Skip to content
Programming Robots

Time and Repeating Motion


Learning Objectives

  • You can measure elapsed time relative to a saved start value.
  • You can explain the center, amplitude, and period of a repeating target.
  • You can calculate a list of joint targets from supplied clock readings.

A list of poses describes a fixed sequence. A supplied calculation can instead choose a joint target for each moment. Before connecting that calculation to movement, we will inspect its values and practise measuring elapsed time.

Measuring Elapsed Time

The supplied object’s time() method returns its clock value in seconds. Subtract a saved value to measure time since a particular step:

def program(arm, scene):
arm.move_to(scene["rest"], duration=2)
start = arm.time()
arm.wait(0.5)
elapsed = arm.time() - start
print(elapsed)

The wait advances the simulation clock by 0.5 seconds, so this prints 0.5. A starting clock value of 12 and a later value of 12.5 give the same elapsed interval. Saving the start after move_to excludes the earlier movement’s two seconds.

The simulation advances when move_to or wait advances its model. Reading time() alone does not make time pass. Playback speed changes how quickly we view the recording, rather than the times used by the program.

Run main.py below to inspect the clock and printed result. Right arrow completes the next command, Space plays or pauses, and R resets playback. Replace program as you try this chapter’s examples, then Run main.py again.

No files opened Select a file to edit

A Supplied Repeating-Target Helper

The supplied motion.py contains wave_position(elapsed, center, amplitude, period). It returns an angle that moves smoothly back and forth. Add the import to main.py and replace program with this version to inspect a target without moving the mechanism:

from motion import wave_position
def program(arm, scene):
print(wave_position(0, 10, 5, 4))

This prints 10.0: the target begins at its center angle. The helper is included in the exercises and in the simulation files; you do not need to implement its formula.

center is the middle angle. amplitude is the greatest departure from that angle. Both use degrees. For example, a center of 10 and amplitude of 5 give targets between 5 and 15 degrees.

Use a nonnegative amplitude. period is the time for one cycle, in seconds, and must be positive. The helper returns a joint angle in degrees.

For a four-second period, the quarter-cycle values are:

Elapsed secondsJoint target in degrees
010
115
210
35
410

The target starts at the center, reaches the upper extreme, passes through the center, reaches the lower extreme, and returns to the center. Intermediate times work too: wave_position(0.5, 10, 5, 4) gives approximately 13.54 degrees.

Keep the import above program, and replace the function with this version to check three rows of the table:

def program(arm, scene):
for elapsed in [0, 1, 3]:
target = wave_position(elapsed, 10, 5, 4)
print(f"{elapsed}: {target:.2f}")

It prints 10.00, 15.00, and 5.00 beside the times. Change the list to inspect other rows. Formatting hides tiny floating-point differences; checks should allow a small numerical tolerance.

Intermediate Targets

0 / 5 points

For a particular motion, the supplied helper gives these values:

Call Target in degrees
wave_position(0, 20, 10, 4) 20
wave_position(0.5, 20, 10, 4) approximately 27.07
wave_position(1, 20, 10, 4) 30

The movement starts when the clock reads 12. Which target belongs to the reading 12.5?

Amplitude and Period

Increasing amplitude widens the movement. Increasing period makes each cycle take longer:

CenterAmplitudePeriod in secondsTarget range in degrees
10545 to 15
101040 to 20
10585 to 15

Check both extremes against the simulated joint’s bounds. For example, an elbow target centered at 118 with amplitude 5 reaches 123, exceeding the elbow’s upper bound of 120 degrees. A longer period would slow the movement without reducing its range.

The total duration determines how long the updates continue. Divide that duration by the period to find the number of cycles. For example, a six-second movement with a two-second period completes three cycles.

Amplitude and Period

0 / 5 points

Two motion records describe an elbow target in degrees:

a = {"center": 20, "amplitude": 10, "period": 4}
b = {"center": 20, "amplitude": 10, "period": 8}

Both would run for eight seconds. Which comparison of their target ranges and cycle counts is correct?

Targets from Clock Readings

The motion formula takes elapsed time, but the clock gives readings relative to its own starting point. A movement that starts at 20 seconds and is sampled at 20, 20.5, and 21 seconds needs elapsed values of 0, 0.5, and 1. Subtract the same saved start from each reading; subtracting consecutive readings would instead give the gaps between samples.

Calculating targets can be separate from sending commands. Collect the results in a list to inspect their values without moving a joint. In the following exercise, use the supplied wave_position in targets_for_times(times, start, motion) to calculate one target for each clock reading.

Targets for Times

0 / 25 points

Write targets_for_times(times, start, motion) in main.py. Return a list containing one target for each clock reading in times, in the same order.

For each reading, calculate elapsed time since start and pass it to the supplied wave_position with motion["center"], motion["amplitude"], and motion["period"]. Keep the supplied list and dictionary unchanged. An empty list gives an empty result.

Run the supplied preview to see the resulting targets control a joint.

To see these targets alongside current joint angles, use the repeating-motion preview in the next chapter.

Inside the supplied helper

The helper scales and shifts a sine wave:

import math
def wave_position(elapsed, center, amplitude, period):
phase = 2 * math.pi * elapsed / period
return center + amplitude * math.sin(phase)

math.sin ranges from -1 to 1. Multiplication sets the amplitude and addition sets the center. The angle inside math.sin uses radians, with 2 * math.pi for one cycle. Understanding this formula is optional; these exercises use the supplied helper.

Check Your Understanding

  1. A movement starts when the clock reads 20. What elapsed value should its calculation receive when the clock reads 21.5?
  2. How do center and amplitude determine the smallest and largest targets?
  3. What changes when the period doubles but center, amplitude, and total duration stay the same?