Beginner needs some Help

Supporting Visual Studio on Windows
Trigger
Phidgetsian
Posts: 5
Joined: Sun Dec 16, 2012 4:44 am
Contact:

Beginner needs some Help

Postby Trigger » Sun Dec 16, 2012 5:14 am

I've been using Microsoft Visual Basic 2010 Express for about two days, and need some help.
Since I have never seen a code programs before I have a little problem with phidget example file for steppers.
I've managed to add buttons and Numericupdown1 so I can type in any position stepper should go to, and it works great (beginner's luck) :)
Have spent much copy / paste from the sample file which has meant that I managed to get it to work.

The problem is that in the sample file uses a "Drop Down List" to select the stepper to move, which also works, BUT, I would like to have "CheckBox"
so I can choose, for example stepper 1 +2 to move at the sametime, or 0 +1 +2 ....
Have tried to see where the infomationen is where to control this but find only this.
Have tried to play with this code
:
For i As Integer = 0 To stepper.steppers.Count - 1
stepperCmb.Items.Add (i)
Next in

stepperCmb.SelectedIndex = 0

It just makes the number changes in the drop down list or more numbers

Does anyone know how to make the steppers in the checkbox
instead of dropdown list?
Remember that I am completely new to this so I might be a bit stupid :)

Thanks for the help!
/ David
Attachments
Stepper-full.rar
Here is the Exempel file
(16.49 KiB) Downloaded 261 times

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

Re: Beginner needs some Help

Postby mparadis » Mon Dec 17, 2012 12:26 pm

To change the example to behave the way you described, you would first need to replace the drop-down menu with check box items, and then you'd need to change every slider so that it used 'if' statements to decide which motors' values to change, instead of the one currently selected by the text box. For example, in "velocityTrk_Scroll", you would need to change:

Code: Select all

stepper.steppers(stepperCmb.SelectedItem).VelocityLimit = vel


with something like

Code: Select all

if CheckBox0.CheckState == True
     stepper.steppers(0).VelocityLimit = vel
if CheckBox1.CheckState == True
     stepper.steppers(1).VelocityLimit = vel
etc...


I might have some of the syntax wrong, but this is the sort of structure you'd need.
Basically every mention of "stepperCmb.SelectedItem" would need to be reworked in this way.

Trigger
Phidgetsian
Posts: 5
Joined: Sun Dec 16, 2012 4:44 am
Contact:

Re: Beginner needs some Help

Postby Trigger » Mon Dec 17, 2012 12:34 pm

Thanks for the reply, i will give it a try.

Trigger
Phidgetsian
Posts: 5
Joined: Sun Dec 16, 2012 4:44 am
Contact:

Re: Beginner needs some Help

Postby Trigger » Mon Dec 17, 2012 1:05 pm

mparadis wrote:To change the example to behave the way you described, you would first need to replace the drop-down menu with check box items, and then you'd need to change every slider so that it used 'if' statements to decide which motors' values to change, instead of the one currently selected by the text box. For example, in "velocityTrk_Scroll", you would need to change:

Code: Select all

stepper.steppers(stepperCmb.SelectedItem).VelocityLimit = vel


with something like

Code: Select all

if CheckBox0.CheckState == True
     stepper.steppers(0).VelocityLimit = vel
if CheckBox1.CheckState == True
     stepper.steppers(1).VelocityLimit = vel
etc...


I might have some of the syntax wrong, but this is the sort of structure you'd need.
Basically every mention of "stepperCmb.SelectedItem" would need to be reworked in this way.


This was to mutch info for me to handle:) lets try to brake it down

So first i need to change the drop-down menu with check box items, right here i dont know how to to that, have added 4 check boxes to the form, can i copy the code from the dropdown menu to the box amd change the namn to CheckBox1? dont know how to make the box work instedd of the dropdown menu.
Sound like its easy but not to me:)

Thanks for helping me out!!

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

Re: Beginner needs some Help

Postby mparadis » Mon Dec 17, 2012 1:44 pm

Well, it doesn't make much sense to copy the code for the dropdown menu straight across, because all that code does is change the position of all the sliders whenever you change motors, so that the proper velocity, position, and acceleration are shown. But with this new setup, you're controlling multiple motors, so it doesn't make sense to update the sliders since a slider can only represent the value for one motor at a time.

So, you can probably delete the dropdown menu and all the code inside it, and then start replacing the code I mentioned in my first post. You don't need to add any special code for when the checkBoxes are clicked, because you just need to read their states whenever you change a slider, to see which motors are selected.

When you delete the dropdown menu, it will create errors every time it's referenced, which might be useful because it'll point you to where you need to add the checkbox 'if' statements.

Trigger
Phidgetsian
Posts: 5
Joined: Sun Dec 16, 2012 4:44 am
Contact:

Re: Beginner needs some Help

Postby Trigger » Tue Dec 18, 2012 5:18 am

Thanks for the input!

I will give it a try.

/David

Trigger
Phidgetsian
Posts: 5
Joined: Sun Dec 16, 2012 4:44 am
Contact:

Re: Beginner needs some Help

Postby Trigger » Wed Dec 19, 2012 9:55 am

I have now try to make one checkbox for one motor.
Is this the right way to go?

Code: Select all

'A reference to the Phidget21.NET.dll must be added to new projects in VB.Net
'From the "Project" menu select "Add Reference"
'From the "Add Reference" window select the ".NET" tab
'select "Phidget21.NET" and click "OK"

Public Class Form1

    Dim WithEvents stepper As Phidgets.Stepper

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        stepper = New Phidgets.Stepper
        stepper.open()
    End Sub

    Private Sub stepper_Attach(ByVal sender As Object, ByVal e As Phidgets.Events.AttachEventArgs) Handles stepper.Attach
        attachedTxt.Text = stepper.Attached.ToString()
        nameTxt.Text = stepper.Name
        serialTxt.Text = stepper.SerialNumber.ToString()
        versiontxt.Text = stepper.Version.ToString()
        numSteppersTxt.Text = stepper.steppers.Count.ToString()

        velocityTrk.SetRange(stepper.steppers(0).VelocityMin, stepper.steppers(0).VelocityMax)
        velocityTrk.TickFrequency = stepper.steppers(0).VelocityMax / 64
        velocityTrk.LargeChange = stepper.steppers(0).VelocityMax / 64
        accelTrk.SetRange(stepper.steppers(0).AccelerationMin, stepper.steppers(0).AccelerationMax)
        accelTrk.TickFrequency = stepper.steppers(0).AccelerationMin * 4
        accelTrk.LargeChange = stepper.steppers(0).AccelerationMin
        positionTrk.SetRange(-20000, 20000) 'Have arbitrarily chosen a range of -20000 - 20000, but the maximum range is closer to +-549000000000
        positionTrk.TickFrequency = 1000
        positionTrk.LargeChange = 250
        currentPositionTrk.SetRange(-20000, 20000) 'Have arbitrarily chosen a range of -20000 - 20000, but the maximum range is closer to +-549000000000
        currentPositionTrk.TickFrequency = 1000
        currentPositionTrk.LargeChange = 250

        velocityTrk.Enabled = True
        accelTrk.Enabled = True
        positionTrk.Enabled = True
        currentPositionTrk.Enabled = True
        engagedCheckBox.Enabled = True
        stepperCmb.Enabled = True

        Select Case stepper.ID
            'Only a Bipolar stepper has access to these features
            Case Phidgets.Phidget.PhidgetID.BIPOLAR_STEPPER_1MOTOR
                currentLimitTrk.Visible = True
                currentLimitTxt.Visible = True
                currentTxt.Visible = True
                currentLimitLbl.Visible = True
                currentLimitTrkLbl.Visible = True
                currentLbl.Visible = True

                currentLimitTrk.Enabled = True

                inputsGrp.Visible = True

                currentLimitTrk.SetRange((stepper.steppers(0).CurrentMin * 100.0), (stepper.steppers(0).CurrentMax * 100.0)) ' Range for the current limit
                currentLimitTrk.TickFrequency = 5

            Case Phidgets.Phidget.PhidgetID.UNIPOLAR_STEPPER_4MOTOR
                currentLimitTrk.Visible = False
                currentLimitTxt.Visible = False
                currentTxt.Visible = False
                currentLimitLbl.Visible = False
                currentLimitTrkLbl.Visible = False
                currentLbl.Visible = False

                inputsGrp.Visible = False
        End Select

        For i As Integer = 0 To stepper.steppers.Count - 1
            stepperCmb.Items.Add(i)
        Next i

        stepperCmb.SelectedIndex = 0
    End Sub

    Private Sub stepper_CurrentChange(ByVal sender As Object, ByVal e As Phidgets.Events.CurrentChangeEventArgs) Handles stepper.CurrentChange
        If e.Index = CheckBox1.CheckState Then
            currentTxt.Text = e.Current.ToString()
        End If
    End Sub

    Private Sub stepper_Detach(ByVal sender As Object, ByVal e As Phidgets.Events.DetachEventArgs) Handles stepper.Detach
        attachedTxt.Text = stepper.Attached.ToString()
        nameTxt.Clear()
        serialTxt.Clear()
        versiontxt.Clear()
        numSteppersTxt.Clear()

        velocityTrk.Enabled = False
        accelTrk.Enabled = False
        positionTrk.Enabled = False
        currentPositionTrk.Enabled = False
        currentLimitTrk.Enabled = False
        engagedCheckBox.Enabled = False
        stepperCmb.Enabled = False

        positionTrk.Value = 0
        currentPositionTrk.Value = 0
        velocityTrk.Value = velocityTrk.Minimum
        accelTrk.Value = accelTrk.Minimum
        currentLimitTrk.Value = currentLimitTrk.Minimum

        velocityLimitTxt.Clear()
        velocityTxt.Clear()
        accelTxt.Clear()
        positionTxt.Clear()
        targetPositionTxt.Clear()
        currentLimitTxt.Clear()
        currentTxt.Clear()

        engagedCheckBox.Checked = False
        stoppedCheckBox.Checked = False

        stepperCmb.Items.Clear()
    End Sub

    Private Sub stepper_Error(ByVal sender As Object, ByVal e As Phidgets.Events.ErrorEventArgs) Handles stepper.Error
        MessageBox.Show(e.exception.Message, "Error Event")
    End Sub

    Private Sub stepper_InputChange(ByVal sender As Object, ByVal e As Phidgets.Events.InputChangeEventArgs) Handles stepper.InputChange
        Dim inputChk As CheckBox = inputsGrp.Controls("input" + e.Index.ToString() + "Chk")
        inputChk.Checked = e.Value
    End Sub

    Private Sub stepper_PositionChange(ByVal sender As Object, ByVal e As Phidgets.Events.StepperPositionChangeEventArgs) Handles stepper.PositionChange
        If e.Index = CheckBox1.CheckState Then
            positionTxt.Text = e.Position.ToString()
            stoppedCheckBox.Checked = stepper.steppers(e.Index).Stopped
            If currentPositionTrk.Focused = False Then
                currentPositionTrk.Enabled = stepper.steppers(e.Index).Stopped
            End If
        End If
    End Sub

    Private Sub stepper_VelocityChange(ByVal sender As Object, ByVal e As Phidgets.Events.VelocityChangeEventArgs) Handles stepper.VelocityChange
        If e.Index = CheckBox1.CheckState Then
            velocityTxt.Text = e.Velocity.ToString()
            stoppedCheckBox.Checked = stepper.steppers(e.Index).Stopped
            If currentPositionTrk.Focused = False Then
                currentPositionTrk.Enabled = stepper.steppers(e.Index).Stopped
            End If
        End If
    End Sub

    Private Sub accelTrk_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles accelTrk.Scroll
        Try
            Dim accel As Double = accelTrk.Value

            If accel < stepper.steppers(CheckBox1.CheckState).AccelerationMin Then
                accel = stepper.steppers(CheckBox1.CheckState).AccelerationMin
            End If

            If accel > stepper.steppers(CheckBox1.CheckState).AccelerationMax Then
                accel = stepper.steppers(CheckBox1.CheckState).AccelerationMax
            End If
            If CheckBox1.CheckState = True Then
                stepper.steppers(1).Acceleration = accel
                accelTxt.Text = accel.ToString()
            End If
        Catch
            accelTxt.Text = "Err"
        End Try
    End Sub

    Private Sub positionTrk_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles positionTrk.Scroll
        Try
            If CheckBox1.CheckState = True Then
                stepper.steppers(1).TargetPosition = positionTrk.Value
                targetPositionTxt.Text = positionTrk.Value.ToString()
            End If
           
        Catch
            targetPositionTxt.Text = "Err"
        End Try
    End Sub

    Private Sub currentPositionTrk_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles currentPositionTrk.Scroll
        Try
            If CheckBox1.CheckState = True Then
                stepper.steppers(1).CurrentPosition = currentPositionTrk.Value
                targetPositionTxt.Text = currentPositionTrk.Value.ToString()
            End If
        Catch
            targetPositionTxt.Text = "Err"
        End Try
    End Sub

    Private Sub currentLimitTrk_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles currentLimitTrk.Scroll
        Try
            Dim current As Double = currentLimitTrk.Value / 100.0

            If current < stepper.steppers(CheckBox1.CheckState).CurrentMin Then
                current = stepper.steppers(CheckBox1.CheckState).CurrentMin
            End If

            If current > stepper.steppers(CheckBox1.CheckState).CurrentMax Then
                current = stepper.steppers(CheckBox1.CheckState).CurrentMax
            End If
            If CheckBox1.CheckState = True Then
                stepper.steppers(1).CurrentLimit = current
                currentLimitTxt.Text = current.ToString()
            End If
        Catch
            currentLimitTxt.Text = "Err"
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        RemoveHandler stepper.Attach, AddressOf stepper_Attach
        RemoveHandler stepper.CurrentChange, AddressOf stepper_CurrentChange
        RemoveHandler stepper.Detach, AddressOf stepper_Detach
        RemoveHandler stepper.Error, AddressOf stepper_Error
        RemoveHandler stepper.PositionChange, AddressOf stepper_PositionChange
        RemoveHandler stepper.VelocityChange, AddressOf stepper_VelocityChange
        RemoveHandler stepper.InputChange, AddressOf stepper_InputChange
        Application.DoEvents()

        If stepper.Attached Then
            For i As Integer = 0 To stepper.steppers.Count - 1
                stepper.steppers(i).Engaged = False
            Next i
        End If

        stepper.close()
        stepper = Nothing
    End Sub
End Class


Return to “VB .NET”

Who is online

Users browsing this forum: No registered users and 1 guest