ARM LAB / STUDY / LEVEL 8 OF 8
The teach
pendant.
Every piece of UI so far was made of 3D things: small cubes for buttons, a line font for text, and a hand-written ray test for each one. The course's G2 library offers a different route. Draw whatever you like with the browser's ordinary 2D canvas, let the engine upload that canvas as a texture every frame, and show it on a square. For input, G2 asks where the controller beam, or the mouse, hits the square, in the canvas's own −1..+1 coordinates, and its widgets do the rest. Arm Lab uses this for a teach pendant, the hand-held control box real robots come with: a dial and a slider for every motor. This page draws the real panel with a copy of G2's drawing routines and the scene's own dial rules; drag a slider and the arm follows.
The panel, and the arm it drives
drag a slider · move the mouse to see the hit coordinatesWhat the square shows (512 × 512 canvas)
The arm, from the side
In the headset the panel floats to the left of the robot, turned toward you; the same canvas is drawn here. The dial's grey arc is the motor's whole physical range, orange is the warning band from level 5, and the needle takes the label's colour. The gripper's slider begins where the jaws touch, so it cannot be dragged into the overlap.
What G2 sees
The six numbers
Keys, sticks and sliders all write the same six numbers, so the sliders have to be written back from the angles each frame when nobody is dragging one. Otherwise a key press would move the arm and leave the slider behind.
Building the panel
armLab.jsOne G2, one texture channel, one square. addWidget gives each motor a slider whose callback writes the angle, selects the motor and stops the demo. render is called by G2 every frame after it clears the canvas: it draws the background, the title, and for each motor a highlight, a dial, a name and a value. The dial is three arcs and a line; dial in the model file says where the arcs and the needle go.
Each frame
armLab.jsThe dial and slider rules
so101Model.jsBoth run over the physical range, 0 at the low limit and 1 at the high limit. The warning bands use the URDF range so they agree with the labels of level 5.
Inside G2: where did the beam hit, and what does a slider do with it
g2.js, the course's librarycomputeUVZ is the whole bridge between 3D and 2D. On the desktop it builds a ray from the mouse; in VR it asks the controller beam; with hand tracking it uses the fingertip. All three end in the same answer, a point in the square's −1..+1 space. Slider only checks whether that point is in its rectangle and turns its x into a 0..1 value.
What you should be able to explain
- A texture is just an image, and a canvas is an image you can redraw. The engine keeps a list of canvas textures and re-uploads each one every frame it is marked animated. Anything the 2D API can draw, any font, any curve, any chart, ends up on the square.
- One coordinate system for drawing and for hits. G2 draws in −1..+1 and
mHitRectreports hits in −1..+1, so a widget's "am I being pointed at" is a rectangle test with no 3D in it. The 3D is done once, by multiplying the beam by the inverse of the square's global matrix. - Three inputs, one path. Mouse, controller beam and fingertip all become a (u, v) point and a press / drag / release state. A widget never knows which one it was.
- Two-way binding by hand. A slider drag writes an angle; every frame without a drag writes the angles back into the sliders. Without the second half, keys and sticks would desynchronise the panel.
- The pendant and the labels agree because they share rules. Colour, warning band and the gripper's contact limit all come from the same functions in the model file, so the dial can never say something different from the label beside the joint.
Exercises: ① Drag the gripper slider to the far left: where does the needle stop, and why is it not at the URDF's −10°? ② Why does sliderToAngle clamp v to 0..1 when G2's slider already does? ③ The panel is a square scaled by 0.3: how wide is it in metres, and how big is one dial? ④ Replace one slider with a button labelled ['OPEN', 'CLOSE']: which line of the callback changes?