PDA

View Full Version : Programming a Continious Rot Servo



Sirstrafe
04-23-2007, 12:00 PM
Hi Everyone, I have a Servo 1 -Motor controller & a HSR-1422 Continious Rot servo. I have made it work with some success but my code is clumky. I am able to zero out the controller to 117 degree's and it stops the rotation of the servo. I have added code to increase and decrease the position of the servo in order to make it rotate. But the code doesn't work the best, any suggests (Code Follows). I guess that does enyone have code they could share or again make suggestions, thanks ...

Public Class Form1
Dim WithEvents Servo1 As Phidgets.Servo
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

If e.KeyCode = Keys.Right Then
Button1.PerformClick()
End If
If e.KeyCode = Keys.Left Then
Button2.PerformClick()
End If
If e.KeyCode = Keys.Space Then
Button3.PerformClick()
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'To reduce code complexity we assume that there is one PhidgetServo
'attached to the PC before the program is run.
Servo1 = New Phidgets.Servo
'Defaults for text fields
TextBox1.Text = Nothing
TextBox2.Text = "Not Connected"
Servo1.open()
End Sub

Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub

Private Sub Servo1_Attach(ByVal sender As Object, ByVal e As
Phidgets.Events.AttachEventArgs) Handles Servo1.Attach
'When the Phidget servo attaches update the form text boxes
TextBox2.Text = "Connected"
TextBox3.Text = Servo1.servos.Count
TextBox4.Text = (Str(Servo1.SerialNumber))
End Sub

Private Sub Servo1_Detach(ByVal sender As Object, ByVal e As
Phidgets.Events.DetachEventArgs) Handles Servo1.Detach
'If the Phidget servo detaches close the form
Me.Close()
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'If the form closes and the Phidget servo is attached Close it.
If Servo1.Attached = True Then
Servo1.close()
End If
End Sub

Private Sub Servo1_Error(ByVal sender As Object, ByVal e As
Phidgets.Events.ErrorEventArgs) Handles Servo1.Error
'If the Phidget servo is not attached when the form opens show
'message box and close the form
MessageBox.Show(e.Description)
Me.Close()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Servo1.servos(0).Position = Servo1.servos(0).Position + 1
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Servo1.servos(0).Position = Servo1.servos(0).Position - 1
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Servo1.servos(0).Position = 118
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
TextBox1.Text = Servo1.servos(0).Position
End Sub
End Class

Alex
04-25-2007, 08:43 AM
I have made it work with some success but my code is clumky. I am able to zero out the controller to 117 degree's and it stops the rotation of the servo. I have added code to increase and decrease the position of the servo in order to make it rotate. But the code doesn't work the best, any suggests (Code Follows). I guess that does enyone have code they could share or again make suggestions

What do you mean by it doesn't work the best? What happens that you don't like, or would like to improve upon?

Sirstrafe
04-25-2007, 01:26 PM
The first problem I noticed is that to stop the servo once it is rotating is to set it's position to 117, which is the position of a standard servo not to rotate.

Is there code to have the servo to stop, instead of settings it's current positon back to 117 ?

Is there code to have it rotate instead of the position += 1 or position -= 1 ?

These are my main questions, the servo itself works good, I just want to clean up my code.

Alex
04-27-2007, 09:11 AM
Is there code to have the servo to stop, instead of settings it's current positon back to 117 ?

Nope, that's the way that you tell a continuous turn servo to stop in the Phidget world. You could always just call a method that you can call anytime you want the servo to stop.

ie.

public void StopServoRotation(Phidgets.PhidgetServo servo, int index)
{
servo.servos[index].motorPosition = 117;
}

or alternately, if you have the PhidgetServo object as a private member in a class, you could just:

public void StopServoRotation(int index)
{
servo.servos[index].motorPosition = 117;
}



Is there code to have it rotate instead of the position += 1 or position -= 1 ?

These are my main questions, the servo itself works good, I just want to clean up my code.


Same as above, you can use a method that tells the servo object to move to either the min clockwise/counterclockwise or the max clockwise/counterclockwise position.

These range numbers vary between servos, but the values in the following link are what we found to be the average on the servos we tested:

http://www.trossenrobotics.com/store/p/3296-HSR-1422CR-Continuous-Rotation-Standard-Servo.aspx