test

///////////////////////////////////////////////////////////
// Stepper Motor skecth for use with the EasyDriver v4.2 //
///////////////////////////////////////////////////////////

// Dan Thompson 2010
//
// Use this code at your own risk.
//
// For all the product details visit http://greta.dhs.org/EasyDriver/
// For the full tutorial visit http://danthompsonsblog.blogspot.com/


////// ED_v4  Step Mode Chart //////
//                                //
//   MS1 MS2 Resolution           //
//   L   L   Full step (2 phase)  //
//   H   L   Half step            //
//   L   H   Quarter step         //
//   H   H   Eighth step          //
//                                //
////////////////////////////////////

int DIR = 3;          // PIN  3 = DIR
int STEP = 2;        // PIN  2 = STEP
int MS1 = 13;        // PIN 13 = MS
int MS2 = 9;         // PIN  9 = MS2
int SLEEP = 12;      // PIN 12 = SLP


void setup() {
  Serial.begin(9600);     // open the serial connection at 9600bps
  pinMode(DIR, OUTPUT);   // set pin 3 to output
  pinMode(STEP, OUTPUT);  // set pin 2 to output
  pinMode(MS1, OUTPUT);   // set pin 13 to output
  pinMode(MS2, OUTPUT);   // set pin 9 to output
  pinMode(SLEEP, OUTPUT); // set pin 12 to output
}



void loop()
{
  int modeType = 1;                         // This number increases by multiple of 2 each through the while loop..
                                            // ..to identify our step mode type.                                            
  while (modeType<=8){                      // loops the following block of code 4 times before repeating .
    digitalWrite(DIR, LOW);                 // Set the direction change LOW to HIGH to go in opposite direction
    digitalWrite(MS1, MS1_MODE(modeType));  // Set state of MS1 based on the returned value from the MS1_MODE() switch statement.
    digitalWrite(MS2, MS2_MODE(modeType));  // Set state of MS2 based on the returned value from the MS2_MODE() switch statement.
    digitalWrite(SLEEP, HIGH);              // Set the Sleep mode to AWAKE.
    
    int i = 0;                              // Set the counter variable.     
    while(i<(modeType*200))                 // Iterate for 200, then 400, then 800, then 1600 steps. 
                                            // Then reset to 200 and start again.
    {
      digitalWrite(STEP, LOW);              // This LOW to HIGH change is what creates the..
      digitalWrite(STEP, HIGH);             // .."Rising Edge" so the easydriver knows to when to step.
      delayMicroseconds(1600/modeType);     // This delay time determines the speed of the stepper motor. 
                                            // Delay shortens from 1600 to 800 to 400 to 200 then resets  
                                                 
      i++;                      
    }                              
    modeType = modeType * 2;                // Multiply the current modeType value by 2 and make the result the new value for modeType.
                                            // This will make the modeType variable count 1,2,4,8 each time we pass though the while loop.
   
    delay(500);
  }
  digitalWrite(SLEEP, LOW);                 // switch off the power to stepper
  Serial.print("SLEEPING..");
  delay(1000);
  Serial.print("z");
  delay(1000);
  Serial.print("z");
  delay(1000);
  Serial.print("z");
  delay(1000);
  Serial.println("");
  digitalWrite(SLEEP, HIGH);
  Serial.println("AWAKE!!!");                // Switch on the power to stepper
  delay(1000);
}



int MS1_MODE(int MS1_StepMode){              // A function that returns a High or Low state number for MS1 pin
  switch(MS1_StepMode){                      // Switch statement for changing the MS1 pin state
                                             // Different input states allowed are 1,2,4 or 8
  case 1:
    MS1_StepMode = 0;
    Serial.println("Step Mode is Full...");
    break;
  case 2:
    MS1_StepMode = 1;
    Serial.println("Step Mode is Half...");
    break;
  case 4:
    MS1_StepMode = 0;
    Serial.println("Step Mode is Quarter...");
    break;
  case 8:
    MS1_StepMode = 1;
    Serial.println("Step Mode is Eighth...");
    break;
  }
  return MS1_StepMode;
}



int MS2_MODE(int MS2_StepMode){              // A function that returns a High or Low state number for MS2 pin
  switch(MS2_StepMode){                      // Switch statement for changing the MS2 pin state
                                             // Different input states allowed are 1,2,4 or 8
  case 1:
    MS2_StepMode = 0;
    break;
  case 2:
    MS2_StepMode = 0;
    break;
  case 4:
    MS2_StepMode = 1;
    break;
  case 8:
    MS2_StepMode = 1;
    break;
  }
  return MS2_StepMode;
}





test

///////////////////////////////////////////////////////////
// Stepper Motor skecth for use with the EasyDriver v4.2 //
///////////////////////////////////////////////////////////

// Dan Thompson 2010
//
// Use this code at your own risk.
//
// For all the product details visit http://greta.dhs.org/EasyDriver/
// For the full tutorial visit http://danthompsonsblog.blogspot.com/


////// ED_v4  Step Mode Chart //////
//                                //
//   MS1 MS2 Resolution           //
//   L   L   Full step (2 phase)  //
//   H   L   Half step            //
//   L   H   Quarter step         //
//   H   H   Eighth step          //
//                                //
////////////////////////////////////

int DIR = 3;          // PIN  3 = DIR
int STEP = 2;        // PIN  2 = STEP
int MS1 = 13;        // PIN 13 = MS
int MS2 = 9;         // PIN  9 = MS2
int SLEEP = 12;      // PIN 12 = SLP


void setup() {
  Serial.begin(9600);     // open the serial connection at 9600bps
  pinMode(DIR, OUTPUT);   // set pin 3 to output
  pinMode(STEP, OUTPUT);  // set pin 2 to output
  pinMode(MS1, OUTPUT);   // set pin 13 to output
  pinMode(MS2, OUTPUT);   // set pin 9 to output
  pinMode(SLEEP, OUTPUT); // set pin 12 to output
}

test

///////////////////////////////////////////////////////////
// Stepper Motor skecth for use with the EasyDriver v4.2 //
///////////////////////////////////////////////////////////

// Dan Thompson 2010
//
// Use this code at your own risk.
//
// For all the product details visit http://greta.dhs.org/EasyDriver/
// For the full tutorial visit http://danthompsonsblog.blogspot.com/


////// ED_v4  Step Mode Chart //////
//                                //
//   MS1 MS2 Resolution           //
//   L   L   Full step (2 phase)  //
//   H   L   Half step            //
//   L   H   Quarter step         //
//   H   H   Eighth step          //
//                                //
////////////////////////////////////

int DIR = 3;          // PIN  3 = DIR
int STEP = 2;        // PIN  2 = STEP
int MS1 = 13;        // PIN 13 = MS
int MS2 = 9;         // PIN  9 = MS2
int SLEEP = 12;      // PIN 12 = SLP


void setup() {
  Serial.begin(9600);     // open the serial connection at 9600bps
  pinMode(DIR, OUTPUT);   // set pin 3 to output
  pinMode(STEP, OUTPUT);  // set pin 2 to output
  pinMode(MS1, OUTPUT);   // set pin 13 to output
  pinMode(MS2, OUTPUT);   // set pin 9 to output
  pinMode(SLEEP, OUTPUT); // set pin 12 to output
}



void loop()
{
  int modeType = 1;                         // This number increases by multiple of 2 each through the while loop..
                                            // ..to identify our step mode type.                                            
  while (modeType<=8){                      // loops the following block of code 4 times before repeating .
    digitalWrite(DIR, LOW);                 // Set the direction change LOW to HIGH to go in opposite direction
    digitalWrite(MS1, MS1_MODE(modeType));  // Set state of MS1 based on the returned value from the MS1_MODE() switch statement.
    digitalWrite(MS2, MS2_MODE(modeType));  // Set state of MS2 based on the returned value from the MS2_MODE() switch statement.
    digitalWrite(SLEEP, HIGH);              // Set the Sleep mode to AWAKE.
    
    int i = 0;                              // Set the counter variable.     
    while(i<(modeType*200))                 // Iterate for 200, then 400, then 800, then 1600 steps. 
                                            // Then reset to 200 and start again.
    {
      digitalWrite(STEP, LOW);              // This LOW to HIGH change is what creates the..
      digitalWrite(STEP, HIGH);             // .."Rising Edge" so the easydriver knows to when to step.
      delayMicroseconds(1600/modeType);     // This delay time determines the speed of the stepper motor. 
                                            // Delay shortens from 1600 to 800 to 400 to 200 then resets  
                                                 
      i++;                      
    }                              
    modeType = modeType * 2;                // Multiply the current modeType value by 2 and make the result the new value for modeType.
                                            // This will make the modeType variable count 1,2,4,8 each time we pass though the while loop.
   
    delay(500);
  }
  digitalWrite(SLEEP, LOW);                 // switch off the power to stepper
  Serial.print("SLEEPING..");
  delay(1000);
  Serial.print("z");
  delay(1000);
  Serial.print("z");
  delay(1000);
  Serial.print("z");
  delay(1000);
  Serial.println("");
  digitalWrite(SLEEP, HIGH);
  Serial.println("AWAKE!!!");                // Switch on the power to stepper
  delay(1000);
}



int MS1_MODE(int MS1_StepMode){              // A function that returns a High or Low state number for MS1 pin
  switch(MS1_StepMode){                      // Switch statement for changing the MS1 pin state
                                             // Different input states allowed are 1,2,4 or 8
  case 1:
    MS1_StepMode = 0;
    Serial.println("Step Mode is Full...");
    break;
  case 2:
    MS1_StepMode = 1;
    Serial.println("Step Mode is Half...");
    break;
  case 4:
    MS1_StepMode = 0;
    Serial.println("Step Mode is Quarter...");
    break;
  case 8:
    MS1_StepMode = 1;
    Serial.println("Step Mode is Eighth...");
    break;
  }
  return MS1_StepMode;
}



int MS2_MODE(int MS2_StepMode){              // A function that returns a High or Low state number for MS2 pin
  switch(MS2_StepMode){                      // Switch statement for changing the MS2 pin state
                                             // Different input states allowed are 1,2,4 or 8
  case 1:
    MS2_StepMode = 0;
    break;
  case 2:
    MS2_StepMode = 0;
    break;
  case 4:
    MS2_StepMode = 1;
    break;
  case 8:
    MS2_StepMode = 1;
    break;
  }
  return MS2_StepMode;
}





################################################
# Module: motor.py
# Created: 21 April 2009
# Author: Daniel Thompson
# http://danthompsonsblog.blogspot.com/
# Version: 0.2
# License: GPLv3
# http://www.fsf.org/licensing/
'''Provides a serial connection abstraction layer
for use with Arduino "MultipleSteppers" sketch.
'''
#######################fhgjfhjfghjfghjgfhjfghjgfhjghjghjghj#########################
import motor
import serial

#setup the serial port
usbport = 'COM4'
ser = serial.Serial(usbport, 115200, timeout=1)

# convert a decimal (denary, base 10) integer to a binary string (base 2)
def d2b(n):
'''convert denary integer n to binary string bStr'''
bStr = ''
if n < 0: raise ValueError, "must be a positive integer"
if n == 0:
return '0'
while n > 0:
bStr = str(n % 2) + bStr
n = n >> 1
return bStr

'''Returns the number of bit bytes (eighths) in binary string.
Always rounds up, never rounds down.'''
def binaryCount(number):
sNumber = str(number)
length = len(sNumber)
numBytes = length / 8.0
numBytesInt = length / 8
if numBytes > numBytesInt:
return numBytesInt + 1
else:
return numBytesInt

'''This function gets the first byte, removes it from the string and
returns a new value'''
def shiftLeft(binary):
sBinary = str(binary)
firstByte = int(sBinary,2) & int('11111111',2)
newBinary = int(sBinary,2) >> 8
nBinary = motor.d2b(newBinary)
print "I took away", firstByte
print "And now I'm left with", nBinary
return nBinary

# Wraps several functions to split
# the "steps" integer into bytes ready for sending
# Then it write packet header (255) and then
# all the other bytes

'''The final move command. I combines all the other functions in this
module to move the motor to the specified position'''
def position(num,degsPerStepFloat,stepsFloat):
steps = int(stepsFloat / degsPerStepFloat)
if(steps < 65535 and steps > -1):
binary = motor.d2b(steps)
numBytes = motor.binaryCount(binary)
# from the shiftLef def
ser.write(chr(255))
# note: the 2 in (0,2,1) is critical for alowing two bytes through every time.
for i in range(0,2,1):
sBinary = str(binary)
firstByte = int(sBinary,2) & int('11111111',2)
newBinary = int(sBinary,2) >> 8
binary = motor.d2b(newBinary)
ser.write(chr(firstByte))
if(steps > -65535 and steps < 0):
stepsNeg = steps * -1
binary = motor.d2b(stepsNeg)
numBytes = motor.binaryCount(binary)
# from the shiftLef def
ser.write(chr(220))
# note: the 2 in (0,2,1) is critical for alowing two bytes through every time.
for i in range(0,2,1):
sBinary = str(binary)
firstByte = int(sBinary,2) & int('11111111',2)
newBinary = int(sBinary,2) >> 8
binary = motor.d2b(newBinary)
ser.write(chr(firstByte))
return stepsFloat
################################################ # Module: motor.py # Created: 21 April 2009 # Author: Daniel Thompson # http://danthompsonsblog.blogspot.com/ # Version: 0.2 # License: GPLv3 # http://www.fsf.org/licensing/ '''Provides a serial connection abstraction layer for use with Arduino "MultipleSteppers" sketch. ''' ################################################ import motor import serial #setup the serial port usbport = 'COM4' ser = serial.Serial(usbport, 115200, timeout=1) # convert a decimal (denary, base 10) integer to a binary string (base 2) def d2b(n): '''convert denary integer n to binary string bStr''' bStr = '' if n < 0: raise ValueError, "must be a positive integer" if n == 0: return '0' while n > 0: bStr = str(n % 2) + bStr n = n >> 1 return bStr '''Returns the number of bit bytes (eighths) in binary string. Always rounds up, never rounds down.''' def binaryCount(number): sNumber = str(number) length = len(sNumber) numBytes = length / 8.0 numBytesInt = length / 8 if numBytes > numBytesInt: return numBytesInt + 1 else: return numBytesInt '''This function gets the first byte, removes it from the string and returns a new value''' def shiftLeft(binary): sBinary = str(binary) firstByte = int(sBinary,2) & int('11111111',2) newBinary = int(sBinary,2) >> 8 nBinary = motor.d2b(newBinary) print "I took away", firstByte print "And now I'm left with", nBinary return nBinary # Wraps several functions to split # the "steps" integer into bytes ready for sending # Then it write packet header (255) and then # all the other bytes '''The final move command. I combines all the other functions in this module to move the motor to the specified position''' def position(num,degsPerStepFloat,stepsFloat): steps = int(stepsFloat / degsPerStepFloat) if(steps < 65535 and steps > -1): binary = motor.d2b(steps) numBytes = motor.binaryCount(binary) # from the shiftLef def ser.write(chr(255)) # note: the 2 in (0,2,1) is critical for alowing two bytes through every time. for i in range(0,2,1): sBinary = str(binary) firstByte = int(sBinary,2) & int('11111111',2) newBinary = int(sBinary,2) >> 8 binary = motor.d2b(newBinary) ser.write(chr(firstByte)) if(steps > -65535 and steps < 0): stepsNeg = steps * -1 binary = motor.d2b(stepsNeg) numBytes = motor.binaryCount(binary) # from the shiftLef def ser.write(chr(220)) # note: the 2 in (0,2,1) is critical for alowing two bytes through every time. for i in range(0,2,1): sBinary = str(binary) firstByte = int(sBinary,2) & int('11111111',2) newBinary = int(sBinary,2) >> 8 binary = motor.d2b(newBinary) ser.write(chr(firstByte)) return stepsFloat

hooray!


################################################
# Module:   motor.py
# Created:  21 April 2009
# Author:   Daniel Thompson
# http://danthompsonsblog.blogspot.com/
# Version:  0.2
# License:  GPLv3
#   http://www.fsf.org/licensing/
'''Provides a serial connection abstraction layer
for use with Arduino "MultipleSteppers" sketch.
'''
################################################
import motor
import serial

#setup the serial port
usbport = 'COM4'
ser = serial.Serial(usbport, 115200, timeout=1)

# convert a decimal (denary, base 10) integer to a binary string (base 2)
def d2b(n):
   '''convert denary integer n to binary string bStr'''
   bStr = ''
   if n < 0: raise ValueError, "must be a positive integer"
   if n == 0:
       return '0'
   while n > 0:
       bStr = str(n % 2) + bStr
       n = n >> 1
   return bStr

'''Returns the number of bit bytes (eighths) in binary string.
Always rounds up, never rounds down.'''
def binaryCount(number):
       sNumber = str(number)
       length = len(sNumber)
       numBytes = length / 8.0
       numBytesInt = length / 8
       if numBytes > numBytesInt:
                       return numBytesInt + 1
       else:
               return numBytesInt

'''This function gets the first byte, removes it from the string and
returns a new value'''
def shiftLeft(binary):
       sBinary = str(binary)
       firstByte = int(sBinary,2) & int('11111111',2)
       newBinary = int(sBinary,2) >> 8
       nBinary = motor.d2b(newBinary)
       print "I took away", firstByte
       print "And now I'm left with", nBinary
       return nBinary

# Wraps several functions to split
# the "steps" integer into bytes ready for sending
# Then it write packet header (255) and then
# all the other bytes

'''The final move command. I combines all the other functions in this
module to move the motor to the specified position'''
def position(num,degsPerStepFloat,stepsFloat):
       steps = int(stepsFloat / degsPerStepFloat)
       if(steps < 65535 and steps > -1):
               binary = motor.d2b(steps)
               numBytes = motor.binaryCount(binary)
       # from the shiftLef def
               ser.write(chr(255))
               # note: the 2 in (0,2,1) is critical for alowing two bytes through every time.
               for i in range(0,2,1):
                       sBinary = str(binary)
                       firstByte = int(sBinary,2) & int('11111111',2)
                       newBinary = int(sBinary,2) >> 8
                       binary = motor.d2b(newBinary)
                       ser.write(chr(firstByte))
       if(steps > -65535 and steps < 0):
               stepsNeg = steps * -1
               binary = motor.d2b(stepsNeg)
               numBytes = motor.binaryCount(binary)
       # from the shiftLef def
               ser.write(chr(220))
               # note: the 2 in (0,2,1) is critical for alowing two bytes through every time.
               for i in range(0,2,1):
                       sBinary = str(binary)
                       firstByte = int(sBinary,2) & int('11111111',2)
                       newBinary = int(sBinary,2) >> 8
                       binary = motor.d2b(newBinary)
                       ser.write(chr(firstByte))
       return stepsFloat

Arduino

################################################ # Module: motor.py # Created: 21 April 2009 # Author: Daniel Thompson # http://danthompsonsblog.blogspot.com/ # Version: 0.2 # License: GPLv3 # http://www.fsf.org/licensing/ '''Provides a serial connection abstraction layer for use with Arduino "MultipleSteppers" sketch. ''' ################################################ import motor import serial #setup the serial port usbport = 'COM4' ser = serial.Serial(usbport, 115200, timeout=1) # convert a decimal (denary, base 10) integer to a binary string (base 2) def d2b(n): '''convert denary integer n to binary string bStr''' bStr = '' if n &lt; 0: raise ValueError, "must be a positive integer" if n == 0: return '0' while n &gt; 0: bStr = str(n % 2) + bStr n = n &gt;&gt; 1 return bStr '''Returns the number of bit bytes (eighths) in binary string. Always rounds up, never rounds down.''' def binaryCount(number): sNumber = str(number) length = len(sNumber) numBytes = length / 8.0 numBytesInt = length / 8 if numBytes &gt; numBytesInt: return numBytesInt + 1 else: return numBytesInt '''This function gets the first byte, removes it from the string and returns a new value''' def shiftLeft(binary): sBinary = str(binary) firstByte = int(sBinary,2) &amp; int('11111111',2) newBinary = int(sBinary,2) &gt;&gt; 8 nBinary = motor.d2b(newBinary) print "I took away", firstByte print "And now I'm left with", nBinary return nBinary # Wraps several functions to split # the "steps" integer into bytes ready for sending # Then it write packet header (255) and then # all the other bytes '''The final move command. I combines all the other functions in this module to move the motor to the specified position''' def position(num,degsPerStepFloat,stepsFloat): steps = int(stepsFloat / degsPerStepFloat) if(steps &lt; 65535 and steps &gt; -1): binary = motor.d2b(steps) numBytes = motor.binaryCount(binary) # from the shiftLef def ser.write(chr(255)) # note: the 2 in (0,2,1) is critical for alowing two bytes through every time. for i in range(0,2,1): sBinary = str(binary) firstByte = int(sBinary,2) &amp; int('11111111',2) newBinary = int(sBinary,2) &gt;&gt; 8 binary = motor.d2b(newBinary) ser.write(chr(firstByte)) if(steps &gt; -65535 and steps &lt; 0): stepsNeg = steps * -1 binary = motor.d2b(stepsNeg) numBytes = motor.binaryCount(binary) # from the shiftLef def ser.write(chr(220)) # note: the 2 in (0,2,1) is critical for alowing two bytes through every time. for i in range(0,2,1): sBinary = str(binary) firstByte = int(sBinary,2) &amp; int('11111111',2) newBinary = int(sBinary,2) &gt;&gt; 8 binary = motor.d2b(newBinary) ser.write(chr(firstByte)) return stepsFloat Arduino Code ################################################ # Module: motor.py # Created: 21 April 2009 # Author: Daniel Thompson # http://danthompsonsblog.blogspot.com/ # Version: 0.2 # License: GPLv3 # http://www.fsf.org/licensing/ '''Provides a serial connection abstraction layer for use with Arduino "MultipleSteppers" sketch. ''' ################################################ import motor import serial #setup the serial port usbport = 'COM4' ser = serial.Serial(usbport, 115200, timeout=1) # convert a decimal (denary, base 10) integer to a binary string (base 2) def d2b(n): '''convert denary integer n to binary string bStr''' bStr = '' if n &lt; 0: raise ValueError, "must be a positive integer" if n == 0: return '0' while n &gt; 0: bStr = str(n % 2) + bStr n = n &gt;&gt; 1 return bStr '''Returns the number of bit bytes (eighths) in binary string. Always rounds up, never rounds down.''' def binaryCount(number): sNumber = str(number) length = len(sNumber) numBytes = length / 8.0 numBytesInt = length / 8 if numBytes &gt; numBytesInt: return numBytesInt + 1 else: return numBytesInt '''This function gets the first byte, removes it from the string and returns a new value''' def shiftLeft(binary): sBinary = str(binary) firstByte = int(sBinary,2) &amp; int('11111111',2) newBinary = int(sBinary,2) &gt;&gt; 8 nBinary = motor.d2b(newBinary) print "I took away", firstByte print "And now I'm left with", nBinary return nBinary # Wraps several functions to split # the "steps" integer into bytes ready for sending # Then it write packet header (255) and then # all the other bytes '''The final move command. I combines all the other functions in this module to move the motor to the specified position''' def position(num,degsPerStepFloat,stepsFloat): steps = int(stepsFloat / degsPerStepFloat) if(steps &lt; 65535 and steps &gt; -1): binary = motor.d2b(steps) numBytes = motor.binaryCount(binary) # from the shiftLef def ser.write(chr(255)) # note: the 2 in (0,2,1) is critical for alowing two bytes through every time. for i in range(0,2,1): sBinary = str(binary) firstByte = int(sBinary,2) &amp; int('11111111',2) newBinary = int(sBinary,2) &gt;&gt; 8 binary = motor.d2b(newBinary) ser.write(chr(firstByte)) if(steps &gt; -65535 and steps &lt; 0): stepsNeg = steps * -1 binary = motor.d2b(stepsNeg) numBytes = motor.binaryCount(binary) # from the shiftLef def ser.write(chr(220)) # note: the 2 in (0,2,1) is critical for alowing two bytes through every time. for i in range(0,2,1): sBinary = str(binary) firstByte = int(sBinary,2) &amp; int('11111111',2) newBinary = int(sBinary,2) &gt;&gt; 8 binary = motor.d2b(newBinary) ser.write(chr(firstByte)) return stepsFloat

fixed??

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
################################################
# Module:   motor.py
# Created:  21 April 2009
# Author:   Daniel Thompson
# http://danthompsonsblog.blogspot.com/
# Version:  0.2
# License:  GPLv3
#   http://www.fsf.org/licensing/
'''Provides a serial connection abstraction layer
for use with Arduino "MultipleSteppers" sketch.
'''
################################################
import motor
import serial

#setup the serial port
usbport = 'COM4'
ser = serial.Serial(usbport, 115200, timeout=1)

# convert a decimal (denary, base 10) integer to a binary string (base 2)
def d2b(n):
   '''convert denary integer n to binary string bStr'''
   bStr = ''
   if n < 0: raise ValueError, "must be a positive integer"
   if n == 0:
       return '0'
   while n > 0:
       bStr = str(n % 2) + bStr
       n = n >> 1
   return bStr

'''Returns the number of bit bytes (eighths) in binary string.
Always rounds up, never rounds down.'''
def binaryCount(number):
       sNumber = str(number)
       length = len(sNumber)
       numBytes = length / 8.0
       numBytesInt = length / 8
       if numBytes > numBytesInt:
                       return numBytesInt + 1
       else:
               return numBytesInt

'''This function gets the first byte, removes it from the string and
returns a new value'''
def shiftLeft(binary):
       sBinary = str(binary)
       firstByte = int(sBinary,2) & int('11111111',2)
       newBinary = int(sBinary,2) >> 8
       nBinary = motor.d2b(newBinary)
       print "I took away", firstByte
       print "And now I'm left with", nBinary
       return nBinary

# Wraps several functions to split
# the "steps" integer into bytes ready for sending
# Then it write packet header (255) and then
# all the other bytes

'''The final move command. I combines all the other functions in this
module to move the motor to the specified position'''
def position(num,degsPerStepFloat,stepsFloat):
       steps = int(stepsFloat / degsPerStepFloat)
       if(steps < 65535 and steps > -1):
               binary = motor.d2b(steps)
               numBytes = motor.binaryCount(binary)
       # from the shiftLef def
               ser.write(chr(255))
               # note: the 2 in (0,2,1) is critical for alowing two bytes through every time.
               for i in range(0,2,1):
                       sBinary = str(binary)
                       firstByte = int(sBinary,2) & int('11111111',2)
                       newBinary = int(sBinary,2) >> 8
                       binary = motor.d2b(newBinary)
                       ser.write(chr(firstByte))
       if(steps > -65535 and steps < 0):
               stepsNeg = steps * -1
               binary = motor.d2b(stepsNeg)
               numBytes = motor.binaryCount(binary)
       # from the shiftLef def
               ser.write(chr(220))
               # note: the 2 in (0,2,1) is critical for alowing two bytes through every time.
               for i in range(0,2,1):
                       sBinary = str(binary)
                       firstByte = int(sBinary,2) & int('11111111',2)
                       newBinary = int(sBinary,2) >> 8
                       binary = motor.d2b(newBinary)
                       ser.write(chr(firstByte))
       return stepsFloat

tester

#!/usr/bin/env python

import sys
import traceback
import utils
from bin.server import LearndServer

if __name__ == '__main__':
learnd = LearndServer( )

try:
learnd.run()
except KeyboardInterrupt:
utils.log("KeyboardInterrupt - Exiting.")

learnd.close()
except:
traceback.print_exc()
utils.log("Exiting.")

learnd.close()