Step 2
Direction, distance & the dot product
Subtract points to get vectors, then use lengths, unit vectors and dot products to aim the gripper.
Points and vectors
A point is a place: the red ball is at , in metres from the robot's base. A vector is a displacement, an arrow with a length and a direction. Subtract two points and you get the vector from the first to the second:
Length and direction
For , the norm is the vector's length: here, the distance from the gripper to the ball (np.linalg.norm(v)). Divide a vector by its norm and you get a unit vector of length 1: a pure direction.
The dot product
The dot product turns two vectors and into one number:
is the angle between them. For unit vectors the dot product is : 1 when they point the same way, 0 when they are perpendicular, −1 when they are opposite. So . Rounding can push the cosine a hair past 1, and np.arccos then returns NaN, so clip it to first.
Projection
The dot product also splits a vector into parts. The part of along a direction is its projection:
says how far goes along (in metres here), and multiplying by turns that distance back into a vector. What's left, , is perpendicular to .
Jacobians, step 3 steers the gripper by commanding vectors like these as velocities.
Your task
- Implement
unit(v),angle_between(a, b)(in radians) andproject(v, onto). - Seen from above, the arm reaches along the gripper's start position with set to 0. Find the ball whose position (also with ) makes the smallest angle with that reach direction.
- Move the gripper to the point 8 cm short of that ball on the straight line from where the gripper started to the ball's centre.
The starter draws an arrow from the gripper to every ball. Draw the chosen ball's arrow split into its part along the reach and the rest, then check that the two are perpendicular. The balls move on every run.