Hello Guest it is August 07, 2020, 06:22:35 PM

Author Topic: File I/O being called out of order  (Read 3387 times)

0 Members and 1 Guest are viewing this topic.

File I/O being called out of order
« on: April 25, 2014, 02:10:14 PM »
First off I am not using the Mach3 to cut things, I am using it to interact with a device and looking at response from the Mach3 stimuli in real time, so what I am trying to do will sound a bit odd.
I am trying to move the robot and at certain locations to have certain scripts called by the Mach3 and run file commands.  I have attached a screen shot of my current settings.

Below are some subs I will use to try to illustrate my issues.

Sub test1
Dim temp As Double
code "g01 x 10 f 40"
While IsMoving()
sleep(10)
Wend
Shell("notepad.exe")
code "g01 x 0 f 40"
temp = FileLen("c:\temp\test.txt")
Print temp
End Sub


Sub test2
Dim temp As Double
code "g01 x 20 f 40"
Shell("notepad.exe")
code "g01 x 0 f 40"
temp = FileLen("c:\temp\test.txt")
Print temp
End Sub


Sub test3
code "g01 x 20 f 40"
sleep(5000)
code "g01 x 0 f 40"
End Sub

When I run “test2” as soon as the robot starts to move I get notepad opening as well as mach3 opening a dialog box displaying the length of “c:\temp\test.txt”. It just executes all of the non-movement commands as it is executing the movement commands.  I am trying to look at the file length at specific parts of the robots motion, so I need to be able to specify when to read the file length.  In “test1” I am able to prevent the execution of the non-movement commands by using a while loop that exits when the first movement stops, but during the second movement all of the non-movement commands are called.  However I need to perform a movement then run a script, and ten seconds after the first script is finished run a second script, then allow movement once the second script has finished, since there is no movement occurring during the first script I have no way to time between the first and second script correctly or the ability to prevent movement until all scripts are completed.  In “test3” the sleep command has no effect since the first movement takes longer to complete than the sleep time, I have noticed that if sleep commands are put sequentially it does delay for each one separately. 

All of my issues seem to boil down to one question.
How do I enable a “wait until line is finished” type of logic into movement and non-movement commands?

Offline BR549

*
  •  6,932 6,932
    • View Profile
Re: File I/O being called out of order
« Reply #1 on: April 25, 2014, 07:04:37 PM »
First Mach3 does NOT run in real time and it is NOT a great general motion control device. It is a CNC motion controller that runs native in Gcode and that it does fairly well. The Macro language is an add on and is NOT synced well with the Gcode side.

IF you want a general better synced macro then consider using semiphors on each line where precise control is needed. That is the OnyY way I know to control line by line and stay in sync.

For the most part it is a pain in the rear end to do so(;-) and eats up a ton of coding that ends up slowing down the execution of the macro .

Just a thought, (;-) TP

Re: File I/O being called out of order
« Reply #2 on: April 28, 2014, 01:34:27 PM »
I was figuring I would have to do something like that or just make a command line interface to use Gcode to resolve the timing issue.  I have searched these forums a fair bit and have never seen a mention of how the Macro language and Gcode interact, is there any documentation on it?

Offline BR549

*
  •  6,932 6,932
    • View Profile
Re: File I/O being called out of order
« Reply #3 on: April 28, 2014, 01:51:17 PM »
NONE that I am aware of.

(;-0) TP
Re: File I/O being called out of order
« Reply #4 on: April 29, 2014, 10:44:49 PM »

How do I enable a “wait until line is finished” type of logic into movement and non-movement commands?



Can you break your movement and non-movement commands up into different subroutines / functions, then call the subroutines from  a main routine in the order/timing you need?


sub main()
Call Sub1
sleep(10000)
Call Sub2
sleep(200)
End Sub


Sub Sub1()
Dim temp As Double
code "g01 x 10 f 40"
While IsMoving()
sleep(10)
Wend
End Sub

sub Sub2()
Shell("notepad.exe")
code "g01 x 0 f 40"
temp = FileLen("c:\temp\test.txt")
Print temp
End Sub

and so on, breaking your routine into chunks you call in order with time delays as you need.  This is also where you could make use of #expand or runscript to repeat chunks of repetitive code, or just keep calling back to a given sub again and again.



Sorry if this isn't remotely close to what you are trying to accomplish.

Regards,
Eric

Offline stirling

*
  • *
  •  2,188 2,188
  • UK
    • View Profile
    • www.razordance.co.uk
Re: File I/O being called out of order
« Reply #5 on: April 30, 2014, 05:35:43 AM »
All of my issues seem to boil down to one question.
How do I enable a “wait until line is finished” type of logic into movement and non-movement commands?

Scripts occupy their own thread - distinct from and concurrent with Mach. Your SCRIPT lines ARE being executed in the order you wrote them. HOWEVER, The things your script asked Mach and the OS to do don't necessarily get started or finished in that same order. It depends on how busy Mach and the OS are and how long it takes them to do the things your script asked them to do. This is the nature of concurrent systems.

What you're wanting to do is to BLOCK until Mach and the OS signal that the things you've asked to be done have "finished" before moving on to the next line of script.

You already know how to BLOCK for Mach's completion - use isMoving() as you did in sub1.

For OS calls, specifically Shell, the Cypress Basic implementation doesn't support blocking so you'll need to use the WSript version instead).

Use it like this:

Set objShell = CreateObject("WScript.Shell")   
objResult = objShell.Run("notepad.exe", 1, True)

This will BLOCK until you close Notepad.

Cheers

Ian
Re: File I/O being called out of order
« Reply #6 on: April 30, 2014, 06:48:51 PM »
Eric-
I have not tried using different sub routines to try to force it to not progress to the next block unless all parts of the subroutine have finished.  The way you have it written with the specified delays between the subroutines assumes that everything will be completed within that amount of time which may not always be true.

Ian-
I had looked at the OLE automation but didn’t realize that this could be done with it. Your method works like a charm.  Just wish i had found it before i wrote a mach3-python timing scheme.

for what is is worth I might at least share the work around i used in case someone else has an issue that this works for.
Start Mach3 running a while true loop and have it look at a file, and when the file says "ready" do nothing but if it says something else copy the line in and execute it as Gcode.  In parallel have a python script running that looks at the same file and if it says "ready" write the next line of Gcode into the file.  Then just build your script to rely on python’s ability to control timing.

objResult = objShell.Run("notepad.exe", 1, True) BLOCKs
objResult = objShell.Run("notepad.exe", 1, False) Does not BLOCK