Hello Guest it is March 28, 2024, 04:50:13 PM

Author Topic: Using VB script to wait for an external event and then execute G code  (Read 30811 times)

0 Members and 1 Guest are viewing this topic.

I am using a Sherline mill as an X-Y table with the goal of automating a sampling machine in my laboratory.  I have the mill set up and it seems to function fine under Mach 3.  The laboratory sampling machine (spectrometer) is able to take a data point at specific time intervals.  I would like to move the samples on the X-Y table using mach 3 so that a new sample is moved into position under the spectrometer after a data file is written.  The spectrometer is set up to sample for 1 minute, wait 20 seconds, sample for 1 minute, wait 20 seconds, etc.

I would like to write a VB script to be the interface between Mach 3 and the spectrometer file writing events.  I was thinking that I could run a VB script in Mach 3 that would watch the directory where the spectrometer stores its files.  Every time a new file appears I would like this event to result in the execution of some G-code that moves the x-y table to the next sample. 

Can anyone here tell me from the start if this is reasonable or if there is some reason why this could never work?

I am a novice with both VB script and mach 3 but since what I'm trying to do is really very simple I am optimistic about my chances of making it happen.

I'll put a tad more detail in here for those that are interested.  There are 96 samples on a 8 X 12 grid with exactly 9 mm between sample centers.  The movement would simply be to step through this grid in a systematic fashion.   Mach3 would move to the first spot and then wait until a file appears in the directory of interest.  These files will be numbered from 1 to 96 in case that helps (number at the end of a string in the file name - name_#.spc).  When a new file or the next numbered file appears then mach 3 will see it and then simply move the table to the next sample in the sequence.  Then it will wait for the next file to be written, etc. 

Any basic suggestions on how to accomplish this most efficiently would be greatly appreciated.  This is the only task that Mach 3 and the mill will do.
Re: Using VB script to wait for an external event and then execute G code
« Reply #1 on: September 07, 2008, 12:12:50 AM »
For example, can I use the following type of script:

Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE " _
        & "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
            & "TargetInstance.GroupComponent= " _
                & "'Win32_Directory.Name=""c:\\\\automation""'")

Do
    Set objLatestEvent = colMonitoredEvents.NextEvent
   
REM  Then execute appropriate movement of stage and then loop.
Re: Using VB script to wait for an external event and then execute G code
« Reply #2 on: September 07, 2008, 09:52:21 AM »
I am concerned that I will not be able to run the script I previously posted because mach3 may not understand that kind of command.  If that is the case, is there a way to run a native VB script and still be able to send a move/G code command to Mach3 from outside the program?

The following is a description of the sequence that I want to script.  Sorry, I'm not a programmer myself so I don't know the proper syntax, but what I'm trying to do is rather simple so I just wrote it out in text so you can get the general idea.  Any comments or suggestions as to how to create this would be appreciated.

Sample layout is a square grid of 96 points with 8 rows on the X axis and 12 columns on the Y axis.  Points are 9.00 mm apart.

Start loop1 with  Y=0
Start loop2 with X=0
Wait for a new file to appear in directory, then move to next line
Gcode command: Move to point (X+9, Y)
If X=63 then goto next line, else goto loop2 repeat point above
If Y=99 then end
Let Y=Y+9, goto loop1 repeat point above
Re: Using VB script to wait for an external event and then execute G code
« Reply #3 on: September 08, 2008, 02:38:10 PM »
Yet another follow up. ;D

I have determined that my spectrometer program has an option to "allow data to be sent via DDE to a spectrum analysis application".  However, I believe it is only set up to work with one particular program.  Still, this may again provide some kind of handle. 
« Last Edit: September 08, 2008, 03:18:12 PM by Octathorpe »

Offline zealous

*
  •  489 489
  • HI!
    • View Profile
    • Artsoft Solutions
Re: Using VB script to wait for an external event and then execute G code
« Reply #4 on: September 08, 2008, 10:31:01 PM »
I think there might be a better ways then looking at the number of files in a directory....

can you trigger an external switch with your machine?

You can also use Flash with Zinc to access full functioning VB!

If you want to look at the amount of files in a directory use:

Code: [Select]
'Get File count
GetFileCount = objFSO.GetFolder(folderpath).Files.Count

You could also look at the file name and have set functions per file name as well.


Here is some quick code to do what you want I think  ::)  ...at least its a good starting point to get what you want perhaps.

I have not tested it on a machine... and barely debugged it  :-\

I'm thinking your machine is staring from the bottom left corner and working to the right 8 steps.....then when reaching 8 steps... up 1 row.... and back over to  step 1 then over 8 steps etc...

1. Copy past the code below in to a text File.
2. Rename the text File to "Macropump.m1s"
3. Drop "Macropump.m1s" in to your profile Example "C:\Mach3\macros\Mach3Mill"
4. Add a button to toggle LED2000 on/off to activate the script

You can also add DRO2000 to see how may files are in there.


Code: [Select]
'Ignore If loading a file
ProgLoading = IsLoading()

'***Make a Button to turn on/OFF LED2000 (This could be your run button)***

'Ignore If loading a file and if LED2000 is off
If(ProgLoading = 0 And GetUserLEd(2000)=1) Then


'***Create file system object***

'File Sytem Object
Dim objFSO As Object

'Set VB System object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Folder path
folderpath= "C:\automation"


'Check to see if the folder is there
If objFSO.FolderExists(folderpath) Then


'Get File count
GetFileCount = objFSO.GetFolder(folderpath).Files.Count


'No files
If GetFileCount = 0 Then

'***Set Default Values***

'Clear interLock
 Call SetUserDRO(2000,"0")

Print "File Count Zero"
'Send Machine to Zero's !!! You might want to set a LED before doing this and send the axis to zero :)
'Code("G1 X0 Y0")



'***File Number return larger then 0 see how many***
Else

'If new file added Store in a DRO InterLock
If Not GetOEMDRO(2000) = StoredFileCount Then

Print("Stored Value " & GetOEMDRO(2000))

'Store new value
StoredFileCount = GetFileCount

'Store interLock User DRO 2000
SetOEMDRO(2000,StoredFileCount)

'***Depending on File count will depend will trigger one of the Case statments***

'***Check Row Count***
Select Case  GetFileCount
   
    Case 8   
   
    'Move Y up + 9mm
MoveY = getoemdro(801) + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0")

Print "Row "& GetFileCount
                 
     Case 16
     
     'Move Y up + 9mm
MoveY = GetOemDRO800 + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0") 

Print "Row "& GetFileCount
       
      Case 24 
     
      'Move Y up + 9mm
MoveY = GetOemDRO800 + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0")

Print "Row "& GetFileCount
     
       Case 32 
       
       'Move Y up + 9mm
MoveY = GetOemDRO800 + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0")

Print "Row "& GetFileCount
       
        Case 40
       
        'Move Y up + 9mm
MoveY = GetOemDRO800 + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0") 

Print "Row "& GetFileCount
       
         Case 48
         
         'Move Y up + 9mm
MoveY = GetOemDRO800 + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0")

Print "Row "& GetFileCount
         
          Case 56
           
           
'Move Y up + 9mm
MoveY = GetOemDRO800 + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0")

Print "Row "& GetFileCount
           Case 64
           
'Move Y up + 9mm
MoveY = GetOemDRO800 + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0")
           
Print "Row "& GetFileCount

            Case 72
           
'Move Y up + 9mm
MoveY = GetOemDRO800 + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0")

Print "Row "& GetFileCount
           
             Case 80
             
'Move Y up + 9mm
MoveY = GetOemDRO800 + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0")

Print "Row "& GetFileCount
             
              Case 88
             
'Move Y up + 9mm
MoveY = GetOemDRO800 + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0")
             
Print "Row "& GetFileCount

               Case 96
               
'Move Y up + 9mm
MoveY = GetOemDRO800 + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0")

Print "Row "& GetFileCount


'End Case Statment
     Case Else   
   
   End Select
   
'END If new file added Store in a DRO InterLock
End If
'END No files
End If
   
Else

'No folder exist
MsgBox "Bad File Path"

'END Folder exists
End If

'END Ignore If loading a file
End If

'Clean up
Set objFSO = Nothing
« Last Edit: September 09, 2008, 04:16:30 PM by zealous »
Re: Using VB script to wait for an external event and then execute G code
« Reply #5 on: September 09, 2008, 07:48:08 AM »
Fantastic! 

I will try to test that code today and let you know what happens.  If I can at least get it to move in response to a single file creation event then all of the movement can be worked out.

When I paste the code into notepad, the question marks appear as little box.  Is that what is expected?

You asked if I could generate an external trigger using the machine.  There is an option to use an LED on the instrument that goes on and off as an analog trigger for mach3.  I have been considering using a phototransistor to trigger a high (5V) setting to one of the parallel port input pins when the LED is on (LED on means collecting  data, LED turns off means that file is done).  This is essentially the same information that can be obtained with the filecount variable you used in the script.  I would like to try the VB filecount route first, but if there is some significant reason why it would be best/easiest to do this whole process using the LED and a 5V input trigger then let me know and I will get that little circuit put together and get a 5V trigger coming into the parallel port.  I have the parts to build the optical sensor, but I have not made it yet - I assume it _has_ to be relatively easy to build such a sensor (I have a few schematics) and get it working, but then again, my first award after joining the department was "most likely to electrocute himself" so I can't guarantee the success of my sensor building abilities  ;)

Meanwhile I will test the script - which I very much appreciate.  I will comment out all of the case statements for now and just see if I can get a response from a filewrite event in that directory.

Offline zealous

*
  •  489 489
  • HI!
    • View Profile
    • Artsoft Solutions
Re: Using VB script to wait for an external event and then execute G code
« Reply #6 on: September 09, 2008, 04:11:15 PM »
Hey,

Here is all you need to get the axis moving depending on number of files:

Open Mach Script editor and add this code in and hit run:

The "IF" statement is looking to see if there are 7 files "GetFileCount = 7"
Code: [Select]
'***Create file system object***

'File Sytem Object
Dim objFSO As Object

'Set VB System object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Folder path
folderpath= "C:\automation"

'Get File count
GetFileCount = objFSO.GetFolder(folderpath).Files.Count

'Message number of files
Print "There are " & GetFileCount &" Files"

'How many to check for
If GetFileCount = 7 Then

Print "If statment True"

'Get current position Move Y up + 9mm
MoveY = getoemdro(801) + 9

'Move Y
Code ("G1 Y" & MoveY)

'Move X
Code ("G1 X0")

End If


'Clean up
Set objFSO = Nothing


It would be better to look at the UserLed and when Not active run the above Macro. That way you don't need to run the macro pump

You might have another button to exit the function in case of an emergency.

You can do it all in a "Brain" would be the best.

Thanks

« Last Edit: September 09, 2008, 04:28:37 PM by zealous »

Offline zealous

*
  •  489 489
  • HI!
    • View Profile
    • Artsoft Solutions
Re: Using VB script to wait for an external event and then execute G code
« Reply #7 on: September 09, 2008, 04:22:01 PM »
hey Octathorpe,

I made a typo in the two codes above but corrected them...I had: "getoemdro801" ....rather then "getoemdro(801)"

There shouldn't be any question marks....perhaps it is an unrecognized font types? What part of the country are you from...this might have something to do with it.
Re: Using VB script to wait for an external event and then execute G code
« Reply #8 on: September 09, 2008, 06:21:55 PM »
It's Alive!!

...well it's pretty exciting for me, anyways.

I was able to paste your code into the script editor and it reads the number of files correctly in the target directory and then moves the machine.  So that is good news - at least that file system function for reading the number of files works and should function as a trigger. 

btw, I'm not sure what the "?" are in my view of the code, could be a font issue - I'm in Atlanta.  I'll keep working with the screen editor (can't get it to work for me yet).  I also have to set up some other variables - it's currently moving 11.5 mm instead of 9 mm per move.  I'll report when I have more info.  Thanks very much for your effort, it's nice to see the critical part functioning!
Re: Using VB script to wait for an external event and then execute G code
« Reply #9 on: September 09, 2008, 10:19:14 PM »
I've managed to get the motors tuned correctly and moving the correct distance.  I've got a button working with VB code attached to it.  I couldn't get screen designer 4 working, but I got it to work in screen designer verision 3.  I'll need to figure out more about how to turn LEDs on and off and get something going to be able to stop the program if something goes wrong.

I've attached an image of what the sample plate looks like as well as some example coordinates.   I am planning to move from the top left in the image (0,0) to the right across the 12 columns in the X direction to (99,0) in multiples of 9 mm and then come back to (0,9) and move across the 12 points to (99,9), etc.   

I am going to do some more testing to get more familiar with the process of working within Mach with VB.  I'm still getting used to the debugging process and how to complete if, then, else logic.  It's not something I have done much of.   Next I'll deal with the logic of making it work through the array in some logical fashion.  I'm sure there are a thousand different ways to accomplish that process.

It's all moving along quite well....
« Last Edit: September 10, 2008, 07:28:06 AM by Octathorpe »