Timer/TrySetTarget problem? Or bad servo modification?


I have posted before about my Maestro clogging up with my timer script. I dont know what im doing wrong. When i set my timer interval to 100 it stalls after 4 turns, 200 takes a bit longer and so on. But eventually it crashes.

Servos are modified fot continious rotation, please help

Imports Pololu.UsbWrapper
Imports Pololu.Usc

Imports System
Imports System.Text
Imports System.ComponentModel

Public Class Form1
    Private Shared Servo_timer_turn As New System.Timers.Timer
    Private Shared Servo_timer_pause As New System.Timers.Timer

    Dim Servo_modus As Boolean
    Dim Servo_pause As Integer
    Dim Servo_turn As Integer

    Sub Timer(ByRef Servo_modus As Boolean)
        ''Voor iedere addhandler word boolean geswitched en nieuwe intervalwaarde aangebracht
        ''Hieronder Pause structuur
        If Servo_modus = False Then
            Servo_timer_pause.Interval = 300
            Servo_timer_pause.Enabled = True
            AddHandler Servo_timer_pause.Elapsed, AddressOf ServoPause
        End If
        ''Servo Turn structuur
        If Servo_modus = True Then
            Servo_timer_turn.Interval = 300
            Servo_timer_turn.Enabled = True
            AddHandler Servo_timer_turn.Elapsed, AddressOf ServoTurn
        End If
    End Sub

    Sub ServoPause()
        TrySetTarget(0, 0)
        ''Wissel voor if-statement in Sub Timer
        If Servo_modus = False Then
            Servo_modus = True
            Servo_timer_pause.Enabled = False
        End If
    End Sub
    Sub ServoTurn()
        TrySetTarget(0, 4000)
        ''Wissel voor if-statement in Sub Timer
        If Servo_modus = True Then
            Servo_modus = False
            Servo_timer_turn.Enabled = False
        End If
    End Sub

    Private Sub StopTimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopTimer.Click
        Servo_timer_pause.Enabled = False
        Servo_timer_turn.Enabled = False
    End Sub

    Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
    End Sub

    Private Sub Status_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Status.Click
        Status.Text = Servo_modus
    End Sub
    Sub TrySetTarget(ByVal channel As Byte, ByVal target As UInt16)
            Using device As Usc = connectToDevice() ' Find a device and temporarily connect.
                device.setTarget(channel, target)
                ' device.Dispose() is called automatically when the "Using" block ends,
                ' allowing other functions and processes to use the device.
            End Using
        Catch exception As Exception  ' Handle exceptions by displaying them to the user.
        End Try
    End Sub
    Function connectToDevice() As Usc
        ' Get a list of all connected devices of this type.
        Dim connectedDevices As List(Of DeviceListItem) = Usc.getConnectedDevices()

        For Each dli As DeviceListItem In connectedDevices
            ' If you have multiple devices connected and want to select a particular
            ' device by serial number, you could simply add some code like this:
            '    If dli.serialNumber <> "00012345" Then
            '        Continue For
            '    End If
            Dim device As Usc = New Usc(dli)  ' Connect to the device.
            Return device                     ' Return the device.

        Throw New Exception("Could not find device.  Make sure it is plugged in to " & _
            "USB and check your Device Manager.")

    End Function
    Sub displayException(ByVal exception As Exception)

        Dim stringBuilder As StringBuilder = New StringBuilder()
            stringBuilder.Append(exception.Message & "  ")
            If TypeOf exception Is Win32Exception Then
                Dim win32Exception As Win32Exception = DirectCast(exception, Win32Exception)
                stringBuilder.Append("Error code 0x" + win32Exception.NativeErrorCode.ToString("x") + ".  ")
            End If
            exception = exception.InnerException
        Loop Until (exception Is Nothing)
        MessageBox.Show(stringBuilder.ToString(), Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub
End Class

Greeting Tom

Didnt mention: im Using XP programming with Visual Basic 2010 Express.

I suggest following the advice that I gave you in this post:

If there is something unclear about that advice, let me know.


Thanks, you’re advice fixed the acces denied problem. I hope System.Windows.Forms.Timer will hold through the whole application. When using this class and the intervals set to 100 ms the turning part lastst longer after a few itterations.

This is a problem because there is an flash animation linked to the ServoTurn part. The .swf has 20 frames per second, the interval of a third time will be (Servo_timer_pause.Interval / 20 frames). This timer will send gotoandstop(f) or nextFrame(f) to the .swf movie. I dont know the precise value of Servo_timer_pause.Interval, but i think those intervals might be to short for the current class.

Will it be safe to use System.Timer for this purpose anyway. Any ideas?

Thanks, Tom

This is starting to go beyond what we can support, but you could use System.Timers.Timer as long as you use a lock to ensure that two different threads don’t call TrySetTarget at once. See this page for an explanation and some VB .NET code examples: