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.
pip install numpy inside a venv).Concept
The big idea: ndarray for joints and poses
import numpy as npthennp.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
Pack joints
np.array of 6 radians for a 6-DOF mental model.
Inspect
shape (6,), dtype float64 typically.
Reduce
sum / max for quick health checks.
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
#!/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.

Debugging
Debugging: when it doesn’t work
| Symptom | Likely cause | Fix |
|---|---|---|
ValueError: setting an array element with a sequence | Ragged nested lists | Make every row the same length |
| Values are huge (e.g. 28.6) | Degrees stored in a _rad array | Use np.radians() first |
| Integer array cuts 0.5 to 0 | dtype is int | Create with floats or dtype=float |
a * b is not a dot product | * is element-wise | Use a @ b or np.dot |
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
- Run numpy_joints.py; confirm sum 0.6 and max 0.5.
- Change one joint; recompute sum.
- 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
Coding practice
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
- Run
numpy_joints.py: shape (6,), dtype float64, sum 0.6, max 0.5, norm 0.13. - Convert
[0, 30, -10, 0, 20, 0]degrees withnp.radians(). - Clamp all joints to ±0.4 rad with
np.clipand print. - Write
numpy_custom.pywith your 6-vector, shape/dtype and a scaled xyz offset with norm.
Stuck? Hints, one at a time
- Hint 1: norm = √(0.12² + 0.05²) = √0.0169 = 0.13.
- Hint 2: 30° = 0.5236 rad, −10° = −0.1745 rad.
- 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
- NumPy docs — Absolute beginner's guide
- NumPy docs — numpy.linalg.norm
- NumPy docs — numpy.clip
- Photos: Wikimedia Commons (credits under each image).
Self-attested for now — keep artifacts in your lab notebook.
Complete
Today's goal
0 of 1 done today
Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).