Machsupport Forum

Mach Discussion => General Mach Discussion => Topic started by: GytarrMann19 on November 08, 2012, 09:52:00 PM

Title: Help with VB script
Post by: GytarrMann19 on November 08, 2012, 09:52:00 PM
Need some help with VB script for my lathe turret macro. What I currently have is:

If GetSelectedTool = 1 Then
 ActivateSignal(OutPut7)
 Sleep 500
 Code "G53 G0 A0"
 While IsMoving()
 Wend
 DeActivateSignal(OutPut7)
 End If

Output 7 activates a solenoid valve to raise and lower the turret. I also have a switch for turret up/down which I'd like to integrate into the macro. Hood suggested the following code a while back, but I haven't had time to really play with it:

 If GetSelectedTool = 1 Then
 Do
  ActivateSignal(OutPut7)
If IsActive(Input1) Then Exit Do
Loop
 Code "G53 G0 A0"
 While IsMoving()
 Wend
 DeActivateSignal(OutPut7)
 End If


Input 1 is a switch, active when the turret is up. The above example keeps the turret from rotating until it is up. I'd also like to add a line to ensure that the turret is back down before exiting the macro (I.E. input 1 is off). I'm sure that's pretty easy?

If possible, I'd also like to add something to either feed hold or e-stop the machine in the event that the turret fails to raise or lower, and maybe even display something in the message box if that happens. "Turret Error, E Stop" or something like that.

Can anyone get me pointed in the right direction?

I've looked through the VB reference doc, but I'm not too well versed on that kind of programming.

Thanks!

Kevin
Title: Re: Help with VB script
Post by: ger21 on November 08, 2012, 10:45:15 PM
Try something like this.

If Not IsActive(INPUT1) then
Message "Turret Error, E Stop"
DoOEMButton(1021) ' Put Mach3 in Reset

End If
Title: Re: Help with VB script
Post by: GytarrMann19 on November 08, 2012, 11:11:13 PM
Would this work? E-stop if the turret fails to lift, E-stop if it fails to go back down? Sleep to give time for the turret to move before the script polls for switch state? I'm sure the syntax needs correction though...


 If GetSelectedTool = 1 Then
 Do
  ActivateSignal(OutPut7)
  Sleep 20
If IsActive(Input1) Then Exit Do
If Not IsActive(INPUT1) then
Message "Turret Error, E Stop"
DoOEMButton(1021) ' Put Mach3 in Reset

End If
Loop
 Code "G53 G0 A0"
 While IsMoving()
 Wend
 DeActivateSignal(OutPut7)
 Sleep 20
If IsActive(INPUT1) then
Message "Turret Error, E Stop"
DoOEMButton(1021) ' Put Mach3 in Reset

End If
 End If
Title: Re: Help with VB script
Post by: GytarrMann19 on November 10, 2012, 09:53:49 PM
This might work better, using the While..Wend to wait for the switch:


If GetSelectedTool = 1 Then
 Do
  ActivateSignal(OutPut7)     'Raise Turret
 
   While Not IsActive(Input1) 'Wait for switch state change
    Or Sleep 500              'Timeout if state does not change
   Wend
If Not IsActive(INPUT1) then
 Message "Turret Error, E Stop"
 DoOEMButton(1021)             ' Put Mach3 in Reset/E-Stop

End If
 Loop
  Code "G53 G0 A0"             ' Gcode to move A axis
   While IsMoving()            ' Wait for completed step pulse output
   Wend
Sleep 50                       ' Ensure rotation is complete before lowering
 DeActivateSignal(OutPut7)     ' Lower Turret
  While IsActive(INPUT1)       ' Wait for switch state change
   Or Sleep 500                ' Timeout if state does not change
  Wend
 If IsActive(INPUT1) then
 Message "Turret Error, E Stop"
 DoOEMButton(1021)             ' Put Mach3 in Reset/E-stop
 End If

End If
Title: Re: Help with VB script
Post by: BR549 on November 11, 2012, 09:31:21 AM
Do you just have one switch to tell you up or down? Does it have a null spot in the middle of travel where neither can be  active?

Do you have an switch that tells you you are at lock up location or are you depending on the A axis to be correct?

IF the motor gets slightly off position at lock up WILL the air cylinder pull it into final position for lockup ? 

(;-) TP
Title: Re: Help with VB script
Post by: GytarrMann19 on November 11, 2012, 10:54:24 PM
Just one switch to indicate up/down. The state changes somewhere approximately in the middle of the turret lift. I may need to play with timing in the script to compensate for that, but that can happen once I have a working macro. No switch to indicate when position is reached, but that might be my next project. For now, just relying on the stepper to get the A axis where it needs to be. Once in position, the turret lowers onto a pair of tapered locking tabs, I'd say they're tapered 5deg or so on a side, so they do provide some room for error in positioning.
Title: Re: Help with VB script
Post by: GytarrMann19 on November 21, 2012, 05:49:59 PM
This is the final code I ended up using. Works as it should, except that I can't get the message box to display the message I want.

Dim cnt
cnt =0
If GetSelectedTool = 1 Then
ActivateSignal(output7)
cnt =0
Do While IsActive(Input1) And cnt < 500
Sleep 10
cnt = cnt + 10
Loop
If cnt >+ 500 And IsActive(Input1) then
DoOEMButton(1021)
Message ("Turret Error, E Stop")
End IF
Code "G0 A0"
While IsMoving ()
Sleep 10
Wend
Sleep 25
DeActivateSignal(Output7)
cnt =0
Do While Not IsActive(Input1) And cnt < 500
Sleep 10
cnt = cnt + 10
Loop
If cnt >= 500 And IsActive(Input1) THen
DoOEMButton(1021)
Message ("Turret Error, E Stop")
End If
End If
Title: Re: Help with VB script
Post by: ger21 on November 21, 2012, 05:52:34 PM
Try:
Message "Turret Error, E Stop"
Title: Re: Help with VB script
Post by: GytarrMann19 on November 21, 2012, 06:01:09 PM
I tried that too. It works if I just use that out of the VB editor, but when it happens within the macro, all I get is the normal "External E Stop Requested"
Title: Re: Help with VB script
Post by: Hood on November 21, 2012, 06:11:55 PM
What about using a message box. I much prefer them for messages you dont want to miss as it pops up on screen and you have to OK to get rid.

Would be
Msgbox "Turret Error, E Stop"

Hood
Title: Re: Help with VB script
Post by: GytarrMann19 on November 21, 2012, 06:34:22 PM
Thanks Hood, I thought of that. Might do it that way. Found that adding a short sleep between the two works:

DoOEMButton(1021)
Sleep 60
Message ("Turret Error, E Stop")


Thanks again guys