Pages

Friday, May 31, 2013

GPi move 1 of 4 motors function writing notes

I am going very slowly on the move 1 of 4 motors function.  I now know how to rotate a list, difference between reversed(xlist) for loop traversal and list.reverse(xlist).

I am using loads of print statements for debugging.  I now appreciate 2 big advantages of Python for newbie doing toy projects.

(1) Python is interpreted, therefore quick to try out things.  Slow execution is no problem. 

(2) Python is very loosely typed, therefore no more time wasting type casts.  Wrong typing related errors is OK for toys.


# ftguzuntypi.py v1.5 tlfong01 2013may31

# *****************************************************************************
# Module - ftguzuntypi.py
# Description - Stepping motor, LED driver 
# *****************************************************************************

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

import time
import spidev
from subprocess import call

import ftspi
import ftprint

# *****************************************************************************
# Guzunty Pi test functions
# *****************************************************************************

# *****************************************************************************
# Function - Stepping motor
# Description - 
# P1 pin assignment 
#   P1-01 = Winding 1 (A-)
#   P1-02 = Winding 2 (A+)
#   P1-03 = Winding 3 (B-)
#   P1-04 = Winding 4 (B+) 
# Activation sequences
#   WaveSequence = [1, 3, 2, 4]
#   FullStepSequence =  [[1, 3], [1, 4], [2, 4], [2, 3]]
#   HalfStepSequence =  [[1], [1,4], [4], [2,4], [2], [2,3], [3]] 
# Unipolar Stepping Motor Switching Sequence 
#   Wave sequence = 1 - 3 - 2 - 4 (A-, B-, A+, B+)
#   Full step sequence = 13 - 14 - 24 - 23 (A-B-, A-B+, A+B+, A+B-)
#   Half step sequence  = 13 - 1 - 14 - 4 - 24 - 2 - 23 - 3 
#   One step swing = 1 - 3 - 1 - 3 (A-, B-, A-, B-)
# Motor windings list
#   Winding        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 - 
# *****************************************************************************

Wave = [0x1, 0x4, 0x2, 0x8] # counter clockwise actually
Clockwise = 0
CounterClockwise = 1
MilliSeconds50 = 0.05
Byj48stepAngle = 5.625 
MotorStateListInit = [0xf, 0xf, 0xf, 0xf] # motors 0, 1, 2, 3, all winding off
MotorStateListTest = [0x1, 0x4, 0x2, 0x2] # motor 0 winding 1 on, etc
MotorStateQuadNibbleWordInit = 0xffff # 2 bytes = 4 nibbles for 4 motors to write SPI GPi

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):

    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

    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

    # ************************************************************************

    print "\n\nMotor state word = ", hex(MotorStateQuadNibbleWordInit)  

    # motorStateQuadNibbleWord = 0x0000 | (stepSequence[0]) | (stepSequence[1] << 4) | (stepSequence[2] << 8) | (stepSequence[3] << 12) 

    motorStateQuadNibbleWord = 0x2222
  
    print "StateQuadNibbleWord = ", hex(motorStateQuadNibbleWord)

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

    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)

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

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

    return motorStateList

.END

No comments:

Post a Comment