Hello Guest it is October 20, 2019, 08:56:07 PM

Author Topic: Adding Socket IO Capabilities to M Codes in Mach4  (Read 3452 times)

0 Members and 1 Guest are viewing this topic.

Adding Socket IO Capabilities to M Codes in Mach4
« on: February 22, 2016, 08:31:18 PM »
Hello Everyone,

I'm a grad student working on a 3D printing research project for my university.  The machine uses a 3D gantry controlled by GCode run from Mach4 in combination with a welder (for additive use) controlled by by a separate Python interface.  The problem I face is coming up with a way to communicate between the GCode, using M commands, and the Python script.  Ideally, we'd be able to call an M command, M23 for example, which would communicate with the welder script that the welder needs to be turned on.  The script would then be able to return a success value of some sort to indicate that the GCode should proceed. I decided that the use of sockets would allow two-way communication and be relatively hassle free.

However, I am not sure how to implement this in Mach4.  If this were normal Lua, I'd use the LuaSocket library, but I feel as though it won't be as simple as that.  I'm now thinking that developing my own plugin would be a better path for me.  Is this feasible to do within the bounds of a plugin? If so, how would I gain access to the SDK?  I assume I'd have to contact ArtSoft somehow and sign an NDA, but would this be through a help ticket or are there other means of contact?

I'd appreciate any advice or questions on the matter.

-Alex

Offline smurph

*
  • *
  •  1,138 1,138
  • "That there... that's an RV."
    • View Profile
Re: Adding Socket IO Capabilities to M Codes in Mach4
« Reply #1 on: February 24, 2016, 12:34:14 AM »
The LUA sockets module is in the Modules directory. 

Setting the package path and cpath is important.  This is an email example, but it is proof of concept for using the LUA sockets module.

Code: [Select]
-- Replace the from and to addresses, as well as the mail server IP address,
-- with something useful.

package.path = package.path .. ";./Modules/?.lua;"
package.cpath = package.cpath .. ";./Modules/?.dll;"

function email()

    -- load the smtp support
    local smtp = require("/socket.smtp")

    -- Connects to server "localhost" and sends a message to users
    -- "fulano@example.com",  "beltrano@example.com",
    -- and "sicrano@example.com".
    -- Note that "fulano" is the primary recipient, "beltrano" receives a
    -- carbon copy and neither of them knows that "sicrano" received a blind
    -- carbon copy of the message.
    from = "<mailfrom@sendingdomain.com>"

    rcpt = {
        "<someuser@somedomain.com>"
    }

    mesgt = {
        headers = {
            to = "Some User <someuser@somedomain.com>",
            subject = "email test"
        },
        body = "This is a message from M300."
    }

    r, e = smtp.send{
        from = from,
        rcpt = rcpt,
        source = smtp.message(mesgt),
        server = "192.168.1.25" -- mail server to use...  defaults to port 25.
    }
    r = r
end   

email()
Re: Adding Socket IO Capabilities to M Codes in Mach4
« Reply #2 on: February 26, 2016, 07:11:40 PM »
Thank you for your response.

I'm still very new to Mach4 and Lua in general. When you say "the LUA sockets module is in the Modules directory" what do you mean by that? To actually get a copy of luasocket I've avoided building it manually by using LuaRocks on my installed version of Lua (seperate from Mach4).  Is this the correct way to go about installing it to begin with?  Which lua files should be in the modules directory?

Offline smurph

*
  • *
  •  1,138 1,138
  • "That there... that's an RV."
    • View Profile
Re: Adding Socket IO Capabilities to M Codes in Mach4
« Reply #3 on: February 27, 2016, 01:17:03 AM »
If you have installed Mach 4, the necessary sockets modules are pre-built and are in the Mach 4 Modules directory.  The Mach 4 LUA environment is completely separate from any other installed environment.  No building or installing is necessary.  However, scripts that used the modules in the Mach4 Modules directory do need to have their package.path and package.cpath set as in the example above.

Steve

Offline Pedio

*
  •  200 200
    • View Profile
Re: Adding Socket IO Capabilities to M Codes in Mach4
« Reply #4 on: February 27, 2016, 08:16:25 AM »
Send us a picture of what you are doing with your machine once you get it running. It sounds COOL! Essentially you are making a welding 3D printer???
Re: Adding Socket IO Capabilities to M Codes in Mach4
« Reply #5 on: March 01, 2016, 11:50:10 AM »
Which 'modules' directory are you referring to? In my install directory the only 'modules ' dir I have is in ' C:\Mach4Hobby\Profiles\Profile_Main\Modules.  All that's contained there is MillModule.lua which doesn't seem to be sockets related.  I'm running Mach 4 Hobby v4.0.1.2472 if that helps.  Would the sockets module come with the new update or does Hobby simply not come with it?

Pedio: Yep :), strapped a MIG welder nozzle onto a 3D gantry controlled by a Hicon Integra.  GCode moves it and M-Codes turn on and off the welder; at least that's the idea.  I'll see if I can get permission from my research advisors to post pictures, but they typically like to keep the finer details under wraps.

Offline smurph

*
  • *
  •  1,138 1,138
  • "That there... that's an RV."
    • View Profile
Re: Adding Socket IO Capabilities to M Codes in Mach4
« Reply #6 on: March 01, 2016, 12:21:28 PM »
Update to the latest.  You are running an extremely OLD version.  The Modules directory will be off of the base installation directory.  C:\Mach4Hobby\Modules.

You may also have to update any motion plugins as well.

Steve
Re: Adding Socket IO Capabilities to M Codes in Mach4
« Reply #7 on: May 24, 2016, 10:32:26 AM »
That fixed the problem. Updating to the latest version includes the LuaSocket library as well as a few others. Thank you for for the advice smurph  :)