I have been using two maestro boards (12 and 24) to successfully control a number of servos around a fairly large model railway system - using VB.Net to construct an overall control package. I successfully modified the examples to do what I needed with servos but would now like to use some ports as input from either reed switches or possibly hall sensors.
I cannot see any example code (in either VB or C+) to read sensors - please can you tell me if there are any examples of such code
You can read the status of Maestro channels using getVariables(), which you can find the definition of on line 567 in Usc.cs. Our Visual Basic and C++ examples do not use getVariables(), but you can use MaestroAdvancedExample (which is in C#) as a reference.
I have tried this but if I use the following code I just get Index Out Of Bounds Of Array
Any thoughts
''' Attempts to get the maestro variables
Sub TryGetVars(ByVal channel As Byte, ByVal target As UInt16)
Dim st As Short()
Try
Using device As Usc = connectToDevice() ' Find a device and temporarily connect.
device.getVariables(st)
MessageBox.Show(st(0))
End Using
Catch exception As Exception ' Handle exceptions by displaying them to the user.
displayException(exception)
End Try
End Sub
Hi Brandon – Thanks for your help – I have now cracked it
For Your Info – VB.Net Code as follows gets the first 4 sets variables for Position, Target and Speed from the Polulu USC
This is working code in VB2019 and 2022 – you are welcome to store it for any future reference
The Button ref is a standard button in VB and the text refs are simple text boxes
Cheers
Andy Whorton
' Maestro GetVars in VB.Net:
Imports Pololu.UsbWrapper
Imports Pololu.Usc
Imports System
Imports System.Text
Imports System.ComponentModel
Public Class MainWindow
''' This subroutine runs when the user clicks the Get Vars Button
Sub Button1000_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1000.Click
GetVars()
End Sub
''' Attempts to get the maestro variables
Sub GetVars()
Dim st As ServoStatus()
Using device As Usc = connectToDevice() ' Find a device and temporarily connect.
device.getVariables(st)
pos0text.Text = st(0).position
Pos1Text.Text = st(1).position
pos2text.Text = st(2).position
pos3text.Text = st(3).position
target0text.Text = st(0).target
Target1Text.Text = st(1).target
Target2Text.Text = st(2).target
Target3Text.Text = st(3).target
Speed0text.Text = st(0).speed
Speed1Text.Text = st(1).speed
Speed2Text.Text = st(2).speed
Speed3Text.Text = st(3).speed
End Using
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
Dim device As Usc = New Usc(dli) ' Connect to the device.
DeviceSNText.Text = dli.serialNumber
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