Simple Servo Position Control Program - Is this right?
I have a pretty simple process that I need carried out via a 16F88 and a servo.
I have an arm that has a heating element on the tip of it. I need this heating element to be lowered onto a bed of grass for one second, then lifted out of the grass. I am testing the flammability of grass within a climate controlled wind tunnel. I want this to be triggered from a simple push of a NO button, then have it carry out the whole process from there. Problem is that I have not messed with a servo motor before and I am not sure if I am controlled it in the correct way. The button is on A0 and the servo control output would be on A1. What I have for code is
Code:
'Set internal clock settings for 16F88
Define OSC 8
OSCCON.4=1
OSCCON.5=1
OSCCON.6=1
'Turn off A/D conversion
ANSEL=0
'Define variables and pins
I VAR BYTE
BUTTON VAR PORTA.0
SERVO VAR PORTA.1
'Continuous loop until triggered by ignition button
buttonloop:
If (BUTTON=0) Then goto buttonloop
Pause 10
If (BUTTON=1) Then goto positionloop
goto button loop
'Servo position control loop
positionloop:
Pulsout PORTA.1, 100 'pulse to servo for a length of 1 ms to rotate servo CCW certain number of degrees
For I=1 TO 100 'pulse to servo every 10 ms for a total of 1 second
Pause 1000
NEXT
Pulsout PORTA.1, 150 'pulse to servo for a length of 1.5 ms to move to "zeroed" position
goto buttonloop
End 'end of program never reached
Is this the correct way of doing it? Any help/suggestions would be great!
-Marcus
Re: Simple Servo Position Control Program - Is this right?
I've made some code that might help you.
It was for a high voltage SPDT servo switch for my research.
It sends a 1 second burst signal to the servo and then release it.
Usually thats more than enough time for the servo to react
Code:
'//ON INITIAL CONFIG
A VAR BYTE 'counter
SerPos var word 'servo position variable
sers var PORTB.0 'servo signal pin out
'//ON MAIN PROGRAM ROUTINE
'-Set servo position
serpos = 1300 'uS pulse width: 0º -> 1000uS / 180º -> 2000uS
gosub servout 'send pulse for 1 second
'//ON SUBROUTINES
servOUT: 'sends servo a PWM position signal for 1 sec.
for a = 0 to 59 '60hz
Sers = 1
pauseus serpos
Sers = 0
PAUSEUS 2000-serpos '2ms TOTAL
pauseus 14667 '2+14.667ms = 16.667ms -> 60hz -> 1seg total refresh time
next a
return
Re: Simple Servo Position Control Program - Is this right?
Not to bring up a dead subject, and I am not sure if anybody is going to have any interest in helping out. But if I am using a code similar to Alain's that he posted above ^^^, how can I slow down the movement of the servo? Basically, the servo goes straight from one point to another. I want to add something that will make it so that rather then jumping from position one to position two and back again that it takes, say 10 seconds, to gradually move from position one to position two. I have an idea in mind to basically have it move from say position 200 to 300 by 1 every .1 seconds so that after 10 seconds it has reached the second position. I would think a counting function or something would work so that it would go
Code:
IF I >= 50 THEN
position=200
pause 100
position=201
pause 100
position=202
pause 100
...
ELSE
POSITION = 200
ENDIF
And if that is what it takes that I need to type out the whole process count down then so be it. But I would think there would be an easier option..
Application: My problem right now is that there is a 4 inch piece of copper rod on the servo with a ceramic insulator attached to the end of that copper arm. So whenever it moves from position one to position two, the momentum of the mass of the ceramic insulator is causing the servo to overshoot its programmed position. Maybe its a servo issue, but I thought I would start with this.
Any thoughts or help?
Re: Simple Servo Position Control Program - Is this right?
Simple solution:
Code:
For position=start to finish
pause 100
gosub Servo_Control
Next position
Ioannis
Re: Simple Servo Position Control Program - Is this right?
Ioannis, would you mind explaining that code a little bit to me? I'm a bit of a newbie and haven't dealt with a lot of different code. Is it as simple as copy and pasting in ....
Code:
For position=start to finish
pause 100
gosub Servo_Control
Next position
Or are there values for start and finish? and is
Code:
gosub Servo_Control
another program loop? If so what for? Sorry if this is a simple code and I am being an idiot.
Thanks.
-Marcus