rpisteper是一个库控制步进电机,使用树莓pi和晶体管阵列
RPistepper的Python项目详细描述
rpistepper是一个库,包含:*一个控制步进器的类 带有RPI的马达。*用两个 马达。*用两个执行方螺旋运动的函数 马达。
接线
在我们的设置中,电机(vm)的电源由5v引脚供电 在rpi中,线圈的接地由 ULN2803A晶体管 阵列。
alt文本
连接RPI-ULN2803A:
RPi Pin (BCM) | ULN2803A |
---|---|
17 | 1B |
27 | 2B |
10 | 3B |
9 | 4B |
14 | 5B |
15 | 6B |
23 | 7B |
24 | 8B |
连接ULN2803A-电机:
ULN2803A | Motors |
---|---|
1C | Motor_1 Coil_A1 |
2C | Motor_1 Coil_A2 |
3C | Motor_1 Coil_B1 |
4C | Motor_1 Coil_B2 |
5C | Motor_2 Coil_A1 |
6C | Motor_2 Coil_A2 |
7C | Motor_2 Coil_B1 |
8C | Motor_2 Coil_B2 |
在这种情况下,两个电机连接到ULN2803A。
用法
类电机
此类允许用户使用4 GPIO控制6针步进电机 RPI的引脚。
软件采用BCM模式进行PIN标引。
此类最好与“with”语句一起使用,以正确处理 清除GPIO。
self.steps是此类的一个属性,它将获取步骤数 从初始位置开始或设置到特定步骤,类似于 自我。行动。
为了节省能源,建议在 马达空转。
参数是一个带有4个管脚的列表(线圈A1,线圈A2,线圈B1, coil_b2),步骤之间的延迟(默认值为20毫秒)和详细到 显示马达运动报告,最后两个是可选的。例如:
importRPistepperasstpM1_pins=[17,27,10,9]withstp.Motor(M1_pins)asM1:foriinrange(10):# moves 20 steps,release and waitprintM1M1.move(20)M1.release()raw_input('enter to execute next step')
如果类是正常实例化的,请使用cleanupprior方法 关闭应用程序以关闭GPIO资源。还有,如果是 重要的是在完成例行程序时回到初始位置, 使用方法reset。
importRPistepperasstpM1_pins=[17,27,10,9]M1=stp.Motor(M1_pins)foriinrange(10):# moves 20 steps,release and waitprintM1M1.move(20)M1.release()raw_input('enter to execute next step')M1.reset()M1.cleanup()
方法
目前有五种实现方法:
defmove(self,steps):''' Moves the motor 'steps' steps. Negative steps moves the motor backwards '''
defrelease(self):''' Sets all pins low. Power saving mode '''
defreset(self):''' Returns the motor to it's initial position '''
defzero(self):''' Sets the motor to the next position which Coil_A1 and Coil_A2 are on. Sets this position as the reference (steps = 0). '''
defcleanup(self):''' Cleans the GPIO resources '''
主要的方法是move,它将电机移动到所需的 步骤
步骤属性
可以检查电机位置或手动设置所需的 使用steps属性的步骤:
importRPistepperasstpM1_pins=[17,27,10,9]withstp.Motor(M1_pins)asM1:foriinrange(10):# moves 20 steps,release and waitprintM1.stepsM1.steps=20*iM1.release()raw_input('enter to execute next step')M1.reset()
属性
此类具有以下属性:
Attribute | Data |
---|---|
DELAY | Time between steps |
VERBOSE | Display motor data on screen |
PINS | GPIOs used by the instance |
actual_state | A list with the status of the coils (on/off) |
函数
这两个函数执行预先确定的移动,需要两个 步进电机对象:
defzig_zag(motor1,motor2,amp1,amp2,delay=None):''' Executes a zig-zag movement with two RPistepper objects. Arguments are: motor1 and motor2 objects and amp1, amp2, the amplitude of movement, a tuple (step, rep) representing the number of steps per iteration and the number of iterations of the following algorithm: Repeat rep1 times: 1. Moves motor 2 step2*rep2 steps forward 2. Moves motor 1 step1 steps forward 3. Moves motor 2 step2*rep2 steps backwards 4. Moves motor 1 step1 steps forward Reset to initial state Release the motors It's possible to change the delay between steps with the 'delay' argument '''
defsquare_spiral(motor1,motor2,amplitude,delay=None):''' Executes a square spiral movement with two RPistepper objects. Arguments are: motor1 and motor2 objects and the amplitude of movement, a tuple (step, rep) representing the number of steps per iteration and the number of iterations of the following algorithm: for i in range(rep): 1. Moves motor 2 to position i 2. Moves motor 1 to position i 3. Moves motor 1 to position -i 4. Moves motor 2 to position -i Reset to initial state Release the motors It's possible to change the delay between steps with the 'delay' argument '''
/垃圾箱/垃圾箱
rpistepper是用于控制马达的外壳。它提供了所有 Motor类中的方法。所有的命令都记录在 贝壳。可以将命令列表通过管道发送到shell:
rpistepper < sample.stp
or
cat sample.stp | rpistepper
使用-g标志调用rpistepper将打开一个gui应用程序 具有类似的功能