_SKU_DFR0592_DC_Motor_Driver_HAT - jimaobian/DFRobotWikiCn GitHub Wiki

DC Motor Driver HAT

简介

       这是一款树莓派直流电机驱动板,板载编码器接口,能同时驱动2路直流电机或带编码器的直流电机,通过I2C与树莓派通信,可以对各个电机接口进行配置和驱动。此款树莓派电机驱动板,采用STM32作为电机驱动微处理器,负责解析由上位机发送的指令,通过计算后转换成电机驱动信号。采用TB6612FNG高性能电机驱动芯片,单通道持续电流1.2A,峰值电流高达2A,电机驱动支持7~12V宽电压(电机驱动供电)输入。

技术规格

  • 驱动主控:STM32
  • 控制电路工作电压:5V
  • 电机驱动芯片:TB6612FNG
  • 电机驱动电压:7-12V
  • 通信接口:I2C接口
  • 设备地址:0x10
  • 1.2A持续驱动能力
  • 峰值电流:2A
  • 工作模式:2路直流电机或2路带编码器直流电机
  • 2个指示灯
    • 电源指示灯
    • 通信状态指示灯
  • 外形尺寸:65x30mm

接口说明

正面示意图 底面示意图
]DC Motor Driver HAT

标号

名称

功能描述

1

+

外接电源输入正极(7-12V)

2

-

外接电源输入负极

3

E1+

编码器E1正极

4

E1-

编码器E1负极

5

E1A

编码器E1 A相输出

6

E1B

编码器E1 B相输出

7

M1+

电机M1正极

8

M1-

电机M1负极

9

M2-

电机M2负极

10

M2+

电机M2正极

11

E2B

编码器E2 B相输出

12

E2A

编码器E2 A相输出

13

E2-

编码器E2负极

14

E2+

编码器E2正极

尺寸图

尺寸图

功能配置

  • 控制方式: I2C命令控制。
  • 驱动信号:100Hz~12750Hz的PWM信号
  • 编码器电机功能(仅限带编码器直流电机使用):
    • 编码器使能/失能
    • 减速比配置
    • 获取编码器电机的转速
  • 基本功能
    • 设置驱动信号频率
    • 设置占空比
    • 设置电机正反转
    • 调速
      • PWM调速(频率一定,改变占空比大小,越大转速越快)
      • 调频(改变驱动信号频率)

树莓派使用教程

硬件

warning_yellow.png

注:额定工作电压在7~12V,电流不超过2A的直流电机均可!

软件

接线图 接线图

准备步骤

step1:将DC Motor Driver HAT直插到树莓派主板上,连接电机板和电机,上电. step2:检测I2C接口是否使能。输入命令i2cdetect -y 1,若未使能,会显示如下图所示提示: I2C接口失能提示 step3:使能I2C接口。(若已开启可忽略此步)键入命令sudo raspi-config,进入配置界面,操作流程如下所示: 步骤1 步骤2 步骤3 配置完成后,键入命令sudo reboot重启树莓派。 step4:检测I2C地址。键入命令i2cdetect -y 1检查设备I2C地址,如下图所示: 检测I2C地址为0x10 step5:下载DFRobot_RaspberryPi_Motor库。键入命令'sudo git clone https://github.com/DFRobot/DFRobot_RaspberryPi_Motor ,输入ls'查看命令,如下图所示:

下载DFRobot_RaspberryPi_Motor库

样例程序

进入DFRobot_RaspberryPi_Motor库python所在目录

   cd /DFRobot_RaspberryPi_Motor/raspberry

如何驱动一个带编码器直流电机?

id参数可用使用变量M1,M2,ALL,分别表示电机M1,电机M2,M1和M2

  • step1.使能编码器;

   set_encoder_enable(self, id)
   例:
     board = Board(1,0x10)
     set_encoder_enable([board.M1])#使能编码器电机M1
     set_encoder_enable([board.M2])#使能编码器电机M2
     set_encoder_enable([board.M1,board.M2])#使能编码器电机M1和M2
     set_encoder_enable(board.ALL)#使能编码器电机M1和M2

  • step2.设置电机减速比(demo默认43:1) 跟电机的减速比有关

   set_encoder_reduction_ratio(self, id, reduction_ratio)

  • step3.设置PWM信号的频率(demo默认为1000Hz)

   set_moter_pwm_frequency(self, frequency)

  • step4.指定M1,M2或M1和M2的转向(正转或反转),和速度(占空比0~100)

   motor_movement(self, id, orientation, speed)

  • step5.获取编码器电机的速度

   get_encoder_speed(self, id)

warning_yellow.png

注:使用不同电机,其减速比参数不同,在运行demo之前,需修改所用电机的减速比!

更多功能配置及使用方法说明'''

运行DC_Motor_Demo.py示例(所用电机减速比为120:1,已修改为120),打印编码器电机的占空比和转速。

   python DC_Motor_Demo.py

程序代码如下所示:

# -*- coding:utf-8 -*-
'''
  # DC_Motor_Demo.py
  #
  # Connect board with raspberryPi.
  # Make board power and motor connection correct.
  # Run this demo.
  #
  # Motor 1 will move slow to fast, orientation clockwise,
  # motor 2 will move fast to slow, orientation count-clockwise,
  # then fast to stop. loop in few seconds.
  # Motor speed will print on terminal
  #
  # test motor: https://www.dfrobot.com/product-634.html
  #
  # Copyright   [DFRobot](http://www.dfrobot.com), 2016
  # Copyright   GNU Lesser General Public License
  #
  # version  V1.0
  # date  2019-3-26
'''
import time
from DFRobot_RaspberryPi_DC_Motor import DFRobot_DC_Motor_IIC as Board
board = Board(1, 0x10)    # Select bus 1, set address to 0x10
def board_detect():
  l = board.detecte()
  print("Board list conform:")
  print(l)
''' print last operate status, users can use this variable to determine the result of a function call. '''
def print_board_status():
  if board.last_operate_status == board.STA_OK:
    print("board status: everything ok")
  elif board.last_operate_status == board.STA_ERR:
    print("board status: unexpected error")
  elif board.last_operate_status == board.STA_ERR_DEVICE_NOT_DETECTED:
    print("board status: device not detected")
  elif board.last_operate_status == board.STA_ERR_PARAMETER:
    print("board status: parameter error, last operate no effective")
  elif board.last_operate_status == board.STA_ERR_SOFT_VERSION:
    print("board status: unsupport board framware version")
if __name__ == "__main__":
  board_detect()    # If you forget address you had set, use this to detected them, must have class instance
  # Set board controler address, use it carefully, reboot module to make it effective
  '''
  board.set_addr(0x10)
  if board.last_operate_status != board.STA_OK:
    print("set board address faild")
  else:
    print("set board address success")
  '''
  while board.begin() != board.STA_OK:    # Board begin and check board status
    print_board_status()
    print("board begin faild")
    time.sleep(2)
  print("board begin success")
  board.set_encoder_enable(board.ALL)                 # Set selected DC motor encoder enable
  # board.set_encoder_disable(board.ALL)              # Set selected DC motor encoder disable
  board.set_encoder_reduction_ratio(board.ALL, 120)    # Set selected DC motor encoder reduction ratio, test motor reduction ratio is 43.8
  board.set_moter_pwm_frequency(1000)   # Set DC motor pwm frequency to 1000HZ
  while True:
    for duty in range(5, 95, 10):   # slow to fast
      board.motor_movement([board.M1], board.CW, duty)    # DC motor 1 movement, orientation clockwise
      board.motor_movement([board.M2], board.CCW, duty)   # DC motor 2 movement, orientation count-clockwise
      time.sleep(1)
      speed = board.get_encoder_speed(board.ALL)      # Use boadrd.all to get all encoders speed
      print("duty: %d, M1 encoder speed: %d rpm, M2 encoder speed %d rpm" %(duty, speed[0], speed[1]))
    for duty in range(95, 5, - 10):   # fast to low
      board.motor_movement([board.M1], board.CW, duty)    # DC motor 1 movement, orientation clockwise
      board.motor_movement([board.M2], board.CCW, duty)   # DC motor 2 movement, orientation count-clockwise
      time.sleep(1)
      speed = board.get_encoder_speed(board.ALL)      # Use boadrd.all to get all encoders speed
      print("duty: %d, M1 encoder speed: %d rpm, M2 encoder speed %d rpm" %(duty, speed[0], speed[1]))
    print("stop all motor")
    board.motor_stop(board.ALL)   # stop all DC motor
    print_board_status()
    time.sleep(4)

程序功能:电机驱动板产生一个频率为1KHz,占空比在5%~95%范围内变化的信号,以驱动编码器电机M1顺时针先加速后减速,电机M2逆时针先加速后减速,再停止,并串口打印编码器电机的转速,如此循环。 串口打印

如何驱动一个直流电机?

只需将直流电机连接到M1+、M1-或M2+、M2-接口上,按照上述demo屏蔽编码器相关的功能,设置好频率、占空比、运转方向即可。

warning_yellow.png
注:不带编码器的直流电机无法获取速度,只能通过PWM调速!

兼容性测试

MCU 测试通过 测试失败 未测试 特别标注
树莓派4B
树莓派3B
树莓派3B+
树莓派Zero W
树莓派2B+ v

更多

DFshopping_car1.png DFRobot商城购买链接

⚠️ **GitHub.com Fallback** ⚠️