ADLINK 8158控制程序-连续运动(VB.NET)

运动平台:日脉的二维运动平台(一个旋转平台和一个滑动平台)

开发环境:VS2010 + .NET Framework + VB.NET

使用文件:pci_8158.vb

motion_8158_2D.vb

2D平台运动类

Public Class motion_8158_2D
  
    Public CardId As Integer
    'move parameter here
    'AxisNo is 0 or 1
    Public AxisNo As Short
    'Dist : mm
    Public Dist As Double
    'StrVel : mm/s
    Public StrVel As Double
    'MaxVel : mm/s
    Public MaxVel As Double
    'Tacc : s
    Public Tacc As Double
    'Tdec : s
    Public Tdec As Double
  
    Public Sub New(ByVal cardId1 As Integer)
        CardId = cardId1
    End Sub
  
    Public Sub New(ByVal axisNo1 As Short, ByVal dist1 As Double, ByVal strVel1 As Double, ByVal maxVel1 As Double, ByVal tacc1 As Double, ByVal tdec1 As Double)
        AxisNo = axisNo1
        Dist = dist1
        StrVel = strVel1
        MaxVel = maxVel1
        Tacc = tacc1
        Tdec = tdec1
    End Sub
  
    Public Sub CardRegedit_8158_2d()
  
        Dim code As Integer
        code = B_8158_initial(CardId, 0)
        'set axis num 0
        B_8158_set_move_ratio(0, 1)
        B_8158_set_pls_outmode(0, 4)
        B_8158_set_servo(0, 1)
  
        'set axis num 1
        B_8158_set_alm(1, 1, 0)
        B_8158_set_inp(1, 0, 1)
        B_8158_set_move_ratio(1, 1)
        B_8158_set_pls_outmode(1, 0)
        B_8158_set_move_ratio(1, 0)
  
        'code = B_8158_config_from_file()
        'the axis num of x axis linear motion is 0
        'the axis num of Ry axis ratary motion is 1
  
        'Is has 8158 card 's drive
        If (code <> 0) Then
            MsgBox("No 8158 Card exit!")
            B_8158_close()
        End If
  
        'configure the input mode of external feedback pulse
        B_8158_set_pls_iptmode(0, 2, 0)
        'set counter input source
        B_8158_set_feedback_src(0, 0)
    End Sub
  
    Public Sub LinearMotion()
        Dim pulse_dist As Double = Dist * 1000
        Dim pulse_strVel As Double = StrVel * 1000
        Dim pulse_maxVel As Double = MaxVel * 1000
        B_8158_start_tr_move(AxisNo, pulse_dist, pulse_strVel, pulse_maxVel, Tacc, Tdec)
    End Sub
  
    Public Sub RotateMotion()
        'rotation stage dec radio 1/5
        Dim pulse_dist As Double = Dist * 18000 / 360
        Dim pulse_strVel As Double = StrVel * 18000 / 360
        Dim pulse_maxVel As Double = MaxVel * 18000 / 360
        B_8158_start_tr_move(AxisNo, pulse_dist, pulse_strVel, pulse_maxVel, Tacc, Tdec)
    End Sub
  
    Public Function GetLinearSpeed() As Double
        Dim s As Double
        B_8158_get_current_speed(0, s)
        GetLinearSpeed = s / 1000
    End Function
  
    Public Function GetRotSpeed() As Double
        Dim s As Double
        B_8158_get_current_speed(1, s)
        GetRotSpeed = s * 360 / 18000
    End Function
End Class

新开辟一个线程,用While循环控制连续运动主窗体程序:

用事件委托的方法,向TextBox中写入数据

''' <summary>
''' aging motion control test software at 20130819
''' mail:bin___03@163.com
''' </summary>
''' <remarks></remarks>
'''
  
Public Class Form1
    'Control motion manually
    Delegate Sub UpdateLogCallback(ByVal [text] As String)
    Private manualMotion As Boolean = False
    Dim MotionThread As System.Threading.Thread
    Dim objMotion As motion_8158_2D = New motion_8158_2D(0)
    Dim timeBegin As Date
    Dim timeEnd As Date
    Dim boolDuration As Boolean = False
    Dim log As IO.StreamWriter
    Dim logFileName As String
  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.MaximumSize = Me.Size
        Me.MinimumSize = Me.Size
  
        objMotion.CardRegedit_8158_2d()
  
    End Sub
  
    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        timeBegin = DateTime.Now
        boolDuration = True
        'MsgBox(timeBegin.ToString("yyyyMMddHHmmss"))
        logFileName = timeBegin.ToString("yyMMddHHmmss") & ".txt"
        log = New IO.StreamWriter(logFileName, True)
  
        manualMotion = True
        btnStop.Enabled = True
        btnStart.Enabled = False
        tsslMovingStatus.Text = "moving..."
        txtLog.Clear()
  
        MotionThread = New System.Threading.Thread(AddressOf ContinuuousMotion)
        MotionThread.Start()
          
    End Sub
  
    Private Function isStopMotion() As Boolean
        isStopMotion = True
        For i = 0 To 1
            Dim temp As Short = B_8158_motion_done(i)
            If B_8158_motion_done(i) <> 0 Then
                isStopMotion = False
                Exit For
            End If
        Next
        Return isStopMotion
    End Function
  
    Sub ContinuuousMotion()
        Dim objRandom As Random = New Random()
        Dim tilt_old As Double = 0
        Dim tilt_new As Double
        'MsgBox(manualMotion)
        While True
  
            If manualMotion Then
                Exit While
            End If
  
        End While
  
        While manualMotion
            Dim rdn As Double = objRandom.NextDouble
            If rdn <= 0.5 Then
                tilt_new = -70 * rdn
            Else
                tilt_new = 70 * rdn
            End If
  
            Dim strLog As String =
                ">> " & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") & "TILT:" & tilt_new.ToString & vbCrLf
            UpdateLog(strLog)
            log.Write(strLog)
  
            Threading.Thread.Sleep(1000)
            ' rotate
            objMotion = New motion_8158_2D(1, tilt_new - tilt_old, 0, 60, 0.05, 0.05)
            objMotion.RotateMotion()
  
            'waiting for the motion stopped
            While True
                If isStopMotion() Then
                    Exit While
                End If
            End While
  
            Threading.Thread.Sleep(1000)
            'acc = 0.18g for 100mm for linear stage
            objMotion = New motion_8158_2D(0, 100, 0, 90, 0.05, 0.05)
            objMotion.LinearMotion()
  
            'waiting for the motion stopped
            While True
                If isStopMotion() Then
                    Exit While
                End If
            End While
  
            Threading.Thread.Sleep(1000)
            'acc = -0.18g for 100mm for linear stage
            objMotion = New motion_8158_2D(0, -100, 0, 90, 0.05, 0.05)
            objMotion.LinearMotion()
  
            'waiting for the motion stopped
            While True
                If isStopMotion() Then
                    Exit While
                End If
            End While
  
            tilt_old = tilt_new
        End While
    End Sub
  
    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        boolDuration = False
        manualMotion = False
        btnStop.Enabled = False
        btnStart.Enabled = True
        tsslMovingStatus.Text = "stoped..."
        UpdateLog(Application.StartupPath & "" & logFileName)
        log.Dispose()
        log.Close()
        MotionThread.Abort()
  
        B_8158_stop_move_all(0)
        For i = 0 To 7
            B_8158_emg_stop(i)
            B_8158_set_motion_int_factor(i, 0)
        Next
        B_8158_int_control(0, 0)
    End Sub
  
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        txtLinearSpeed.Text = objMotion.GetLinearSpeed.ToString
        txtRotSpeed.Text = objMotion.GetRotSpeed.ToString
  
  
        If boolDuration Then
            'start button and stop button control boolDuration parameter
            timeEnd = DateTime.Now
            'timespan format write to duration time text
            txtDurationTime.Text = (timeEnd - timeBegin).ToString("hh:mm:ss.ff")
        End If
  
    End Sub
  
    Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        Try
            B_8158_stop_move_all(0)
            B_8158_close()
            MotionThread.Abort()
            log.Dispose()
            log.Close()
        Catch ex As Exception
  
        End Try
          
    End Sub
  
    Private Sub UpdateLog(ByVal [text] As String)
        If InvokeRequired Then
            Dim d As New UpdateLogCallback(AddressOf UpdateLog)
            Me.Invoke(d, New Object() {[text]})
        Else
  
            If txtLog.Lines.Count > 5000 Then
                txtLog.Clear()
            End If
            txtLog.AppendText([text])
        End If
    End Sub
  
End Class

工程文件下载页:http://download.csdn.net/detail/asan2006/5993907

原文地址:https://www.cnblogs.com/jmpep/p/4486141.html