Please help to debug - 360 positional Servo (default servo extension)

Hi, hoping that someone here can illuminate the way for this. I’m trying to get makecode to be able to programme my 360 positional servo (geekServo grey) precisely, but I’m hitting a wall here.


^ I can’t get the servo to move post 180 degrees with the code above.


^ With this code, the servo is incrementing its position by around 20 degrees even though it’s suppose to only increment by 10. ie it turns 180 degrees, even though the variable is still at 90, and it turn a full 360 when the variable is at 180.

First let’s take a look at the built-in servo package:

  /**
         * Set the possible rotation range angles for the servo between 0 and 180
         * @param minAngle the minimum angle from 0 to 90
         * @param maxAngle the maximum angle from 90 to 180
         */
        //% help=servos/set-range
        //% blockId=servosetrange block="set %servo range from %minAngle to %maxAngle"
        //% minAngle.min=0 minAngle.max=90
        //% maxAngle.min=90 maxAngle.max=180 maxAngle.defl=180
        //% servo.fieldEditor="gridpicker"
        //% servo.fieldOptions.width=220
        //% servo.fieldOptions.columns=2
        //% parts=microservo trackArgs=0
        //% group="Configuration"
        //% blockGap=8
        public setRange(minAngle: number, maxAngle: number) {
            this._minAngle = Math.max(0, Math.min(90, minAngle | 0));
            this._maxAngle = Math.max(90, Math.min(180, maxAngle | 0));
        }

What we can see in this code is that it only expects a servo to be 0 to 180. So a 360 servo isn’t going to work exactly as you expect.

That Geekservo Grey is part of the Nezha set. So let’s look at how the Nezha works with it:

        switch(servoType){
            case ServoTypeList._180:
                angel = Math.map(angel, 0, 180, 0, 180)
                break
            case ServoTypeList._270:
                angel = Math.map(angel, 0, 270, 0, 180)
                break
            case ServoTypeList._360:
                angel = Math.map(angel, 0, 360, 0, 180)
                break
        }

What we see here is that Nezha changes the range of the input to the servo to always be from 0 to 180.

What can we get from this? Well, when you try to set the servo range from 0 to 360 it will ignore you and set it to 0 to 180. Which is what you are seeing when you test it. We also can surmise that to actually go from 0 to 360 you need to (essentially) divide by 2 as the Nezha code does. That is what you see in testing as well.

There may be another extension that works with the Geekservo Gray better than the default package.

Thanks for figuring this out! I managed to get the servo to make 360 turns by dividing the servo direction to 2. The default servo block in the Pins group works too. No need to load the Nezha extension.