Skip to content

Instantly share code, notes, and snippets.

@bowserthewizard
Created November 24, 2014 18:56
Show Gist options
  • Save bowserthewizard/10f14ae521603587cd10 to your computer and use it in GitHub Desktop.
Save bowserthewizard/10f14ae521603587cd10 to your computer and use it in GitHub Desktop.
Public Class LinearStageForm
Dim k, counter, delay As Long
Dim spd, Direction, enable, sensorMax As Short
Dim startT, chkT As New Double
Dim pos, min_pos, max_pos As New Integer
Dim seqSt, seqNext, motionSt, motionNext, command As String
Dim tmr As PerformanceTimer
Dim sim, Pause, ptimer As Boolean
Private Sub LinearStageForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
sim = True
ptimer = True
Pause = True
StagePosition.Minimum = -1000
StagePosition.Maximum = 1000
StagePosition.Value = 0
spd = 1000
delay = 85
counter = 0
leftLabel.Visible = False
rightLabel.Visible = False
If (sim = True) Then
sim_button.Checked = True
Else
Call Initialize_IO()
sim_button.Checked = False
position_output.Enabled = False
position_output.Enabled = False
End If
If (ptimer = True) Then
Call InitializeTimer() ' Initialize the Performance Timer if selected
End If
End Sub
Private Sub sim_button_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sim_button.CheckedChanged
If sim_button.Checked = True Then
sim = True
position_output.Enabled = True
position_output.Enabled = True
Else
sim = False
Call Initialize_IO()
position_output.Enabled = False
position_output.Enabled = False
End If
End Sub
Public Sub InitializeTimer()
'A routine to initialize the QueryPerformanceTimer
tmr = New PerformanceTimer
tmr.StartTimer()
End Sub
Public Function GetTimeNow() As Double
'This function uses either the Timer Property or the Performance Counter depending on the setting of 'the PerformanceTimerFlagSelect
If (ptimer = True) Then
tmr.ReadCurrentTimer() ' Read the timer value
GetTimeNow = tmr.TimeElapsed(PerformanceTimer.PerformanceValue.pvSecond)
Else
GetTimeNow = Microsoft.VisualBasic.Timer()
End If
End Function
Private Sub slow_button_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles slow_button.CheckedChanged
If slow_button.Checked = True Then
spd = 15000
delay = 250
End If
End Sub
Private Sub medium_button_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles medium_button.CheckedChanged
If medium_button.Checked = True Then
spd = 7500
delay = 150
End If
End Sub
Private Sub fast_button_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fast_button.CheckedChanged
If fast_button.Checked = True Then
spd = 1000
delay = 85
End If
End Sub
Private Sub start_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start_button.Click
start_button.Visible = False
left_button.Enabled = True
right_button.Enabled = True
stop_button.Enabled = True
stop_button.Text = "PAUSE"
command = "Idle"
motionNext = "SendLow"
Do While (2 > 1) ' This is an infinite loop
k = k + 1
If k Mod 3000 = 0 Then ' The mod function is used so that the following
' two statements do not get executed all the time
System.Windows.Forms.Application.DoEvents() ' Allows background processing
statebox.Text = seqSt
End If
Call sequence() ' This function processes commands
Call motion() ' This function simulates (or enabled) stage motion
Loop
End Sub
Public Sub sequence()
If command = " " Then
seqSt = seqNext
Else
seqSt = command
End If
Select Case (seqSt)
Case "Idle"
command = " "
counter = 0
enable = 0
If (StagePosition.Value = StagePosition.Minimum) Or sensorMax = 2 Then
leftLabel.Visible = True
left_button.Enabled = False
End If
If (StagePosition.Value = StagePosition.Maximum) Or sensorMax = 1 Then
rightLabel.Visible = True
right_button.Enabled = False
End If
seqNext = "Idle"
Case "Left"
command = " "
leftLabel.Visible = False
rightLabel.Visible = False
Direction = 2 ' Send Cart Left
enable = 1
If sim = False Then
sensorMax = Read_IO()
If sensorMax = 2 Then
seqNext = "Idle"
Else
seqNext = "Left"
End If
Else
If StagePosition.Value = StagePosition.Minimum Then
seqNext = "Idle"
Else
seqNext = "Left"
End If
End If
Case "Right"
command = " "
leftLabel.Visible = False
rightLabel.Visible = False
Direction = 0 ' Send Cart Right
enable = 1
If sim = False Then
sensorMax = Read_IO()
If sensorMax = 1 Then
seqNext = "Idle"
Else
seqNext = "Right"
End If
Else
If StagePosition.Value = StagePosition.Maximum Then
seqNext = "Idle"
Else
seqNext = "Right"
End If
End If
End Select
End Sub
Public Sub motion()
motionSt = motionNext
Select Case motionSt
Case "SendLow"
If sim = True Then
counter = counter + 1
If counter >= spd Then
If Direction = 0 Then
StagePosition.Value = StagePosition.Value + Enable ' Move to the right
ElseIf Direction = 2 Then
StagePosition.Value = StagePosition.Value - Enable ' Move to the left
End If
position_output.Text = StagePosition.Value
counter = 0
End If
Else
Send_IO(Direction) ' Send Low Step Signal
motionNext = "SendHigh"
startT = GetTimeNow()
End If
Case "SendHigh"
If ((GetTimeNow() - startT) >= (delay * 0.00001)) Then
Send_IO(Direction + enable) ' Move Cart Left(3)/Right(1)
motionNext = "SendHigh"
End If
End Select
End Sub
Private Sub left_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles left_button.Click
command = "Left"
left_button.Enabled = False
right_button.Enabled = True
stop_button.Enabled = True
stop_button.Text = "PAUSE"
Pause = True
End Sub
Private Sub right_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles right_button.Click
command = "Right"
left_button.Enabled = True
right_button.Enabled = False
stop_button.Enabled = True
stop_button.Text = "PAUSE"
Pause = True
End Sub
Private Sub stop_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stop_button.Click
If Pause = True Then
command = "Idle"
stop_button.Text = "EXIT"
left_button.Enabled = True
right_button.Enabled = True
Pause = False
Else
End
End If
End Sub
Private Sub exit_button_Click(sender As Object, e As EventArgs) Handles exit_button.Click
Dim Response As Integer
3:
Response = MessageBox.Show("Do you really want to exit?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
5:
If Response = vbYes Then
6:
Me.Close()
7:
End If
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment