summaryrefslogtreecommitdiff
path: root/mg996r.py
blob: cc70c24d065e6022793883059b81920b6011706a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import logging

from time import sleep
from orangepwm import OrangePwm
from pyA20.gpio import gpio
from pyA20.gpio import port


class MG996R:
    def __init__(self, pin: str, prev: int = 360):
        self.pwm = OrangePwm(50, getattr(port, pin))
        self.prev = prev
        gpio.init()

        logging.debug(f'Initialized MG996R class with starting degree of {prev}')

    def move(self, deg, delay: int = 0):
        distance = abs(self.prev - deg)
        sleep_time = distance / 60 * .5 + delay

        duty = deg / 18 + 2
        logging.debug(f'Moving to {deg} degrees, duty {duty:.0f}%, sleeping for {sleep_time:.1f} sec.')

        self.prev = deg

        self.pwm.start(duty)
        sleep(sleep_time + delay)
        self.pwm.stop()