Hello Guest it is April 16, 2024, 11:19:14 PM

Author Topic: Arduino - Mach 3 - Gcode M6 T0101  (Read 4548 times)

0 Members and 1 Guest are viewing this topic.

Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #10 on: March 26, 2022, 12:33:05 AM »
Good to see that you have progressed so far. You can view the brain using the menu Operator/Brain Control ... and then click on the Brain View button.

In the Arduino sketch you should handle the inputs and outputs through the DIO pins. What I prefer to do is use two of the pins of Arduino to communicate with Mach3. One pin receives signal from Mach3 to initiate the toolchange sequence and the other signals back to Mach3 when the tool change sequence has completed. You can connect all your tool change actuators and sensors to the input and output pins of the Arduino. Be careful when using the delay command in Arduino. During the delay loop the communication with Mach3 through modbus will stop. Better use the timer function in Arduino for the delay. Also be careful of the infinite loops in case of any actuator or sensor malfunction. Add checks in your code to break out of infinite loops based on a time limit.

Also you can program some error codes which you can feed back to Mach3 through the modbus registers.

Zafar

I had to uncheck the box "ignore tool change" in the general config and check the box "Auto toolchanger"
Now the macro is working fine along with userDRO of 2001. How will I test the brain I made to see if it is working the way it should?
Now it seems it is just the arduino sketch left to modify? So how do I do this and how do I add my toolchanger code to it? without either one interfering with the other?
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #11 on: March 26, 2022, 12:52:24 AM »
The terminator in the new brain should be like this.



So to clarify just check my images if this is correct?

and then in the sketch:

enum {       
        MB_REG0,
        MB_REG1,
        MB_REG10,
        // line below is for holding registers.    'vinny'
        MB_REGS   = 40    /* total number of registers on slave */
};

I added MB_REG10, ? is that correct?

I'm struggling with the brain where you say modbus output register as the terminal:
enter the modbus adres to use. what number should i enter here?
the CFG# would be number 2 then?
and macro modbus emulation?


« Last Edit: March 26, 2022, 01:07:46 AM by zafarsalam »
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #12 on: March 26, 2022, 01:04:22 AM »
It is not needed there.

I added MB_REG10, ? is that correct?


Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #13 on: March 26, 2022, 02:32:37 AM »
Thank you Zafar, this is really good information and much appreciated!

The delays in my sketch might be a bit of a problem. As I use delays to turn the stepper motors a step each time.

I will try to implement a timer method, I will study this as I have not done it yet I have only used delays and millis before.

What else do I need to add to the sketch? As I'm using an Adruino Mega I can use Pins 18 and 19 which are TX1 and RX1 respectively for the receiving and sending of the signals? But then I'd need to use a RS232 to USB adapter? Or did I perhaps not understand well? So then I do not need the USB 2.0 Cable Type A/B ? Can I attach my code for you to have a look at?
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #14 on: March 26, 2022, 08:14:24 AM »
I normally use the MAX3232 module to connect the arduino to serial port of the computer. Using pins 0 and 1 on arduino for RX and TX.

https://www.amazon.com/MAX3232-Converter-breadboard-Electronic-Connector/dp/B082B97DYQ

Why are you not using mach3 to drive the stepper motor for the tool changer. You can share the code if you wish.

Zafar
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #15 on: March 26, 2022, 09:04:27 AM »
Ah excellent, I'll purchase one of those, thank you.

The plan is:

The tool changer should be a standalone accesory for normal manual lathe operation as well. In other words. You can add it to any Lathe that is not CNC and use either the app, the buttons or the digital to control it. so I want to use an automatic tool changer on our lathes that are not CNC. I have been working on these Lathes for about 12 years now and even with a quick change it can really sometimes be a pain... It is just to make life a bit easier, and expand my knowledge in doing so. Oh and also make the wife angry... Also I want to be able to incorporate it on other systems as well such as GRBL. I'm a mechanical Engineer, so designing everything is pretty easy but when it comes to the programming side I really lack the skills and I consider myself to be a "noob" as they call it.

I have attached my arduino sketch.

I have read some posts about the timers and see now that the Delay() function is a really bad way to code. I'd say one of my biggest problems are where I move the stepper motor each step at a time and checking when my limit switch was activated. How do I replace those delays with a non-blocking timer? this is the segment of code I'm referring to:

while (!digitalRead(home_switch)==LOW) { // Do this until the switch is activated

    digitalWrite(dirPin, HIGH);
    digitalWrite(stepPin, LOW);
    delay(delayTime);                       
    digitalWrite(stepPin, HIGH);
    delay(delayTime);                       
   
  }

Also I have attached the sketch in txt format if you would like to give some pointers of where to start and incorporate this with your modbus sketch.
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #16 on: March 26, 2022, 09:05:25 AM »
Also I'm using a Mega with a CNC shield. It will not be possible for me to use pins 0 and 1
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #17 on: March 26, 2022, 09:32:22 AM »
So i received this feedback from a user on the arduino forums:

A while loop is blocking by its very nature and a delay() in a while loop magnifies the length of the blocking

The solution is to use an if instead of a while and millis() for timing. No doubt you have seen the BlinkWithoutDelay example which uses this method

So, your code would in essence look like this

if the switch is activated
  if the step period has elapsed as measured using millis
    take a step
    save the time of the step
  end if
end if

A Timer is non-blocking and will also allow other code to run while this one is running which could be catastrophic as the ATC uses a solenoid pin, therefore is there any way to replace the Delay() and make it non-blocking code but not run the rest of the loop? any other method or function?
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #18 on: March 26, 2022, 12:21:22 PM »
You can try the stepper library to drive the stepper motor. With it you can put acceleration, deceleration, Max speed etc on your pulse train.

So i received this feedback from a user on the arduino forums:

A while loop is blocking by its very nature and a delay() in a while loop magnifies the length of the blocking

The solution is to use an if instead of a while and millis() for timing. No doubt you have seen the BlinkWithoutDelay example which uses this method

So, your code would in essence look like this

if the switch is activated
  if the step period has elapsed as measured using millis
    take a step
    save the time of the step
  end if
end if

A Timer is non-blocking and will also allow other code to run while this one is running which could be catastrophic as the ATC uses a solenoid pin, therefore is there any way to replace the Delay() and make it non-blocking code but not run the rest of the loop? any other method or function?
« Last Edit: March 26, 2022, 12:23:52 PM by zafarsalam »
Re: Arduino - Mach 3 - Gcode M6 T0101
« Reply #19 on: March 26, 2022, 01:21:27 PM »
I did try that, after a lot of trial and error I managed to workout a  timer but the code does not work well in the loop as it becomes extremely slow.

The stepper library can not move the motor in the setup where the homing takes place either, so that is not going to work?

I wrote this sketch to test it before I add it to the main sketch and it works well as is:

//Include the Stepper library
#include <AccelStepper.h>

// Time periods between steps in milliseconds
const unsigned long Stepinterval =1;

const int solenoidPin = 53; //This is the solenoid output pin

const int home_switch = 52; // Pin 52 connected to Homing Switch (Sensor)

const byte stepPin = 2;
const byte dirPin = 5;
const byte enablePin = 8;

int StopHoming = 0;

// Variable holding the timer value so far.
unsigned long Steptimer;

AccelStepper x_stepper(AccelStepper::DRIVER, stepPin, dirPin);

void setup ()
  {

    Serial.begin(9600);
    pinMode(enablePin, OUTPUT);
    digitalWrite(enablePin, LOW);
    pinMode(solenoidPin, OUTPUT);
   
  Steptimer = millis ();
 
           

  }  // end of setup

void MoveStepper ()
  {
    digitalWrite(dirPin, HIGH);
   if (digitalRead (stepPin) == HIGH)
      digitalWrite (stepPin, LOW);
   else
      digitalWrite (stepPin, HIGH);

  Steptimer = millis (); 
 
  }  // end of MoveStepper



void loop ()
  {
   
                  if (!digitalRead(home_switch)==LOW){
                      if ( (millis () - Steptimer) >= Stepinterval){
                          MoveStepper ();         
                      }
}

}  // end of loop

But if I add it to my main sketch it's just extremely slow and has to run through the loop the whole time. It seems with controlling the motor and trying to home it there just is no alternative then delay() ?