Would someone be willing to explain the following code to me. I am having a hard time understanding how timing gets the servo to move into different positions? My servos are 500~2500usec. I don't know that that means. I just want to create a simple sweep then stop at center program for the 12f629. I may be just being dumb here but I just don't get how this works. Here is the code I have that seems to work. It's from the demo but I changed the PulseTime to 50 and the repeats to 200, and it seems to work.
#chip12F629, 4
#optionexplicit; ----- VariablesDimPulseTimeAsbyte; ----- Main body of program commences here.DoForever'Rotate servo'(Gradually change pulse length from 650 to 2250 us)PulseTime=50repeat200'Send the right length pulsePulseOutGPIO.2, PulseTime10us'After sending pulse, wait a bit before sending the next oneWait6ms'increment PulseTimePulseTime=PulseTime+1endrepeat'Then rotate it back'(Gradually change pulse length from 2250 to 650 us)repeat200'Send the right length pulsePulseOutGPIO.2, PulseTime10us'After sending pulse, wait a bit before sending the next oneWait6ms'decrement PulseTimePulseTime=PulseTime-1endrepeatLoopend
Last edit: Anobium 2022-10-02
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Brian,
I am not sure what you are not understanding because you have said all that is important.
"My servos are 500~2500usec"
To send the servo to the most ccw position you would send it 500 usec pulses
Tosend it to the middle position you would send it 1500 us pulses
to send it to max position send it 2500 us pulses.
Some people say you have to continue sending pulses to stay in your set position But I have found you only need to send pulses of your set size until the servo settles in that position and then you can quit sending until you want another position.
Maybe the "10 us" wait in the pulse command is giving you problem.
So you start at "PulseTime" variable loaded with 50 which gives the first time 50 10us = 500 us , just what you want for a full ccw rotation.
The repeat keeps adding 1 to the pulsetime variable so 51 * 10us = 510 us
52 10us = 520 us
every repeat adds 1 and the pulse is sent and rotates the servo a little more cw.
by the 200 th repeat you are at 250 * 10us = 2500 us which is the max rotationfor you servo
The second repeat subtracts 1 and does the opposite.
It cranks the servo from your max of 2500 to the beginning value of 500 us
Have fun
Mike
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Some people say you have to continue sending pulses to stay in your set position But I have found you only need to send pulses of your set size until the servo settles in that position and then you can quit sending until you want another position.
There is no active holding force when the PWM is turned off ( no pulses). The only thing holding the servo position is the resistance of the gear box.
This may be ok in some applications, but not where there is a considerable opposing force acting against the servo.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Would someone be willing to explain the following code to me. I am having a hard time understanding how timing gets the servo to move into different positions? My servos are 500~2500usec. I don't know that that means. I just want to create a simple sweep then stop at center program for the 12f629. I may be just being dumb here but I just don't get how this works. Here is the code I have that seems to work. It's from the demo but I changed the PulseTime to 50 and the repeats to 200, and it seems to work.
Last edit: Anobium 2022-10-02
Thank you in advance. I appreciate any help here.
Brian,
I am not sure what you are not understanding because you have said all that is important.
"My servos are 500~2500usec"
To send the servo to the most ccw position you would send it 500 usec pulses
Tosend it to the middle position you would send it 1500 us pulses
to send it to max position send it 2500 us pulses.
Some people say you have to continue sending pulses to stay in your set position But I have found you only need to send pulses of your set size until the servo settles in that position and then you can quit sending until you want another position.
Maybe the "10 us" wait in the pulse command is giving you problem.
So you start at "PulseTime" variable loaded with 50 which gives the first time 50 10us = 500 us , just what you want for a full ccw rotation.
The repeat keeps adding 1 to the pulsetime variable so 51 * 10us = 510 us
52 10us = 520 us
every repeat adds 1 and the pulse is sent and rotates the servo a little more cw.
by the 200 th repeat you are at 250 * 10us = 2500 us which is the max rotationfor you servo
The second repeat subtracts 1 and does the opposite.
It cranks the servo from your max of 2500 to the beginning value of 500 us
Have fun
Mike
mmotte, that was exactly what I needed. My brain just did not click with 10 us calculations. That simple break down makes it very clear. Thank you.
Mike, I send the servo position using a 50Hz interrupt in case they are loaded and would slip.
There is no active holding force when the PWM is turned off ( no pulses). The only thing holding the servo position is the resistance of the gear box.
This may be ok in some applications, but not where there is a considerable opposing force acting against the servo.