Asked — Edited

VB Tutorial #2 - Servos

Visual Basic Tutorial 2 Working With servo Motors 2.1. One of the most basic things a robot does is move; either itself or an arm, leg, or object. This is normally accomplished using servo motors. Getting a servo motor to move is actually quite straight forward.

2.1.1. We will put a control on the form that will handle a servo motor that is attached to the EZ module. Open the Toolbox and display the All Window Forms tab. Double click on the TrackBar and a TrackBar control will be put on your form. Resize it horizontally so that it is about 2 inches wide. It can be adjusted to a different size later on if you wish.

2.1.2. Now add a Label Control to the form and change its Text Property to servo Control. Position this label to near the TrackBar.

2.1.3. Now we want to get the TrackBar to do something. Click on the form. Go to the Property window and click on the Lightning Bar which will display the events that can be assigned to this control. Click on Load and now a bunch of code is added to your program which should look like this: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

VB Express tends to be very verbose. Sometimes, the information in the brackets can be deleted without affecting the program. This is one such case. Delete everything inside the brackets to get:

Private Sub Form1_Load() Handles MyBase.Load

2.1.4 On a new line right after this code snippet, start typing Track and the program should show a dropdown box with options. The following code will set the minimum value that the TrackBar will have: NOTE: EZ_B Servo.Servo_Min, Max and Center are system variables from the DLL.

TrackBar1.Minimum = EZ_B.Servo.Servo_Min

2.1.4.1. Now we will set the maximum value of the TrackBar.

TrackBar1.Maximum = EZ_B.Servo.Servo_Max

2.1.4.2. The next step is to position the TrackBar to centre.

TrackBar1.Value = EZ_B.Servo.Servo_Center

2.1.4.3. The parameters for the TrackBar are now set. Next we get the TrackBar to talk to the EZ module. Double click on the TrackBar control and the code window pops up with a code snippet.

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll

End Sub

Again, delete everything inside the brackets to shorten the code.

Private Sub TrackBar1_Scroll() Handles TrackBar1.Scroll

This code does something based on the scrolling of the TrackBar. What we want the code to do is move a sensor attached RB0 on the EZ module.

EzB_Connect1.EZB.Servo.SetServoPosition(EZ_B.Servo.ServoPortEnum.RB0, TrackBar1.Value)

This sets the servo to the value of the TrackBar which we initially set to the center. If you run the project and move the scroll bar, the servo will rotate (hopefully). Do NOT try to scroll past the internal stops of the servo motor as you could damage it.

2.15. Now let's add some information to the form. Add 3 Label controls and align them over the TrackBar. They will show the value of things as the TrackBar is changed. I decided to put the code for the controls in the first section of codes which loads with the form at the start of the program.

Public Class Form1

Private Sub Form1_Load() Handles MyBase.Load
    TrackBar1.Minimum = EZ_B.Servo.SERVO_MIN ' servo to system minimum
    TrackBar1.Maximum = EZ_B.Servo.SERVO_MAX  ' servo to servo maximum
    TrackBar1.Value = EZ_B.Servo.SERVO_CENTER ' start TrackBar to servo centre
    Label2.Text = EZ_B.Servo.SERVO_MIN * 5
    Label3.Text = EZ_B.Servo.SERVO_MAX * 5 it seems that predefined SERVO_MAX 						           defaults to 36, this scales it to 180.
    Label4.Text = 90  center label
End Sub

Private Sub TrackBar1_Scroll() Handles TrackBar1.Scroll
    EzB_Connect1.EZB.Servo.SetServoPosition(EZ_B.Servo.ServoPortEnum.RB0, TrackBar1.Value)

End Sub

End Class

2.2 Something should now (hopefully) be apparent. If you want to send information to the EZ module you use the EzB_Connect1 expression. To get information from a sensor attached to the EZ module, you use the EZ_B expression.

2.3. Think of some other ways you can control the servos. Maybe use Select Case statements!

The next tutorial will deal with LEDs.


ARC Pro

Upgrade to ARC Pro

Unleash your robot's full potential with the cutting-edge features and intuitive programming offered by Synthiam ARC Pro.

PRO
Synthiam
#1  

again, more awesome work:)

Belgium
#2  

Hi DJ,

I need a hand with some VB code. I'd like to read a (micro)switch when
an impact occured, so the robot goes into reverse. So the idea is to use a variable like a integer or a boolean. I tried a few things, but when I push the switch the BT connection with the EZ-B is lost. I need to restart the program. Do I connect the 5V over the switch to GND directly or is a resistor necessary ? This impact sensor/switch is in combination with IR and ultrasonic. Any ideas ? Help is much appreciated.

User-inserted image

Best regards,

Philip

PRO
Synthiam
#3  

Ah, you're shorting out the EZ-B. There are three wires on each Digital I/O. There is GND, +5 and Signal. Your switch would connect between +5 and Signal.

Belgium
#4  

I thought so too, but that didn't work. I connected Signal with +5 and also tried Signal with GND. The result is the same. I'm puzzled.

PRO
Synthiam
#5  

Where are you getting the +5 from? You must get it from the Digital I/O. You can not take it from the battery directly

Australia
#6  

hi DJ Fixed my problem, also finish VB Tutorial #2 - Servos, with no problems:) this is fun:)

#7  

baloo

Do you have any luck using EZ-SDK with Visual Studio (VB) Express 2010 ? I have no problem with Visual Studio Express 2008, but cannot get it to work with 2010.

Of course my best solution is to stick with 2008, but I am curious if it works with 2010.

Australia
#8  

hi pashley i could not get vb express 10 to run ok it keep locking up

Canada
#9  

Try changing your code from: 'EzB_Connect1.EZB.Servo.SetServoPosition(EZ_B.Servo.ServoPortEnum.RB0, TrackBar1.Value) TO: EzB_Connect1.EZB.Servo.SetServoPosition(EZ_B.Servo.ServoPortEnum.D13, TrackBar1.Value) If your servo is connected to D13...

This worked for me in VB2012