Guest User

Untitled

a guest
Apr 28th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #########################################################################
  3. #Author:Paul Binder Jr
  4. ########################################################################
  5.  
  6. import smbus
  7. import time
  8. import RPi.GPIO as GPIO
  9.  
  10. rev = GPIO.RPI_REVISION
  11. if rev == 2 or rev == 3:
  12.     bus = smbus.SMBus(1)
  13. else:
  14.     bus = smbus.SMBus(0)
  15.  
  16.  
  17.    
  18. MOTOR_SPEED_SET = 0x82
  19. PWM_FREQUENCE_SET = 0x84
  20. DIRECTION_SET = 0xaa
  21. MOTOR_SET_A = 0xa1
  22. MOTOR_SET_B = 0xa5
  23. NOTHING = 0x01
  24. ENABLE_STEPPER = 0x1a
  25. UNENABLE_STEPPER = 0x1b
  26. STEPERNU = 0x1c
  27. I2C_MOTOR_DRIVER_ADD = 0x0f #Set the address of the I2CMotorDriver
  28.  
  29. ##set the steps you want, if 255, the stepper will rotate continuely;
  30. def SteperStepset(stepnu):
  31.     bus.write_i2c_block_data(I2C_MOTOR_DRIVER_ADD,STEPERNU,[stepnu,NOTHING])
  32.  
  33. ## Enanble the i2c motor driver to drive a 4-wire stepper. the i2c motor driver will
  34. ##driver a 4-wire with 8 polarity  .
  35. ##Direction: stepper direction ; 1/0
  36. ##motor speed: defines the time interval the i2C motor driver change it output to drive the stepper
  37. ##the actul interval time is : motorspeed * 4ms. that is , when motor speed is 10, the interval time
  38. ##would be 40 ms
  39.  
  40. def StepperMotorEnable(Direction,motorspeed):
  41.     bus.write_i2c_block_data(I2C_MOTOR_DRIVER_ADD,ENABLE_STEPPER,[Direction,motorspeed])
  42.        
  43. ##function to uneanble i2C motor drive to drive the stepper.
  44. def StepperMotorUnenable():
  45.     bus.write_i2c_block_data(I2C_MOTOR_DRIVER_ADD,UNENABLE_STEPPER,[NOTHING,NOTHING])
  46.    
  47. ##Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow,
  48. ##a value of fromHigh to toHigh, values in-between to values in-between, etc.
  49. ##see http://www.arduino.cc/en/Reference/Map
  50. def Map(x, in_min, in_max, out_min, out_max):
  51.     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
  52.  
  53.  
  54. ##Function to set the 2 DC motor speed
  55. ##motorSpeedA : the DC motor A speed; should be 0~100;
  56. ##motorSpeedB: the DC motor B speed; should be 0~100;
  57.  
  58. def MotorSpeedSetAB(MotorSpeedA , MotorSpeedB):
  59.     MotorSpeedA=Map(MotorSpeedA,0,100,0,255)
  60.     MotorSpeedB=Map(MotorSpeedB,0,100,0,255)
  61.     bus.write_i2c_block_data(I2C_MOTOR_DRIVER_ADD,MOTOR_SPEED_SET,[MotorSpeedA,MotorSpeedB])
  62.  
  63. ##set the prescale frequency of PWM, 0x03 default;
  64. def MotorPWMFrequenceSet(Frequence):  
  65.     bus.write_i2c_block_data(I2C_MOTOR_DRIVER_ADD,PWM_FREQUENCE_SET,[Frequence,NOTHING])
  66.  
  67. ##set the direction of DC motor.
  68. ## Adjust the direction of the motors 0b0000 I4 I3 I2 I1
  69. def MotorDirectionSet(Direction):
  70.     bus.write_i2c_block_data(I2C_MOTOR_DRIVER_ADD,DIRECTION_SET,[Direction,NOTHING])
  71.  
  72. ##you can adjust the driection and speed together
  73. def MotorDriectionAndSpeedSet(Direction,MotorSpeedA,MotorSpeedB) :
  74.     MotorDirectionSet(Direction)
  75.     MotorSpeedSetAB(MotorSpeedA,MotorSpeedB)
Advertisement
Add Comment
Please, Sign In to add comment