Build a foam dart launcher | Viam
Build a foam dart launcher
Hazal Mestci
Developer Advocate
This tutorial will show you how to build your very own foam dart launcher robot using Viam, a Raspberry Pi, a generic foam dart launcher with foam darts, a USB camera, a solenoid, a relay, and a motor controller. This robot will be able to move around and launch foam darts.
What you’ll need for this tutorial
You will need the following hardware, software, tools, and consumables to complete this project:
IMPORTANT: If you use a different rover and/or motor component, ensure that the motor driver is compatible with the motors on your rover. For example, a brushed DC motor requires a brushed DC motor driver that is rated for the power requirements of the motor. Also, the configuration files shown in this tutorial must be modified if you use a different setup.
Hardware and software requirements
- Raspberry Pi with microSD card, with
viam-serverinstalled per our Raspberry Pi setup guide. - A wheeled rover
- A foam dart launcher
- A USB camera (webcam) (optional, to see where you are going and aiming)
- A solenoid
- A relay
- A dual motor controller If you use a different motor driver, refer to the manufacturer’s data sheet and our motor component topic to learn how to configure the pins.
- Jumper wires (breadboard wires)
Tools and consumables
- Solder (optional)
- Small flathead screwdriver
- Cutting pliers (flush-cutting pliers preferred)
- Electrical tape
- Elastic/rubber bands
How to assemble your hardware
Motor controller setup
A motor controller is a piece of hardware that takes digital signals from the Raspberry Pi and sends power to the motors accordingly. For this setup we have one dual motor controller for the two motors.
We need only worry about OUT1 through OUT4, IN1 through IN4, 12V, and ground.
- Attach left to the motor controller.
- Use a small flathead screwdriver to loosen the Out1 and OUT2 screw terminals.
- Place the red motor wire into OUT1
- Place the black motor wire into OUT2
- Use the flathead screwdriver to tighten the terminals to firmly hold the wires in place.
- Connect the control wires IN1 and IN2 to your Raspberry Pi.
- The example robot has IN1 -> pin 11, IN2 -> pin 13
- Repeat steps 1-2 for the right motor using OUT3, OUT4, IN3, IN4
- The example robot has IN3 -> pin 16, IN4 -> pin 18
- Connect the external power that will supply power to the motors.
- In the example we have 4 AA batteries connected to the motor driver giving us 6V of power for the motors.
Camera setup
This is as easy as plugging the camera into a USB slot on your Pi. We’ll configure the camera in the Viam app in later steps.
Relay/Solenoid setup
The solenoid component actuates the foam dart launcher trigger. The relay works as a switch to turn on and off the solenoid. This allows us to activate the foam dart launcher with a GPIO pin on the board.
- Connect the solenoid to the relay.
- Connect a wire to the Normally Open ( NO) terminal connector.
- Connect the other end to a ground pin on the Raspberry Pi.
- Connect the Relay COM pin.
- Connect the COM pin to the 3.3V power of the Raspberry Pi.
- Connect VCC ( DC+) and ground ( DC-).
- Connect DC+ to the 6V of the external battery pack.
- Connect IN.
- Connect IN to a GPIO pin on the Raspberry Pi.
- For this example, we've connected to pin 37.
Assemble solenoid/foam dart launcher
- Modify the foam dart launcher to make room for the solenoid. Using cutting pliers, we cut the trigger guard off of the front as seen in the picture above.
- Test that the solenoid has enough power to press the trigger when the foam dart launcher is loaded.
- If the solenoid is not strong enough we can:
- Wrap the trigger with rubber bands to make the trigger easier to activate.
- Increase the voltage to the solenoid.
- Tape the solenoid in such a manner that it makes good contact with the trigger when activated with the relay.
- Attach all of your components to the base.
Configure your foam dart launcher robot with the Viam app
Add a new machine in the Viam app and give it a name. Then, follow the setup instructions to install viam-server and connect your Pi to the Viam app.
Board configuration (Raspberry Pi)
Add a board component to represent your single-board computer, which in this case is the Raspberry Pi. To create the new component, click the + icon next to your machine part in the left-hand menu and select Component. Select the board type, then select the pi model. Enter a name or use the suggested name for your board and click Create. We used the name "local". You can name your board whatever you want, just remember to use that name consistently in the following steps.
Motor configuration
Right motor
Click the + icon next to your machine part in the left-hand menu and select Component. Select the motor type, then select the gpio model. Enter right as the name and click Create.
Then from the Board dropdown, select local, the Raspberry Pi the motor is wired to.
Next, configure the Component Pin Assignment section to represent how the motor is wired to the board. In the Component Pin Assignment section of the right motor card, toggle the Type to In1/In2 to use the compatible mode for this motor driver. For A/In1 select 16 GPIO 23 and for B/In2 select 18 GPIO 24.
Left motor
Click the + icon next to your machine part in the left-hand menu and select Component. Select the motor type, then select the gpio model. Enter left as the name and click Create.
Then from the Board dropdown, select local, the Raspberry Pi the motor is wired to.
Next, configure the Component Pin Assignment section for the left motor. For A/In1 select 11 GPIO 17 and for B/In2 select 13 GPIO 27.
Base configuration
Next, configure the base component, which describes the geometry of your chassis and wheels so that the software can calculate how to steer the rover in a coordinated way.
- Select the motors attached to the base in the fields as your right and left motors.
- Enter
200forwheel_circumference_mm. - Enter
130forwidth_mm. (measured between the midpoints of the wheels).
If you used different hardware, measure the diameter of your wheels and multiply by pi for the circumference. Measure the distance between the centers of the right and left wheels to find the width.
Camera configuration
Add your USB camera as a webcam. Please refer to our webcam documentation for complete instructions.
Final config
The full raw JSON config file generated by the configuration steps you just completed.
{
"components": [
{
"name": "local",
"model": "pi",
"type": "board",
"namespace": "rdk",
"attributes": {},
"depends_on": []
},
{
"name": "left",
"model": "gpio",
"type": "motor",
"namespace": "rdk",
"attributes": {
"pins": {
"a": "11",
"b": "13",
"pwm": ""
},
"board": "local",
"max_rpm": 150
},
"depends_on": []
},
{
"name": "right",
"model": "gpio",
"type": "motor",
"namespace": "rdk",
"attributes": {
"pins": {
"a": "16",
"b": "18",
"pwm": ""
},
"board": "local",
"max_rpm": 150
},
"depends_on": []
},
{
"name": "base",
"model": "wheeled",
"type": "base",
"namespace": "rdk",
"attributes": {
"width_mm": 130,
"wheel_circumference_mm": 200,
"left": ["left"],
"right": ["right"]
},
"depends_on": []
},
{
"name": "camera",
"model": "webcam",
"type": "camera",
"namespace": "rdk",
"attributes": {
"video_path": "video0"
},
"depends_on": []
}
]
}
Now that you have configured your motors, you can actuate them. Navigate to the Control tab.
Activating the foam dart launcher
- Make sure your foam dart launcher is loaded and ready to go.
- Navigate to the Viam app -> CONTROL tab -> board component and set the IN pin (pin 37 in our example) to high to activate.
Troubleshooting
If any component fails to appear when connecting to the robot in the Viam app, check the Logs tab for potential errors.
Summary
In this tutorial, you learned how to create a remotely-controlled foam dart launching robot activated by the GPIO pins on a Raspberry Pi using Viam. You could use this same concept as the basis for various robotic implementations.