WayneKeenan / picraftzero

Robot remote controls with streaming video and VR HMD support.
MIT License
22 stars 4 forks source link

Smooth servo movement #21

Open WayneKeenan opened 7 years ago

WayneKeenan commented 7 years ago

either running average or do interpolation/fade between present pos and target

thymjan commented 6 years ago

This is my version for a smoother servo movement:

/usr/local/lib/python3.4/dist-packages/picraftzero/providers/servo/piconzero.py

    def set_angle(self, angle):
        msg = "Set Angle {}".format(angle)
        logger.debug(msg)
        if angle == self.last_angle or angle is None:
            return
        if self.last_angle is None:
            self.last_angle = 90

        maxanglechange = 3
        delta = angle - self.last_angle
        if abs(delta) > maxanglechange:
            logger.debug("Too fast change of angle. Throttling! angle {}, last {}".format(angle, self.last_angle))
            if delta > 0:
                angle = self.last_angle + maxanglechange
            if delta < 0:
                angle = self.last_angle - maxanglechange

        self.last_angle = angle
        logger.debug(msg)
        pz.setOutput(self.servo_id, angle)