0

Python usage on motor bonnet and stopping over turning on st
Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Please be positive and constructive with your questions and comments.

Python usage on motor bonnet and stopping over turning on st

by knoxvilles_Joker on Sat Jun 12, 2021 10:35 pm

I am somewhat following this:
https://learn.adafruit.com/adafruit-dc- ... per-motors

I got everything imported into the raspberry pi.
Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/python

import cwiid
import RPi.GPIO as GPIO
import sys
from time import *
import subprocess
import board

#this is for stepper motor controls, keyboard initiates movement, Button is limit switches
import keyboard

#gpiozero is the only library I have found that functions with the current pi and hardware setup I am using
from gpiozero import Button

#these are part of the Blinka libraries Adafruit has. 
from adafruit_motorkit import MotorKit
from adafruit_motor import stepper
kit1 = MotorKit()

# this is for stepper motors and servo board
from board import SCL, SDA
import busio
from adafruit_pca9685 import PCA9685
i2c_bus = busio.I2C(SCL, SDA)
pca = PCA9685(i2c_bus)
pca.frequency = 50

# standard servo declearations for controls
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)

#12 and 13 are for one motion assembly backing up one full turn
button12 = Button(12)
button13 = Button(13)
#27 and 5 are for the other motion assembly
button27 = Button(27)
button5 = Button(5)
# while True loops endlessly.  This can be run as a service using systemd or setup as an init.d script or as an rc.local script.
while True:

  if button12.is_pressed:
    print("button12 pressed")
    for i in range (200):
      kit1.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)

  if button13.is_pressed:
    print("button13 pressed")
    for i in range (200):
      kit1.stepper1.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)

  if keyboard.is_pressed('f'):
    print("f is pressed")
    for i in range (200):
      kit1.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)

  if keyboard.is_pressed('b'):
    print("b is pressed")
    for i in range (200):
      kit1.stepper1.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)

# This is for channel 2 stepper motor. 

  if button27.is_pressed:
    print("button12 pressed")
    for i in range (200):
      kit1.stepper2.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)

  if button5.is_pressed:
    print("button13 pressed")
    for i in range (200):
      kit1.stepper2.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)

  if keyboard.is_pressed('r'):
    print("f is pressed")
    for i in range (200):
      kit1.stepper2.onestep(direction=stepper.BACKWARD, style=stepper.DOUBLE)

  if keyboard.is_pressed('l'):
    print("b is pressed")
    for i in range (200):
      kit1.stepper2.onestep(direction=stepper.FORWARD, style=stepper.DOUBLE)


The issue I am running into is it is not stopping once one of the gpio buttons get triggered. The individual parts work, I am just thinking I am missing an obvious stop command in python with event triggering.

knoxvilles_Joker
 
Posts: 154
Joined: Wed Mar 01, 2017 9:15 pm

Re: Python usage on motor bonnet and stopping over turning o

by mikeysklar on Mon Jun 14, 2021 8:05 pm

Switching to an even shorter example from the same page of the link you provided does this stop after 100 steps?

Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Simple test for using adafruit_motorkit with a stepper motor"""
import time
import board
from adafruit_motorkit import MotorKit

kit = MotorKit(i2c=board.I2C())

for i in range(100):
    kit.stepper1.onestep()
    time.sleep(0.01)


If the above keeps going let's get into your wiring and motor models.

mikeysklar
 
Posts: 5020
Joined: Mon Aug 01, 2016 8:10 pm

Re: Python usage on motor bonnet and stopping over turning o

by knoxvilles_Joker on Sat Jun 26, 2021 1:04 am

I found the issue to be one:
I had hooked up the limit switches incorrectly
I had the directions reversed on the limit switches
I had to add:
mid and end step instructions on the range command

Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/python

import cwiid
import RPi.GPIO as GPIO
import sys
from time import *
import subprocess
import board
import time

#this is for stepper motor controls, keyboard initiates movement, Button is limit switches
import keyboard

#gpiozero is the only library I have found that functions with the current pi and hardware setup I am using
from gpiozero import Button

#these are part of the Blinka libraries Adafruit has.
from adafruit_motorkit import MotorKit
from adafruit_motor import stepper
kit1 = MotorKit()

# this is for stepper motors and servo board
from board import SCL, SDA
import busio
from adafruit_pca9685 import PCA9685
i2c_bus = busio.I2C(SCL, SDA)
pca = PCA9685(i2c_bus)
pca.frequency = 50

# standard servo declearations for controls
from adafruit_servokit import ServoKit
kit = ServoKit(channels=16)

# this is for the new non-conflicting gyroscope
from adafruit_lsm6ds.lsm6ds33 import LSM6DS33
i2c = board.I2C()
sensor = LSM6DS33(i2c)


#12 and 13 are for one motion assembly backing up one full turn
button12 = Button(12)
button13 = Button(13)
#27 and 5 are for the other motion assembly
button27 = Button(27)
button5 = Button(5)
# while True loops endlessly.  This can be run as a service using systemd or setup as an init.d script or as an rc.local script.
while True:

#  sensor.gyro_rate=250
  gyro_x, gyro_y, gyro_z=sensor.gyro
#  rightxa=(((int(gyro_x--256)/(256--256))*180))
  rightxa=(((int(gyro_x--5)/(5--5))*180))
#  rightya=(((int(gyro_y--256)/(256--256))*180))
  rightya=(((int(gyro_y--5)/(5--5))*180))
#  rightza=(((int(gyro_z--256)/(256--256))*180))
  rightza=(((int(gyro_z--5)/(5--5))*180))
  rightx = round(rightxa)
  righty = round(rightya)
  rightz = round(rightza)
  kit.servo[5].angle=rightx
  kit.servo[14].angle=righty
  kit.servo[7].angle=rightz
  print("gx", rightx, "gy", righty, "gz", rightz)
  time.sleep(0.6)
  print("rx", gyro_x, "ry", gyro_y, "rz", gyro_z)
  print("Gyro X:%.2f, Y: %.2f, Z: %.2f degrees/s" % (sensor.gyro))


  if button12.is_pressed:
    print("button12 pressed")
    for i in range (200):
 #     kit1.stepper1.release()
      kit1.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.SINGLE)

  if button13.is_pressed:
    print("button13 pressed")
    for i in range (200):
  #    kit1.stepper1.release()
      kit1.stepper1.onestep(direction=stepper.FORWARD, style=stepper.SINGLE)

  if keyboard.is_pressed('f'):
#    kit.servo[14].angle=10
 #   kit.servo[5].angle=10
  #  kit.servo[7].angle=10
    print("f is pressed")
    for i in range (3300):
      if button13.is_pressed:
        i = 3300
        break
      kit1.stepper1.onestep(direction=stepper.BACKWARD, style=stepper.SINGLE)

  if keyboard.is_pressed('b'):
#    kit.servo[14].angle=170
 #   kit.servo[5].angle=170
  #  kit.servo[7].angle=170
    print("b is pressed")
    for i in range (3300):
      if button12.is_pressed:
        i = 3300
   #     kit1.stepper1.release()
        break
      kit1.stepper1.onestep(direction=stepper.FORWARD, style=stepper.SINGLE)

# This is for channel 2 stepper motor.

  if button27.is_pressed:
    print("button12 pressed")
    for i in range (200):
      kit1.stepper2.onestep(direction=stepper.FORWARD, style=stepper.SINGLE)
      if i == 200:
        kit1.stepper2.release()

  if button5.is_pressed:
    print("button13 pressed")
    for i in range (200):
      kit1.stepper2.onestep(direction=stepper.BACKWARD, style=stepper.SINGLE)
      if i == 200:
        kit1.stepper2.release()

  if keyboard.is_pressed('r'):
    print("f is pressed")
    for i in range (3500):
      if button27.is_pressed:
        break
      kit1.stepper2.onestep(direction=stepper.BACKWARD, style=stepper.SINGLE)
      if i == 3500:
        kit1.stepper2.release()

  if keyboard.is_pressed('l'):
    print("b is pressed")
    for i in range (3500):
      if button5.is_pressed:
        break
      kit1.stepper2.onestep(direction=stepper.FORWARD, style=stepper.SINGLE)
      if i == 3500:
        kit1.stepper2.release()

knoxvilles_Joker
 
Posts: 154
Joined: Wed Mar 01, 2017 9:15 pm

Please be positive and constructive with your questions and comments.