Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Servo control in VB.NET


  • Please log in to reply
5 replies to this topic

#1 aerialtarget

aerialtarget

    New Member

  • Members
  • Pip
  • 1 posts

Posted 10 September 2011 - 01:40 PM

Hi all, First post. I just got going with my netduino in VB.NET. Does anyone have a simple servo control example? Thankx

#2 Moskus

Moskus

    Advanced Member

  • Members
  • PipPipPip
  • 132 posts
  • LocationNorway

Posted 11 September 2011 - 01:33 PM

I'm not aware of one, but 1) I'll build one myself in a couple of weeks time and 2) almost every C# example I've seen can be easily converted to VB.net. :)

#3 Jon M

Jon M

    New Member

  • Members
  • Pip
  • 6 posts
  • LocationNorwood Young America, MN

Posted 11 September 2011 - 02:41 PM

Hi all,

First post.
I just got going with my netduino in VB.NET.

Does anyone have a simple servo control example?

Thankx

Try this

Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.Netduino

Module Module1

    Sub Main()
        Dim MoveServo As PWM = New PWM(Pins.GPIO_PIN_D5)
        Dim led As OutputPort = New OutputPort(Pins.ONBOARD_LED, False)

        Do
            MoveServo.SetPulse(20000, 2250)
            led.Write(True)
            Thread.Sleep(1000)
            MoveServo.SetPulse(20000, 500)
            led.Write(False)
            Thread.Sleep(1000)
        Loop

    End Sub

End Module


#4 fileark

fileark

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationNM

Posted 08 May 2012 - 05:43 PM

Here, I converted Chris Seto's Netduino servo library from C# to VB, not just for you, it was primarily for selfish reasons but hope this is helpful to a few people.
The original can be found here: Netduino Servo Class
It probably needs to be optimized and tweaked, I will be working with the code for a while so I'll update if I make any fixes.



Start by creating a new class in your VB project called "Servo_API"
'                                                                                       '
' * Servo NETMF Driver                                                                  '
' *      Coded by Chris Seto August 2010                                                '
' *      <chris@chrisseto.com>                                                          '
' *                                                                                     '
' * Use this code for whatveer you want. Modify it, redistribute it, I don't care.      
' * I do ask that you please keep this header intact, however.                          '
' * If you modfy the driver, please include your contribution below:                    '
' *                                                                                     '
' * Chris Seto: Inital release (1.0)                                                    '
' * Chris Seto: Netduino port (1.0 -> Netduino branch)                                  '
' * Chris Seto: bool pin state fix (1.1 -> Netduino branch)                             '
' * Fileark: 2012-05-08 Converted to VB (http://filear.com)                             '
' *                                                                                     '
' *                                                                                     '


Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware

Namespace Servo_API
    Public Class Servo
        Implements IDisposable

        ' PWM handle'
        Private servo As PWM


        ' Timings range'
        Private range As Long() = New Long(1) {}


        ' Set servo inversion'
        Public inverted As Boolean = False

        ' Create the PWM pin, set it low and configure timings'
        Public Sub New(pin As Cpu.Pin)

            ' Init the PWM pin'

            servo = New PWM(DirectCast(pin, Cpu.Pin))

            servo.SetDutyCycle(0)

            ' Typical settings'
            range(0) = 800
            range(1) = 3200
        End Sub

        Public Sub Dispose() Implements IDisposable.Dispose
            disengage()
            servo.Dispose()
        End Sub


        ' Allow the user to set cutom timings'
        Public Sub setRange(fullLeft As Long, fullRight As Long)
            range(1) = fullLeft
            range(0) = fullRight
        End Sub


        ' Disengage the servo. '
        ' The servo motor will stop trying to maintain an angle'
        Public Sub disengage()
            servo.SetDutyCycle(0)
        End Sub

        ' Set the servo degree'
        Public WriteOnly Property Degree() As Double
            Set(value As Double)
                ' Range checks'
                If value > 180 Then
                    value = 180
                End If

                If value < 0 Then
                    value = 0
                End If

                ' Are we inverted?'
                If inverted Then
                    value = 180 - value
                End If

                ' Set the pulse'
                servo.SetPulse(20000, CUInt(map(CLng(Math.Truncate(value)), 0, 180, range(0), range(1))))
            End Set
        End Property

        ' Used internally to map a value of one scale to another'
        Private Function map(x As Long, in_min As Long, in_max As Long, out_min As Long, out_max As Long) As Long
            Return (x - in_min) * (out_max - out_min) \ (in_max - in_min) + out_min
        End Function
    End Class
End Namespace


Then your main module should look something like this.
Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.Netduino


Module Module1
    Sub Main()
        Dim servo As New Servo_API.Servo(Pins.GPIO_PIN_D9)
        Dim i As Integer = 0

        Do While True

            Do While i < 100
                'Loop to fade from off to full on'
                servo.Degree = i
                'wait for 15 milliseconds'
                Thread.Sleep(10)
                i = i + 1
            Loop

            i = 100
            Do While i > 0
                'Loop to fade from full on to off'
                servo.Degree = i
                'wait for 15 milliseconds'
                Thread.Sleep(10)
                i = i - 1
            Loop

        Loop
    End Sub
End Module


#5 Moskus

Moskus

    Advanced Member

  • Members
  • PipPipPip
  • 132 posts
  • LocationNorway

Posted 11 May 2012 - 11:27 AM

Great! I'll try that. :)

#6 EndlessHunt

EndlessHunt

    New Member

  • Members
  • Pip
  • 5 posts
  • LocationPretoria, South Africa

Posted 07 September 2013 - 08:23 AM

Hi

 

Although I just started playing with the NEtDuino Plus 2 I do have a few years of .NET VB under the belt.

 

I'm trying to get the Servo-API code to work on a ND+2

 

I'm[font="arial, helvetica, sans-serif;"] using VS[/font] 2010 Premium SP1

Microsoft .NET Micro Framework SDK v4.2 Version 4.2.0.0 and

Netduino SDK v4.2.2.0 (64-bit)

 

On the line: servo = New PWM(DirectCast(pin, Cpu.Pin))

[font="arial, helvetica, sans-serif;"]I get the message: "[/font][font="arial, helvetica, sans-serif;"]Overload resolution failed because no accessible 'New" accepts this number of statements"[/font]

 

[font="arial, helvetica, sans-serif;"]In the first overload it expects: servo = New PWM(Channel,Frequency,DutyCycle,Invert)[/font]

The second Overload expects even more parameters

 

[font="arial, helvetica, sans-serif;"]Then on the line: servo.SetDutyCycle(0)[/font]

[font="arial, helvetica, sans-serif;"][font="arial, helvetica, sans-serif;"]I get the message: "[/font][/font]'SetDutyCycle' is not a member of 'Microsoft.SPOT.Hardware.PWM'"

The intelisense only shows 'DutyCycle' and it have to be use as: servo.DutyCycle = 0

 

[font="arial, helvetica, sans-serif;"] [/font]

[font="arial, helvetica, sans-serif;"]and the same with: servo.SetPulse(20000, CUInt(map(CLng(Math.Truncate(value)), 0, 180, range(0), range(1))))[/font]

[font="arial, helvetica, sans-serif;"][font="arial, helvetica, sans-serif;"]I get the message: "[/font][/font]'SetPulse' is not a member of 'Microsoft.SPOT.Hardware.PWM'"

there is no equivalent shown for 'SetPulse'

 

Did something in the PWM libraries change? How do we implement servo control now? Or am I missing something?

 

[font="arial, helvetica, sans-serif;"]Cheers[/font]

[font="arial, helvetica, sans-serif;"]Deon[/font]






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.