Menu

servo control pwm

Help
2009-01-17
2013-05-30
  • Nobody/Anonymous

    i have :
    #chip 12F675, 4 'mhz
    #config OSC=INTOSC, MCLRE=off, WDT=off

    #define pwm_out1 GPIO.1
    #define pwm_delay 20 ms

    dir gpio.0 in
    dir pwm_out1 out

    Main:
    do
    pwmout 1, readad(an0), 1

    loop

    I want it to send the pulse 50x/second continuously, but i get an output that turns  on and off once a second for a good portion of a second, why?

    also is it possible to have two outputs going at the same time, instead of them alternating?

     
    • Nobody/Anonymous

      Wouldn't use the softpwm sub for servo control.  But it wouldn't hurt to look at its structure for ideas;  It can be found in the include/lowlevel/stdbasic.h file.

      Very loose code (not tested) for say a two servo driven by pots.  Starting servo's at the same time would look something like:

      #define Servo1 GPIO.4
      #define Servo2 GPIO.5
      dir Servo1 out
      dir Servo2 out

      Main:
      Servo1 = readAD(ano)
      Servo2 = readAD(an1)
      If Servo1 >= 200 Then Servo1 = 200  ;limit travel to 2ms
      If Servo2 >= 200 Then Servo2 = 200
      If Servo1 <= 100 Then Servo1 = 100  ;limit travel to 1ms
      If Servo2 <= 100 Then Servo2 = 100

      For Speed = 100 to 200  ;approx. 10us loop? for 1ms to 2ms range
        If Servo1 > Speed then set Servo1 On
        If Servo2 > Speed then set Servo2 Off  ;invert direction for robot
        If Servo1 <= Speed then set Servo1 Off
        If Servo2 <= Speed then set Servo2 On  ;invert direction for robot
        wait 6 us  ;will have to play with this, don't use variable here
      Next
      wait 18 ms  ;refresh servo's every 20ms (loop?+delay)
      goto Main

      Kent

      test code with quotes
      "If code quotes work then
          great
      end if"

       

       
    • kent_twt4

      kent_twt4 - 2009-01-17

      Well, after a cup of coffee this morning, was thinking about the amount of time the For Speed.....Loop actually took.  Hopefully you didn't grind any gears, for being overdriven.  Also had Servo1 & 2 defined as a pin and a variable in previous code...nice.

      Put the assembly file in MPLAB SIM and found the Speed Loop took 22us at 4mhz osc...oops!    So kind of lousy resolution at 4mhz osc (45 steps).  A round trip thru the whole of the (new) Speed Loop was 1.01 ms.  A 12f683 has an 8mhz internal osc., so twice the resolution.   Code still not tested on servo's.

      'GCBASIC program to move servo's with potentiometers
      #chip 12F675, 4 'mhz
      #config OSC=INTOSC, MCLRE=on, WDT=off

      #define Servo1 GPIO.4
      #define Servo2 GPIO.5
      dir Servo1 out
      dir Servo2 out
      dir GPIO.0 in
      dir GPIO.1 in

      Main:
      Speed1 = readAD(ano)
      Speed2 = readAD(an1)
      If Speed1 >= 91 Then Speed1 = 91 ;limit travel to 2ms
      If Speed2 >= 91 Then Speed2 = 91
      If Speed1 <= 46 Then Speed1 = 46 ;limit travel to 1ms
      If Speed2 <= 46 Then Speed2 = 46

      For Speed = 46 to 91 ;approx. 22us loop @4mhz Osc, for a 1ms to 2ms range
      If Speed1 > Speed then set Servo1 On
      If Speed2 > Speed then set Servo2 Off ;invert direction
      If Speed1 <= Speed then set Servo1 Off
      If Speed2 <= Speed then set Servo2 On ;invert direction
      Next
      wait 18 ms ;refresh servo's approx. every 20ms (loop?+delay)
      goto Main

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.