Two Phidgets 1065 controllers in same VB 6 program

Legacy support with Phidget21
Halfmens
Fresh meat
Posts: 1
Joined: Sun Jan 20, 2013 7:31 am
Location: The Netherlands
Contact:

Two Phidgets 1065 controllers in same VB 6 program

Postby Halfmens » Sat May 11, 2013 8:03 am

The object of the exercise:
Use two Phidgets 1065 controllers simultaneously in the same VB6 program.

In the actual project one DC motor controls the movement of a table and one DC motor controls the movement of a chuck.
Both motors are equipped with encoders which are used to measure the way (table) or number of degrees (chuck)
In both cases it is absolutely necessary to keep track of the encoder pulses.
In short:
First the table is moved from a set zero point to a specified point along a shaft.
Motor 1 is stopped when the table reached its position. With a Laser Micrometer a diameter measurement is taken. Motor 2 (chuck) is then instructed to rotate the chuck a number of encoder pulses and a new measurement is taken. Again Motor 2 moves a specified number of pulses and a new measurement is taken. After the chuck has completed 360 degrees Motor 1 is instructed to move the table to the next position.
The whole process with Motor 2 is the repeated until all diameters along the whole length of the shaft have been measured.
It is very important to keep track of the table movement as the movement of the table is to be “absolute” to ensure accuracy, therefore the encoder should NOT be reset within a complete cycle. With Incremental movement the required repeatability is not reached.

With the listed code I can control both motors but when I re-open the motor control the encoder is set to zero every time.

How can I change my code to switch control from one motor to the other motor without using the command:
PhidgetMotorControl.Open (sngTableMotorSerialNr)

Any help would be highly appreciated.

Code: Select all

Option Explicit
    Dim WithEvents phidgetIFK As PhidgetInterfaceKit
    Dim WithEvents PhidgetMotorControl As PhidgetMotorControl
    Dim sngTableEncoder As Single
    Dim intTableVelocity As Integer
    Dim intTableBrake As Integer
    Dim intTableAcceleration As Integer
    Dim sngTableMotorSerialNr As Single
    Dim lngTableEncoder As Long
    Dim blnTableMotor As Boolean
    Dim lngChuckEncoder As Long
    Dim intChuckVelocity As Integer
    Dim intChuckBrake As Integer
    Dim intChuckAcceleration As Integer
    Dim sngChuckMotorSerialNr As Single
    Dim blnChuckMotor As Boolean
    Dim sngStartTimer As Single
    Dim lngTimer1 As Long


Private Sub Form_Load()
    Set phidgetIFK = Controls.Add("Phidget21COM.PhidgetInterfaceKit", "phidgetIFK")
    Set PhidgetMotorControl = Controls.Add("Phidget21COM.PhidgetMotorControl", "phidgetMotorControl")
    phidgetIFK.Open
'Two 1065 controls
    sngTableMotorSerialNr = 146770
    sngChuckMotorSerialNr = 146479
    'PhidgetMotorControl.Open (sngTableMotorSerialNr)
    'PhidgetMotorControl.Open (sngChuckMotorSerialNr)
    intTableVelocity = 10
    intTableBrake = 50
    intTableAcceleration = 20
    intChuckVelocity = 40
    intChuckAcceleration = 20
    intChuckBrake = 50
End Sub

Private Sub PhidgetMotorControl_OnSensorUpdate(ByVal Index As Long, ByVal SensorValue As Long)
    'sensorValueF(Index).Text = SensorValue
End Sub

Private Sub PhidgetMotorControl_OnVelocityChange(ByVal Index As Long, ByVal newVal As Double)
    'If (Index = motorIndex) Then
    '    currentVelocityF.Text = newVal
    'End If
End Sub

Private Sub PhidgetMotorControl_OnEncoderPositionChange(ByVal Index As Long, ByVal Time As Long, ByVal PositionChange As Long)
    If blnTableMotor = True Then
        lngTableEncoder = PhidgetMotorControl.EncoderPosition(0)
        txtTableEncoder.Text = Str(lngTableEncoder)
    End If
    If blnChuckMotor = True Then
        lngChuckEncoder = PhidgetMotorControl.EncoderPosition(0)
        txtChuckEncoder.Text = Str(lngChuckEncoder)
    End If
End Sub

Private Sub cmdTest_Click()
    blnTableMotor = True
'Set 1065 for Table motor
    PhidgetMotorControl.Open (sngTableMotorSerialNr)
    lngTimer1 = GetTickCount
    Do Until GetTickCount - lngTimer1 > 100 'wait at least 100ms for the controller to mount
        DoEvents
    Loop
        'PhidgetMotorControl.Velocity(sngTableMotorSerialNr) = intTableVelocity  (Unsuccesfull Trial to set with 1065 serialnumber)
    sngStartTimer = Timer
'Start movement of Table Motor
    PhidgetMotorControl.Velocity(0) = 40
    txtStatus.Visible = True
    txtStatus.Text = "Table Motor Run"
'Keep motor running for at least 5 seconds, dislaying the encoder readings in a text field
    Do Until Timer - sngStartTimer > 5
    DoEvents
    Loop
'Stop Table motor
    intTableVelocity = 0
    PhidgetMotorControl.Velocity(0) = intTableVelocity
    PhidgetMotorControl.Close
    txtStatus.Text = "Table Motor Stop"
    DoEvents
    blnTableMotor = False
    blnChuckMotor = True
'Set 1065 for Chuck Motor
    PhidgetMotorControl.Open (sngChuckMotorSerialNr)
    lngTimer1 = GetTickCount
    Do Until GetTickCount - lngTimer1 > 100
        DoEvents
    Loop
    txtStatus.Text = "Chuck Motor Run"
    sngStartTimer = Timer
'Start the Chuck Motor
    PhidgetMotorControl.Velocity(0) = intChuckVelocity
    Do Until Timer - sngStartTimer > 5
        DoEvents
    Loop
    intChuckVelocity = 0
    txtStatus.Text = "Chuck Motor Stop"
    PhidgetMotorControl.Velocity(0) = intChuckVelocity
'Stop Chuck Motor
    PhidgetMotorControl.Close
    DoEvents
    blnTableMotor = True
    blnChuckMotor = False
'Set Table Motor again
    PhidgetMotorControl.Open (sngTableMotorSerialNr)
    lngTimer1 = GetTickCount
    Do Until GetTickCount - lngTimer1 > 100
        DoEvents
    Loop
    intTableVelocity = 10
    sngStartTimer = Timer
    PhidgetMotorControl.Velocity(0) = intTableVelocity
    txtStatus.Text = "Table Motor Run"
    Do Until Timer - sngStartTimer > 5
        DoEvents
    Loop
    cmdStop_Click
End Sub

Private Sub chkTable_Click()
    If chkTable.Value = 1 Then
        blnTableMotor = True
    End If
End Sub

Private Sub cmdStop_Click()
    PhidgetMotorControl.Velocity(0) = 0
    txtStatus.Text = ""
    txtStatus.Visible = False
End Sub



User avatar
mparadis
Site Admin
Posts: 656
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Two Phidgets 1065 controllers in same VB 6 program

Postby mparadis » Mon May 13, 2013 7:47 am

Rather than using one motor controller object and constantly opening and closing with the two devices, you could just use two objects, one for each device (eg. PhidgetMotorControlTable and PhidgetMotorControlChuck). Alternatively you could keep track of each motor's index by storing the values in variables between the opens and closes.


Return to “VB 6.0”

Who is online

Users browsing this forum: No registered users and 1 guest