Article Index

Page 135

from machine import Pin, PWM
from time import sleep
class Motor:
    def __init__(self, pinNo):
        self.pwm1 = PWM(Pin(pinNo), freq=2000, duty_u16=0)
        self.gpio = pinNo
        self._on = False
        self.speed=0
    def setSpeed(self,s):
        self._on=True
        self.speed=s
        self.pwm1.duty_u16(int(65535*s/100))
    def off(self):
        self._on=False
        self.pwm1.duty_u16(0)
    def on(self):
        self._on=True
        self.pwm1.duty_u16(int(65535*self.speed/100))

motor=Motor(4)
sleep(1)
motor.setSpeed(50)
sleep(1)
motor.off()
sleep(1)
motor.setSpeed(90)
sleep(1)
motor.off()

Page 139

from machine import Pin, PWM
from time import sleep
class Motor:
    def __init__(self, pinNo):
        self.pwm1 = PWM(Pin(pinNo), freq=2000, duty_u16=0)
        self.gpio = pinNo
        self._on = False
        self.speed=0
    def setSpeed(self,s):
        self._on=True
        self.speed=s
        self.pwm1.duty_u16(int(65535*s/100))
    def off(self):
        self._on=False
        self.pwm1.duty_u16(0)
    def on(self):
        self._on=True
        self.pwm1.duty_u16(int(65535*self.speed/100))

class BiMotor(Motor):
    def __init__(self, pinNo1,pinNo2):
        super().__init__(pinNo1)
        self.forward = True
        self.pwm2 = PWM(Pin(pinNo2),freq=2000,duty_u16=0)

    def setForward(self, forward):
        if self.forward == forward:
            return
        self.pwm1.duty_u16(0)
        self.pwm1, self.pwm2 = self.pwm2, self.pwm1
        self.forward = forward
        self.pwm1.duty_u16(int(65535 * self.speed / 100))


motor = BiMotor(2,4)
sleep(1)
motor.setSpeed(50)
sleep(1)
motor.setForward(False)
sleep(1)
motor.setSpeed(90)
sleep(1)
motor.setForward(True)
motor.off()

Page 143 

from machine import Pin, PWM
from time import sleep
class Servo:
    def __init__(self, pinNo):
        self.pwm = PWM(Pin(pinNo),freq=50,duty_u16= int(65535*2.5/100))
    def setPosition(self, p):
        self.position = p
        self.pwm.duty_u16(int(65535*p/1000 + 65535*2.5/100))
    def off(self):
        self.pwm.deinit()
servo=Servo(4)
sleep(1)
servo.setPosition(100.0)
sleep(1)
servo.setPosition(50.0)
sleep(1)
servo.setPosition(0)
sleep(1)
servo.off()

Page 150

from machine import Pin, mem32
from time import sleep_ms
class StepperBi4():
    def __init__(self, pinA):
        self.phase = 0
        self.pinA = pinA
        self.gpios = tuple([Pin(pinA, Pin.OUT),
                            Pin(pinA + 1, Pin.OUT),
                            Pin(pinA + 2, Pin.OUT),
                            Pin(pinA + 3, Pin.OUT)])
        self.gpios[0].on()
        self.gpioMask = 0xF << self.pinA
        self.halfstepSeq = [0x1, 0x5, 0x4, 0x6, 0x2, 0xA, 0x8, 0x9]

    #   [          B- B -A  A
    #             [0, 0, 0, 1],
    #             [0, 1, 0, 1],
    #             [0, 1, 0, 0],
    #             [0, 1, 1, 0],
    #             [0, 0, 1, 0],
    #             [1, 0, 1, 0],
    #             [1, 0, 0, 0],
    #             [1, 0, 0, 1]
    #    ]
    #  change 0x3FF44004 to 0x60004004 for an S3
    def _gpio_set(self,value, mask):
        mem32[0x3FF44004] = mem32[0x3FF44004] & ~mask |
                                                  value & mask

    def setPhase(self, phase):
        value = self.halfstepSeq[phase] << self.pinA
        self._gpio_set(value, self.gpioMask)
        self.phase = phase

    def stepForward(self):
        self.phase = (self.phase + 1) % 8
        self.setPhase(self.phase)

    def stepReverse(self):
        self.phase = (self.phase - 1) % 8
        self.setPhase(self.phase)

step = StepperBi4(16)
step.setPhase(0)

while True:
    step.stepForward()
    sleep_ms(1)

Page 154

from machine import Pin, mem32, Timer
from machine import Timer
from time import sleep_ms      


class StepperBi4():
    def __init__(self, pinA):
        self.phase = 0
        self.pinA = pinA
        self.gpios = tuple([Pin(pinA, Pin.OUT),
                            Pin(pinA + 1, Pin.OUT),
                            Pin(pinA + 2, Pin.OUT),
                            Pin(pinA + 3, Pin.OUT)])
        self.gpios[0].on()
        self.gpioMask = 0xF << self.pinA
        self.halfstepSeq = [0x1, 0x5, 0x4, 0x6, 0x2, 0xA, 0x8, 0x9]
        self.forward = True
        self.speed = 0
        self.timer = None

    #   [          B- B -A  A
    #             [0, 0, 0, 1],
    #             [0, 1, 0, 1],
    #             [0, 1, 0, 0],
    #             [0, 1, 1, 0],
    #             [0, 0, 1, 0],
    #             [1, 0, 1, 0],
    #             [1, 0, 0, 0],
    #             [1, 0, 0, 1]
    #    ]
    #  change 0x3FF44004 to 0x60004004 for an S3
    def _gpio_set(self,value, mask):
        mem32[0x3FF44004] = mem32[0x3FF44004] & ~mask | value & mask

    def setPhase(self, phase):
        value = self.halfstepSeq[phase] << self.pinA
        self._gpio_set(value, self.gpioMask)
        self.phase = phase

    def stepForward(self):
        self.phase = (self.phase + 1) % 8
        self.setPhase(self.phase)

    def stepReverse(self):
       self.phase = (self.phase - 1) % 8
       self.setPhase(self.phase)
    def rotate(self, forward, speed):
       self.forward = forward
       self.speed = speed
       if speed == 0:
            self.timer.deinit()
            self.timer = None
            return
       if self.timer == None:
           self.timer = Timer(0)
       self.timer.init(freq=speed, mode=Timer.PERIODIC,callback=self.doRotate)
    def doRotate(self,timer):
       if self.forward:
           self.stepForward()
       else:
           self.stepReverse()

step = StepperBi4(16)
step.setPhase(0)

while True:
    step.rotate(True,100)
    sleep_ms(500)
    step.rotate(True,0)
    sleep_ms(500)