Pages

Thursday, May 30, 2013

Guzunty Pi stepping motor function writing notes

I looked at my GPi function and found that it uses the tuple thing.  This reminds me of the laughable mistake I made when trying to use list in Python.  I learned the basic things of Python when watching the MIT OCW EECS1 videos, some 2 years ago.  The professor mentions something about "tuple".  I wrongly thought that "tuple" is the same thing of a pair, so was a list with 2 items.  It is only some months ago when I tried to understand about Python tuple, then I realized that for Python, there is actually a list thing beside tuple, and that tuple is perhaps faster than list, but one bad thing is that it cannot be changed (if I remember and understand the tutorial correctly).  In other words, Python list is more general and flexible, though might be slower than tuple.  For my toy projects, speed is not a thing to bother.  So I think I should convert my old functions using tuple to using lists.


# *** GPi stepping motor functions ***

# * Guzunty Pi 16o8i Pin numbering ********************************************

# Pin O0  = Winding 1 (Unipolar stepping motor A- winding)
# Pin O1  = Winding 2 (Unipolar stepping motor A+ winding)
# Pin O2  = Winding 3 (Unipolar stepping motor B- winding)
# Pin O3  = Winding 4 (Unipolar stepping motor B+ winding)

# *** Guzunty Pi 28BYJ48 Unipolar Stepping Motor Functions ********************

def WindingHex(windingTuple):

    windingHex0 = 0x01 << (windingTuple[0] - 1)
    windingHex1 = 0x01 << (windingTuple[1] - 1)

    windingHex = windingHex0 | windingHex1

    return windingHex

def TestGuzuntyPiSteppingMotor(windingTuple0, windingTuple1, stepCount, stepTime):

    PrintDoubleSpaceLine("*** Start testing Guzunty Pi Stepping Motor ***")    

    windingHex0 = WindingHex(windingTuple0)
    windingHex1 = WindingHex(windingTuple1)

    guzuntypiSpi = spidev.SpiDev() 
    guzuntypiSpi.open(0, 0) 

    Beep(4)

    for i in range(stepCount):
        guzuntypiSpi.xfer2([windingHex0, 0x00])
        time.sleep(stepTime)
        guzuntypiSpi.xfer2([windingHex1, 0x00])
        time.sleep(stepTime)  

    guzuntypiSpi.close() 

    PrintDoubleSpaceLine("*** Stop testing GuzuntyPi ***")

    # *** Sameple call - 
    # TestGuzuntyPiSteppingMotor(windingTuple0 = (1,3), windingTuple1 = (2, 4), 
    #                            stepCount = 50, stepTime = 0.05)



# *** MCP23008 stepping motor functions ***************************************

# *** Unipolar Stepping Motor 28BYJ48/NPM-PF35/PX245 **************************

# Unipolar Stepping Motor Switching Sequence 
# 1. Wave sequence = 1 - 3 - 2 - 4 (A-, B-, A+, B+)
# 2. Full step sequence = 13 - 14 - 24 - 23 (A-B-, A-B+, A+B+, A+B-)
# 3. Half step sequence  = 13 - 1 - 14 - 4 - 24 - 2 - 23 - 3 
# 4. One step swing = 1 - 3 - 1 - 3 (A-, B-, A-, B-)
# Winding        A-(1)    A+(2)    B-(3)   B+(4)    COM
# NPM PF35       Black    Yellow   Brown   Orange   Red
# 28BYJ48        Pink     Orange   Blue    Yellow   Red  
# PX245          Black    Green    Blue    Red      Yelow/White

# * Convert decimal pin number to hex *
def convert1PinToHex(p): # convert 1 of 8 high pin to hex
    hexString = 0x01
    for count in range(p-1):
    hexString = hexString << 1
    return hexString

def convert2PinToHex(p1, p2):  # convert 2 of 8 high pins to hex
    return (convert1PinToHex(p1) | convert1PinToHex(p2))

def convert2PinToHighNibble(p1, p2):  # convert 2 of 8 high pins to high nibble
    lowNibble = convert1PinToHex(p1) | convert1PinToHex(p2)
    highNibble = lowNibble << 4
    return highNibble

# * Move unipolar stepping motor *

def WriteMotorWindingWaveSequence1324(RegisterBaseAddress, NibbleType, StepCount, StepTime): # move motor using 13-24 sequence  
    # Set port all output

    WriteDataByte(Mcp23008BaseAddress1, RegisterAddressOffsetArrayBank0, InputOutputDirection, PortA, AllOutputByte)

    if NibbleType == LowNibble:
hexString1 =  convert2PinToHex(1, 3)
  hexString2 =  convert2PinToHex(2, 4)
    else:
hexString1 = convert2PinToHighNibble(1, 3)
hexString2 = convert2PinToHighNibble(2, 4)
    for i in range(StepCount):
        WriteDataByte(Mcp23008BaseAddress1, RegisterAddressOffsetArrayBank0, OutputLatch, PortA, hexString1) 
    time.sleep(StepTime)
        WriteDataByte(Mcp23008BaseAddress1, RegisterAddressOffsetArrayBank0, OutputLatch, PortA, hexString2) 
        time.sleep(StepTime)

def WriteMotorWindingFullStepSequence13232414(RegisterBaseAddress, NibbleType, StepCount, StepTime): #move motor using 13-23-24-14 sequence 
    # Set port all output
    WriteDataByte(Mcp23008BaseAddress1, RegisterAddressOffsetArrayBank0, InputOutputDirection, PortA, AllOutputByte)

    if NibbleType == LowNibble:
    motorWindingActivationPatternArray = (0x05, 0x06, 0x0a, 0x09)
    else:
motorWindingActivationPatternArray = (0x50, 0x60, 0xa0, 0x90)
    for i in range(StepCount):
for pattern in motorWindingActivationPatternArray:
            WriteDataByte(Mcp23008BaseAddress1, RegisterAddressOffsetArrayBank0, OutputLatch, PortA, pattern) 
   time.sleep(StepTime)

def TestConvert1PinToHex(): # test convert 1 high pin to hex
    print "*** Testing 1 pin number decimal 0 ~ 7 converted to hexdecimal 0x01 ~ 0x80"
    for d in range(8):
    print hex(convert1PinToHex(d))

def TestConvert2PinToHex(p1, p2): # test convert 2 high pins to hex
    print "*** Testing 2 pin numbers decimal 0 ~ 7 converted to hexdecimal"
    print "Pin 1 = ", p1, "Pin 2 = ", p2
    print "Hex = ", hex(convert2PinToHex(p1, p2))

def TestConvert2PinToHighNibble(p1, p2): # test convert 2 of 8 high pins to high nibble
    print "*** Testing 2 pin numbers decimal 0 ~ 7 converted to high nibble"
    print "Pin 1 = ", p1, "Pin 2 = ", p2
    print "HighNibble = ", hex(convert2PinToHighNibble(p1, p2))

def MoveTwoMotors(RegisterBaseAddress):  
OneBeep()
WriteMotorWindingWaveSequence1324(Mcp23008BaseAddress1, LowNibble, TwentyTimes, FiftyMilliSeconds)
OneBeep()
WriteMotorWindingWaveSequence1324(Mcp23008BaseAddress1, HighNibble, TwentyTimes, FiftyMilliSeconds)
OneBeep()
WriteMotorWindingFullStepSequence13232414(Mcp23008BaseAddress1, LowNibble, TwentyTimes, OneHundredMilliSeconds)
OneBeep()
WriteMotorWindingFullStepSequence13232414(Mcp23008BaseAddress1, HighNibble, TwentyTimes, OneHundredMilliSeconds)

def TestMotor():
    MotorRegisterBaseAddress = Mcp23008BaseAddress1
    MoveTwoMotors(MotorRegisterBaseAddress)

def MoveUnipolarSteppingMotor(registerBaseAddress, NibbleType, StepCount, StepTime): 
    SetupPortAoutputPortBinputPullUpMcp23017(registerBaseAddress)

    if NibbleType == LowNibble:
hexString1 =  convert2PinToHex(1, 3)
  hexString2 =  convert2PinToHex(2, 4)
    else:
hexString1 = convert2PinToHighNibble(1, 3)
hexString2 = convert2PinToHighNibble(2, 4)

    for i in range(StepCount):
        WriteDataByte(registerBaseAddress, RegisterAddressOffsetArrayBank0, OutputLatch, PortA, hexString1) 
    time.sleep(StepTime)
        WriteDataByte(registerBaseAddress, RegisterAddressOffsetArrayBank0, OutputLatch, PortA, hexString2) 
        time.sleep(StepTime)

def TestMoveMotor():
    registerBaseAddress = Mcp23017BaseAddressSystemB1
    nibbleType = LowNibble
    stepCount = OneHundredTimes
    stepTime = OneHundredMilliSeconds
    MoveUnipolarSteppingMotor(registerBaseAddress, nibbleType, 500, 0.05)

.END

No comments:

Post a Comment