Hello Guest it is June 28, 2022, 02:50:58 PM

Author Topic: OPERATING MACH 3 GCODE LINE BY SWITCHES  (Read 10550 times)

0 Members and 1 Guest are viewing this topic.

« on: May 05, 2010, 11:49:36 PM »
Hello, I am cnc controlling a sewing machine for doing embroidery work, Until now, Gcode Works in this way X and Y axis moving hoop , and Z axis controls needle movement.
Z axis works in this way
One revolution = Z=1.00
Small movement from Z=0 , until needle is 5mm aprox above hoop Z= 0.2 (Coming inside fabric)
Small movement from Z= 0.2 until needle is 5mm above hoop Z= 0.8 (Coming outside fabric)
for example
G00 X0.00 Y0.00 Z0.2 (Hoop and needle moving togheter)
G00 Z0.8
M786 (Macro reseting  Z values to cero)
G00X1.0 Y1.3 Z0.4 (From position Z=0.8 to previous position Z=0.2)
G00 Z1.00 (Completing revolution)
In this work Needle movements are in steps and thread is over tensed and in occasions loosed.
Original Embroidery machines, works in different way, because pulley is always in movement and hoop moves are only  in the interval when needle is going outside from fabric, and GCODE line for X and Y movements (hoop) should be concluded before needle arrives to   position previously called Z= 0.2 , (at 3-5mm above fabric). Of course than needle speed movements is in relationship with Z and Y velocity for hoop movements, and is X and Y are slow, then Needle movements or stitches per minute are a small quantity.
What I need to know. I need to move wheel and needle continuously, like write before, in this case needle velocity movement should be like a spindle velocity, and X and Y lines should operate only when needle is out of fabric. I can put a optical sensor in wheel to know when needle is out of fabric and initiate hoop movements in this movement, but I need to know how can I tell MACH, that this is the moment to move one Gcode Line.
In other word, optical sensor, will manage every gcode line for X and Y movements.
Is possible to send these commands to Mach 3?
Thanks in advance and sorry my bad English

Offline poppabear

  • *
  •  2,235 2,235
  • Briceville, TN, USA
    • View Profile
« Reply #1 on: May 06, 2010, 02:26:43 AM »
Sure you can, I would do this, put in your G code a custom user macro, (that is the line above the first line that you actualy start your sewing).
i.e. "M900" in this macro is VB code to do your line by line set up, you will need to look at the VB Mach specific on the wiki. I am not at my computer so you will need to look the OEM codes up for the buttons and LEDs below.
Note you will need to have the "optional stop M1" turned on.


code "M1"
If !GetOEMLED(code for Is in Single step mode) then
DoOEMButton(code for single step mode toggle)
End If

DoOEMButton(Cycle start) 'this will advance the program to your first line.

'/////////////////////////// end of macro ////////////////

From this point, I would see if it would be better to put another custom macro at the end of the XYZ movement line,
verses, putting the Z index watch function in the Macro pump.

you might have to play around with this, but here is a "Generalized idea".

Since watching the Z index input is the key to advance your line, put something like this in your second custom macro, or in your macropump. Assumes your Z index is comming in on INPUT1, also there is a UserLED there as a safety/control LED (see explaination below).

if IsActive(INPUT1) and GetUserLED(2000) then
DoOEMButton(Cycle Start) ' I think that is code 1000
End If

NOTE: you could also put some kind of safety code in there that lets say looks for and ANDs in a UserLED that if that LED is not lit, then that means you are not wanting to "Auto Run" the single step Gcode file, (i.e. in case your messing with your machine, and you accidently trigger the Z index the machine may then move on you).
You could Make an "Init" Macro script also, that when Mach starts up, it will make sure that your Control LED is NOT lit.
i.e. lets say your control led is User LED 2000.

'Init Macro M902.m1s
If GetUserLED(2000) Then
SetUserLED(2000, 0)
End If

You can also put a custom button that is Next to your control Led that will toggle that LED on or off on your Program Run screen set.

Here is the way I see the above sequence working.

you load your Gcode and hit cycle start like normal.

You will Make sure you have pushed your screen button that Enables the Auto Single Step function!!!
if it is NOT on then the below will not work.

it goes to the first macro, which optional stops all motion, then it sets the machine to Single step, and auto hits
the cycle start, which advances it to the first Motion line.

at the end of that line, (or optionally from the Macro pump), there is another macro, it is looking for the Z Index
to go hot, that represents you Z needle has finished its cycle, Also it will varify that the Control LED is ON.
it will then auto push the Cycle start button again, and since you are in Single step mode, it will advance to the next
line, where it will run the XY, the Z will cycle, and at the end of that line (or macro pump), it will wait for the Z index when it hits again, the next line will execute, and so on.

So basically, it will single step through your gcode driven by the Z index Input.

I would also recommend, putting a Finished Macro right on the line prior to your M30 (on a line by itself)


in that macro I would put some code to return Mach state to normal.


DoOEMButton(Single Step) 'takes mach out of single step mode to normal
SetUserLED(2000,0) 'Turns OFF your control LED.

'have fun

« Last Edit: May 06, 2010, 02:38:21 AM by poppabear »
fun times
« Reply #2 on: May 06, 2010, 09:58:55 AM »

Thanks a lot for your answer, I will traduce and read slowly, also I will send a copy to Khalid than is a embroidery software programmer based in GCODE.
We are working a lot in this project, but this new improvement is useful to get more speed in job
Link is



« Reply #3 on: May 06, 2010, 03:36:09 PM »
Hello, Scott
Maybe I din’t explain me well.  Sure than we need some macros for do it all job,  programming is not my strong part.
But forgot to explain some things
Actually I am using X, y and Z axis, but  our idea is  CNC control of  X and Y axis  and for wheel than moves needle, use an AC or DC motor, than turns continuously.
Then locate a disc and optical sensor in wheel, having two slots, one for initial position (needle up) and other for hoop movement beginning of movement (locate in position where needle is coming outside fabric at 2-3mm height.
First slot is used when I reference axis to cero position (hoop is in 0, 0 position) centered with needle, and needle is in upper position.
Second slot via optical sensor send a signal to controller, when needle is coming out of fabric, and X and Y axis, move a hoop one line to next embroider work position and stopped. In Mach is an button (Single BLK), than permits to operate line by line, if I can push this sensor  with a optical sensor signal, then we can control than hoop movement is only when needle is out of fabric.
I don’t know how connect this sensors to controller, and how get that signals from sensor, will be converted in commands to Mach3, to move x and Y axis, line by line.
One time that we can get this, must be makes some trial and error, in needle motor velocity, accord X and Y max velocity and considering a max travel, to guarantee than X and Y axis line movement finish before, needle is coming inside fabric again. This trial gives us real Stitches per minute of this embroidery machine, than is direct relationship with max hoop velocity.
Some example calculus.
If X and Y max velocity is 3600 mm/min, then means is 60mm/second.
If selected max embroidery distance between stitches is 5 mm, then we need 0.083 seconds (83 milliseconds) for max travel at this speed.
Needle movement works in this way.
 50% of travel is above sewing/embroidery machine table and 50% is under table, we should consider some security height (like milling), of 5% before coming inside fabric, and when is coming outside fabric, theoretically will be 60% of needle movement is in security zone (where is not possible hoop movement), and 40% of difference time is where we can move hoop, because needle is out of fabric.
Then if 40% of time needed for one needle movement is when we can move hoop, these 83 milliseconds than we need to move hoop in max travel is 40% of time to make one wheel rotation.
If wheel motor can rotate at 300 RPM, we can get this easily because, If motor RPM= 300, then is 5 revolutions x second = 1 revolution in 200milliseconds. If hoop movement  for 5mm is in 83 milliseconds , and 1revolution is in 200 milliseconds, then 40% of this time is than we need to move hoop, 200*40% = 80 milliseconds.  In any superior speed, works can do it easily. If hoop movements speed is greater, we can increase RPM’S and vice versa.
For jump movements (when hoop travel a long distance moving from first embroider object to next in sequence, we can  selected a max travel too without stitches (for example 15mm), and reduce at half motor velocity, this complicate a lot project, because I think than we will  need a PWM, to control motor speed in G CODE.
Thanks in advance for your attention and answer.

Offline poppabear

  • *
  •  2,235 2,235
  • Briceville, TN, USA
    • View Profile
« Reply #4 on: May 07, 2010, 01:53:09 AM »
Ok, your going to have to slow down your motor RPM and speed of distance/time from 0.83ms to a longer time of at least 100ms or mabey a little more. Machs "Current" update loop is 10hrz, (100ms), and in reality runs around 100-120ms (at least on my computer). So ever 1/10th of a second, mach can act on the information, that is in your macros.

the stuff I posted previously, address what you want, but again, until Rev4 comes out which is suppossed to have a much faster overall loop time (current extimates around 50hrtz). Your going to have to slow down your machine. If your using "Opto's" for you index slots of your needle, you should make sure that thier trigger and recovery time is fast enough for what you want as well.

fun times
« Reply #5 on: May 07, 2010, 08:40:11 AM »
thanks scott
« Reply #6 on: June 01, 2010, 02:48:33 PM »
Iam trying to set a CNC embroidery machine,  I figure that  I have a G CODE like this, first line will be activated in manual Cycle Start, and consecutively lines will be  activated using  Single Block mode  activating  Cycle Start for next lines  using  opt switches  and slotted disks located in a sewing machine shaft.  

M900.m1s-------Macro setting Single Block Mode

M3 S500---------Start  CW spindle rotation at 500RPM----Disk 1 –Small 1x2 mm slot. (NC CIRCUIT) Spindle speed controls needle movement and is in relationship with long of next stitch, is calculated in CAM program.

G00 X1 Y1 ------For X and Y movements—Disk 2—Slot 120 degree, and begin of slot rotated 285 degree from Disk 1.  

M7---------------For activate a solenoid if next stitch is longer 7mm (Sqr (X2+Y2), slot 60 degree   activate opt switch, slot begin when slot is in front it, is rotated 270 degree from Disk 1 slot. (NO circuit) This is for JUMP STITCHES where we must release thread tension to feed more thread than take up thread lever can do.

M9--------------For deactivate solenoid when 60 degree slot is finished  pass in front to opt switch, exactly at 330 degree from Disk 1 slot. (NC Circuit)
(This M7 and M9 command are present, when predetermined long stitches are, if not this lines are empty, CAM program will set this line or not)

M3 S455---------Change spindle speed to 455RPM.--- Activated by Disk 1

G00 X3 Y3-------For X and Y movements--Slot disk 2 .

Consecutively to the end of code.

This opto switches, will be connected at inputs in controller, and sated like OEM Trigger using code for Cycle Start (43010?).
More and lees operation will be in this way

G Code Lines         Initial slot Angle              Final Slot angle                     Interval
M3 S500                      1                                  359                              385
G00 X and Y              285                             360 +25= 405                   120            
M7                               0                                   45                                45

My questions are:

1-Could be work this G code and system?

2-Opto switches always will be activated, before that precedent g code line finish, Could interrupt new g code line, precedent g code line? In other words.

First LINE---Macro sets Single Block MODE

Second line –Spindle starts rotate and desired speed

Third line- At 285 degree or rotation, optic switch “push” Cycle Start and begins G00 X, Y movements.

In this moment Spindle Stops or continue rotation together X and Y movement?  

Sorry my bad English

hanks for your help and answer.

« Reply #7 on: June 02, 2010, 11:13:56 PM »

English is not may native idiom, many times is hard to express ideas specially this, where I am not a specialist.

I post here a non conclude excel table, where I figure how  generate embroidery G code and how works, needle speed motor always be variable because must be in accord to hoop speed and travel distance.

For begins work I think to use a stepper for needle like a spindle and control it using step and dir motor. How we don't need highest speeds I think that a stepper is enough for 500-550 RPMs and is possible speed drive by Mach and CNC controller. If I will  use a AC/DC motor, then I must find a PWM card for speed controll and  I don't have it right now. I think go step by step and don't spend money unnecessarily.

I must test this system, but I think than can be usable.
Thanks for all help than can give me.

Sorry my bad english


« Last Edit: June 02, 2010, 11:18:00 PM by renenpaz »