Shop OBEX P1 Docs P2 Docs Learn Events
Receive RC remote PWM — Parallax Forums

Receive RC remote PWM

Hi,
Is there any existing code for receiving 2 channels of RC remote PWM ? I know there is some s-bus stuff out there, but looking for PWM.

Thanks'

Comments

  • On the P2 it would be easy with the P_HIGH_TICKS smart pin mode. I've attached my Ping))) sensor code so you can see how that mode works. You should be able to read servo pulses with this.

  • If you need a different example https://github.com/timmmoore/Swervebot/blob/master/src/pwm_in.spin2
    It uses the same P_HIGH_TICKS with 4 absolute encoders that output pwm. The additional complexity is allowing the min/max pwm times to be set per encoder and tracking max/min pwm values.

    Tim

  • Brian_BBrian_B Posts: 842
    edited 2023-10-03 11:27

    Thanks' Jon,
    The ping seems to work great by itself to read RC pwm, but I'm having a problem getting proper convert values on one channel, code attached.

  • JonnyMacJonnyMac Posts: 9,102
    edited 2023-10-03 22:27

    The ping seems to work great by itself to read RC pwm...

    Sorry for the confusion, I did not mean for you to use the ping object, simply to examine it to see how to read an incoming pulse. The ping object sets the pin to output mode first and this could collide with what the RC receiver is doing. Please, if you haven't already, put a 4.7K resistor between the RC receiver output pins and the P2.

    I have a lot of experience driving two DC motors from a joystick, but my experience comes from helping my friend John at JonyJib.com with pan and tilt motors on camera platform controls -- controls that have been used t shoot video at the Super Bowl, the World Series, and the World Cup. We've learned quite a lot over the years, and I will share a couple tips with you.

    First, you map() function works, but only with good input. An out-of-range value could wreak havoc on a system. Here is a better version.

    pub map(value, inmin, inmax, outmin, outmax) : result
    
    '' Maps value in range inmin..inmax to new value in range outmin..outmax
    
      if (value <= inmin)
        return outmin
    
      elseif (value >= inmax)
        return outmax
    
      else
        return (value - inmin) * (outmax - outmin) / (inmax - inmin) + outmin
    

    The other tip is using a joystick -- they never spring back to the exact place. Putting a dead band into your code will ensure that letting the joystick go will stop the motor. I'm a very visual person so I often use Inkscape to create graphs like the one I've attached. What that shows on the X axis is the inputs from the RC receiver: 1000 for full reverse, 2000 for full forward. The green (fwd) and red (rev) indicate speed. Notice the section in the center where the speed is zero -- this is the dead band. By having this you can ensure the motor(s) won't "creep" if the joystick is not exactly at center when you release it.

    This is how I convert joystick input on a camera platform system to motor (pan or tilt) speed. I've updated this for servo-pulse values on the input, and -50% to +50% speed from your program.

    pub get_speed(pwidth) : result
    
    '' Convert pulse width input to motor speed
    '' -- uses deadband to ensure motor stops with centered joystick
    
      pwidth := SVO_REV #> pwidth <# SVO_FWD                        ' constrain input
    
      if (pwidth < DB_REV)                                          ' below deadband
        result := map(pwidth, SVO_REV, DB_REV, -50,  0)
    
      elseif (pwidth > DB_FWD)                                      ' above deadband
        result := map(pwidth, DB_FWD, SVO_FWD,   0, 50)
    
      else
        return 0                                                    ' in dead band (stop)
    

    I have wrapped everything into a template for you (attached). You'll need to add your motor stuff. You should do the configuration of the motor in the setup() method and keep main() very clean so that you can focus on the operation and not be overwhelmed by a lot of visually noisy code.

    Remember the MIT license: Use this at your own risk.

  • Jon,
    That works beautiful, I added a third RC button input to put it into safety mode if no signal detected. Thank You very much for the help!

  • link to testing video.

Sign In or Register to comment.