带有easydriver和rasberry pi的步进电机控制

时间:2017-04-23 18:29:33

标签: python raspberry-pi driver numericstepper

我最近开始参加我的Rpi项目 我需要一些关于调整代码的帮助或建议。

我有一个(Pi,+ easydriver V4.4,+ nema 17步进电机)
使用BCM布局

我需要一个间隔为@ 的无限嵌套循环

# Do some steps.
for i in range(200):#800 == full rotation
stepper.step()

我希望我的步进器步进200步然后停止0.1秒然后再步进200步等(无限循环)

easydriver:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import RPi.GPIO as gpio
import time, sys


class easydriver(object):
    def __init__(self,pin_step=0,delay=0.1,pin_direction=0,pin_ms1=0,pin_ms2=0,pin_sleep=0,pin_enable=0,pin_reset=0,name="Stepper"):
        self.pin_step = pin_step
        self.delay = delay / 2
        self.pin_direction = pin_direction
        self.pin_microstep_1 = pin_ms1
        self.pin_microstep_2 = pin_ms2
        self.pin_sleep = pin_sleep
        self.pin_enable = pin_enable
        self.pin_reset = pin_reset
        self.name = name

        gpio.setmode(gpio.BCM)
        gpio.setwarnings(False)

        if self.pin_step > 0:
            gpio.setup(self.pin_step, gpio.OUT)
        if self.pin_direction > 0:
            gpio.setup(self.pin_direction, gpio.OUT)
            gpio.output(self.pin_direction, True)
        if self.pin_microstep_1 > 0:
            gpio.setup(self.pin_microstep_1, gpio.OUT)
            gpio.output(self.pin_microstep_1, False)
        if self.pin_microstep_2 > 0:
            gpio.setup(self.pin_microstep_2, gpio.OUT)
            gpio.output(self.pin_microstep_2, False)
        if self.pin_sleep > 0:
            gpio.setup(self.pin_sleep, gpio.OUT)
            gpio.output(self.pin_sleep,True)
        if self.pin_enable > 0:
            gpio.setup(self.pin_enable, gpio.OUT)
            gpio.output(self.pin_enable,False)
        if self.pin_reset > 0:
            gpio.setup(self.pin_reset, gpio.OUT)
            gpio.output(self.pin_reset,True)


    def step(self):
        gpio.output(self.pin_step,True)
        time.sleep(self.delay)
        gpio.output(self.pin_step,False)
        time.sleep(self.delay)

    def set_direction(self,direction):
        gpio.output(self.pin_direction,direction)

    def set_full_step(self):
        gpio.output(self.pin_microstep_1,False)
        gpio.output(self.pin_microstep_2,False)

    def set_half_step(self):
        gpio.output(self.pin_microstep_1,True)
        gpio.output(self.pin_microstep_2,False)


    def set_quarter_step(self):
        gpio.output(self.pin_microstep_1,False)
        gpio.output(self.pin_microstep_2,True)

    def set_eighth_step(self):
        gpio.output(self.pin_microstep_1,True)
        gpio.output(self.pin_microstep_2,True)

    def sleep(self):
        gpio.output(self.pin_sleep,False)

    def wake(self):
        gpio.output(self.pin_sleep,True)

    def disable(self):
        gpio.output(self.pin_enable,True)

    def enable(self):
        gpio.output(self.pin_enable,False)

    def reset(self):
        gpio.output(self.pin_reset,False)
        time.sleep(1)
        gpio.output(self.pin_reset,True)

    def set_delay(self, delay):
        self.delay = delay / 2

    def finish(self):
        gpio.cleanup()

Stepper.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import easydriver as ed


cw = True
ccw = False

"""
Arguments to pass or set up after creating the instance.

Step GPIO pin number.
Delay between step pulses in seconds.
Direction GPIO pin number.
Microstep 1 GPIO pin number.
Microstep 2 GPIO pin number.
Sleep GPIO pin number. #not in use
Enable GPIO pin number.#not in use
Reset GPIO pin number.#not in use
Name as a string.#not in use
"""

# Create an instance of the easydriver class.
# Not using sleep, enable or reset in this example.
stepper = ed.easydriver(8, 0.001, 11, 18, 22)

# Set motor direction to clockwise.
stepper.set_direction(cw)

# Set the motor to run in 1/8 of a step per pulse.
stepper.set_full_step()
# Do some steps.
 for i in range(200):
   stepper.step()

# Clean up (just calls GPIO.cleanup() function.)
stepper.finish()

1 个答案:

答案 0 :(得分:0)

感觉像个傻瓜......在我睡觉的时候有异常! 这就是我创建循环的方式:

loopinfinity = 1
# Do some steps.(800) full revolution
def step_motor():
  for i in range(120):
    stepper.step()

while loopinfinity > 0:
  try:
     step_motor()
     time.sleep(0.07) #this is in seconds
  except KeyboardInterrupt :
     loopinfinity -=1
     print("stopped")