正面示意图
底面示意图
]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,若未使能,会显示如下图所示提示:
step3:使能I2C接口。(若已开启可忽略此步)键入命令sudo raspi-config,进入配置界面,操作流程如下所示:
配置完成后,键入命令sudo reboot重启树莓派。
step4:检测I2C地址。键入命令i2cdetect -y 1检查设备I2C地址,如下图所示:
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
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)
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)
|