“编码直流电机驱动器”的版本间的差异
来自Labplus盛思维基百科
Tangliufeng(讨论 | 贡献) (→Arduino) |
Tangliufeng(讨论 | 贡献) (→Arduino) |
||
第73行: | 第73行: | ||
#include <Wire.h> | #include <Wire.h> | ||
− | int | + | int fourbottonPin = A0; |
− | int | + | int sensorvalue = 0; |
− | int | + | |
− | + | void MotorSetPwm(char address, int pwm1, int pwm2) //pwm模式 | |
− | + | { | |
− | + | Wire.beginTransmission(address); //ransmit to device #17 (0x11) | |
− | + | Wire.write(0x06); //写pwm寄存器地址 | |
− | + | Wire.write(highByte(pwm1)); //向pwm寄存器地址写编码电机转速 | |
− | + | Wire.write(lowByte(pwm1)); //向pwm寄存器地址写编码电机转速 | |
− | void | + | Wire.write(highByte(pwm2)); //向pwm寄存器地址写编码电机转速 |
+ | Wire.write(lowByte(pwm2)); //向pwm寄存器地址写编码电机转速 | ||
+ | Wire.endTransmission(); // stop transmitting | ||
+ | delay(10); | ||
+ | } | ||
+ | |||
+ | |||
+ | void MotorSetSpeed(char address, short speed1 ,short speed2) // auto模式 | ||
{ | { | ||
− | Wire. | + | Wire.beginTransmission(address); //ransmit to device #17 (0x11) |
− | + | Wire.write(0x0A); //写速度寄存器地址 | |
− | + | Wire.write(highByte(speed1)); //向速度寄存器地址写编码电机转速 | |
− | + | Wire.write(lowByte(speed1)); //向速度寄存器地址写编码电机转速 | |
− | + | Wire.write(highByte(speed2)); //向速度寄存器地址写编码电机转速 | |
− | + | Wire.write(lowByte(speed2)); //向速度寄存器地址写编码电机转速 | |
− | + | Wire.endTransmission(); // stop transmitting | |
− | + | delay(10); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
+ | |||
+ | void MotorSetMode(char address, char mode) // 设置模式 | ||
+ | { | ||
+ | Wire.beginTransmission(address); //ransmit to device #17 (0x11) | ||
+ | Wire.write(byte(0x00)); //写模式寄存器地址 | ||
+ | Wire.write(byte(mode)); //向模式寄存器写编码电机模式 | ||
+ | Wire.endTransmission(); // stop transmitting | ||
+ | delay(10); | ||
+ | } | ||
+ | |||
+ | void MotorSetPosition(char address, long truns1, long turns2) //定位模式 | ||
+ | { | ||
+ | Wire.beginTransmission(address); //ransmit to device #17 (0x11) | ||
+ | Wire.write(0x10); //写turns寄存器地址 | ||
+ | Wire.write((byte)(truns1 >> 24 & 0x000000FF)); | ||
+ | Wire.write((byte)(truns1 >> 16 & 0x000000FF)); | ||
+ | Wire.write((byte)(truns1 >> 8 & 0x000000FF)); | ||
+ | Wire.write((byte)(truns1 & 0x000000FF)); | ||
+ | Wire.write((byte)(turns2 >> 24 & 0x000000FF)); | ||
+ | Wire.write((byte)(turns2 >> 16 & 0x000000FF)); | ||
+ | Wire.write((byte)(turns2 >> 8 & 0x000000FF)); | ||
+ | Wire.write((byte)(turns2 & 0x000000FF)); | ||
+ | Wire.endTransmission(); // stop transmitting | ||
+ | delay(10); | ||
+ | } | ||
+ | |||
+ | void MotorStop(char address) | ||
+ | { | ||
+ | Wire.beginTransmission(address); //ransmit to device #17 (0x11) | ||
+ | Wire.write(byte(0x00)); //写停止寄存器地址 | ||
+ | Wire.write(byte(0x00)); //向模式寄存器写编码电机模式 | ||
+ | Wire.endTransmission(); // stop transmitting | ||
+ | delay(10); | ||
+ | } | ||
+ | |||
void setup() | void setup() | ||
{ | { | ||
Wire.begin(); // join i2c bus (address optional for master) | Wire.begin(); // join i2c bus (address optional for master) | ||
− | Serial.begin( | + | Serial.begin(115200); |
} | } | ||
void loop() | void loop() | ||
{ | { | ||
− | + | //定位模式 | |
+ | MotorSetPosition(0x13, 200, 200); | ||
+ | MotorSetMode(0x13,0x0F); | ||
+ | delay(3000); | ||
+ | MotorSetPosition(0x13, -200, -200); | ||
+ | MotorSetMode(0x13,0x0F); | ||
+ | delay(3000); | ||
+ | MotorStop(0x13); | ||
+ | delay(1000); | ||
+ | |||
+ | //auto模式 | ||
+ | MotorSetSpeed(0x13, 150, 150); | ||
+ | MotorSetMode(0x13,0x0A); | ||
+ | delay(1000); | ||
+ | MotorSetSpeed(0x13, -150, -150); | ||
+ | MotorSetMode(0x13,0x0A); | ||
+ | delay(1000); | ||
+ | MotorStop(0x13); | ||
+ | delay(1000); | ||
+ | |||
+ | //pwm模式 | ||
+ | MotorSetPwm(0x13, 500, 500); | ||
+ | MotorSetMode(0x13,0x05); | ||
+ | delay(2000); | ||
+ | MotorSetPwm(0x13, -500, -500); | ||
+ | MotorSetMode(0x13,0x05); | ||
+ | delay(2000); | ||
+ | MotorStop(0x13); | ||
+ | delay(1000); | ||
+ | |||
+ | delay(10000000); | ||
+ | |||
} | } | ||
2019年5月10日 (五) 13:40的最新版本
概述
Encoder DC Motor 提供两路的编码电机的驱动模块。提供三种工作模式:PWM模式、巡航模式和定位模式。
技术参数
- 工作电压:VCC 3.3-5V
- 两路的电机驱动
- I2C数字信号输出
- 模块尺寸:24x46x7.5mm
编码电机参数
引脚定义/接口说明
- I2C接口
VCC | 电源 |
SDA | I2C数据 |
SCL | I2C时钟 |
GND | 地 |
|
- 编码电机接口
- 触发信号的输入接口
VCC | 电源 |
CH0 | NA(未用) |
CH1 | 触发信号输入 |
GND | 地 |
- 可调电位器:调节触发信号的触发电平
使用教程
- 在程序编程中需要注意拨动开关地址和程序中的地址一致!
- 驱动模式
- pwm模式
- -通过调整PWM输出,调节电机的转速,-1023~1023
- 巡航模式
- -在pwm模式基础上,增加了PID算法,可自动稳定在固定的转速。当外界的力量(比如电机的驱动电压变化,电机受到的阻力变大等)迫使转速改变时,程序会通过PWM调节,保持恒定输出。-1023~1023
- 定位模式
- -可旋转指定位置,对控制步长有要求,较为有用。范围-1023~1023
Arduino
#include <Wire.h>
int fourbottonPin = A0;
int sensorvalue = 0;
void MotorSetPwm(char address, int pwm1, int pwm2) //pwm模式
{
Wire.beginTransmission(address); //ransmit to device #17 (0x11)
Wire.write(0x06); //写pwm寄存器地址
Wire.write(highByte(pwm1)); //向pwm寄存器地址写编码电机转速
Wire.write(lowByte(pwm1)); //向pwm寄存器地址写编码电机转速
Wire.write(highByte(pwm2)); //向pwm寄存器地址写编码电机转速
Wire.write(lowByte(pwm2)); //向pwm寄存器地址写编码电机转速
Wire.endTransmission(); // stop transmitting
delay(10);
}
void MotorSetSpeed(char address, short speed1 ,short speed2) // auto模式
{
Wire.beginTransmission(address); //ransmit to device #17 (0x11)
Wire.write(0x0A); //写速度寄存器地址
Wire.write(highByte(speed1)); //向速度寄存器地址写编码电机转速
Wire.write(lowByte(speed1)); //向速度寄存器地址写编码电机转速
Wire.write(highByte(speed2)); //向速度寄存器地址写编码电机转速
Wire.write(lowByte(speed2)); //向速度寄存器地址写编码电机转速
Wire.endTransmission(); // stop transmitting
delay(10);
}
void MotorSetMode(char address, char mode) // 设置模式
{
Wire.beginTransmission(address); //ransmit to device #17 (0x11)
Wire.write(byte(0x00)); //写模式寄存器地址
Wire.write(byte(mode)); //向模式寄存器写编码电机模式
Wire.endTransmission(); // stop transmitting
delay(10);
}
void MotorSetPosition(char address, long truns1, long turns2) //定位模式
{
Wire.beginTransmission(address); //ransmit to device #17 (0x11)
Wire.write(0x10); //写turns寄存器地址
Wire.write((byte)(truns1 >> 24 & 0x000000FF));
Wire.write((byte)(truns1 >> 16 & 0x000000FF));
Wire.write((byte)(truns1 >> 8 & 0x000000FF));
Wire.write((byte)(truns1 & 0x000000FF));
Wire.write((byte)(turns2 >> 24 & 0x000000FF));
Wire.write((byte)(turns2 >> 16 & 0x000000FF));
Wire.write((byte)(turns2 >> 8 & 0x000000FF));
Wire.write((byte)(turns2 & 0x000000FF));
Wire.endTransmission(); // stop transmitting
delay(10);
}
void MotorStop(char address)
{
Wire.beginTransmission(address); //ransmit to device #17 (0x11)
Wire.write(byte(0x00)); //写停止寄存器地址
Wire.write(byte(0x00)); //向模式寄存器写编码电机模式
Wire.endTransmission(); // stop transmitting
delay(10);
}
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(115200);
}
void loop()
{
//定位模式
MotorSetPosition(0x13, 200, 200);
MotorSetMode(0x13,0x0F);
delay(3000);
MotorSetPosition(0x13, -200, -200);
MotorSetMode(0x13,0x0F);
delay(3000);
MotorStop(0x13);
delay(1000);
//auto模式
MotorSetSpeed(0x13, 150, 150);
MotorSetMode(0x13,0x0A);
delay(1000);
MotorSetSpeed(0x13, -150, -150);
MotorSetMode(0x13,0x0A);
delay(1000);
MotorStop(0x13);
delay(1000);
//pwm模式
MotorSetPwm(0x13, 500, 500);
MotorSetMode(0x13,0x05);
delay(2000);
MotorSetPwm(0x13, -500, -500);
MotorSetMode(0x13,0x05);
delay(2000);
MotorStop(0x13);
delay(1000);
delay(10000000);
}
掌控板
- GitHub:bluebit for mPython library
- bluebit for mPython library API: https://mpython-lib.readthedocs.io/zh/master/bluebit/index.html
"""
由于编码电机模块存在此缺陷:当程序上电后,会有异常脉冲信号导致I2C总线挂机
临时解决方案:将SCL(P19)引脚上电后先拉低
"""
from machine import I2C,Pin
p19=Pin(Pin.P19,Pin.OUT,value=0)
#
from mpython import *
import bluebit
motor = bluebit.EncoderMotor(0x10)
while True:
print("PWM mode")
motor.set_pwm(500,-500) # pwm模式,M1 speed 500; M2 speed -500
sleep(2)
print("Cruise mode")
motor.set_cruise(800,800) # 巡航模式 M1,M2恒速800
sleep(5)
print("Position mode:500")
motor.set_position(500,500) # 定位模式 电机转到位置500
sleep(3)
print("Position mode:-100")
motor.set_position(-100,-100) # 定位模式 电机转到位置-100
sleep(2)
microbit
应用案例
FAQ
版本历史记录
Version | Date | 新增/删除/修复 |
---|---|---|