Stage 02 · Lesson 12/15

Programming & Python · Lesson 12

NumPy Fundamentals

Est. 35–45 min

Try it · Python in your browser

Run this lesson's example (numpy_joints.py), change a number, run it again.

Why

Why joint vectors love arrays

By the end you will create 1-D arrays, read shape/dtype, scale vectors, and compute a Euclidean norm for a tip offset.

NoteRequires NumPy installed in your environment (pip install numpy inside a venv).

Concept

The big idea: ndarray for joints and poses

  • import numpy as np then np.array([...]).
  • .shape, .dtype, elementwise + *, np.linalg.norm.
  • Prefer arrays for joint_rad and xyz offsets.
  • Still keep units in variable names.

How it works

How it actually works, step by step

  1. Pack joints

    np.array of 6 radians for a 6-DOF mental model.

  2. Inspect

    shape (6,), dtype float64 typically.

  3. Reduce

    sum / max for quick health checks.

  4. 2-D offset

    scale and norm like a boundary vector on a cricket field.

Visual

Visualise it: pose / vector intuition

Pose intuition: the numbers behind the picture are arrays of joints / coordinates.

Interactive · Robot pose / transformation demo

Tip (x, y)

(152.1, 75.4)

Tip yaw

-5.0°

Simplification: planar 2R arm. Each joint adds a rotation; tip pose is the product of link transforms — the same idea as homogeneous chains on a 6DOF arm.

Code lab

Code lab: joints as arrays

numpy_joints.py · Python
#!/usr/bin/env python3
"""NumPy arrays for joint / pose vectors (simplified)."""
import numpy as np

joints_rad = np.array([0.0, 0.5, -0.2, 0.0, 0.3, 0.0])
print("joints_rad:", joints_rad)
print("shape:", joints_rad.shape, "dtype:", joints_rad.dtype)
print("sum:", float(joints_rad.sum()), "max:", float(joints_rad.max()))

# scale a 2D tip offset like a cricket boundary vector
offset_m = np.array([0.12, -0.05])
scale = 2.0
print("offset*scale:", offset_m * scale)
print("norm:", float(np.linalg.norm(offset_m)))

Expected output

joints_rad: [ 0.   0.5 -0.2  0.   0.3  0. ]
shape: (6,) dtype: float64
sum: 0.6 max: 0.5
offset*scale: [ 0.24 -0.1 ]
norm: 0.13

Real world

Where NumPy shows up in robotics hosts

  • FK/IK notebooks use arrays heavily.
  • Batch transforms on point clouds (later vision stages).
  • Logging joint vectors as lists inside JSON.

Mistakes

Common mistakes and confusions

Hardware & safety

Hardware lab and safety notes

Read joint angles from your arm (or from servo commands you sent) into a NumPy array. A 6-joint arm is just a 6-element vector — and NumPy checks all limits in one line.

  • Store angles in radians in arrays named _rad.
  • Check limits at once: np.all(np.abs(joints_rad) < np.pi/2).
  • np.clip(joints_rad, lo, hi) clamps all joints together.
Two hobby servo motors
Each servo is one joint — a 6-servo arm is a 6-element NumPy array.Photo: Kborer · Public domain (released by the author) · Source: Wikimedia Commons · resized, converted to WebP

Debugging

Debugging: when it doesn’t work

Symptom → likely cause → fix
SymptomLikely causeFix
ValueError: setting an array element with a sequenceRagged nested listsMake every row the same length
Values are huge (e.g. 28.6)Degrees stored in a _rad arrayUse np.radians() first
Integer array cuts 0.5 to 0dtype is intCreate with floats or dtype=float
a * b is not a dot product* is element-wiseUse a @ b or np.dot
NoteRead the last line of a Python error first — it names the problem — then the line number above it. Change one thing at a time and re-run.

Think

Think about it

Think

Why is shape (6,) nicer than six variables j1…j6 for an arm?

Experiment

Try it in the lab: array worksheet

Experiment

Array worksheet

  1. Run numpy_joints.py; confirm sum 0.6 and max 0.5.
  2. Change one joint; recompute sum.
  3. Compute norm of [0.3, 0.4] by hand (expect 0.5) and with NumPy.

Interactive · Robot pose / transformation demo

Tip (x, y)

(152.1, 75.4)

Tip yaw

-5.0°

Simplification: planar 2R arm. Each joint adds a rotation; tip pose is the product of link transforms — the same idea as homogeneous chains on a 6DOF arm.

Knowledge check

Check your understanding

Knowledge check

1. np.array([1.0, 2.0]).shape is…

2. In the lab, joints sum was…

3. np.linalg.norm on a 2-D offset gives…

4. Elementwise * on arrays…

5. NumPy belongs primarily…

Practice quiz

Loading practice quiz…

Coding practice

Loading coding exercises…

Mini assignment

Hands-on assignment

Mini assignment

Joints as arrays

Create a 6-vector of joint radians, print shape/dtype, and a scaled xyz offset with norm.

Deliverables

  • numpy_custom.py
  • Printed shape, dtype, norm

Do this, step by step

  1. Run numpy_joints.py: shape (6,), dtype float64, sum 0.6, max 0.5, norm 0.13.
  2. Convert [0, 30, -10, 0, 20, 0] degrees with np.radians().
  3. Clamp all joints to ±0.4 rad with np.clip and print.
  4. Write numpy_custom.py with your 6-vector, shape/dtype and a scaled xyz offset with norm.

Stuck? Hints, one at a time

NoteTry the task first. Read one hint, try again, and only then read the next one.
  1. Hint 1: norm = √(0.12² + 0.05²) = √0.0169 = 0.13.
  2. Hint 2: 30° = 0.5236 rad, −10° = −0.1745 rad.
  3. Hint 3: After np.clip(j, -0.4, 0.4) the 0.5 becomes 0.4; the others stay.

Challenges (pick by level)

  • 🟢 Easy: Print the index of the largest joint with np.argmax.
  • 🟡 Medium: Make a (10, 6) array of 10 poses and print the mean of each joint.
  • 🟠 Hard: Use NumPy to compute two-link FK for 100 angles at once (see Stage 1).

Next step

Next, Lesson 13: Python + Sensors. Related: vectors and matrices in Stage 1 — Math for Robotics and FK in Stage 8 — Kinematics & IK.

Sources

Self-attested for now — keep artifacts in your lab notebook.

Complete

Today's goal

0 of 1 done today

0-day streak
Daily goal:lessons, quizzes or assignments a day

Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).

Stuck somewhere?

Ask a doubt about this lesson

Saved with this lesson's id and your email so we can reply. Privacy · or email contact@aihubstation.in