Shop Learn
P2/Spin2 servo360 driver code — Parallax Forums

P2/Spin2 servo360 driver code

I am now seriously considering pulling the Activity Board from my bot and trying out the P2 Edge setup. A major missing piece is Spin2 code for the servo360.

Has anybody created such a thing?




  • The P2's smartpins should be able to the PWM drive and PWM feedback decoding for the servos all without any dedicated cog. It should all be pretty simple - only a few lines to set up the smartpins and a few more to set the servo output and read the feedback signal.

  • JonnyMacJonnyMac Posts: 7,622

    I (and, perhaps, others) have written servo drivers that use Smart Pin modes. To read the feedback pulse you need the equivalent of PULSIN which can be accomplished with Smart Pin mode P_HIGH_TICKS.

  • I am attaching a driver and its demo program. These are .spin2 files from March 2019 and do not use any of the P2 methods that were added to spin2 in the last two years. It sort of almost works. All the guts are PASM (I see you were asking specifically about spin). If I were to do it over, I would almost certainly use in-line PASM for the few places that needed to go fast.

    I believe I optimized the fetching of the feedback (both high and low) signal (which is most time-consuming). This version is four servos; P2 certainly could do six in a single cog.

    As I recall, I didn't use the LUT memory at all; it was really a hack on the P1 driver I got working so many years ago.

    I think (and there are others who disagree) that the 360 servo has a show-stopper dead zone, especially at 5V. IMO.

  • RsadeikaRsadeika Posts: 3,565

    Thanks tomcrawford. I will look at the driver code and see if I could run it with FlexBASIC.


  • Here is a spin2 snippet that programs the control and feedback pins (using smart pins of course.) It runs using PropellorTools out of the box.

    I use P_PULSE for the control pin; that means you start a pulse every 20 ms.

    I use P_STATE_TICKS to read both widths of the feedback pin, high pulse first. Attached is a logic capture showing about the worst case, that is, when you begin to sample right after the pin goes HIGH. In that case you have to wait through a high pulse and then a low pulse. That is, you have to burn the entire period. (Best case is when you begin to sample during the low period; in that case you get the high period right away and just have to wait for the end of the low period (but you cannot depend on anything other than worst case)).

    If you figure the total period of the feedback signal is 1.1 ms (say 1.2 worst case). Then if you are driving six servos, you have 20/6 - 1.2 = 2.1 msec to calculate the next pulse width; if you are driving eight then you have 20/8 - 1.2 = 1.3 msec. P2 can do a lot in 1300 microseconds...

    1202 x 684 - 71K
  • JonnyMacJonnyMac Posts: 7,622
    edited 2021-06-01 20:51


  • Here is a driver for feedback servo. It does NOT include SetPosition but I figure you don't need that for a bot.

    It is all 100% spin. P2 will do eight fb servos with seven cogs tied behind its back.

    I will likely add SetPosition sometime.

Sign In or Register to comment.