Advertisement
silver2row

MBC LIB

Dec 31st, 2019
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.84 KB | None | 0 0
  1. # /*
  2.  # * MotorBridge.py
  3.  # * This is a library for BBG/BBB motor bridge cape
  4.  # *
  5.  # * Copyright (c) 2015 seeed technology inc.
  6.  # * Author      : Jiankai Li
  7.  # * Create Time:  Nov 2015
  8.  # * Change Log : From #beagle on Freenode and my changes (Seth 2019)!
  9.  # *
  10.  # * The MIT License (MIT)
  11.  # *
  12.  # * Permission is hereby granted, free of charge, to any person obtaining a copy
  13.  # * of this software and associated documentation files (the "Software"), to deal
  14.  # * in the Software without restriction, including without limitation the rights
  15.  # * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16.  # * copies of the Software, and to permit persons to whom the Software is
  17.  # * furnished to do so, subject to the following conditions:
  18.  # *
  19.  # * The above copyright notice and this permission notice shall be included in
  20.  # * all copies or substantial portions of the Software.
  21.  # *
  22.  # * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23.  # * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24.  # * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25.  # * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26.  # * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27.  # * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28.  # * THE SOFTWARE.
  29.  # */
  30. # FileName : MotorBridge.py
  31. # by Jiankai.li
  32.  
  33. from smbus2 import SMBus
  34. import time
  35. import pathlib
  36.  
  37. # reset pin is P9.23, i.e. gpio1.17
  38. reset_pin = pathlib.Path('/sys/class/gpio/gpio49/direction')
  39. reset_pin.write_text('low')
  40.  
  41. MotorBridge = SMBus('/dev/i2c-2')
  42.  
  43. ReadMode  = 0
  44. WriteMode = 1
  45. DeAddr    = 0X4B
  46. ConfigValid =  0x3a6fb67c
  47. DelayTime = 0.005
  48.  
  49. # TB_WORKMODE
  50.  
  51. TB_SHORT_BREAK  = 0
  52. TB_CW           = 1
  53. TB_CCW          = 2
  54. TB_STOP         = 3
  55. TB_WORKMODE_NUM = 4
  56.  
  57.  
  58. # TB_PORTMODE
  59.  
  60. TB_DCM          = 0
  61. TB_SPM          = 1
  62. TB_PORTMODE_NUM = 2
  63.  
  64.  
  65. # SVM_PORT
  66.  
  67. SVM1            = 0
  68. SVM2            = 1
  69. SVM3            = 2
  70. SVM4            = 3
  71. SVM5            = 4
  72. SVM6            = 5
  73. SVM_PORT_NUM    = 6
  74.  
  75. # SVM_STATE
  76.  
  77. SVM_DISABLE     = 0
  78. SVM_ENABLE      = 1
  79. SVM_STATE_NUM   = 2
  80.  
  81. # IO_MODE
  82.  
  83. IO_IN           = 0
  84. IO_OUT          = 1
  85. IO_MODE_NUM     = 2
  86.  
  87. # IO_PUPD
  88.  
  89. IO_PU           = 0
  90. IO_PD           = 1
  91. IO_NP           = 2
  92. IO_PUPD_NUM     = 3
  93.  
  94. # IO_PPOD
  95.  
  96. IO_PP           = 0
  97. IO_OD           = 1
  98. IO_PPOD_NUM     = 2
  99.  
  100. # IO_STATE
  101.  
  102. IO_LOW          = 0
  103. IO_HIGH         = 1
  104. IO_STATE_NUM    = 2
  105.  
  106. # IO_PORT
  107.  
  108. IO1             = 0
  109. IO2             = 1
  110. IO3             = 2
  111. IO4             = 3
  112. IO5             = 4
  113. IO6             = 5
  114. IO_NUM          = 6
  115.  
  116.  
  117. # PARAM_REG
  118.  
  119. CONFIG_VALID        = 0
  120. CONFIG_TB_PWM_FREQ  = CONFIG_VALID + 4
  121.  
  122. I2C_ADDRESS         = CONFIG_TB_PWM_FREQ + 4
  123.  
  124. TB_1A_MODE          = I2C_ADDRESS + 1
  125. TB_1A_DIR           = TB_1A_MODE + 1
  126. TB_1A_DUTY          = TB_1A_DIR + 1
  127. TB_1A_SPM_SPEED     = TB_1A_DUTY + 2
  128. TB_1A_SPM_STEP      = TB_1A_SPM_SPEED + 4
  129.  
  130. TB_1B_MODE          = TB_1A_SPM_STEP + 4
  131. TB_1B_DIR           = TB_1B_MODE + 1
  132. TB_1B_DUTY          = TB_1B_DIR + 1
  133. TB_1B_SPM_SPEED     = TB_1B_DUTY + 2
  134. TB_1B_SPM_STEP      = TB_1B_SPM_SPEED + 4
  135.  
  136. TB_2A_MODE          = TB_1B_SPM_STEP + 4
  137. TB_2A_DIR           = TB_2A_MODE + 1
  138. TB_2A_DUTY          = TB_2A_DIR + 1
  139. TB_2A_SPM_SPEED     = TB_2A_DUTY + 2
  140. TB_2A_SPM_STEP      = TB_2A_SPM_SPEED + 4
  141.  
  142. TB_2B_MODE          = TB_2A_SPM_STEP + 4
  143. TB_2B_DIR           = TB_2B_MODE + 1
  144. TB_2B_DUTY          = TB_2B_DIR + 1
  145. TB_2B_SPM_SPEED     = TB_2B_DUTY + 2
  146. TB_2B_SPM_STEP      = TB_2B_SPM_SPEED + 4
  147.  
  148. SVM1_STATE          = TB_2B_SPM_STEP + 4
  149. SVM1_FREQ           = SVM1_STATE + 1
  150. SVM1_ANGLE          = SVM1_FREQ + 2
  151.  
  152. SVM2_STATE          = SVM1_ANGLE + 2
  153. SVM2_FREQ           = SVM2_STATE + 1
  154. SVM2_ANGLE          = SVM2_FREQ + 2
  155.  
  156. SVM3_STATE          = SVM2_ANGLE + 2
  157. SVM3_FREQ           = SVM3_STATE + 1
  158. SVM3_ANGLE          = SVM3_FREQ + 2
  159.  
  160. SVM4_STATE          = SVM3_ANGLE + 2
  161. SVM4_FREQ           = SVM4_STATE + 1
  162. SVM4_ANGLE          = SVM4_FREQ + 2
  163.  
  164. SVM5_STATE          = SVM4_ANGLE + 2
  165. SVM5_FREQ           = SVM5_STATE + 1
  166. SVM5_ANGLE          = SVM5_FREQ + 2
  167.  
  168. SVM6_STATE          = SVM5_ANGLE + 2
  169. SVM6_FREQ           = SVM6_STATE + 1
  170. SVM6_ANGLE          = SVM6_FREQ + 2
  171.  
  172. IO1_STATE           = SVM6_ANGLE + 2
  173. IO1_MODE            = IO1_STATE + 1
  174. IO1_PUPD            = IO1_MODE + 1
  175. IO1_PPOD            = IO1_PUPD + 1
  176.  
  177. IO2_STATE           = IO1_PPOD + 1
  178. IO2_MODE            = IO2_STATE + 1
  179. IO2_PUPD            = IO2_MODE + 1
  180. IO2_PPOD            = IO2_PUPD + 1
  181.  
  182. IO3_STATE           = IO2_PPOD + 1
  183. IO3_MODE            = IO3_STATE + 1
  184. IO3_PUPD            = IO3_MODE + 1
  185. IO3_PPOD            = IO3_PUPD + 1
  186.  
  187. IO4_STATE           = IO3_PPOD + 1
  188. IO4_MODE            = IO4_STATE + 1
  189. IO4_PUPD            = IO4_MODE + 1
  190. IO4_PPOD            = IO4_PUPD + 1
  191.  
  192. IO5_STATE           = IO4_PPOD + 1
  193. IO5_MODE            = IO5_STATE + 1
  194. IO5_PUPD            = IO5_MODE + 1
  195. IO5_PPOD            = IO5_PUPD + 1
  196.  
  197. IO6_STATE           = IO5_PPOD + 1
  198. IO6_MODE            = IO6_STATE + 1
  199. IO6_PUPD            = IO6_MODE + 1
  200. IO6_PPOD            = IO6_PUPD + 1
  201.  
  202. PARAM_REG_NUM = IO6_PPOD + 1
  203.  
  204. def WriteByte(Reg,Value):
  205.     data = [0 for i in range(2)]
  206.     data[0] = Reg
  207.     data[1] = Value
  208.     MotorBridge.write_i2c_block_data(0x4b, 1, data)
  209.  
  210. def WriteHalfWord(Reg,Value):
  211.     data = [0 for i in range(3)]
  212.     data[0] = Reg
  213.     data[1] = Value & 0xff
  214.     data[2] = (Value>>8) & 0xff
  215.     MotorBridge.write_i2c_block_data(0x4b, 1, data)
  216.  
  217. def WriteOneWord(Reg,Value):
  218.     data = [0 for i in range(5)]
  219.     data[0] = Reg
  220.     data[1] = Value & 0xff
  221.     data[2] = (Value>>8) & 0xff
  222.     data[3] = (Value>>16) & 0xff
  223.     data[4] = (Value>>24) & 0xff
  224.     MotorBridge.write_i2c_block_data(0x4b, 1, data)
  225.  
  226. def SetDefault():
  227.     WriteOneWord(CONFIG_VALID,0x00000000)
  228.  
  229. class MotorBridgeCape:
  230.     def __init__(self):
  231.         reset_pin.write_text('high')
  232.         time.sleep(1)
  233.  
  234.     # init stepper motor A
  235.     def StepperMotorAInit(self):
  236.         WriteByte(TB_1A_MODE,TB_SPM) #Stepper
  237.         time.sleep(DelayTime)
  238.         WriteHalfWord(TB_1A_DUTY,1000)    # voltage
  239.         time.sleep(DelayTime)
  240.  
  241.     # MoveSteps > 0 CW
  242.     # MoveSteps < 0 CCW
  243.     # StepDelayTime : delay time for every step. uint us
  244.     def StepperMotorAMove(self,MoveSteps,StepDelayTime):
  245.         if MoveSteps > 0:
  246.             WriteByte(TB_1A_DIR,TB_CW)   #CW
  247.         else:
  248.             WriteByte(TB_1A_DIR,TB_CCW)   #CW
  249.             MoveSteps = -MoveSteps
  250.         time.sleep(DelayTime)
  251.         WriteOneWord(TB_1A_SPM_SPEED,StepDelayTime)  # unit us
  252.         time.sleep(DelayTime)
  253.         WriteOneWord(TB_1A_SPM_STEP,MoveSteps)
  254.         time.sleep(DelayTime)
  255.  
  256.  
  257.     # init stepper motor B
  258.     def StepperMotorBInit(self):
  259.         WriteByte(TB_2A_MODE,TB_SPM) #Stepper
  260.         time.sleep(DelayTime)
  261.         WriteHalfWord(TB_2A_DUTY,1000)    # voltage
  262.         time.sleep(DelayTime)
  263.  
  264.     # MoveSteps > 0 CW
  265.     # MoveSteps < 0 CCW
  266.     # StepDelayTime : delay time for every step. uint us
  267.     def StepperMotorBMove(self,MoveSteps,StepDelayTime):
  268.         if MoveSteps > 0:
  269.             WriteByte(TB_2A_DIR,TB_CW)   #CW
  270.         else:
  271.             WriteByte(TB_2A_DIR,TB_CCW)   #CW
  272.             MoveSteps = -MoveSteps
  273.         time.sleep(DelayTime)
  274.         WriteOneWord(TB_2A_SPM_SPEED,StepDelayTime)  # unit us
  275.         time.sleep(DelayTime)
  276.         WriteOneWord(TB_2A_SPM_STEP,MoveSteps)
  277.         time.sleep(DelayTime)
  278.  
  279.     # Init DC Motor
  280.     def DCMotorInit(self,MotorName,Frequency):
  281.     # Init the DC Frequency
  282.         WriteOneWord(CONFIG_TB_PWM_FREQ,Frequency)
  283.         time.sleep(DelayTime)
  284.  
  285.     # Set the port as DC Motor
  286.         if MotorName == 1 or MotorName == 2:
  287.             WriteByte(TB_1A_MODE,TB_DCM)
  288.             time.sleep(DelayTime)
  289.             WriteByte(TB_1A_DIR,TB_STOP)
  290.             time.sleep(DelayTime)
  291.             WriteByte(TB_1B_MODE,TB_DCM)
  292.             time.sleep(DelayTime)
  293.             WriteByte(TB_1B_DIR,TB_STOP)
  294.             time.sleep(DelayTime)
  295.         if MotorName == 3 or MotorName == 4:
  296.             WriteByte(TB_2A_MODE,TB_DCM)
  297.             time.sleep(DelayTime)
  298.             WriteByte(TB_2A_DIR,TB_STOP)
  299.             time.sleep(DelayTime)
  300.             WriteByte(TB_2B_MODE,TB_DCM)
  301.             time.sleep(DelayTime)
  302.             WriteByte(TB_2B_DIR,TB_STOP)
  303.             time.sleep(DelayTime)
  304.  
  305.     # Drive the DC Motor
  306.     # Direction 1 CW | 2 CCW
  307.     # PWNDuty  0 ~ 100
  308.     def DCMotorMove(self, MotorName,Direction,PWMDuty):
  309.         if MotorName == 1:
  310.             WriteByte(TB_1B_DIR,Direction)
  311.             time.sleep(DelayTime)
  312.             WriteOneWord(TB_1B_DUTY,PWMDuty*10)
  313.             time.sleep(DelayTime)
  314.  
  315.         if MotorName == 2:
  316.             WriteByte(TB_1A_DIR,Direction)
  317.             time.sleep(DelayTime)
  318.             WriteOneWord(TB_1A_DUTY,PWMDuty*10)
  319.             time.sleep(DelayTime)
  320.  
  321.         if MotorName == 3:
  322.             WriteByte(TB_2B_DIR,Direction)
  323.             time.sleep(DelayTime)
  324.             WriteOneWord(TB_2B_DUTY,PWMDuty*10)
  325.             time.sleep(DelayTime)
  326.  
  327.         if MotorName == 4:
  328.             WriteByte(TB_2A_DIR,Direction)
  329.             time.sleep(DelayTime)
  330.             WriteOneWord(TB_2A_DUTY,PWMDuty*10)
  331.             time.sleep(DelayTime)
  332.  
  333.     # Stop the DC motor
  334.     def DCMotorStop(self, MotorName):
  335.         if MotorName == 1:
  336.             WriteByte(TB_1B_DIR,TB_STOP)
  337.         if MotorName == 2:
  338.             WriteByte(TB_1A_DIR,TB_STOP)
  339.         if MotorName == 3:
  340.             WriteByte(TB_2B_DIR,TB_STOP)
  341.         if MotorName == 4:
  342.             WriteByte(TB_2A_DIR,TB_STOP)
  343.         time.sleep(DelayTime)
  344.  
  345.     # init the Servo
  346.     def ServoInit(self,ServoName,Frequency):
  347.         if ServoName == 1:
  348.             WriteHalfWord(SVM1_FREQ,Frequency)
  349.             time.sleep(DelayTime)
  350.             WriteByte(SVM1_STATE,SVM_ENABLE)
  351.             time.sleep(DelayTime)
  352.  
  353.         if ServoName == 2:
  354.             WriteHalfWord(SVM2_FREQ,Frequency)
  355.             time.sleep(DelayTime)
  356.             WriteByte(SVM2_STATE,SVM_ENABLE)
  357.             time.sleep(DelayTime)
  358.         if ServoName == 3:
  359.             WriteHalfWord(SVM3_FREQ,Frequency)
  360.             time.sleep(DelayTime)
  361.             WriteByte(SVM3_STATE,SVM_ENABLE)
  362.             time.sleep(DelayTime)
  363.  
  364.         if ServoName == 4:
  365.             WriteHalfWord(SVM4_FREQ,Frequency)
  366.             time.sleep(DelayTime)
  367.             WriteByte(SVM4_STATE,SVM_ENABLE)
  368.             time.sleep(DelayTime)
  369.         if ServoName == 5:
  370.             WriteHalfWord(SVM5_FREQ,Frequency)
  371.             time.sleep(DelayTime)
  372.             WriteByte(SVM5_STATE,SVM_ENABLE)
  373.             time.sleep(DelayTime)
  374.  
  375.         if ServoName == 6:
  376.             WriteHalfWord(SVM6_FREQ,Frequency)
  377.             time.sleep(DelayTime)
  378.             WriteByte(SVM6_STATE,SVM_ENABLE)
  379.             time.sleep(DelayTime)
  380.  
  381.     def ServoMoveAngle(self,ServoName,Angle):
  382.         if ServoName == 1:
  383.             WriteHalfWord(SVM1_ANGLE,Angle)
  384.             time.sleep(DelayTime)
  385.  
  386.         if ServoName == 2:
  387.             WriteHalfWord(SVM2_ANGLE,Angle)
  388.             time.sleep(DelayTime)
  389.  
  390.         if ServoName == 3:
  391.             WriteHalfWord(SVM3_ANGLE,Angle)
  392.             time.sleep(DelayTime)
  393.  
  394.         if ServoName == 4:
  395.             WriteHalfWord(SVM4_ANGLE,Angle)
  396.             time.sleep(DelayTime)
  397.  
  398.         if ServoName == 5:
  399.             WriteHalfWord(SVM5_ANGLE,Angle)
  400.             time.sleep(DelayTime)
  401.  
  402.         if ServoName == 6:
  403.             WriteHalfWord(SVM6_ANGLE,Angle)
  404.             time.sleep(DelayTime)
  405.  
  406. if __name__=="__main__":
  407.     print( 'Hello From MotorBridge' )
  408.     #motor = MotorBridgeCape()
  409.     #motor.StepperMotorBInit()
  410.     #while True:
  411.         #motor.StepperMotorBMove(1000,1000) # 20 steppers  1000us every step
  412.         #time.sleep(1)
  413.         #motor.StepperMotorBMove(-1000,1000) # 20 steppers  1000us every step
  414.         #time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement