0

I am trying to run the following the code on PI4.

from Adafruit_MotorHAT import Adafruit_MotorHAT  # pylint: disable=import-error
import RPi.GPIO as GPIO  # pylint: disable=import-error
import time
import atexit

from .logger import logger
from src.main import constants

SWITCH_GPIO_CHANNEL = 21

mh = Adafruit_MotorHAT(addr=0x60)
stepper = None
current_pos = 0
GPIO.setmode(GPIO.BCM)
GPIO.setup(SWITCH_GPIO_CHANNEL, GPIO.IN)
SWITCH_GPIO_CHANNEL = 21

IS_ON = False


def turn_on():
    global stepper  # pylint: disable=global-statement
    stepper = mh.getStepper(200, 1)
    stepper.setSpeed(255)  # SETTING THE RPM, NOT SURE IF NEEDED


def turn_off():
    global stepper  # pylint: disable=global-statement
    for i in range(1, 5):
        mh.getMotor(i).run(Adafruit_MotorHAT.RELEASE)
    stepper = None


atexit.register(turn_off)


def initialize_stepper_if_needed():
    if not stepper:
        turn_on()


def move(direction, size):
    initialize_stepper_if_needed()
    stepper.oneStep(direction, size)





def step_down():
    """
    Move the stage a full step up using the DOUBLE setting.
    """
    global current_pos  # pylint: disable=global-statement
    move(Adafruit_MotorHAT.BACKWARD, Adafruit_MotorHAT.DOUBLE)
    current_pos = current_pos + 1


def step_up():
    global current_pos  # pylint: disable=global-statement
    move(Adafruit_MotorHAT.FORWARD, Adafruit_MotorHAT.DOUBLE)
    current_pos = current_pos - 1


def move_to_top():
    """
    Move the stage up until the stage is hit to find the zero position.
    """
    logger.info('moving to top')
    global current_pos  # pylint: disable=global-statement
    # first move down in case the slide is too high
    while GPIO.input(SWITCH_GPIO_CHANNEL) == 0:
        step_down()
        time.sleep(0.02)

    # then move up to the switch point
    while GPIO.input(SWITCH_GPIO_CHANNEL) == 1:
        step_up()
        time.sleep(0.02)

    current_pos = 0

For some reason when I run the code, the following will be printed. I am running this very same code on PI3 and it is working perfectly. But on PI4 it's printing the following messages. Does anyone know where and why the message is coming from? What am I missing? None of these messages are printed on PI3.

I am trying to run a stepper motor.

2019-09-10 17:04:11,630 Reseting PCA9685 MODE1 (without SLEEP) and MODE2
2019-09-10 17:04:11,631 Wrote 0x00 to register 0xFA
2019-09-10 17:04:11,632 Wrote 0x00 to register 0xFB
2019-09-10 17:04:11,633 Wrote 0x00 to register 0xFC
2019-09-10 17:04:11,634 Wrote 0x00 to register 0xFD
2019-09-10 17:04:11,635 Wrote 0x04 to register 0x01
2019-09-10 17:04:11,636 Wrote 0x01 to register 0x00
2019-09-10 17:04:11,642 Read 0x01 from register 0x00
2019-09-10 17:04:11,643 Wrote 0x01 to register 0x00
2019-09-10 17:04:11,648 Setting PWM frequency to 1600 Hz
2019-09-10 17:04:11,649 Estimated pre-scale: 2
2019-09-10 17:04:11,649 Final pre-scale: 3
2019-09-10 17:04:11,650 Read 0x01 from register 0x00
2019-09-10 17:04:11,651 Wrote 0x11 to register 0x00
2019-09-10 17:04:11,652 Wrote 0x03 to register 0xFE
2019-09-10 17:04:11,652 Wrote 0x01 to register 0x00
2019-09-10 17:04:11,658 Wrote 0x81 to register 0x00
  • 1
    I read MagPi84 Page 44 an article about AdaFruit PCA9685 PWM controller based servo board and instructions on how to setup Rpi4B I2C (lowering speed to 10 kHz to do cycle stretching etc). I remember the article is about "servo board" not motor board, though there are one or two paragraphs mentioning that PCA9685 PWM controller can also PWM control stepping motors. I am confused if you are controlling servos, or stepping motors. – tlfong01 Sep 11 '19 at 5:26
  • My bad, I meant stepper motor. – RAS Sep 11 '19 at 5:30
  • I see. So you are using the DC/Stepper motor HAT, not servo HAT. I noticed that you import a logger module, which therefore displays which registers the program is using, config mode, prescaling, PWM frequency etc. I remember I read the AdaFruit PCA8685 HAT and SERVO HAT user guide and found them good. Perhaps you can also find a similar user guide for the stepper motor hat. (1) Adafruit DC & Stepper Motor HAT for Raspberry Pi (PCA9685 + TB6612)- $23 adafruit.com/product/2348 – tlfong01 Sep 11 '19 at 6:30
  • You don't say if the motor does what you expect or not. If it works in the same way as on the Pi3 then I would ignore log messages. – joan Sep 11 '19 at 8:14
  • @RAS, Are you using this AdaFruit Stepping Motor library? - circuitpython.readthedocs.io/projects/motorkit/en/latest – tlfong01 Sep 11 '19 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.