Snack Delivery Machine — Part 2, the Electronics and Tech

Written by Sean Johnson

Categories: What's New

4 minute read time

Before we get technical, if you haven’t read our previous post about our wonderous snack machine, pop over and check that out first! We bought an off-the-shelf snack dispenser and turned it into a robot, but now it’s time to give it some brains. And by brains, we mean a raspberry pi.

If you’ve read up on some of our other mechanic projects, you may have noticed that we tend to use a Raspberry Pi for any hardware hack. They’re easy to set up, have a ton of extensions, and just about everyone loves playing with them. It was the perfect choice for what we wanted to accomplish — ultimate automated snackage.

Stepper Control

Speaking of extensions, one of the extensions out there is Adafruit DC & Stepper Motor HAT, which looks to be perfectly suited for us.

Electrical system as we’re going to use it

The board consists of a Pulse Width Modulation controller PCA9685 and 2 motor drivers based on TB6612FNG. The controller receives the commands over the I2C bus from the Pi and translates them to proper PWM signals. These low voltage signals enter the drivers and control the 12V outputs to power the motors.

Our motors are bi-polar steppers, which means they consist of 2 groups of coils that have to be energized with alternating polarity. Luckily, TB6612FNG is designed for working with dual motors, so each driver will be dedicated to its own stepper. The drivers have a separate power distribution circuit, which we’ll cover in the next section.

Power

We got our Pi from CanaKit and it came with a basic micro USB 2.5A 5V power adapter. The motors on the hat need 12V and should be powered separately. Since they draw 350mA each, we need to provide at least 700mA to them. The drivers draw about 2mA each, which is not that significant. However, when designing any system (even a software one), we highly recommend the safety factor of 2-3 or even more.

To build safe robust systems, we have to take our ignorance about the real world into account. Take, for example, the Tacoma Narrows Bridge vs. the Brooklyn Bridge. The Tacoma Narrows Bridge didn’t have a safety factor and it inevitably collapsed. Conversely, the Brooklyn Bridge was constructed with a safety factor of a 5-6 and is still around to this day.

Fortunately, there are tons of options when it comes to power supplies to keep our snack machine nice and safe. We just went with a 5A power supply. If you’re still worried about how to hook up the power supply safely, this guide can help.

Putting it all together

Now that we had everything we needed to complete the snack machine, it was time to put it all together. Picture time!

The LED rings are for Snack Machine v2.0.0

Before we really get going, we had to solder the HAT.

GPIO inputs are in the front, power and motor terminals are in the back.

Then it was time to attach the power connector.

12V for motors

With the power in place, we could attach the motors to the HAT.

It’s so beautiful.

Control software

To control the HAT over I2C, we need to install Adafruit CircuitPython MotorKit. The installation is pretty straightforward, and we ended up using this guide to get the steppers working correctly. In our case, we came up with a decent back-and-forth algorithm and tuned it to dispense the right amount of snack in a single run.

#!/usr/bin/python
# -*- coding: utf-8 -*-
# vim: set ai et ts=4 sts=4 sw=4 syntax=python:

import atexit
import logging
import sys
import time
from collections import OrderedDict
from contextlib import contextmanager
from datetime import datetime, timedelta

from Adafruit_MotorHAT import Adafruit_MotorHAT, Adafruit_DCMotor, Adafruit_StepperMotor

_log = logging.getLogger(__name__)
_mh = None
_last_dispensed_time = None

DEFAULT_SPR = 200  # Default Steps-per-Revolution
DEFAULT_RPM = 30  # Default Revolutions-per-Minute
DISPENSE_COOLDOWN = timedelta(seconds=30)
VALID_CHANNELS = [1, 2]
CHANNEL_MOTOR_MAP = {
    1: [1, 2],
    2: [3, 4],
}
DISPENSER_SERVE_STRATEGY = {
    # RIGHT
    #1: [
    #      OrderedDict([
    #          (Adafruit_MotorHAT.FORWARD, 50),
    #          (Adafruit_MotorHAT.BACKWARD, 25),
    #      ]),
    #      OrderedDict([
    #          (Adafruit_MotorHAT.BACKWARD, 50),
    #          (Adafruit_MotorHAT.FORWARD, 75),
    #      ]),
    #],
    1: [
        OrderedDict([
            (Adafruit_MotorHAT.FORWARD, 66),
            (Adafruit_MotorHAT.BACKWARD, 50),
        ]),
    ],
    # LEFT
    2: [
          OrderedDict([
              (Adafruit_MotorHAT.FORWARD, 100),
              (Adafruit_MotorHAT.BACKWARD, 50),
          ]),
          OrderedDict([
              (Adafruit_MotorHAT.BACKWARD, 125),
              (Adafruit_MotorHAT.FORWARD, 50),
          ]),
    ],
}


def init(hat_config=None):

    global _mh

    # create a default object, no changes to I2C address or frequency
    if hat_config is None:
        hat_config = {}

    _mh = Adafruit_MotorHAT(**hat_config)

    # recommended for auto-disabling motors on shutdown!
    atexit.register(turn_off_motors)


def turn_off_motors():
    """ Release all of the motors
    """

    global _mh

    _log.info("Shutting down all motors")

    for i in range(1, 5):
        _mh.getMotor(i).run(Adafruit_MotorHAT.RELEASE)


def can_dispense():
    """ Figure out if snacks can be dispensed
    """

    global _last_dispensed_time

#     if _last_dispensed_time is None:
#         return True
# 
#     if (_last_dispensed_time + DISPENSE_COOLDOWN) < datetime.utcnow():
#         return True

    return True


@contextmanager
def using_stepper(channel, steps_per_rev=DEFAULT_SPR, rpm=DEFAULT_RPM):

    global _mh

    # initialize the stepper
    _log.info("Create stepper controller: {} (spr={}, rpm={})".format(
        channel,
        steps_per_rev,
        rpm,
    ))
    stepper = _mh.getStepper(steps_per_rev, channel)
    stepper.setSpeed(rpm)

    # pass the stepper up to the parent context
    yield stepper

    # release the stepper
    _log.info("Releasing stepper: {}".format(channel))
    motor_nums = CHANNEL_MOTOR_MAP[channel]
    for motor in motor_nums:
        _mh.getMotor(motor).run(Adafruit_MotorHAT.RELEASE)


def dispense_to(motor_channel):

    if motor_channel not in VALID_CHANNELS:
        raise ValueError("Invalid motor channel: {}, expected one of: {}".format(
            motor_channel,
            VALID_CHANNELS,
        ))

    if not can_dispense():
        raise RuntimeError("Dispenser cooldown period - {}".format(str(DISPENSE_COOLDOWN)))

    with using_stepper(motor_channel) as stepper:
        # Get the dispense rates
        dispenser_strategy = DISPENSER_SERVE_STRATEGY[motor_channel]
        for operations in dispenser_strategy:
            _log.info("Executing operation set: {}".format(operations))

            for direction, steps in operations.items():
                stepper.step(steps, direction,  Adafruit_MotorHAT.DOUBLE)

Here it is! A working prototype!

Magic in action!

As usual, we connected the machine to Mailgun. This could be done in a few different ways and since we already covered it in other posts we are not going to concentrate on it here. However, there is an interesting opportunity to turn it into a real IoT device 😉

So far we just use it in the kitchen so a fellow mailgunner can feast on M&Ms like a boss. Still, it’s brought plenty of people delicious candy, and quite a few laughs when it became unplugged and then delivered too many M&Ms once plugged back in.

Tags: | | | |

Modified on: June 19, 2019

Stay up-to-date with our blog & new email resources

We'll let you know when we add new email resources and blog posts. We promise not to spam you.