Просмотр исходного кода

stepper controller by i2c and silnik krokowy 28BYJ-48 maly on ULN2003 with laser on 0x24, rest on 0x20 on PCF8574T extender

a.binder 5 лет назад
Родитель
Сommit
f675aceb1f

+ 96 - 0
SE/stuff/P5_Automation_can-dev-res-working-1930/___SUBMODULES___/home/pi/stepper.py

@@ -0,0 +1,96 @@
+#!/usr/bin/python
+# Import required libraries
+import sys
+import time
+
+import smbus
+bus = smbus.SMBus(1)
+
+PCF_ADDR = 0x20 
+PCF_LASER_ADDR = 0x24
+
+
+
+step_currX_A = 1
+step_currY_A = 1
+
+
+bus.write_byte_data(PCF_ADDR, 0, 0)
+
+
+step_seqX_A = [
+    [ 0b00000001 ],
+    [ 0b00000011 ],
+    [ 0b00000010 ],
+    [ 0b00000110 ],
+    [ 0b00000100 ],
+    [ 0b00001100 ],
+    [ 0b00001000 ],
+    [ 0b00001001 ]
+]
+
+step_seqY_A = [
+    [ 0b00010000 ],
+    [ 0b00110000 ],
+    [ 0b00100000 ],
+    [ 0b01100000 ],
+    [ 0b01000000 ],
+    [ 0b11000000 ],
+    [ 0b10000000 ],
+    [ 0b10010000 ]
+]
+
+
+
+
+def  step_ctrl_send( stepsX, stepsY):
+    global step_seqX_A, step_seqY_A,  step_currX_A, step_currY_A, PCF_ADDR
+    if ( stepsX > 0 ) :
+           step_currX_A+=1
+    elif ( stepsX < 0 ) : 
+           step_currX_A-=1
+    #else:
+    #    //   step_currX_A --;
+    
+    if(stepsY > 0):
+#           print("Y>", step_currY_A, "] ")
+           step_currY_A+=1
+    elif(stepsY < 0): 
+           step_currY_A-=1
+    
+    if(step_currX_A > 7): step_currX_A = 0
+    if(step_currX_A < 0): step_currX_A = 7
+
+    if(step_currY_A > 7): step_currY_A = 0
+    if(step_currY_A < 0): step_currY_A = 7
+
+#    print("a=", step_seqX_A[step_currX_A][0] )    
+#    print(" step=", step_currX_A, " sX", stepsX )    
+    #    print("a & b =", step_seqX_A[step_currX_A] | step_seqY_A[step_currY_A])    
+    bus.write_byte_data(PCF_ADDR, 0, step_seqX_A[step_currX_A][0] | step_seqY_A[step_currY_A][0] )
+#    bus.write_byte_data(PCF_ADDR, 0, int(step_seqX_A[step_currX_A][0]) )
+    time.sleep(0.01)
+
+#def step_ctrl(step_seqX_A, step_seqY_A, step_currX_A, step_currY_A, stepsX, stepsY):
+def step_ctrl( stepsX, stepsY):
+    global step_seqX_A, step_seqY_A,  step_currX_A, step_currY_A, PCF_ADDR
+    i = 1
+    while (  i<=abs(stepsX) or i<= abs(stepsY) ):
+         if(abs(stepsX) == abs(stepsY) or stepsX == 0 or stepsY == 0):
+            step_ctrl_send(stepsX, stepsY )
+         i+=1
+
+
+
+
+
+#step_ctrl(  step_seqX_A, step_seqY_A,  step_currX_A, step_currY_A, 250, 250) ;
+#step_ctrl(  step_seqX_A, step_seqY_A,  step_currX_A, step_currY_A, -250, -250) ;
+
+step_ctrl(   250, 250) ;
+bus.write_byte_data(PCF_LASER_ADDR, 0, 0)
+step_ctrl(  -250, -250) ;
+bus.write_byte_data(PCF_LASER_ADDR, 0, 0xff)
+
+
+