Hello,
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
Timer(Servo_modus)
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
Timer(Servo_modus)
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
Timer(Servo_modus)
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)
Try
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.
displayException(exception)
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.
Next
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()
Do
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