A camera, and the geometry behind it
You will: Turn pixels into a position in metres, measure the error, and find its physical cause.
Builds on Frames that save you a day each.
A camera gives you pixels. The arm needs metres, in its own frame. The path between them is geometry you can check: the pinhole model turns a pixel into a ray, the camera's intrinsics say how, and the camera's place in the TF tree turns that ray into the arm's frame. Get the optical frame wrong and the arm reaches 90° away from the object, with every rate and number looking fine — the frame lesson in stage 2 was about exactly this.
The SO-101 needed a camera first. Two placements are common: on the wrist, where it sees what the jaw is about to touch and moves with every joint, or fixed above the bench, where the geometry is simpler and the arm gets in its own way. This project chose fixed overhead, and wrote down why (ADR 0006): from a camera that never moves, a pixel is a ray in a frame that never moves, so every step from pixel to metres can be checked by hand. The wrist camera's pixel also carries five joints of forward kinematics and their timing, which is a later lesson.
Upstream's description has no camera, so it is overlay, like the
world anchor: the profile says where it is, and the description
gains camera_link and camera_optical_frame on every target, and a
rendered sensor in Gazebo. The world decides whether to pay to render
it, and on a CPU that is a real decision: with shadows on, the
real-time factor fell from about 0.9 to 0.12.
A pixel alone does not say how far away anything is. Something has to: here, a guess at the height the cube is at, a plane the ray is met with. Then measure, against the simulator's ground truth, how far off the estimate is — not one number but a distribution, against where the cube was in the picture — and find the physical cause of its shape before reaching for a model.
In the simulation
A fixed overhead camera looks straight down at the bench; its picture is inset in the view and its frustum is drawn. Move the jaw through it and compare the picture with what tf says. The page is kinematic, a drawing of the arm with no physics and no rendered sensor; turning a real rendered picture into metres, and measuring the error, is the Gazebo half below.
Kinematic: joints move at their real speed limit, but there is no gravity, no load and no physics. That is what the steps below are for.
The SO-101 loads as you reach it.
make drive keysIn this lesson
- Open Inspect, then tf, and move the tool to the centre of the camera's picture, x 0.20 to 0.25, y within 2.5 cm of zero.
- Lower it, still centred, to below 8 cm. Same pixel, different depth; one pixel is a ray, not a point.
- Put the tool on the cube. tf now gives the ground truth a pixel estimate would be measured against.
Joints · SO-101 follower
1–5 joint · , . jog · [ ] jaw · - + step · h home · f frames
the keys of make drive
The buttons do what the keys do: hold one to repeat.
Limits from the description; speed capped at 1.7434 rad/s, the servo's datasheet derated. Poses from the profile.
Inspector
Code
The repository's own code behind what you do here appears once you move the arm.
Do it
Read where the camera is. It is in the robot's profile, beside the anchor, because upstream's description has none.
sed -n '/^sensors:/,/^control:/p' src/threevn_robot_description/config/so101_follower.yamlExpect:
parent: base_link,xyz: [0.226, 0.0, 0.723], pitched1.5707963to look straight down, 640 x 480 athfov_deg: 72.9,update_rate: 5.0.See what the overlay made of it. Two frames, and the rotation between them is the optical convention.
make urdf | grep -A4 'joint name="camera'Expect:
camera_link_jointfrombase_linkat0.226 0.0 0.723, andcamera_optical_frame_jointwithrpy="-1.5707963 0.0 -1.5707963". The frames are there on every target; onlyTARGET=gzadds a<sensor>.Start the arm in the world that renders the camera. Leave it running.
make sim WORLD=so101_benchExpect: Three controllers come up. The world has the lab's 35 mm cube, in green, and shadows turned off.
In a second terminal, read the intrinsics the camera publishes, and check one by hand: fx = (640 / 2) / tan(72.9° / 2).
make shell ros2 topic echo --once /camera/camera_info --field kExpect: Nine numbers, a 3 x 3 matrix row by row:
433.24539185twice on the diagonal (fx = fy),320.and240.for the centre, and a1.last. 320 / tan(36.45°) is 433.2. At 0.7 m, one pixel is 1.6 mm on the bench.Measure. The simulator puts the cube at 30 places on the bench; each time the script finds it in the picture, back-projects the pixel onto the plane through the cube's centre, and compares with where the simulator says it is. About three minutes.
make cameraExpect:
30 positions seen, 0 missed,median 1.3 mm,worst 2.0 mm, and the shape:the same everywhere: +1.0 mm in x, +0.8 mm in yand+0.4 mm outward per 100 mm. Thenok.The part that is the same everywhere is about half a pixel, 0.8 mm, in each direction. The blob's centre is an average of pixel indices, and index 0 is the first pixel's centre. The intrinsics put the image centre at 320, the edge between pixels 319 and 320, so they count pixel centres at 0.5, 1.5, and so on: every index is half a pixel short. Add the half pixel back and measure again.
make camera ARGS="--shift-px 0.5"Expect:
median 0.4 mm,worst 0.9 mm, and+0.2 mm in x, +0.0 mm in y. The constant part was the half pixel.Now get the height wrong on purpose, and assume the cube lies flat on the bench.
make camera ARGS="--plane-z 0"Expect:
median 4.8 mm,worst 8.2 mm, and+2.9 mm outward per 100 mm. The geometry predicts 17.5 / (723 − 17.5) = 2.5 mm per 100 mm on top of the 0.4 already there: a ray met too low lands too far out, in proportion to how far out it is.Run the arithmetic's tests, which need no simulator.
make shell cd /ws/src/threevn_perception python3 -m pytest test/test_detector.py -qExpect:
33 passed, among them the principal point landing straight below the camera, the two signs a mirrored picture gets wrong, and a wrong height costing in proportion to distance.
make camera in so101_bench sees the cube at all 30 places with a median error under 2 mm, and you can say what each part of the error is: half a pixel that is the same everywhere, and a part that grows with distance from the point under the camera when the assumed height is wrong.When it goes wrong
make camera says there is no picture on /camera/image_raw.
Why: The world does not render. empty_bench, where the simulator tests run, loads no rendering system on purpose; the camera is described but never drawn.
Fix: make sim WORLD=so101_bench.
The simulation crawls, a real-time factor of 0.1 to 0.25.
Why: Rendering upstream's full-resolution meshes on a CPU, twice when shadows are on. Resolution matters much less than shadows.
Fix: Keep shadows off in any world that renders this arm, as so101_bench does, and measure before raising update_rate.
One position is wrong by centimetres, and its estimate is the previous position's.
Why: A frame from before the cube moved, still queued. The script's first version did this, 70 mm out, because it took its reference time from a frame instead of the clock.
Fix: Wait on simulated time after the move, from /clock, then for a frame stamped after it; verify_camera.py now does.
Every estimate is mirrored across the arm's centre line.
Why: The image was read in camera_link instead of camera_optical_frame, or a sign was "fixed" in the back-projection.
Fix: The image says which frame it is in, header.frame_id; transform from that one, and keep the detector's tests green.
Terms
- Pinhole model
- The simplest camera model: every pixel is a ray through one point. Good enough for most robotics, and checkable by hand.
- Intrinsics
- A camera's focal lengths and image centre, which turn a pixel into a ray.
- Back-projection
- Turning a pixel back into a ray, and the ray into a point by meeting it with something whose distance is known or assumed, here a plane at the object's height.
Every term in the course: glossary.
Check yourself
Answer in your head first, then open it.
Why can one pixel not give a position on its own?
Every point along its ray lands on it. A position needs one more fact, the distance along the ray, or a surface it meets; here, the height of the plane the cube is on.
The error has a part that is the same everywhere and a part that grows with distance from the point under the camera. Which is which cause?
The constant part is the half-pixel convention between the blob's centre and the intrinsics. The growing part is the height assumption, plus what the camera sees of the cube's top and side faces off-axis.
Why does empty_bench not render the camera, when the camera is in the robot's description?
Rendering is a world service and costs most of the simulation's speed on a CPU. The tests run there and read no pictures, so they should not pay; worlds that look load the system.
Stretch
Find a height for --plane-z that makes the growing part of the error zero, and explain why it is not the cube's top, 35 mm, even though the top is most of what the camera sees.
Hint
Off-axis, the camera also sees the side of the cube that faces it, which pulls the blob towards the point under the camera.