Menu

Pic12f629 Help with servo

Help
2022-10-01
2022-10-13
  • Brian Williams

    Brian Williams - 2022-10-01

    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.

              #chip 12F629, 4
     #option explicit
    
    
    ; ----- Variables
      Dim PulseTime As byte
    
    
    ; ----- Main body of program commences here.
      Do Forever
    
                'Rotate servo
                '(Gradually change pulse length from 650 to 2250 us)
    
                PulseTime = 50
    
                repeat 200
    
                          'Send the right length pulse
                          PulseOut GPIO.2, PulseTime 10us
    
                          'After sending pulse, wait a bit before sending the next one
                          Wait 6 ms
    
                          'increment PulseTime
                          PulseTime = PulseTime + 1
    
                end repeat
    
                'Then rotate it back
                '(Gradually change pulse length from 2250 to 650 us)
                repeat 200
    
                          'Send the right length pulse
                          PulseOut GPIO.2, PulseTime 10us
    
                          'After sending pulse, wait a bit before sending the next one
                          Wait 6 ms
    
                          'decrement PulseTime
                          PulseTime = PulseTime - 1
    
                end repeat
      Loop
    
      end
    
     

    Last edit: Anobium 2022-10-02
  • Brian Williams

    Brian Williams - 2022-10-01

    Thank you in advance. I appreciate any help here.

     
  • mmotte

    mmotte - 2022-10-01

    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

     
  • Brian Williams

    Brian Williams - 2022-10-02

    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.

     
  • stan cartwright

    stan cartwright - 2022-10-02

    Mike, I send the servo position using a 50Hz interrupt in case they are loaded and would slip.

     
  • William Roth

    William Roth - 2022-10-13

    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.

     

Log in to post a comment.