Build a Robotic Claw Game with a Raspberry Pi | Tutorial
Build a Robotic Claw Game with a Raspberry Pi
Create your own version of the famous arcade claw machine game using a robotic arm, an arcade claw grabber, and some fun items to pick from. Fine-tune every intricate detail, from the precision of each grab to the claw’s strength and even the aesthetics of your control interface.
In this tutorial, you will:
- assemble the claw game machine and learn how to fabricate your own encasement for the machine
- learn how to configure the components using Viam
- master the art of controlling your robot with our motion service using the Viam Python SDK
- learn how to create a custom control interface using the Viam TypeScript SDK
Requirements
Hardware
To build your own claw game machine, you need the following hardware:
- A Raspberry Pi with a microSD card, set up following the Raspberry Pi Setup Guide.
- An xarm6 robotic arm
- An Arcade claw
- A Relay
- A 24V power supply for the claw
- An iPad or other tablet
- 1 x 4’x4’ fiberboard
- 10 x 2”x4”x8’ lumber
- 4 x 5/16 2 inch lag screws
- A box of 3” deck screws
- 8 x ⅜” 4 inch hex bolts with nuts and washers
- Velcro cable ties
- 3 x 4’x2.5’ sheets of plexiglass
- A box of small wood screws for mounting the plexiglass
- Items for the claw to grab. We used about 600 foam balls for our claw game.
Software
To build your own claw game machine, you need the following software:
Tools
You will also need the following tools:
- Drill and drill bit set
- Miter saw or handsaw
- Jigsaw
- Sliding square rule
- Socket wrench set
- Wood glue
- 3D printer
- Clamps
- Tape measure
- Safety glasses and ear protection
Build the robot
First, assemble the hardware components.
Build the table
To support the arm and create a surface to hold the prizes, create a flat surface that also has supporting beams so you can securely mount the arm. You will need to build the table to a size that allows for the arm to reach anywhere on the table surface. For the xArm6, a table that is 4’x4’ works.
Cut one 2"x4" in half, creating two 48 inch sides.
Then take two 2x4s and cut them to four 46.5 inch lengths.
Next, place the 48 inch sides parallel to each other on a flat surface, and place the four other sides perpendicular to and between the 48 inch sides.
Attach the 48 inch sides to two of the shorter lengths with deck screws, forming a 4 foot by 4 foot square.
Then find the center of the 48 inch sides and mark this on both sides.
Measure 2.5 inches from that center mark in either direction, and mark those points as well.
These will be the points where we attach the remaining lengths, which will provide a support structure for the robotic arm.
Attach these lengths with deck screws.
Use the fiberboard as a tabletop and cut a prize exit hole into it. Center the exit, make it 10 inches wide, 8 inches long and cut the opening with a jigsaw. We used two 2 foot by 4 foot fiberboards, but ideally you can use one 4 foot by 4 foot fiberboard.
Glue and clamp the tabletop and let it dry overnight.
Add legs and mount the arm
To finish the table construction, attach the supporting legs and mount the arm to the center of the table.
Cut four 2x4s to 78 inches to serve as legs for the cabinet.
Measure 30 inches from the top of each and mark this with the sliding square rule.
For each leg, line the 30 inch mark up with the top surface of the table, and drill two holes ⅜ inch holes through the leg and table surface.
Push two hex bolts through the drilled holes, add washers and nuts, and tighten.
For extra stability, cut two more 2x4s to 48 inch lengths, and mount these with deck screws on all four sides of the bottom of the cabinet, bridging the legs. You can also cut an additional 2x4 and mount it as a fifth leg, going from the center arm supports to the floor.
Finally, mount the xArm6 to the top of the table using the lag screws. Be sure that the lag screws sink into the 2x4 posts below, and that you are mounting the arm so that it is straight, with the X axis facing the player. You’ll need at least two people to ensure a smooth installation of the arm.
Configure the robot
Next, configure your newly-built robot.
Add a new machine on Viam. On the machine’s page, follow the setup instructions to install viam-server on the computer you’re using for your project. Wait until your machine has successfully connected to Viam.
Machines are organized into parts, where each part represents a computer (a single-board computer, desktop, laptop, or other computer) running viam-server, the hardware components attached to it, and any services or other resources running on it.
Every machine has a main part which is automatically created when you create the machine. Since you just created a new machine, your machine’s main part is already defined. Multi-part machines also have one or more sub-parts representing additional computers running viam-server. If you have two computers within the same machine, you can use one as the main part and connect the other to it as a sub-part. This is the approach this tutorial follows: you’ll run the motion planning service on a laptop and connect that laptop as a sub-part to your machine.
Configure the board
Click the Components subtab. Click the Create component button in the lower-left corner.
Add your board with type board and model viam:raspberry-pi:rpi if you are using a Raspberry Pi 4, Raspberry Pi 3 or Raspberry Pi Zero 2 W. If you are using a Raspberry Pi 5, use the pi5 model. Name your board myBoard and click Add to machine.
Click Save config in the lower-left corner of the screen.
Configure the arm
Click the + icon next to your machine part in the left-hand menu and select Blocks. Select type arm, and model viam:ufactory:xArm6. Name it myArm and click Add to machine.
Configure the arm component with the arm’s IP address in the host field. Our arm’s address was 10.1.1.26, but you should use the IP address for your arm.
Click Save config in the lower-left corner of the screen.
Configure the gripper
Click Create component and add your gripper. Choose type gripper and model fake. Name it gripper and click Add to machine.
Set up a fake model. You will only use this as a placeholder for the size of the gripper to use with Viam’s frame system later. Measure the claw’s height and width, and enter these values for the fake model. Ours was 120mm for the width and 180mm for the height.
Click Save config in the lower-left corner of the screen.
Set up the claw
3D print the claw mount
Download the STL for the claw mount, and use a 3D printer to print the mount for in between the claw and the xArm6.
Attach the claw to the printed mount
- With a screwdriver, remove the metal top cap from the claw by removing the side screws.
- Remove the string that came with the claw; it is not needed.
- Extend 2 or 3 M3 button socket cap screws through the recessed inner holes of the printed mount and through the slots on the top of the claw cap.
- Secure the M3 screws with nuts and tighten.
- Attach the printed mount and claw end cap to the claw, adding the previously removed screws and tighten.
Mount the claw to the arm
Using two M20 screws, attach the printed mount to the end of the arm and tighten.
Using hook-and-loop cable ties, run the claw’s cable along each segment of the arm to the arm base, making sure the cord is secure but with some slack to allow for movement.
Wire and test the claw
The arcade claw is actuated when a solenoid is powered, acting as a magnet to pull the claw shut. For this project, we use a relay, which allows us to programmatically control when power flows to the claw’s solenoid.
- Using a barrel jack adapter, connect the positive (red) wire from the claw to the positive terminal of the adapter.
- Then, connect the negative (black) wire from the claw to the
COMterminal on the relay. - Cut a length of wire and connect it between the
NOterminal on the relay and the negative terminal on the barrel jack adapter. This creates a normally open circuit, which means the circuit is normally not complete and the claw is therefore normally not powered.
In order to control the claw through Viam, you will now wire the relay to the Raspberry Pi.
- First, power down the Pi. Then take three female jumper wires, cut off one end of each, and strip the ends.
- Use one wire to connect the
DC+terminal on the relay to pin 2 (5v) on the Pi. - Use the second wire to connect the
DC-terminal on the relay to pin 6 (ground) on the Pi. - Use the third wire to connect the
INterminal on the relay to pin 8 (GPIO) on the Pi. - Now power on the Pi and plug the 24V DC adapter into the wall and the barrel jack adapter.
Once viam-server has started, you can test closing and opening the claw. The Viam board component gives us an interface for this. Go to the Control tab for your machine, open the MyBoard card, enter 8 next to Set under the GPIO interface, choose high and click Set Pin State. The relay will trigger the claw circuit to be closed when the GPIO pin state is set to high and your claw will close. Now select low and click Set Pin State again: the claw will open.
Create obstacles and a world state
The claw game machine will use the motion service to plan its movements. To make sure the arm doesn’t hit the walls of the enclosure or the prize drop hole, you need to create representations of obstacles around the arm that the motion service can use when planning.
Obstacles are geometries located at a pose relative to some frame. When solving a motion plan with movable frames that contain inherent geometries, for example parts of the arm, the solved path is constrained such that none of those inherent geometries intersect with the obstacles.
You can pass information about the robot’s environment, including obstacles, to the Viam platform through a data structure named WorldState.
These obstacle representations are defined in a JSON. You can find the file we used in our claw game repository. Represented in that file are obstacles for the prize drop hole and each of the four walls based on measurements we took from our enclosure. If the dimensions of your enclosure differ from ours, adjust your obstacles.json file to match your enclosure.
Find the home pose within the enclosure
By moving the arm through the Control tab, you can determine the arm’s ideal home pose, which is the position the arm starts each game and the one it returns to after making a grab.
In our enclosure, the hole pose and dimensions are as follows:
hole_origin = Pose(x=470, y=125, z=0, o_x=0, o_y=0, o_z=1, theta=15)
hole_dims = Vector3(x=250, y=400, z=300)
Additionally, the home position origin and dimensions where the arm needs to be in order to drop the prize are as follows:
home_pose = Pose(x=390.0, y=105.0, z=500.0, o_x=0, o_y=0, o_z=-1, theta=0)
home_pose_in_frame = PoseInFrame(reference_frame="world", pose=home_pose)
Use Python code to control the arm
Use git to clone the Claw Game project repository:
git clone https://github.com/viam-labs/claw-game
The claw game repository includes the Python test script CLI-test.py, which connects to your robot, creates an orientation constraint so the last arm joint is always facing down, and provides functions to:
- Grab and release the claw
- Move the arm to the home position
- Move the arm to a test position
- Move the arm forward, backward, right, and left
- Move the arm to the drop position
- Move the arm to the up position
In the following, you can read about what the different sections of the code do.
Summary
In this tutorial, you learned how to:
- Make your own claw machine.
- Test, configure, and control a robot arm using Viam’s motion service, the Viam Python SDK, and the web UI.
- Design your own custom interface using the Viam TypeScript SDK.
For some next steps, you could:
- Use the advanced interface included in the project repository to leverage the motion service for larger, more complex arm movement within the enclosure.
- Add a camera and use the vision service to add color detection, or use an ML model to determine grab success rate and create a score counter.
- Design a hard mode where the prizes are shuffled around with the arm every few attempts.
- Add a camera and extend the interface to allow folks from anywhere in the world to play the claw game and win.