Python Forum
stepper motor and servo key control
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
stepper motor and servo key control
#1
Hello,
I've been working on a turret that uses a 24-byj stepper motor to control the x axis and an sg-90 servo to control the y axis. Through my raspberry pi I was able to control both with a program I wrote. The problem I'm having is that when both are running, the stepper motor moves slower to the left than usual and does not move to the right at all, and I have tried using an external power source.

import RPi.GPIO as GPIO
import time
import curses

servo_value = 9
screen = curses.initscr()
curses.noecho() 
curses.cbreak()
screen.keypad(True)

try:
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(3, GPIO.OUT)
    servo1 = GPIO.PWM(3, 50)
    
    ControlPin = [7,11,13,15]

    for pin in ControlPin:
        GPIO.setup (pin, GPIO.OUT)
	GPIO.output (pin, 0)
	
    seq2 = [[1,0,0,0],
            [1,0,0,0],
            [0,1,0,0],
            [0,1,0,0],
            [0,0,1,0],
            [0,0,1,0],
            [0,0,0,1],
            [0,0,0,1],
            [1,0,0,0],]

    seq3 = [[1,0,0,0],
            [0,0,0,1],
            [0,0,0,1],
            [0,0,1,0],
            [0,0,1,0],
            [0,1,0,0],
            [0,1,0,0],
            [1,0,0,0],
            [1,0,0,0],]
        
    while True:
        
        servo1.start(servo_value)
        
        char = screen.getch()
        if char == ord('q'):
            break
        elif char == curses.KEY_UP:
            print("key up pressed")
            servo_value = servo_value + 0.25
            servo1.ChangeDutyCycle(servo_value)
            print(servo_value)
            time.sleep(0.001)

        elif char == curses.KEY_DOWN:
            print("key down pressed")
            servo_value = servo_value - 0.25
            servo1.ChangeDutyCycle(servo_value)
            print(servo_value)
            time.sleep(0.001)
            
                    # right turning
        elif char == curses.KEY_RIGHT:
            for i in range(15):
                print("right")
                for halfstep in range(9):
                    for pin in range(4):
                        GPIO.output(ControlPin[pin], seq2[halfstep][pin])
                        time.sleep(0.001)
                        
                        # left turning
        elif char == curses.KEY_LEFT:
            for i in range(15):
                print("left")
                for halfstep in range(9):
                    for pin in range(4):
                        GPIO.output(ControlPin[pin], seq3[halfstep][pin])
                        time.sleep(0.001)
    GPIO.cleanup()
    
except AttributeError:
    servo1.stop()
    GPIO.cleanup()
Reply
#2
This is some time ago for me, but is it a software or hardware problem?
They sell controllers ('drivers) to connect multiple motors working together in the same project.
(6 servo, 12 servo, 24 servo...etc). There are zillions of models like : https://www.pololu.com/product/1350
I am unsure if you can connect 2 different types to the same controller though.(servo,stepper)
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
see also: https://pypi.org/search/?q=stepper&o=-zscore
Reply
#4
The servo is connected directly to the pi but im using a UNL2003 driver for the stepper motor.
Reply
#5
OK, UNL2003 is the one for the stepper.
Judging a hardware project from a distance is tricky.
One suggestion/test: invest in a second servo to replace the stepper and a controller for the 2 servos.
(Should be around 10$) They do articulated cranes with 4 servos, not unlike a turret.
Doing that , i would look into post #3, seems interesting.
If you have tower pros, always check on their webpage if you have the genuine article;
They are the best, but also the most faked. You can tell by the way the logo is printed
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  micropython-stepper 1.0.3 trix 2 552 Nov-20-2023, 06:00 AM
Last Post: trix
  Stepper motor/Easy Driver/HELP Harney 1 1,852 Jul-31-2021, 08:05 AM
Last Post: Harney
  python 3 raspberry pi 4 dual control motor programming problem yome 0 1,938 Mar-21-2021, 05:17 PM
Last Post: yome
  Stepper Motor key control cel 2 1,861 Jul-25-2020, 12:19 AM
Last Post: cel
  EOF error using stepper motor cel 1 1,684 Jul-24-2020, 01:27 PM
Last Post: Larz60+
  Audio and servo at the same time cgreenwell 4 2,756 Jul-04-2019, 01:41 AM
Last Post: Larz60+
  Control 2 stepper motor simultaneously jihene 2 3,959 May-08-2019, 05:27 PM
Last Post: DeaD_EyE
  How to use servo motor with TFMini Distance Sensor in python script? programerguy 1 3,209 Dec-04-2018, 04:57 PM
Last Post: Larz60+
  stepper motor with raspberry pi bowen73 3 4,545 Aug-15-2017, 04:46 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020