Pages

Saturday, June 01, 2013

GPi step motor function writing notes

# ftguzuntypi.py v1.6 tlfong01 2013jun01

# *****************************************************************************
# Module - ftguzuntypi.py
# Description - Step motor, Led driver 
# *****************************************************************************

# *****************************************************************************
# Imports
# *****************************************************************************

import time
import spidev
from subprocess import call

import ftspi
import ftprint

# *****************************************************************************
# Function - Step motor
# Description - 
#   Guzunty Pi P1 pin assignment 
#     Motor #0
#       P1-01 = Winding 1 (A-)
#       P1-02 = Winding 2 (A+)
#       P1-03 = Winding 3 (B-)
#       P1-04 = Winding 4 (B+) 
#     Motor #1 P1 pins 5 ~ 8, Motor # P1 pins 9 ~ 12, Morot #3 P1 pins 13 ~ 16
# Activation sequences
#   Wave = [1, 3, 2, 4] 1-3-2-4 (A-, B-, A+, B+)
#   FullStep =  [[1, 3], [1, 4], [2, 4], [2, 3]]  13-14-24-23 (A-B-, A-B+, A+B+, A+B-)
#   HalfStep =  [[1], [1,4], [4], [2,4], [2], [2,3], [3]] 13-1-14-4-24-2-23-3
# Motor windings list
#   --------------------------------------------------------------
#   Motor          A-(1)    A+(2)    B-(3)   B+(4)    COM (+5V)
#   --------------------------------------------------------------
#   28BYJ48        Pink     Orange   Blue    Yellow   Red  
#   NPM PF35       Black    Yellow   Brown   Orange   Red
#   PX245          Black    Green    Blue    Red      Yelow/White
#   --------------------------------------------------------------
# BYJ series geared step motor - 28byj48-12-300-01
#   Rated voltage - 12V
#   Number of phases - 2
#   Resistance per phase 300R
#   Gear reduction ratio - 1:64
#   Step angle - 5.625 degree /64 ??? or / 48
#   Pull-in torque at 500pps - >= 300 gm
#   Pull-in rate - >= 500 pps
#   Detent torque - >= 350
#   Noise - <= 35dB
# Sample call - 
#   ftguzuntypi.MoveStepMotor(motorStateList = ftguzuntypi.MotorStateListInit,
#                             motorNumber = 1,
#                             stepSequence = ftguzuntypi.Wave,  
#                             stepAngle = ftguzuntypi.Byj48stepAngle,
#                             stepTime = ftguzuntypi.MilliSeconds50,
#                             direction = ftguzuntypi.Clockwise,  
#                             angularDistance = 10)
# *****************************************************************************

Wave = [0x1, 0x4, 0x2, 0x8]
WaveClockwise = [0x1, 0x4, 0x2, 0x8]
WaveCounterClockwise = list.reverse(WaveClockwise)
Clockwise = 0
CounterClockwise = 1
MilliSeconds50 = 0.05
Byj48stepAngle = 5.625 
MotorStateListInit = [0xf, 0xf, 0xf, 0xf]
MotorStateListTest = [0x1, 0x4, 0x2, 0x2] 
MotorStateQuadNibbleWordInit = 0xffff
MotorStateQuadNibbleWordTest = 0x1234

def rotateRight(stepSequence, stepNumber):
    return stepSequence[-stepNumber:] + stepSequence[:-stepNumber]

def getStepNumber(stepSequence, stepState):
    for stepNumber in range(4):
        if (stepSequence[stepNumber] == stepState):
   break
    return stepNumber

def MoveStepMotor(motorStateList, motorNumber, stepSequence, stepAngle, stepTime, direction, angularDistance):

    # *** Function parameters ***
    ftprint.PrintDoubleSpaceLine("*** Start MoveStepMotor() ***")    
    print "Motor state list = ", hex(motorStateList[0]), hex(motorStateList[1]), hex(motorStateList[2]), hex(motorStateList[3])
    print "Motor number = ", motorNumber
    print "Step sequence = ", stepSequence
    print "Step angle = ", stepAngle
    print "Step time = ", stepTime
    print "Direction = ", direction
    print "Angular distance = ", angularDistance

    # *** Winding activation pattern sequence adjustment for direction and state synchronization
    print "\nStep sequence original = ", stepSequence
    if (direction == Clockwise):
        stepSequence == list.reverse(stepSequence)
    print "\nStep sequence reversed = ", stepSequence

    stepNumber = getStepNumber(stepSequence, motorStateList[motorNumber])
    stepSequence = rotateRight(stepSequence, stepNumber)
    print "\nStep sequence rotated = ", stepSequence

    # *** Motor state quad nibble (double byte) word testing ***
    motorStateQuadNibbleWord = MotorStateQuadNibbleWordTest # = 0x1234
    print "MotorStateQuadNibbleWord = ", hex(motorStateQuadNibbleWord)

    motorStateHighByte = motorStateQuadNibbleWord >> 8
    motorStateLowByte  = motorStateQuadNibbleWord & 0x00ff
    print "motorStateHighByte = ", hex(motorStateHighByte)
    print "motorStateLowByte = ", hex(motorStateLowByte)

    # *** Motor state quad nibble (double byte) word setting for SPI ***
    motorStateQuadNibbleWord = (motorStateQuadNibbleWord & (~(0x000f << (motorNumber * 4)))) | (0x0005 << (motorNumber * 4))
    print "StateQuadNibbleWord = ", hex(motorStateQuadNibbleWord)

    motorStateHighByte = motorStateQuadNibbleWord >> 8
    motorStateLowByte  = motorStateQuadNibbleWord & 0x00ff
    print "motorStateHighByte = ", hex(motorStateHighByte)
    print "motorStateLowByte = ", hex(motorStateLowByte)

    # *** Spi to GPi *** 

    spiChannel = spidev.SpiDev() 
    spiChannel.open(0, 0)  
    
    spiChannel.close() 

    ftprint.PrintDoubleSpaceLine("*** Stop testing stepping motor N ***")
    return motorStateList

.END

No comments:

Post a Comment