Monday, December 27, 2021

The Claw!

Just in time for Christmas Eve, 2021, we completed The Claw!


 

This is a project that was a befuddling concept from July, 2021 until November, 2021. 

In initial conception, I mainly looked to the Arduino Claw Machine v4 videos on youtube.  The concerns rolling in my head, in no particular order, were:

  • How does the claw mechanism work?  How much power does it need?  How much does it weigh?
  • How do people do the linear motion for these?  Lead screws?  Belt-drive mechanisms?  Rack-and-pinion?
  • How will I build a frame for it?

Sadly, due to the pandemic, I'd lost access to my preferred makerspace.

It was my good fortune in July to have gone to a garage sale where they had been remodeling, and were giving away some clean bathroom cabinets.  Those would form the basis of the physical structure.

I ordered a solenoid claw on eBay in July, 2021, just to get an idea of how it worked.  I was surprised by how heavy it was, and that made me think that I'd need a strong framework for the X-Y linear motion mechanism.

The Cubex Duo

In November, the project got a big boost when a friend gave me the frame of his old Cubex Duo 3D printer, after he'd heard I was building the claw machine.  

 


The machine had a belt-driven motion assembly and reed limit switches for both axes.  All of that was working.  The print bed and extruder and hot end were removed, and weren't necessary for my build.  The main frame was comprised of 12mm rods in a cube layout, where the rods were about 18 inches (500mm) apart.

The Cubex also came with a BigTreeTech SKR 1.3 motor driver board, and several BTT TMC 2209 v1.2 motor drivers.  There was a 12v power supply, too, but I ended up using a separate DC transformer.  (This was only because I was doing prototyping in my kitchen, and the 12v PSU would pop the GFCI circuit.)


 


 

For my purposes, I intended to use the joystick signals to indicate motor movement, and just step the motors in different directions.  The SKR 1.3 motor driver board was more geared toward having a Marlin implementation set up on it, and having motion managed by gcode.  I ended up just using the TMC 2209 boards directly, leaving the SKR 1.3 out of it.


 

Unfortunately, most web sites that talk about the TMC 2209 don't address direct usage in an Arduino style.  I had hoped to take advantage of some of the sensorless homing capabilities of the drivers.  After a few experimental attempts at that, I just went with a basic bit-banging implementation.  That worked out really well, and I still got very quiet motor movement.

The Cubex Y axis actually has two NEMA 17 motors driving belts that move the X axis motor and rails.  With some clever wiring, they made it such that both motors could be driven from a single stepper controller.  In fact, the machine could run with just one or the other motor, as long as it's properly lubricated.

The Cubex X axis moves the print head platform left and right.  That platform rides along two 12mm rails and has its own magnet for triggering the X axis minimum limit reed switch.

When I got the Cubex, it still had its NEMA 23 motor and centered lead screw that would have raised or lowered the print bed.  I didn't need that motor or lead screw, so those were removed.

The Z axis (spool/winch)

For this build's Z motion, I needed a reel or winch-type assembly to reel in or release the cord holding the claw.  I didn't want to disassemble the platform, because it was already clamped to the X axis belts, and properly tensioned.  Instead, I measured it and chose to use its center hole for the rope.  


 

I printed a NEMA 17 motor mount so that the motor could directly drive a spool.  That was designed to allow it be held in place with screws and fender washers, but I ended up placing it with double-stick tape, and it remains that way now.  I might revisit that when it comes time to transport the machine, especially if I have to lay it down sideways.




I lathed the spool out of some excess hardwood.  One end of the spool had a 5mm hole and set screws to attach it to the NEMA 17.  The opposite end had an 8mm nub sticking out, and that went into a pillow block.  The pillow block rested atop another printed plastic block so that the spool could run level.


 

The claw rope isn't held to the spool in any clever way.  It's just taped to the interior of the spool, and sufficient excess rope is wound around it to keep it in place.

There is a printed gland that the rope goes through, and it serves two purposes.  First, it prevents the rope from chafing against the sharp aluminum edges of the platform hole.  Second, it has mount points for a microswitch.  I used an existing microswitch and bent its metal lever so that it'd be hit by the claw.  That serves as my "Z up" limit switch.  (The picture shown here has v1 of the gland, where I hadn't added the limit switch mount points yet.)

  

This is "version 2" of the gland, shown from below, including the Z limit switch.  The gland was extended to be a longer tube, and the microswitch is held in place with just some twist ties.



Breadboarding motor controllers

The TMC 2209 motor controllers are mounted in two 1x8, 2.54mm pitch, female headers.  Those, in turn, are soldered to solderable breadboards (ElectroCookie 9 pack, $21).  One easy mistake to make with TMC 2209 boards is to have their DIAG or INDEX pins touch the breadboard.  I bent those out of the way but kept them in case I reuse them in some other way in the future.


 

I wired up the TMC 2209's in a pretty basic way.

  • EN is always low, hardwired to GND.  As such, I don't control motor enablement in software.
  • MS1 and MS2 are set for half-stepping.  It's interesting to note that these same pins are used for UART addressing.  Many web sites don't mention that.
  • I'm keeping the motor voltage and ground isolated from the Arduino 5v and GND lines.  Some sites suggest tying the Motor and Arduino GND lines together, but I like the isolation.
  • The motor outputs are connected to JST-SM headers (pack of 20, pre-wired, $10).  While not as neat as having JST-XH headers, these JST-SM sets allowed me extra wire length, and it was nice having four wires for each.
  • I did put in a 100uF 50V capacitor across motor GND and motor V.  The specs typically call for a 47 uF or larger cap there.  If you're using a motor carrier board, it's probably built on to the board already.  (NB: specs also call for small caps near digital GND and 5V, but those actually already are on the TMC 2209 boards.)

As I soldered breadboards, I found that it was useful (from a physical layout perspective) to have the limit switch signal lines of each axis co-located with the axis motor lines.  

Solenoid and relay

For the claw solenoid, I found a 30V power supply at the thrift store.  Based on what was written on it, I think it was originally for some kind of powered speakers.  I wired that through a relay module and it worked fine.  I'm pretty sure the relay module has built-in flyback diodes to avoid reverse power spikes that solenoids can generate.



Sound

The sound system is built using the Mini DF Player module.  I had some of those from my earlier R/C Roomba project.  That took a bit of getting re-familiarized with the module and its quirks.  
 

 
The main things about the DF Mini Player:
  • Communication is done on a Serial line.  The Arduino TX line runs at 5V and the module's RX runs at 3.3V, so you have to step it down or reduce it in some way.  Many sites just have a 1k resistor in line between the two.  I don't totally get how that works, but it does.  There was one site showing how they'd voltage-divide it down to 3.3, and I think that's a better reasoned approach.
  • For my purposes, sound files go onto a FAT32-formatted SD card and within the /mp3 folder.  They must follow a nnnn_<whatever>.mp3 file naming convention.  And despite the numeric nnnn prefixing, the actual play order and file indexing is based on the order in which the files are added to the SD card.
  • The Busy line coming from the module is directly useful.  There are some sites out there where people are asking, "How can I tell if the song is done playing?" and then people tell them to use the API to figure that out.  Instead, I wired directly to BUSY line and check its value with digitalRead.
  • I couldn't find a way to pause a song and resume it.  Ideally, I'd want to signal recognition of a coin drop by playing a special sound, but if that happens in the middle of a long file, I end up fully stopping the current song, playing the "coin drop" song, and then not restarting what I'd interrupted.  Of course, one way around this would be to have a second Mini DF Player.

Front panel controls

I'm using an original Mortal Kombat arcade joystick.  I probably could have used something with less authentic retro cred, but it was kind of cool to use the real thing.

The arcade button is a typical one you can buy these days that has an internal spring and causes closure of a mounted microswitch.

The coin mechanism made of three pieces: a "slot" piece, a microswitch mount, and the microswitch itself.  The slot piece has a hole 3mm wide and about 24.5mm long to allow a US quarter to slide through.  It's a simple, straight drop slot.  Shown here is the original design with countersunk screw holes that, as it turns out, I didn't really need.  (Ideally, it should drop mount holes from above, and allow screw attachment below.)
 
The lower piece allows the microswitch to be mounted.  I ended up just using a length of wire (blue in the picture below) to twist-tie the microswitch to the mount piece.  The switch's metal lever was bent so that the coin would hit it firmly while passing through.  In early prototypes, the coin would get stuck, held up by the switch lever, but it works fine.

 

The Arduino loop polls the coin on each pass, and it's fast enough to recognize the coin drop without needing an interrupt.  It also ignores duplicate coin slot signals within a 1 second period.

Keeping the signal wires tight

The front panel electronics -- joystick microswitches, button microswitch, coin microswitch -- all connect their lines through an MDFLY ethernet breakout board.

 
There's one of those boards on the Front Panel side, and one near the Arduino.  That allowed me to use terminal block-style wire clamping to arrange all the wires, and neatly carry all the signals from the cabinet front to the cabinet back using a single ethernet cable.  
 
The ethernet cable allows me to have one GND signal, four signals for joystick, one for button, one for coin.  There's one spare that I could have used for something like an electric eye to detect a win, but I ran out of time to build that.

Motion control prototype

During this phase of prototyping, the Cubex Duo was just that -- a cube.  It's shown operational here.




By the time the project was completed, the Cubex Duo was reduced to just the top "square" of what was the printer.  That is, I am using the four top 12mm rods of the frame, the four top machined "cylinders" that hold those rods together, the original X axis with its drag chain assembly, and the Z platform.  All the vertical supporting rods, and the bottom "square", and the bototm Z axis motor were removed and saved. 

Physical structure

Physically, the main framework of the build is two bathroom cabinets, one stacked atop the other.  The front panel is attached to the upper cabinet.  
 

Upper cabinet

The claw motor assembly barely fits inside the top portion of the upper cabinets.  I added a few boards to support the cylinders better, and routed out a few sections to allow space for the drag chain assembly.

Though the Cubex top section is slightly too large for the cabinet interior, I found that the cabinet's removable front panel gave me the extra space I needed.  But, it also was too much space, allowing the top section to fall, if the whole thing were to slide forward too far.  To prevent that, I printed a few blocks to keep the cylinders from moving too far.  Shown here is the front, left cylinder with supporting wood and black plastic block.  I've also put the frame on felt pads to reduce vibration, so that's the little black disc under the metal cylinder.

Front Panel (physical)

The front panel was built from 1/2" plywood.  I measured multiple ways, and prototyped it with hardboard.  The plywood pieces are joined using printed L brackets.  When done, the whole front panel was screwed to the cabinet's front edge at five points, and held to the cabinet sides with metal, 4" mending plates.
 
This is the front panel / upper cabinet assembly as it evolved, before the FP was attached to the cabinet:
The main difficulty with the joystick (and other similar joysticks) is that you have to undo a C clip to install it.  It's screwed into the front panel from below.
 
For both the joystick and arcade button, I used a 1" spade bit.  I cut in (drilled in) from both the top and bottom to avoid tearout.  I still needed to sand a bit to get the button to slip in, and I used a Dremel to chamfer the edges for the joystick.

This is the FP underside:

 

 
To get the slot cut out of the front panel, I printed a separate template to help me drill 1/8" holes, whose edges wouldn't go past the ends of the "slot" piece.  That's the grey rectangle in the picture above.  I then used a scrollsaw to cut the section between the holes, and then cut into the "corners" of the rectangle.  It would have been easier for me to re-print the "slot" piece in such a way that it could hide my mistakes, and allow screw-in attachment from behind, but I ran out of black PLA at the end!

The ethernet cord goes through the base part of the upper cabinet.  I drilled 1/2" holes, but should have gone larger.  I ended up having to cut extra out.
 

Electronics (PC) assembly

In all, the computation part of the machine was comprised of
  • One Arduino Mega 2560
  • One breadboard for the X, YLeft and YRight motor drivers
  • One breadboard for the Z motor driver and MiniDF Player module
  • One mini breadboard for hardwired pull-up resistors
  • One MDFLY ethernet board (to talk to the front panel via an ethernet cord)
  • One relay module (for the claw solenoid) and the appropriate jack for the 30V power supply
  • One wire connector for the 12v motor supply
I call all of that together the "PC" for the claw machine.
 
The PC components are all mounted on a piece of spare acrylic using nylon M3 standoffs ($12). Shown here is initial layout, and then standoff placement.  I didn't do this in a super precise way (using CNC to drill holes).  Instead, I placed the components, marked holes using a fine tip Sharpie, and drilled by hand.  I feel good about the non-conductivity of the nylon standoffs, but some of the nuts/screws didn't thread all that well, compared to brass standoffs.



Here's what it looks like with components attached to the PC base.

The top of the PC is another piece of acrylic, cut to the same dimensions as the PC base board, but with a section cut out to allow room for the ethernet cable.  

I used 2.5" lengths of 3/4" square hardwood (3' length, 3/4" hardwood square, Home Depot, $4) as standoffs separating the PC base and top.  I put those in as an afterthought, figuring out their safe placement only after the PC components were screwed down.  I used a pen lathing vise and a drill press to get consistent, centered holes on both sides of each "standoff" wood piece.  The standoffs were drilled to accept #6 x 1/2" screws, and the acrylic had corresponding larger holes that would allow pass-through of those screws.  I used countersink screws only because they were available.  Using hex-head ones would have been better.


Front window

The front of the game cabinet is a big sheet of acrylic with a hole cut out for prize access.  Its top slots into the same wood piece that I used for supporting the Cubex Duo frame.  That was routed out in several passes to accommodate the acrylic thickness and also allow for some slop in case I cut the acrylic poorly.
 
This is the front frame before painting and protective plastic removal. The T-shaped "window frame" originally was the cabinet spacer on the right side of the cabinet.  Note the screws now holding the FP to the cabinet.

 

Hopper bin

The "hopper bin" is a bent piece of acrylic held to the game area base by L brackets (wood screws into base, metal #8-32 screws through the hopper bin plastic).  I modeled that initially with cardboard to get a sense of size and to make sure its dimensions would be appropriate for the drop zone of the claw. 
 

I was pleasantly surprised with the acrylic bending technique.  I used a hot gun on low setting, warming the acrylic for a little over 2 minutes.  Then, I bent it over an edge of a makeshift vise.

This mostly worked, but to make sure the edges were square in both dimensions, I reheated it and bent it within a garage cabinet.  That let me keep an eye on the alignment of all the edges as it cooled.

Back panel

The cabinet back panel is just a sheet of 1/4" plywood.  We added contact paper on the inside to hide its wood look.  Holes were cut out of the top to allow the motor control, solenoid, and limit lines through.  There are three 3D-printed pieces that keep it in place, two on the bottom, and one on top.

PC-to-Back panel mounting

I printed my own panel holders to support the PC on the back panel.  They provide about 5mm separation between the PC base and the back panel, allowing room for the standoff nuts and such.  There are six of them holding the PC in place.  

 I only had #6 x 1/2" machine screws and nuts for this part, and they extend as far as I wanted to get through the back 1/4" plywood panel and Con-Tact paper, so I used the heated insert trick: I put a hot soldering iron to the screw, and melted it into all but one of the 3D-printed panel holders.  That got the screw in farther, and also made the screw fixed to the plastic.

Roof

There is a simple hardboard panel that serves as the roof for the machine.  I drilled holes so that it could be positioned using the cabinet's existing corner braces, which I assume had holes for holding a countertop or similar.  The holes allow bolts to keep the roof in place.  But, I chose not to actually have nuts attaching the roof firmly.  That way, one can open up the roof to do repairs or even toss in toys.

The roof also serves as the mount point for interior lights.  But since it's hardboard, and the rough side is on the inside, it's not something that would work for adhesives, such as are found on LED light strips.

Speaker

The speaker is visible in the picture above at the lower edge of the PC board.  By size and shape, I'm assuming it's a typical coaxial car stereo type of speaker, and thus probably 4-ohm impedance.  (The DF Mini Player specs I've seen online recommend 3-ohm or higher impedance.) 

Out of sheer laziness, I attached the speaker with just one screw to one of the bottom PC wooden standoffs.  That saved me the effort of cutting a speaker hole, etc.

Lighting

The initial build didn't have any lighting.  It looked good in earlier pictures, but that's just because it was positioned under a garage light fixture (exposed LED T4 bulbs) with the roof off.

I had three under-cabinet, LED puck lights that ran on 12v (1.1W) so I positioned them inside -- two in front, one in back -- and hooked them up.  The wires ran out the back, and all I had to do was power them.

Using the same power connection point as the motors, the lighting was pretty good.  It wasn't as bright as the garage lighting.  The problem was that the lights flickered a lot as the motors would run.  I was using a 12V (measured 11.5V), 2.1A wall wart.

I tried running the lights off a separate 12V supply and that went better, but the lights constantly flickered.  I attribute that to having a bad power supply.

Finally, I got a 12V, 4A wall wart, and used that on its own for both motors and lighting.  That went well, so now I have lights.

Cabinet stacking

The upper cabinet is stacked atop the lower cabinet.  I used some spare 1/2" plywood to make something akin to mending plates in order to join them.

These mending plates were roughly 6" tall and 10" wide.  I kind of blew it with their positioning.  Originally, the plan was to mount them near the rear of the cabinet.  That's because cabinets come with a toe kick area, which makes the range of placement shorter for the upper cabinet.  I just habitually put one side's mending plate centered, and then did it the intended way for the other side.  Measure twice, right?

The mending plates were sanded on all edges.  Part of the reason for that was so that the upper cabinet could slip over the plates more easily.

I pre-drilled 3/16" holes at four points on each plate, and drew a midline horizontally on each.  With the midline facing outward, I positioned one on a side, and drilled 1/4" holes through the plate and the lower cabinet side wall.  I used 1/4" carriage bolts, so I had to make a little extra room for the "square" inner portion of the bolt head.  I had hoped that simple compression into the cabinet wall's MDF would work, but the MDF was too strong for that.  So, I ended up using something like a 19/64" drill, and just drilled a small amount inward to help seat the bolt head.

After both plates were installed on the bottom cabinet, we lifted and positioned the upper cabinet on top, sliding it over the mending plates.  Then, I repeated the hole drilling -- again from inside to outside -- on the upper holes of each plate.

This is roughly what it looks like on the inside, though by the time I'm done it'll have fender washers.  Without fender washers, due to how soft the plywood is, the nut and lock washer bite too deeply (for my tastes) into the wood.

This is the outside appearance.  Using these kinds of carriage bolts is nice, because they have a smooth surface, meaning now unscrewing/unbolting it from the outside.  Also here you can see the mending plate that keeps the front panel attached to the upper cabinet at those side points.  I couldn't hide those as easily, because I didn't want to break the structure of the toe kick plate of the upper cabinet.


Painting and cosmetics

My wife did all the painting of the exterior and interior of this, and it really made a difference.  We used black semi-gloss (Behr Urethane Alkyd Enamel, Home Depot, 29 fl oz for $18!!).  That stuff has gotten so expensive!

It was really cold and humid during the build, so the paint wasn't completely dry by Christmas Eve.  We accelerated some of the drying by moving smaller parts indoors, and using a heat lamp on the upper cabinet.

Inside the cabinets, we used Con-Tact white shelf lining.  Doing that as a two person job was a requirement, at least the way we were doing it.

Doing both the painting and lining made the machine look much, much better.

Power

Finally, the whole thing is powered by single power strip that has the 30v solenoid power supply, 12v motor and lighting power supply, and USB wall wart (to power the Arduino).

Software

The software structure is a fairly simple state machine:
  • Init state does X, Y, and Z homing and moves to Advert mode
  • Advert state plays a random song every 15 seconds.  If credits are added, it moves to Play Intro state.  Advert state also moves the claw to a Y position of 100 steps.
  • Play Intro state plays a start song, waits for it to end, and then moves to Play state.
  • Play state looks for joystick and button operations.  Joystick positions set up dx and dy values of 1 or -1 as appropriate.  If the button is pushed it moves to Grab Descend state.
  • Grab Descend state drops the claw a pre-determined number of steps, and then goes to Grab Engage state.
  • Grab Engage state activates the claw solenonid and goes to Grab Ascend state.
  • Grab Ascend state raises the claw until the Z limit switch is seen (and re-zeros the Z axis when the limit is hit).  It then moves to Grab Deliver state.
  • Grab Deliver state moves the claw to the hopper bin location and moves to Grab Release state
  • Grab Release state releases the claw solenoid and goes back to Advert state, which moves the claw back to Y position 100 (which is roughly 1/6th inch from the back edge).  Part of the reason for the move to Y 100 is to get the claw over the toy bin, so if someone turns off the machine, the claw falls in an area that's away from the hopper acrylic.
At each state change, there's an option to stop the current music, set up a new song to play, and choose to put that music into repeat mode.  Each state change also has some kind of event that causes transition to the next.  The code evolved this way, and could have been written such that the states were more  like their own StateHandler objects with defined transitions and events, but it's all linear C code right now.
 
The software doesn't use Arduino interrupts.  The main thing I think of for those would have been to recognize coin drops, but even the different state transitions could have been done with interrupts.

The overall software loop does these things
  • Check to see what the current song number is.  If the DF Mini Player is busy, wait.  Otherwise, play the current song, if the repeat counter is 1 or more, and decrement the repeat counter.
  • Based on the current mode, check input mechanisms (coin slot, joystick, button, timer).
  • Depending on inputs, set up dx/dy/dz values (1, 0 or -1) and/or stop the current song and/or move to the next state.
  • Process motor controls.  At this point, dx, dy, dz are set up with some values.  If those are non-zero, and if we're not already at limit points, perform stepper motor steps, and possibly add a short delayMicroseconds delay for smoother motion (and to avoid stalling).

Additional steps

There remains one last ugly bug in the software.  If the machine is turned on when the Y axis is already homed, it seems like it thinks Z is homed.  I'm not sure why.

As it turns out, I circumvented this problem by adding the Advert state function, which moves the head to Y position 100.  I had done that to get the claw away from the hopper bin acrylic, because upon power-off, the claw drops rapidly, and I didn't want the hopper bin to be damaged.  But as it turns out, that same function moves the head away from Y position zero in this errant case, so a second boot will start normally.

I have a sneaking suspicion that the root cause of that bug is in the limit switch wiring.  I'm kind of thinking that all the limit switches should run with physical pull-ups that are the same, rather than using Arduino internal pull-up resistors.  But that's totally a guess right now.

Final thoughts

What?  "Final" thoughts?  No way, I want to make another one!

The Claw was really, really well received by the kid.  Merry Christmas!

Getting the Cubex Duo frame was the real kickstarter for this build.  When I look back at the project timeline, it went in fits and spurts from July through mid-November, but then it kicked into high gear.

Having the bathroom cabinets for the frame also helped a ton.  Overall, I was fortunate to have lots of stuff so I could do this on the cheap.  I also was fortunate to have various power tools for this.  I used almost every power tool at my disposal.  

It was super useful to have a 3D printer for this.  Modeling was done in Fusion 360.  I'm using a somewhat aging Artillery X1 and mostly printed with Hatchbox black PLA.  I printed pieces for

  • NEMA 17 mount for the Z axis
  • Z axis pillow block support
  • Z axis gland and limit switch
  • Blocks that prevent the whole assembly from falling into the cabinet
  • Back panel clamps
  • Jig for centering screw holes to attach FP to cabinet
  • Front Panel L brackets (dual and single)
  • PC supports/braces
  • The coin slot top and bottom pieces

I didn't really get to learn how to build my own belt-driven, linear drive mechanism.  Now that I've seen how they did it with the Cubex Duo, I feel like I could do it.

I don't quite understand how real claw machines leave their claws in an "up" position.  When my system powers off, the claw falls.  I'm guessing that they use geared DC motors, and it's the gearing that provides really strong physical resistance.

Next time, I'll have a better way to lay out the circuit boards.  I'll put limit switch circuits, including pull-ups, closer to each motor, and I'll plan for having limit switches on both ends of the X and Y axes.

I'd like to learn more about those TMC 2209 motor drivers and get them documented better for Arduino people.

If I were to build this again, I think I'd have a different layout of the window and window frame. 

Huh.  I should probably post a video of the thing in action.