CNC, PCB

CNC for PCB: Firmware

I have considered two open sourced firmwares: Marlin and GRBL. I have already used Marlin as a firmware for my modified Anet A8. Since it is mainly focused on 3D printers and is pretty advanced I have decided to go with simpler option. This is how I have decided to go with GRBL.

GRBL adaptation code

All changes that I had to do to make GRBL running with Anet A8 mainboard can be found under feature/support-anet-a8-based-cnc branch in my fork.

Stepper motor adaptation

GRBL works nicely with CNC Shield. It is maximizing precision of XYZ axis movement by keeping control of stepper motors on a single port. We can find this part of configuration in GRBL sources:

// Define step pulse output pins. NOTE: All step bit pins must be on the same port.
#define STEP_DDR        DDRD
#define STEP_PORT       PORTD
#define X_STEP_BIT      2  // Uno Digital Pin 2
#define Y_STEP_BIT      3  // Uno Digital Pin 3
#define Z_STEP_BIT      4  // Uno Digital Pin 4
#define STEP_MASK       ((1<<X_STEP_BIT)|(1<<Y_STEP_BIT)|(1<<Z_STEP_BIT)) // All step bits

The same part is being defined for other pins required by stepper driver - direction and enablement pins. Anet A8 mainboard is not compliant with this restriction as we can see in the schematic.

So first thing I had to do to adopt GRBL was to make changes in abstraction layer which would allow to keep control pins for different motors on different ports. I have defined couple macros which are taking care of pin control and hidding implementation detail of this operation:

#define SET_STEP_DDR()                    { ... }
#define WRITE_STEP_PORT(val)              { ... }
#define SET_DIRECTION_DDR()               { ... }
#define WRITE_DIRECTION_PORT(val)         { ... }
#define SET_STEPPERS_DISABLE_DDR()        { ... }
#define WRITE_STEPPERS_DISABLE_PORT_ON()  { ... }
#define WRITE_STEPPERS_DISABLE_PORT_OFF() { ... }

All changes related with abstraction change can be found in commit Allow stepper pins to be configured on different ports by changing abstraction.

So far I had only correct abstraction but still missing appropriate configuration for Anet A8 mainboard. GRBL calls this “cpu map”. I did not want to tamper with default cpu map for CNC shield. Even when I was working on my private branch I wanted to keep things fairly clean. To have better undestanding of what I had to change in cpu map I have decided to make an exact copy of CNC shield configuration and called it CPU_MAP_ANET_A8. This was my next commit Copy default configuration as Anet A8 board base.

So right now I have started to adapt cpu map for Anet A8 mainboard:

  1. Change output type to atmega1284p - since CNC shield uses different microcontroller
  2. Configure Anet A8 stepper pins - finally I was able to configure appropriate pins for used mainboard

Assertions on configuration

I have decided to put some constraints to GRBL configuration and validate them in cpu map:

First part about dual axis is pretty self-describing. Regarding second part I have decided that I do not want to use variable spindle speed because spindle motor probably will need to be powered from more than 12V (which is available on the mainboard).

After making first runs I have found our that power supply that I have chosen was not able to handle starting of the spindle. As a workaround I have used voltage regulator which was successfully lowering starting current. In parallel I have connected regular switch to manually close it after spindle start. In this circumstances decision about excluding variable spindle speed looks like appropriate one.

Cleanup unused parts

There are couple part of the configuration that I wanted to cleaup just to not get any conflicts or unexpected behaviours:

Those were the parts that I did not plan to use so I have set it up to unused pins just to prevent conflicts.

Additionally I have defined pins for limit switches to be inline with the mainboard in Setup limit pins for Anet A8 board commit. For now I have not planed to use them right now but I might use them in the future.

Final setup

Only two things left to finish the configuration of cpu map: