# 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](https://docs.viam.com/components/motor/), 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](https://a.co/d/bxEdcAT), with `viam-server` installed per [our Raspberry Pi setup guide](https://docs.viam.com/get-started/installation/).
- [A wheeled rover](https://www.amazon.com/Smart-Chassis-Motors-Encoder-Battery/dp/B01LXY7CM3/)
- [A foam dart launcher](https://www.amazon.com/Nerf-N-Strike-Elite-Jolt-Blaster/dp/B01HEQHXE8)
- [A USB camera (webcam)](https://www.amazon.com/gp/product/B0972KK7BC/) (optional, to see where you are going and aiming)
- [A solenoid](https://www.amazon.com/0530-Frame-Solenoid-Electromagnet-Stroke/dp/B07K35L4TH/)
- [A relay](https://www.amazon.com/HiLetgo-Channel-Isolation-Support-Trigger/dp/B00LW15D1M/)
- [A dual motor controller](https://www.amazon.com/Qunqi-Controller-Module-Stepper-Arduino/dp/B014KMHSW6/) If you use a different motor driver, refer to the manufacturer’s data sheet and our [motor component topic](https://docs.viam.com/components/motor/) 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.

1. Attach left to the motor controller.
1. Use a small flathead screwdriver to loosen the **Out1** and **OUT2** screw terminals.
2. Place the red motor wire into **OUT1**
3. Place the black motor wire into **OUT2**
4. Use the flathead screwdriver to tighten the terminals to firmly hold the wires in place.
2. Connect the control wires **IN1** and **IN2** to your Raspberry Pi.
1. The example robot has **IN1** -> pin 11, **IN2** -> pin 13
3. Repeat steps 1-2 for the right motor using **OUT3**, **OUT4**, **IN3**, **IN4**
1. The example robot has **IN3** -> pin 16, **IN4** -> pin 18
4. Connect the external power that will supply power to the motors.
1. 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.

1. Connect the solenoid to the relay.
1. Connect a wire to the Normally Open ( **NO**) terminal connector.
2. Connect the other end to a ground pin on the Raspberry Pi.
2. Connect the Relay **COM** pin.
1. Connect the **COM** pin to the 3.3V power of the Raspberry Pi.
3. Connect **VCC** ( **DC+**) and ground ( **DC-**).
1. Connect **DC+** to the 6V of the external battery pack.
4. Connect **IN**.
1. Connect **IN** to a GPIO pin on the Raspberry Pi.
2. For this example, we've connected to pin 37.

### Assemble solenoid/foam dart launcher

1. 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.
2. Test that the solenoid has enough power to press the trigger when the foam dart launcher is loaded.
3. 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.
1. Tape the solenoid in such a manner that it makes good contact with the trigger when activated with the relay.
2. 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](https://docs.viam.com/get-started/installation/#install-viam-server).

### Board configuration (Raspberry Pi)

Add a [_board component_](https://docs.viam.com/components/board/) 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`](https://docs.viam.com/components/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`](https://docs.viam.com/components/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](https://docs.viam.com/components/base/), which describes the geometry of your chassis and wheels so that the software can calculate how to steer the rover in a coordinated way.

1. Select the motors attached to the base in the fields as your **right** and **left** motors.
2. Enter `200` for `wheel_circumference_mm`.
3. Enter `130` for `width_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](https://docs.viam.com/components/camera/webcam/) for complete instructions.

### Final config

The full raw JSON config file generated by the configuration steps you just completed.

```json
{
  "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

1. Make sure your foam dart launcher is loaded and ready to go.
2. 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.
