Shop OBEX P1 Docs P2 Docs Learn Events
PWM Duty Cycle or Pulse measurement example spin code — Parallax Forums

PWM Duty Cycle or Pulse measurement example spin code

EdKirkEdKirk Posts: 27
edited 2006-06-05 22:09 in Propeller 1
I have a Propstick that is working fine.·

I have an RC controller receiver that delivers several differing pulse widths in a row; each width controls the speed of a·robot motor.·

I am seeking example PWM Duty Cycle. or Pulse measurement, spin code.

Although Chapter 4 of the Manual mentions these words, I find no examples there or elsewhere.

Who can help?

EdKirk


Post Edited (EdKirk) : 6/4/2006 1:27:49 AM GMT

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-06-04 17:22
    Actually its not too difficult assuming the pulse width is not too small, one way would be to do use waitpe/waitpne to detect the sought after pin(s) state record the cnt value into a variable, then do a waitpn/waitpne to detect the end of the sought after pin(s) state and record the cnt value again, subtract the first from the second and you will obtain the width of the pulse, do it again with the opposite phase of the cycle to calculate the duty cycle.

    A more elegant method of doing what you want (using a cog's counters) is outlined in Martin Hebel's BS2 function library availible in the Objects Library Download section of the Propeller website.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1+1=10
  • EdKirkEdKirk Posts: 27
    edited 2006-06-04 17:29
    Thanks Paul,

    I have tried to use waitpe/waitpne without success.

    I have been successful with

    PRI MeasureOneCycle
    ····· Repeat While Ina[noparse][[/noparse]8]
    ····· Repeat While Not InA[noparse][[/noparse]8]
    ····· Time1 := Cnt
    ····· Repeat While Ina[noparse][[/noparse]8]
    ····· Time2 := Cnt
    ····· Repeat While Not InA[noparse][[/noparse]8]
    ····· Time3 := Cnt
    ····· Highs := (Time2 - Time1)/80
    ····· Lows· := (Time3 - Time2)/80
    ····· Cycle := (Time3 - Time1)/80

    Giving answers in us on my monitor.

    But would like to see how to use waitpe?

    EdKirk
  • Martin HebelMartin Hebel Posts: 1,239
    edited 2006-06-04 17:35
    Here's the section of code Paul referred to using the Cog's counter in case you don't want to go digging.

    PUB PULSIN_Clk(Pin, State) : Duration 
    {{
      Reads duration of Pulse on pin defined for state, returns duration in 1/clkFreq increments - 12.5nS at 80MHz
      Note: Absence of pulse can cause cog lockup if watchdog is not used - See distributed example
        x := BS2.Pulsin_Clk(5,1)
        BS2.Debug_Dec(x)
    }}
    
      DIRA[noparse][[/noparse]pin]~
      ctra := 0
      if state == 1
        ctra := (%11010 << 26 ) | (%001 << 23) | (0 << 9) | (PIN) ' set up counter, A level count
      else
        ctra := (%10101 << 26 ) | (%001 << 23) | (0 << 9) | (PIN) ' set up counter, !A level count
      frqa := 1
      waitpne(State << pin, |< Pin, 0)                         ' Wait for opposite state ready
      phsa:=0                                                  ' Clear count
      waitpeq(State << pin, |< Pin, 0)                         ' wait for pulse
      waitpne(State << pin, |< Pin, 0)                         ' Wait for pulse to end
      Duration := phsa                                         ' Return duration as counts
      ctra :=0                                                 ' stop counter
      
    
    



    You can use the following to call the above and convert it to uS depending on your clock speed.
     Duration := PULSIN_Clk(Pin, State) / (clkfreq / 1_000_000)
    
    



    -Martin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Martin Hebel
    Southern Illinois University Carbondale - Electronic Systems Technologies

    Personal Links with plenty of BASIC Stamp info
    StampPlot - Graphical Data Acquisition and Control
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2006-06-04 18:12
    If you need to measure duty cycle continuously, you might use a routine ping-pongs back and forth between counter A and counter B. Counter A measures time high and counter B measures time low. The central snippet would look like this in pseudocode:

    set cnta to count high level on pin x, frqa=1, ctra="pos level detector mode %01000"
    set cntb to count low level on pin x, frqb=1, ctrb="neg level detector mode %01100"
    wait for low on pin x
    clear phsa   ' won't start incrementing until x goes high
    wait for high on pin x
    repeat
       wait for low on pin x
       save phsa to high duration variable
       clear phsa
       ' depending on the pulse lengths, there is time to do other tasks here
       wait for high on pin x
       save phsb to low duration variable
       clear phsb
       ' and other tasks here
    



    In its own cog, it could keep the variables updated.

    I'm not sure what you mean by "several differing pulse widths in a row; each width controls the speed of a robot motor. " Do you have several different channels to monitor for different motors, or one motor that receives a sequence of PWM commands, or ????

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • EdKirkEdKirk Posts: 27
    edited 2006-06-05 00:39
    Tracy Allen,

    And thank you for asking about my project.· I am using a Vex (Don't tell on me) robit kit and I have copied the Kiwi which I found others have built.· I and others had great difficulty controlling its movement.· Its frame is an equilateral triangle wth a·wheel at each corner; each wheel is an Omni Wheel which has rollers around the rim; each roller is parallel to the rim so that the wheel exerts no thrust along its own axis.· This remarkable structure can rotate about its own axis and it can move in the x-y plane at any compass point.· One friend of mine suggested that this is an ideal set of movements for a wheel chair.· Just imagine being able to scoot sideways along the kitchen counter!

    The Vex controller has a PIC controller which I might have reprogrammed, but I wanted to get familiar with my new PropKit.· The six channel Vex radio receiver delivers one sync pulse and six PWM pulses to the controller.· Each pulse can range from one to two msec long; there is a one msec pause between each and the cycle is repeated 50 times per sec.

    I am cutting that cable and inserting the PropKit to receive 4 channels from two joysticks and send three channels to the motors.

    Ed·Kirkham· ·edk@wi.rr.lcm
  • kerrywkerryw Posts: 61
    edited 2006-06-05 01:05
    Sounds like an interesting project Ed. I like the Vex stuff also, but apparently it hasn't sold well. Radio Shack looks like they are going to discontinue it as they have marked everything down 50%.

    I'd love to hear how your project turns out.

    Kerry
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2006-06-05 19:19
    Interesting about the equilateral triangle platform and wheel action. I'm still confused about the sequence of pulses that comes in from the Radio. Is the information encoded in the ratio of high time to low time, and how do you recognize the sync pulse?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • EdKirkEdKirk Posts: 27
    edited 2006-06-05 22:09
    Kerry,

    Thanks for your interest. I will let you know how it goes. My tentative plan now (I am always in a tentative state of mind) is to use two joysticks, one to control rate and direction of rotation, the other to control rate and direction of travel in xy.

    It will still be hard for the operator to keep track of which direction +x will take him. Therefore I plan to install (independantly of the PropStick) a small video camera from my junk box which has a radio link to my good old, old Radio Shack 5-inch monitor. A shaft encoder on the servo controlled pan axis of the camera will hopefully make it possible to move left or right in the picture. A double throw toggle switch will then allow me to continue to use the monitor for debugging the PropStick.

    Any suggestions are welcome.

    This is a misuse of this forum. If you prefer send along your email address.

    EdKirk
    Ed Kirkham edk@wi.rr.com
Sign In or Register to comment.