Machsupport Forum

Third party software and hardware support forums. => Modbus => Topic started by: zafarsalam on December 05, 2011, 11:33:54 PM

Title: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on December 05, 2011, 11:33:54 PM
Hi,

I've been successful in communicating with an Arduino Uno though modbus. Analog input, output, LED's, buttons and all. Why Arduino? Instead of Pokeys, ModIO or Cubloc? Because it's easily available where I am.

Today I tried MPG with Arduino and found it smoother than Pokeys. I am using ModIOMPG brain for the MPG. The only problem is that the axis keeps on rolling for almost half a second when I stop rotating the MPG. I am not using MPG tick count in the brain. Not sure how to calculate it in my Arduino. Is this the reason for this behaviour of MPG or do I need to tweak some other settings in Mach. I've tried different baud rates but they don't make any difference.

Another thing I've noticed is that when I switch to single or multi-step mode from velocity mode and then back to the velocity then the axis jog speed slows down to one fourth of the previous one. When I restart Mach, I can jog at full speed once again. I am running it on Mach V3.042.020.

Zafar
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: crob09 on April 08, 2012, 07:18:53 PM
I am trying to do the same thing, have you looked at the Pokeys software? Let me know if you find anything..
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on April 08, 2012, 09:58:40 PM
Yes. I've tried pokeys too. But not the latest plugin. Too many toys, too little time...

Zafar
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: ivicam on November 17, 2012, 03:34:05 PM
can everyone post some tutorial how to program inputs for FRO and spindle override in brain and modbus seting for arduino modbus.thanks
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on November 20, 2012, 01:14:39 AM
can everyone post some tutorial how to program inputs for FRO and spindle override in brain and modbus seting for arduino modbus.thanks

http://www.machsupport.com/forum/index.php/topic,22982.0.html
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: mrprecise44 on November 21, 2012, 04:34:32 AM
Hello Zafar:

Is there any chance you could post the Arduino sketch in a .zip format. I  don't like the .rar type as the programs that upzip it want to  install other stuff on my computer.
I am a sometime user of Arduino, but have never interfaced it with Mach3. My current I/O for my mill is Cubloc.

Regards,
John
 
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on November 21, 2012, 04:49:15 AM
I use winrar for the rar files. It's a nice little harmless utility.
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: mrprecise44 on November 25, 2012, 06:26:28 PM
Hello Zafar:

I got the modbus program into the Arduino sketch file finally. Looking it over, it uses a higher level language, C++ perhaps, than the standard Arduino, and is definitely not user friendly to decipher. However, after some effort, I got it to recognize digital input pins, i.e., D2, D3, and compile. What threw me off was the program setup section has analog input pins listed as the letter "t". ??

Also, I did a search on the Arduino site, and located the list of Modbus functions used, and their meaning. In the Arduino sketch, they list the functions like 0x01; 0x03; but do not use the comment feature to explain what they do.
-----------------------

0x01 - Read Coils
0x02 - Read discrete Inputs
0x05 - Write Single Coil
0x0F - Write Multiple Coils

0x03 - Read Holding Registers
0x04 - Read Input Registers
0x06 - Write Single Register
0x10 - Write Multiple Registers
0x16 - Mask Write Register
0x17 - Read Write Multiple Registers

--------------------
Anyhow, I will try it on a Mega, and see if the digital inputs higher than 15 will work.
Thanks for your effort. The zip file was appreciated.

I did find an un-zipper for RAR on the SourceForge site. All the winRAR files I located by Google were 40 to 50 megabytes, and were a pain to try to use. The SourceForge program was very small, and downloaded in a few seconds.

If the program will run the Mega pins above 15,  you could have lots of digital I/O available.

Regards,
John
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on November 25, 2012, 09:50:23 PM
John,

See the developer's notes at the website in the initial comments section of the sketch. Not all the modbus functions are implemented in this one. For digital inputs and other functions of Arduino, I'd recommend to go through the example codes of Arduino software and the tutorial section of the Arduino website.

Zafar
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: mrprecise44 on November 26, 2012, 01:56:27 PM
Zafar:

I have not tried the actual hardware yet, but what I plan to do is copy some working code from one of my working Arduino programs, like a digital pin I/O actuating an LED, into the "void/loop" and see if it works. If I get one to work, changing the pin numbers and re-compiling for higher numbers will show what is or is not working.

John
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: dotn on February 08, 2013, 05:33:24 PM
Zafar:
Were you able to properly configure the MPG?.
As you connected? .
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on February 08, 2013, 10:44:28 PM
Zafar:
Were you able to properly configure the MPG?.
As you connected? .

MPG works with Arduino in Mach3 but there's a delay of about 0.5 seconds in it's response. Couldn't figure out to fix that. Haven't touched it for quite some time. Maybe I'll give it another shot in a few days.

Zafar
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: mrprecise44 on February 09, 2013, 02:21:49 PM
Hello Zafar:

I have not yet connected an MPG through the ModBus, but have all the I/O pins on the Arduino Mega, pin #2 through #53 digital, and all of the Analog inputs tested, working just fine.

However, as I understand the ModBus functionality in Mach3, in the Serial ModBus Setup screen, there is a column for the millisecond pause for each Cfg.#, and that is a time period the scan will pause at each register waiting for an input. and another millisecond time in the upper right corner, for the "timeout" period. This seems to me, that there cannot be instantaneous response to an input in ModBus, due to the accumulated milliseconds during a scan. The Cfg# time period can be as small as 25 or 50, but it still adds up to some given period of time. I believe this is the cause of the delay in MPG response you mention.

Since the Arduino Mega has so many I/O pins, but the common parallel port has limited inputs, I think the MPG should be wired directly to the Breakout board. The BB itself can be 12 outputs, and 5 inputs, or vice versa. There are only a few kinds of inputs that need to be instant, i.e., the probe signal, the MPG, and perhaps an optical home switch, and an optical sensor spindle index.

On one of my Cubloc plc, there are the standard I/O, and then there are a couple of "High Speed" I/O pins, which are for an MPG.

I am not claiming to be a ModBus expert, and might have some some points wrong, but this seems a reasonable understanding. There is not a lot of info in the inner workings of the Serial ModBus Setup. Also the baud rate might have a different effect, say from 9600 bps, to higher settings. ModBus works perfectly fine for I/O that are not super time critical, like limit switches, spindle on/off
LED's, FRO, etc.


John

You want your limit switches to be fast as well.

Edited...... Brett
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on February 10, 2013, 12:50:17 PM
John,

I had tried differnt timeout values and baud rates etc. with my modbus MPG, but they had no effect on it's response. Maybe there was some difference and I couldn't notice it. The lag only becomes noticeable when you try it on the real machine. On your desktop PC with no hardware attached to Mach you can't catch it.

I am using MPG hooked up with PP or Smooth Stepper now. I'll give it a try once again with Arduino when I return back from my out of town visit.

Zafar
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on February 10, 2013, 12:53:09 PM
John,

Have you tried LCD output with Arduino and modbus. I did it once to display axes DRO's on a 4x24 LCD module. Have to give it another try too some time.

Zafar
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: mrprecise44 on February 10, 2013, 02:25:19 PM
Hello Zafar:

I have not yet done anything with the LCD/serial display output on the Mega, connected with Mach3.

That is next; however have done a lot of Arduino stuff, using the LCD. One project was a Marine Autopilot, displaying compass bearings with a Compass module, and also putting out GPS coordinates, by parsing the Mag compass outputs, and the GPS output stream. That project used a serial backpack module with a 20X4 LCD, combined with a power converter board from Polulu, which can take a pin signal, and drive a 300+ watt DC motor, which in turn moved the ships wheel with hydraulic steering. Worked just like the commercial units.

Want display the jog status, current step value, jog %  setting, etc., since these are not so easily seen on the Mach screen.

Just takes time to sit down and work with it. Of course, there are bits and pieces scattered about on the Forum how to do lots of obscure tricks.


John
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: poppabear on February 11, 2013, 08:09:15 AM
Hey just thought I would add my two cents here.....

Modbus: 
The time values on the config is how often you want it to poll that register group.
For MPG to work reasonably well, your hardware would need to buffer the counts,
and to get some kind of "smooth" action you would need to send in a "Tick Count"
to Mach3 as well.

Delay: since BOTH Modbus, and a motion plugin, all run at mach3's update loop, 1/10 per second your
not going to get "real time" MPG control......  see the thing about the count buffers in the hardware
above.

Scott
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on February 11, 2013, 11:48:00 AM
Hey just thought I would add my two cents here.....

Modbus: 
The time values on the config is how often you want it to poll that register group.
For MPG to work reasonably well, your hardware would need to buffer the counts,
and to get some kind of "smooth" action you would need to send in a "Tick Count"
to Mach3 as well.

Delay: since BOTH Modbus, and a motion plugin, all run at mach3's update loop, 1/10 per second your
not going to get "real time" MPG control......  see the thing about the count buffers in the hardware
above.

Scott


Scott,

That's what I wanted to ask. How do I calculate the tick count. Maybe that is what's causing the delay. The delay I get is definitely much bigger than 100 ms, the refresh cycle duration of brains.

Zafar
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: poppabear on February 11, 2013, 08:31:29 PM
Zafar,

   I have a cheezy, Arduino here, and there is a "Timer" style kinda function you can call.
The resolution of that time would need to be in the ms granularity.
In any case, the "Tick Counter" in the arduino would need to at the minimum be MUCH faster,
than the 10hrz update loop.

If it where me, I would then send that Tick Value across the modbus in the standard 16 bit register,
I would then map that register address (in the modbus config in mach), to the "Tick Count" termination object.

NOTE: ( and I suspect you already know this ), You will need to set your MPG count type to "Multi-step", and set your
counts per pulse to 4 in the Ports/Pins. (assuming a 100ppr encoder). The "Single Step" choice will usually (boarding on almost always), loose steps.

Also, given the update fx of the ardino, I would make sure my count buffer is large enough, to sum the total counts of how ever many scans,
it is before it can be read and cleared by mach3. I suspect you already have a mechanism for this, but if not, I would set an modbus output hot,
as it comes around, and I would set a clearing bit, that would control the clearing of the count register.

Tick count, make sure you put "Roll Over" code in the 16bit register that holds your count on the arduino side.

Mach will use both your count register, AND your tick count to not only see how far you have moved, but how fast since last update loop.
again, DON'T expect ANY SUPER results even with the above, you can OUTRUN your Machs ability to keep up with your MPG if you turn it fast.
the 10hrz lag, is a BUZZ KILL no doubt.

Scott

Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on February 11, 2013, 08:39:50 PM
Thanks a lot Scott for the explanation. I am out of town right now. Will give it a try when I get back home and let you know if I get any better results.
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: poppabear on February 15, 2013, 08:01:25 AM
so, any word, did you get it working?
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on February 15, 2013, 09:51:09 AM
so, any word, did you get it working?

Haven't tried it yet. Maybe next week. Another out of town visit is on schedule.
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: dotn on February 25, 2013, 01:20:14 PM
Hi guys
I think the problem with arduino mpg is the same as when connected to some BOB, the debouncing, Mach3 has to adjust it but does not properly with the mpg, this requires putting buffer and / or convert step and direction signals (see attached)
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: PhuocLieu on March 16, 2013, 10:47:59 AM
Zafar

I am a newbie and i am working with your source.

I have a problem. I want to set pin default state is low but I don't know how (Sorry, I'm just beginer with code).
For example, when I connect pin 2 to gnd, the rx is "01 04 02 DF FF" but i need rx is "01 04 02 20 00"
How can I config it ? Please help me!

Phuoc
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: mrprecise44 on March 16, 2013, 04:29:03 PM
(1) Go to the Arduino website.
(2) Study the examples in the "Learning section."
(3) Learn operation procedure and functions for I/O with an Arduino board.
(4) Write some simple sketches to verify the many kinds of functions in the Arduion IDE.
(5) Test a basic I/O on the ModBus sketch, such as creating an input from a pushbutton, and a brain to cause a Mach3 action, or copy the example posted by Zafar using a potentiometer.

Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on September 18, 2013, 08:09:57 AM
Zafar,

   I have a cheezy, Arduino here, and there is a "Timer" style kinda function you can call.
The resolution of that time would need to be in the ms granularity.
In any case, the "Tick Counter" in the arduino would need to at the minimum be MUCH faster,
than the 10hrz update loop.

If it where me, I would then send that Tick Value across the modbus in the standard 16 bit register,
I would then map that register address (in the modbus config in mach), to the "Tick Count" termination object.

Scott


Scott,

I've programmed the TickCount function in Arduino too. I am counting the pulses produced during the last 50 ms, multiply it by 20 to get the pulses per second and then pass it over to Mach through modbus. But still get the same result :(. Almost 1 second's delay between MPG movement and machine axes response. The same thing I got when I was not using the MPG TickCount in Mach brains.

Zafar
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: mrich0908 on September 06, 2014, 11:34:32 PM
Hello Zafar:

I have not yet connected an MPG through the ModBus, but have all the I/O pins on the Arduino Mega, pin #2 through #53 digital, and all of the Analog inputs tested, working just fine.

mrprecise44,
Care to share your sketch of the Mega ? Im new to the arduino and this would help me out tremendously with my mega.
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: zafarsalam on September 07, 2014, 08:35:54 AM
Care to share your sketch of the Mega ? Im new to the arduino and this would help me out tremendously with my mega.

I'll be busy all this week. Maybe this weekend, I'll fish out the arduino code and post it here.

Zafar
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: SolverGuy on January 18, 2017, 05:49:47 PM
Maybe I solved the problem:
I had a delay of something about 0.5 and 1.5 seconds and tried everything, till i found a "Serial.print("*********");" in my code... After deleting it, everything worked out perfect! I hope I can help someone with that in the future!

Cheers!
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: florin2901 on March 11, 2019, 03:41:14 PM
Hi there,

I know that this topic is old but can you manage to make Mach3 to "talk" with Arduino?
If yes.... How manage to send out to Arduino the signals for moving axes? (xstep, xdir.....)
And how can be adresed bit's upper than 15?

In one word.... Can you share with us what you done?

Thank's in advance from Romania.
Title: Re: Mach3, Arduino, Modbus and MPG
Post by: ZASto on March 11, 2019, 04:18:57 PM
You can not send step/dir from Mach to Arduino through MODBUS. It is to sloooow.
If you want to use arduino as a motion controller (GRBL) with Mach, you will have to write a plugin. There is no plugin for GRBL.