Control Pan/Tilt using Visual Studio 2013

Hi, I have the Maestro 6-Channel with two Hitec servos attached.

I’m new to VB programming. I’ve been able to load and run the “MaestroEasyExample”. and it works.

However, I would like to modify this example to using either a button-down click event or use a slider to move the servos and have them stay there when I have them in the orientation I like.

I plan to only use this small program to operate the Maestro connected to the PC from my desktop.

I’ve sorted through many pages in this forum, and found next to nothing regarding programming examples using Visual Studio 2013 save the above mentioned “MaestroEasyExample”

It should be simple, and I’m sure someone must’ve already done something very similar, any help would be appreciated.

Hello.

You would probably need to add a button or track bar component to the main form and call TrySetTarget in the event handler subroutine. I added a track bar event to the MaestroEasyExampleVb example using Microsoft Visual Studio and tested the code with a Maestro and a servo. It worked. Here is the event handler code:

Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
    TrySetTarget(0, TrackBar1.Value * 4)
End Sub

I set the minimum and maximum properties of the track bar to 1000 (1ms) and 2000 (2ms), but you could set them to 4000 and 8000 if you do not want to perform calculations in the code.

By the way, in case you did not notice, you might consider using the Maestro Control Center. You can download the Maestro Servo Controller under the “Resources” tab on any of the Maestro product pages. The program contains a slider control feature for each channel, which can be found under the “Status” tab.

- Amanda

Thank you Amanda!

I think that is what I needed (I’ll try it out), I did not see the “TrackBar” control in Visual Studio 2013 until you mentioned it, But I did find an “HScrollBar” and an “VScrollBar” in the Toolbox under “all Windows Forms”.

The bit of code you provided in showing me how to employ it should give me the nudge I need to make things happen!

I’ll cross my fingers, and giver it a whirl!

Well, I got that to work, but what I would like to do is have the servo centered and move it left or right.

I can do it in the control center, but I’m at a loss as to how I can transfer that motion into a simple program that would allow me to do it without the control center?

I do not plan to send the script to the Maestro and have it run at startup or anything like that. I just want to control it from the desktop without the control center.

I would just like to have a small “.exe” like desktop app to do that. This is why I chose Visual Studio.

If I could figure out how to put the slider in the center, and slide it left or right from the app to move the servo, that would accomplish what I am after.

Any help greatly appreciated!

Eureka!

Not long after my last post, I tried a few things and got it to work. (I read Amanda’s reply a bit closer)!

Like Amanda said, I had to go to the Properties of each control and set the minimum and Maximum values, then set the “Value” propertie to 1500 which centered the control on startup.

Here is the Modified example code “a bit sloppy, I’m a newbie”

'  MaestroEasyExampleVb:
'    Simple example GUI for the Maestro USB Servo Controller, written in
'    Visual Basic .NET.
'    
'    Features:
'       Temporary native USB connection using Usc class.
'       Button for disabling channel 0.
'       Button for setting target of channel 0 to 1000 us.
'       Button for setting target of channel 0 to 2000 us.
' 
'  NOTE: Channel 0 should be configured as a servo channel for this program
'  to work.  You must also connect USB and servo power, and connect a servo
'  to channel 0.  If this program does not work, use the Maestro Control
'  Center to check what errors are occurring.

Imports Pololu.UsbWrapper
Imports Pololu.Usc

Imports System
Imports System.Text
Imports System.ComponentModel

Public Class MainWindow
    ''' <summary>
    ''' This subroutine runs when the user clicks the Target=1000us button.
    ''' </summary>
    ' Sub Button1000_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' TrySetTarget(1, 1000 * 4) ' Set the target of channel 0 to 1000 microseconds.
    'End Sub

    ''' <summary>
    ''' This subroutine runs when the user clicks the Target=2000us button.
    ''' </summary>
    ' Sub Button2000_Click(ByVal sender As Object, ByVal e As EventArgs)
    ' TrySetTarget(1, 2000 * 4) ' Set the target of channel 0 to 2000 microseconds.
    ' End Sub

    ''' <summary>
    ''' This function runs when the user clicks the Disable Servos button.
    ''' </summary>
    Sub ButtonDisable_Click(ByVal sender As Object, ByVal e As EventArgs)
        ' Set target of channels  1,2 to 0.  This tells the Maestro to stop
        ' transmitting pulses on that channel.  Any servo connected to it
        ' should stop trying to maintain its position.
        TrySetTarget(1, 0)
        TrySetTarget(2, 0)
    End Sub

    ''' <summary>
    ''' Attempts to set the target of 
    ''' </summary>
    ''' <param name="channel">Channel number from 0 to 23.</param>
    ''' <param name="target">
    '''   Target, in units of quarter microseconds.  For typical servos,
    '''   6000 is neutral and the acceptable range is 4000-8000.
    ''' </param>
    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

    ''' <summary>
    ''' Connects to a Maestro using native USB and returns the Usc object
    ''' representing that connection.  When you are done with the
    ''' connection, you should close it using the Dispose() method so that
    ''' other processes or functions can connect to the device later.  The
    ''' "Using" statement can do this automatically for you.
    ''' </summary>
    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

    ''' <summary>
    ''' Displays an exception (error) to the user by popping up a message box.
    ''' </summary>
    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

    Private Sub MainWindow_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub VScrollBar1_Scroll(sender As Object, e As ScrollEventArgs) Handles VScrollBar1.Scroll
        TrySetTarget(2, VScrollBar1.Value * 4)
    End Sub

    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        TrySetTarget(1, TrackBar1.Value * 4)
    End Sub

End Class

Interesting: I’m using the Maestro for a similar project, but would like to have the ability to control the Maestro Control Center with a a wired XBOX 360 gamepad or alternatively a PS3 controller.

I want to make an RC controlled pan/tilt/zoom head that I constructed for a video camera (for live streaming) computer controlled: see video http://youtu.be/j04zkO31gjA
(since the video it has been slightly modified though: I replaced the tilt servo with rod by a continuous one with gears for more accuracy and a larger range; new video still to be posted)

The idea is to use the Maestro to make it manually controllable with a wired XBOX 360 gamepad (or alternatively a PS3 controller) with the extra possibility to store a number of camera positions under the buttons of the gamepad. For the latter I want to add two potentiometers for feedback of the horizontal/vertical camera positions that will be connected to the analog inputs of the Maestro (because for horizontal and vertical I use continous servos).

I think the basic functionality for this is already available in the Maestro Control Center, except for the possibility to control the sliders and buttons of the Control Center by the gamepad (although I haven’t tested this yet, maybe it is possible out-of-the-box?).

Do you know of any other projects that use a game controller to interface with the Maestro Control Center?

Hello.

The Maestro Control Center does not have a built-in feature that allows it to be controlled from a game controller. However, it is probably possible to control the Maestro with a game controller from your computer. Unfortunately, we do not have any example code for doing that, so you would probably have to write your own program to interpret the signals from your game controller and then send commands to the Maestro.

We do not know of any projects that involve using a game controller to control the Maestro Control Center, but one of our forum members seems to have gotten his Xbox 360 controller to communicate with his Maestro via serial commands sent from his computer. He posted some code you can look at in this forum thread.

- Amanda