Using my 4th axis as a "poor man's lathe"

If you milled something cool and you want to show it to others post it here.

Using my 4th axis as a "poor man's lathe"

Postby A_Camera » Mon May 17, 2021 8:04 am

I bought a rotational axis ( 4th axis or A axis call it whatever you like) for my CNC with a 4 jaws chuck. I intend to use this as both a 4th axis on my CNC, or as an independent "poor man's lathe". I know, it will be a very primitive lathe with a lot of limitations, but I intend to try it out anyway to toy with and to learn. Maybe later on I'll buy a real lathe as well, but for now this will do. I am sure I can use it for some sort of work, but I will see that later.

To get the lathe function I had to design a control box. This is based on an Arduino Uno module with a 2x16 lines LCD. It is a very simple design, using only three buttons, one for clockwise rotation start, one for counter clockwise rotation start and of course, one for stop. Stepper acceleration and deceleration is also implemented, with a possibility to select acceleration in 7 steps. The chuck rotation can be adjusted between zero and 380 RPM, which is the maximum. This corresponds to 2280 RPM on the stepper, due to the 1/6 reduction pulley the stepper drives the chuck with. So it is a respectable rotation speed for being a stepper. The rotation speed is set using a rotary encoder and that speed is saved in the EEPROM, so after a power off, the last used rotation speed is restored. The rotary encoder push button is connected to the Arduino reset and acts as an emergency stop, in case there is a need for stopping as fast as possible. Of course, this means no deceleration, just an abrupt instant stop.

For powering and control I also built a separate PSU, which provides 48V 7A DC through four individual 12V power supplies connected in series. Three of these are 9A supplies, the fourth is though only 7A, which is why the maximum is limited to 7A, but considering that the stepper is only 3A, I figure that the PSU will be good enough for the purpose.

The PSU box also contains a DM542 stepper driver, which is configured to the maximum current and 2x micro stepping, which results in 400 steps per rev.

There is also a relay inside the box. This relay decides if the 4th axis is to be driven as A axis by my CNC using UCCNC software, or independently run as a "lathe", controlled from my control box and rotated non-stop until i press the stop button. The relay can be switched from the control box and the default setting (with the relay off) is that the rotational axis is controlled by UCCNC.

Yesterday I made a short video about testing the control box functions and showing the rotations. I have not used it yet as a lathe, still waiting for some necessary tools before I can do any real tests, but I thought this can be interesting to show as well. Maybe it will give other people some ideas as well.

I intend to share the Arduino code here as well. It is well commented, so based on the code, the simple box can easily be built by anyone understanding the basics of Arduino. I will need to add some file header information before I post it here, but it will be posted soon.

I hope you will enjoy watching this video. I will make a new one once I start using it as well.

A_Camera
 
Posts: 571
Joined: Tue Sep 20, 2016 11:37 am

Re: Using my 4th axis as a "poor man's lathe"

Postby A_Camera » Mon May 17, 2021 12:40 pm

For those interested, here is the Arduino code:

Code: Select all
//------------------------------------------------------------------------------------------------------------
//
//  Version information to display at start:
//
#define Ver_nr 10     
#define Ver_date 20210516
//
//  This software is created by Adapting Camera. You are free to use it in a non-commercial basis.
//
//  YouTube: https://www.youtube.com/c/AdaptingCamera/videos
//  Blogger: https://adapting-camera.blogspot.com/
//
//  This is a program to run a stepper motor an infinite number of steps in CW or CCW direction
//  to allow constant rotation. The idea is to use a 4th axis of a CNC as a lathe.
//  This will result in a sort of "poor man's lathe", with limited useability, but enough for my needs for now.
// 
//  Functionality is simple, three buttons, one for each direction and one for stoping the stepper rotation.
//  Maximum speed is 380 rpm in this implementation, which in my case is equal to 2280 RPM for the stepper
//  motor, due to the 1/6 reduction gear I have. The chuck RPM can be set via a rotational encoder
//  between 0 - Chuck_rpm_max, which is set to 380 below.
//  If used with faster motors or a different gear ratio then Gear_Ratio and Chuck_rpm_max must be changed.
//
//  Stepper motor acceleration and deceleration is implemented, which can be set in seven steps.
//  Fastest is Max_Acceleration (7000) steps/s² and that value is divided by the value of the BCD wheel value.
//  The push button of the rotary encoder is connected to the Arduino reset and acts as an emergency stop.
//
//  Uses interrupt for the stepper pulse generation and also for the rotary encoder handling.
//  The rotary encoder is using a simple acceleration parameter, rotating the kob very fast results in jumps
//  of ten units per detent, less fast results in jumps of 5 units per detent, and slow rotation results in
//  one unit per detent. The drawback of using interrupt on the rotarional encoder is that if the knob is
//  rotated very fast it may interfere with the timing of stepper pulses, which may stop the stepper, or
//  result in step loss and loud bang when the stepper jumps into roattion again. This can only be avoided
//  if the RPM is left alone during stepper rotation, or if the rotary encoder would be handled outside
//  the interrupt handling routines, but in that case reading the encoder would present a problem.
//  The three buttons are outside the interrupt chain and are read in the normal program loop. This has the
//  Disadvantage of a bit slower reaction, so an very short push may not result in any reaction.
//  I may change this later to handle it as state change IRQ on the inputs, but I think this is good enough.
//
//  The last used RPM is saved in the EEPROM every time one of the two rotation direction button is pressed.
//  This saved value is read back after each reset or after powering up the unit.
//
//  This code is tested only with Arduino Uno, but it will probably work also with other Arduino devices.
//
//  Good luck.
// 
#include "FastAccelStepper.h"
#include <LiquidCrystal.h>
#include <EEPROM.h>
#include <RotaryEncoder.h>

#define Step_Pin    10
#define Dir_Pin     11
#define Enable_Pin  12

#define pinA  2 // Our first hardware interrupt pin is digital pin 2
#define pinB  3 // Our second hardware interrupt pin is digital pin 3

#define RightButton A0 
#define LeftButton  A2 
#define DownButton  A1 

#define BCD_0       A3 
#define BCD_2       A4 
#define BCD_4       A5 
volatile byte Read_BCD = 0;
volatile byte  Old_BCD = 0;

int Gear_Ratio = 6.0;                       // 6:1 gear in rotary axis
float Chuck_rpm_k = (1/0.15) * Gear_Ratio;  // This is a constant which is used for chuck RPM for frequency conversion
int Chuck_rpm_max = 380;                    // Maximum rpm for the chuck
int Running_rpm;                            // This is the speed now

int SpeedInHz;
int Acceleration;               // Set after the read of BCD wheel
#define Max_Acceleration 7000   // Set as default acceleration 7000 steps/s²

volatile boolean  START_STOP = false;
volatile boolean  Running_CW = true;
volatile boolean  Enable_ON  = false;

volatile byte A_Flag          = 0;  // Rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte B_Flag          = 0;  // Rising edge on pinB to signal that the encoder has arrived at a detent
volatile int  Encoder_Pos     = 0;  // Current value of encoder position.
volatile byte EEPROM_Enc_pos  = 0;  // EEPROM adress of the saved encoder position. This adress read at start.
volatile int  Old_Enc_Pos     = 0;  // Last encoder position value.
volatile byte Reading         = 0;  // Direct values read from our interrupt pins before checking to see if moved a whole detent
volatile int  ms              = 0;  // Time between roraty encoder pulses

FastAccelStepperEngine Engine = FastAccelStepperEngine();
FastAccelStepper *A_stepper = NULL;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);    // Use these IO pins for LCD RS,E,D4,D5,D6,D7
RotaryEncoder Rotary(pinA, pinB, RotaryEncoder::LatchMode::FOUR3);

void setup() {
  Serial.begin(115200);
  lcd.begin(16, 2);                 // Initialize the 16 x 2 LCD
 
  byte hiByte = EEPROM.read(EEPROM_Enc_pos);      // Read the last saved speed before reset
  byte loByte = EEPROM.read(EEPROM_Enc_pos + 1);
  Encoder_Pos = word(hiByte, loByte);             // Need to split the integer into two bytes for the EEPROM
 
  Engine.init();
  A_stepper = Engine.stepperConnectToPin(Step_Pin);
  A_stepper->setDirectionPin(Dir_Pin);
  A_stepper->setEnablePin(Enable_Pin);
  A_stepper->setAutoEnable(true);

  A_stepper->setSpeedInHz(SpeedInHz);
  A_stepper->setAcceleration(Acceleration);
  DisplayOnLCD();
  pinMode (RightButton, INPUT_PULLUP);
  pinMode (DownButton,  INPUT_PULLUP);
  pinMode (LeftButton,  INPUT_PULLUP);

  pinMode (BCD_0,       INPUT_PULLUP);
  pinMode (BCD_2,       INPUT_PULLUP);
  pinMode (BCD_4,       INPUT_PULLUP);

  digitalWrite(RightButton, HIGH);  //use the internal pullup resistor
  digitalWrite(DownButton,  HIGH);  //use the internal pullup resistor
  digitalWrite(LeftButton,  HIGH);  //use the internal pullup resistor

  digitalWrite(BCD_0,       HIGH);  //use the internal pullup resistor
  digitalWrite(BCD_2,       HIGH);  //use the internal pullup resistor
  digitalWrite(BCD_4,       HIGH);  //use the internal pullup resistor

  pinMode(pinA, INPUT_PULLUP);    // Set pinA as an input, pulled HIGH
  pinMode(pinB, INPUT_PULLUP);    // Set pinB as an input, pulled HIGH
  attachInterrupt(0,PinA,RISING); // Set an interrupt on PinA for a rising edge and execute PinA ISR
  attachInterrupt(1,PinB,RISING); // Set an interrupt on PinB for a rising edge and execute PinB ISR

  lcd.setCursor(0,0);                     // Erase the display
  lcd.print("                ");
  lcd.setCursor(0,1);
  lcd.print("                ");
  lcd.setCursor(0,0);                     // Display the values
  lcd.print("Version: ");
  lcd.print(Ver_nr);
  lcd.setCursor(0,1);
  lcd.print("Date: ");
  lcd.print(Ver_date);
  delay (2000);
}

//------------------------------------------------------------------------------------------------------------
//
//  Start the main loop
//
//  Read the BCD wheel, the rotary encoder and three buttons in the loop.
//
void loop() {

//------------------------------------------------------------------------------------------------------------
//
//  Read the BCD encoder and set acceleration accordingly.
//  The acceleration is set by dividing Max_Acceleration with the value of the encoder.
//
    Read_BCD = PINC & B00111000;                  // Read port C and mask the BNC encoder bits
    Read_BCD = Read_BCD >> 3;                     // Right shift 3 times
    if(Old_BCD != Read_BCD) {
      Acceleration = Max_Acceleration / Read_BCD;   // Calculate the divisor
      A_stepper->setAcceleration(Acceleration);     // Set the new acceleration value     
      Old_BCD = Read_BCD;
      DisplayOnLCD();
    }

//------------------------------------------------------------------------------------------------------------
//
//  Read the rotary encoder and set chuck rpm accordingly.
//  The rpm is set through converting the encoder value to pulse time in microseconds.
//  This value is used as speed value for the stepper.
//
  if(Old_Enc_Pos != Encoder_Pos) {
//    ms = Rotary.getMillisBetweenRotations();
    if ( Encoder_Pos <= 0 ) {
      Encoder_Pos = 0;
      SpeedInHz = 0;
    } else {
      if ( Encoder_Pos >= Chuck_rpm_max ) {
        Encoder_Pos = Chuck_rpm_max;
      }
    }
    Old_Enc_Pos = Encoder_Pos;
    if ( Encoder_Pos << 0 ) {
      SpeedInHz = (Encoder_Pos * Chuck_rpm_k);
    }
    DisplayOnLCD();
    attachInterrupt(0,PinA,RISING); // Set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
    attachInterrupt(1,PinB,RISING); // Set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  }

//------------------------------------------------------------------------------------------------------------
//
//  Read buttons to set rotation direction or to stop the rotation
//
  if ( digitalRead(DownButton) == false) {    // Stop with decelerating to zero RPM
    A_stepper->stopMove();
    Running_rpm = 0;
    DisplayOnLCD();
    delay (2000);
  }

  if (digitalRead(LeftButton) == false) {   // Set CCW rotation...
    if (Running_CW == true) {               // ...but only if not rotating to CCW now.
      A_stepper->stopMove();
      delay (2000);
      Running_CW = false;
    }
    if ( Encoder_Pos >> 0 ) {
      A_stepper->setAcceleration(Acceleration);
      A_stepper->setSpeedInHz(SpeedInHz);
      A_stepper->runBackward();
    } else {
      A_stepper->stopMove();
    }
    Running_rpm = Encoder_Pos;
    DisplayOnLCD();
    EEPROM.write(EEPROM_Enc_pos, highByte(Encoder_Pos));    // Save in EEPROM the current speed as last speed
    EEPROM.write(EEPROM_Enc_pos + 1, lowByte(Encoder_Pos));
  }

  if ( digitalRead(RightButton) == false) {   // Set CW rotation...
    if (Running_CW == false) {                // ...but only if not rotating to CW now.
      A_stepper->stopMove();
      delay (2000);
      Running_CW = true;
    }   
    if ( Encoder_Pos >> 0 ) {
      A_stepper->setAcceleration(Acceleration);
      A_stepper->setSpeedInHz(SpeedInHz);
      A_stepper->runForward();
    } else {
      A_stepper->stopMove();
    }
    Running_rpm = Encoder_Pos;
    DisplayOnLCD();
    EEPROM.write(EEPROM_Enc_pos, highByte(Encoder_Pos));      // Save in EEPROM the current speed as last speed
    EEPROM.write(EEPROM_Enc_pos + 1, lowByte(Encoder_Pos));
  }
  delay(100);
}

void DisplayOnLCD() {
  lcd.setCursor(0,0);                     // Erase the display
  lcd.print("                ");
  lcd.setCursor(0,1);
  lcd.print("                ");
  lcd.setCursor(0,0);                     // Display the values
  lcd.print("Set RPM: ");
  lcd.print(Encoder_Pos);
  lcd.setCursor(0,1);
  lcd.print("Run RPM: ");
  lcd.print(Running_rpm);
  if (Running_rpm == 0) {
    lcd.setCursor(12,1);
    lcd.print("STOP");   
  } else {
    if (Running_CW == false) {
      lcd.setCursor(13,1);
      lcd.print("CCW");
    } else {
        lcd.setCursor(13,1);
        lcd.print("CW");
    }
  }
}
//------------------------------------------------------------------------------------------------------------
//
//    Interrupt handling routines for the rotation encoder to interpret left or right rotation.
//    The rotation will increment or decrement the value of Encoder_Pos.
//    Time between pulses is beasured and acceleration is applied if it is faster than 150ms between pulses.
//
void PinA(){
  Rotary.tick();
  ms = Rotary.getMillisBetweenRotations();

  detachInterrupt(PinA);                // Disable interrupts to stop happening before we read pin values
  Reading = PIND & 0xC;                 // Read all eight pin values then strip away all but pinA and pinB's values
  if(Reading == B00001100 && A_Flag) {  // Check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    Encoder_Pos --;                     // Decrement the encoder's position count
    if ( ms <= 150) {                   // Accelerate the counting if time between pulses is shorter than 150ms
      if ( ms <= 25){
        Encoder_Pos = Encoder_Pos - 9;  // Decrement 9 each time if time between pulses is shorter than 25ms   
      } else {
        Encoder_Pos = Encoder_Pos - 5;  // Else decrement 4
      }     
    }
    if (Encoder_Pos <= 0) {
      Encoder_Pos = 0; 
    }
    B_Flag = 0;                         // Reset flags for the next turn
    A_Flag = 0;                         
  }
  else if (Reading == B00000100)  B_Flag = 1; // Signal that we're expecting pinB to signal the transition to detent from free rotation
                                              // Enable interrupts again in the main loop
}

void PinB(){
  Rotary.tick();
  ms = Rotary.getMillisBetweenRotations();
  detachInterrupt(PinB);                // Disable interrupts to stop happening before we read pin values
  Reading = PIND & 0xC;                 // Read all eight pin values then strip away all but pinA and pinB's values
  if (Reading == B00001100 && B_Flag) { // Check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    Encoder_Pos ++;                     // Increment the encoder's position count
    if ( ms <= 150) {                   // Accelerate the counting if time between pulses is shorter than 150ms
      if ( ms <= 25){
        Encoder_Pos = Encoder_Pos + 9;  // Add 9 each time if time between pulses is shorter than 25ms 
      } else {
        Encoder_Pos = Encoder_Pos + 4;  // Else add 4
      }     
    }     
    if (Encoder_Pos >= Chuck_rpm_max) {
      Encoder_Pos = Chuck_rpm_max; 
    }
    B_Flag = 0;                         // Reset flags for the next turn
    A_Flag = 0;
  }
  else if (Reading == B00001000) A_Flag = 1;  // Signal that we're expecting pinA to signal the transition to detent from free rotation
                                              // Enable interrupts again in the main loop
}


Feel free to use it or improve or change it to your taste.
A_Camera
 
Posts: 571
Joined: Tue Sep 20, 2016 11:37 am

Re: Using my 4th axis as a "poor man's lathe"

Postby A_Camera » Sat May 22, 2021 5:18 pm

So I took the next steps and my experimental project continues. I couldn't help myself and could not wait, I just HAD to do some tests, which I documented yesterday in a short video.



While one could wish for higher possible speeds, I think considering it is driven by a stepper motor, the results are very good so far, so I will definitely continue. I am still waiting for cutters to be delivered, but for drilling it seems to work. I am now pretty sure that it will work also for turning, but of course, that still remains to be seen. Anyway, I'll be happy I made this, also because I often end up wanting to drill centre hole into something, and that's not that easy without a lathe, so if for nothing else, I will definitely be able to use it for that.

Anyway, while I will finish this with the stepper I have on the chuck now, I am already planning for an upgrade with a more powerful servo or a closed loop stepper. I have already started to look for solutions even for that step. Unfortunately it is not just about changing the motor, because it means I have to change the plate it is fixed to as well, and also get a new timing belt as well as maybe a new pulley. So it may take some time before that step is done.
A_Camera
 
Posts: 571
Joined: Tue Sep 20, 2016 11:37 am

Re: Using my 4th axis as a "poor man's lathe"

Postby A_Camera » Wed Jul 28, 2021 1:31 pm

The fun continues. I continued with my "poor man's lathe" project, which has become quite an expensive activity, but fun. :P I made the first major upgrade, changed the stepper to a 180W servo, which required changes in gearing, the pulleys are now 60-20 teeth, so the ratio is 3:1. I also replaced the aluminium extrusion with heavy duty type, added 1605 ball screws and 20mm linear rails on all axes. Of course, the control box also needed to be updated since I can now get 1000 rpm chuck speed.

The change in gearing forced me to replace the belt to a longer one, and I also had to make a new fixture for the motor. I wish I could replace the old fixture with a new one, but I could not find any way of removing the old one, so I added a sort of piggy back plate. Anyone ever removed the motor holder plate, please tell me how to do it without destroying the whole thing. I can not for my life figure it out. I know that removing the chuck and the large pulley is not enough and it looks like the spindle needs to be dismantled. I don't want to do it without knowing how.

One thing I am disappointed is the noise. I find the servo is quite noisy. I don't know why, but it is not what I expected. Also, the holding torque seems to be a joke compared with the stepper I had, so maybe this servo is far too weak as a rotational axis, where high holding torque is necessary. Anyway, I will finish the project first, before I buy a more expensive and larger servo.



By the way, the nosise comes from the timing belt, so that's normal, not much I can do about that, except if I build a sound proofing box around the motor and the spindle and of course, the pulleys.
A_Camera
 
Posts: 571
Joined: Tue Sep 20, 2016 11:37 am


Return to What you've made with your machine post the pictures, videos here

Who is online

Users browsing this forum: No registered users and 1 guest