Skip to content
Programming Robots

Joint Positions and Poses


Learning Objectives

  • You can represent two joint angles with a dictionary.
  • You can copy and adjust a pose without changing the original.
  • You can distinguish requested joint targets from current positions.

The car used one angle to steer its front wheels. In this chapter’s simulation, two joint angles control two connected segments. Changing either angle moves the end of the second segment. We first control the joints; the next chapter uses prepared poses to move a token.

Two Joint Angles

The simulation shows the mechanism from above. Its base stays fixed. The first joint, shoulder, turns the first segment relative to the horizontal direction pointing right. The second joint, elbow, turns the second segment relative to the first. Positive angles turn counterclockwise.

A pose is a dictionary of joint angles in degrees:

pose = {"shoulder": 0, "elbow": 90}
print(pose["elbow"])

This prints 90. In this pose, the first segment points right and the second points upward. An elbow angle of zero would instead make the second segment continue in the same direction as the first. The supplied model calculates the endpoint from these angles.

The runner calls program(arm, scene), supplying the simulated mechanism and a dictionary of prepared poses. This program moves to a pose over one simulation second:

def program(arm, scene):
pose = {"shoulder": 0, "elbow": 90}
arm.move_to(pose, duration=1)
print(arm.positions())

Run main.py below to open the Pyxel view. Right arrow completes the next command, Space plays or pauses, and R resets playback. The cyan segments show current positions; the grey outline shows requested targets. After changing program, Run main.py again to record the new movement.

No files opened Select a file to edit

positions() returns a fresh dictionary of current joint angles. After the movement, it contains {"shoulder": 0.0, "elbow": 90.0}. The simulator accepts shoulder angles from -90 to 90 degrees and elbow angles from -120 to 120 degrees. These are joint angles, rather than the gripper’s horizontal and vertical coordinates.

Copying a Pose

To keep the original pose while changing one joint, copy its dictionary:

pose = {"shoulder": 0, "elbow": 90}
adjusted = pose.copy()
adjusted["elbow"] = 45
print(pose["elbow"])
print(adjusted["elbow"])

The output is 90 followed by 45. Assignment such as adjusted = pose would make both names refer to the same dictionary. Changing a value through either name would change that shared dictionary.

The values here are numbers, so a dictionary copy gives us an independent record of the angles.

Pose Copies

0 / 5 points

The two joints are represented by a dictionary. Read this program:

rest = {"shoulder": -10, "elbow": 60}
first = rest.copy()
second = rest
first["elbow"] = 25
second["shoulder"] = 15
print(rest)
print(first)

Which pair of dictionaries does it print?

Practise copying and changing joint records before writing a helper that prepares a new pose.

An angle-adjusting helper can prepare a pose without moving the mechanism. In adjusted_pose(pose, joint, target), return a changed copy while preserving the original. Its caller can inspect that dictionary or pass it to move_to; the helper itself needs no arm argument.

Adjust a Pose

0 / 20 points

Write adjusted_pose(pose, joint, target) in main.py. Return a new dictionary with the same joint angles as pose, except that the value for joint is replaced with target. Keep the original dictionary unchanged.

For example, adjusted_pose({"shoulder": -10, "elbow": 60}, "shoulder", 25) returns {"shoulder": 25, "elbow": 60}. The caller supplies a joint name and an allowed angle.

Run the supplied preview to see the adjusted pose.

Targets and Current Positions

The car retained its steering setting between calls. The joint targets are retained too. A command can change one target while leaving the other unchanged.

move_to completes the movement over its stated duration. The lower-level set_target method changes targets without advancing time. Replace program with:

def program(arm, scene):
arm.move_to({"shoulder": 0, "elbow": 90}, duration=1)
arm.set_target({"elbow": 40})
print(arm.targets())
print(arm.positions())
arm.wait(1)
print(arm.positions())

The first two records contain elbow values 40 and 90: the requested angle has changed, but the joint has not moved yet. During wait, the model moves toward its targets at up to 90 degrees per second. The final elbow position is therefore 40. The shoulder target stays at zero throughout.

The diagram separates the requested angles from the current modeled positions. Sending a new target changes the first record; advancing time lets the model update the second.

A target can change before the current joint position reaches it.

Practise the two readings and the wait that connects a target to movement.

Use the difference between a request and a current position to trace this short program.

Targets and Positions

0 / 5 points

The arm starts at shoulder 20 and elbow 70. What does this program print?

def program(arm, scene):
arm.set_target({"elbow": 40})
print(arm.targets()["elbow"])
print(arm.positions()["elbow"])

Check Your Understanding

  1. How does an elbow angle of zero differ from an elbow angle of 90 degrees when the shoulder angle is zero?
  2. Why would adjusted = pose be unsuitable when the original pose must remain unchanged?
  3. Why can targets() and positions() return different angles immediately after set_target?
  4. After changing only the elbow target, what determines whether the shoulder also moves during a wait?