我正在构建使用4DC电机运行的机器人,并且在将蓝牙连接到它时我遇到了小问题。当我为没有蓝牙的电机运行测试代码时,电机运行时充满力量,没有问题。这是仅适用于电机的测试代码:
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4.
Adafruit_DCMotor *M1 = AFMS.getMotor(1);
Adafruit_DCMotor *M2 = AFMS.getMotor(2);
Adafruit_DCMotor *M3 = AFMS.getMotor(3);
Adafruit_DCMotor *M4 = AFMS.getMotor(4);
void setup() {
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// turn on motor
M1->setSpeed(255);
M2->setSpeed(255);
M3->setSpeed(255);
M4->setSpeed(255);
M1->run(RELEASE);
M2->run(RELEASE);
M3->run(RELEASE);
M4->run(RELEASE);
Serial.begin(9600); // set up Serial library at 9600 bps
}
void loop()
{
M1->run(FORWARD);
M2->run(FORWARD);
M3->run(FORWARD);
M4->run(FORWARD);
delay (5000);
M1->run(BACKWARD);
M2->run(BACKWARD);
M3->run(BACKWARD);
M4->run(BACKWARD);
delay (5000);
}
当我连接蓝牙并运行下面的代码时,电机以如此低的扭矩运行,几乎无法移动机器人。
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4.
Adafruit_DCMotor *M1 = AFMS.getMotor(1);
Adafruit_DCMotor *M2 = AFMS.getMotor(2);
Adafruit_DCMotor *M3 = AFMS.getMotor(3);
Adafruit_DCMotor *M4 = AFMS.getMotor(4);
const int BTState = 11;
int state;
void setup() {
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
// turn on motor
M1->setSpeed(255);
M2->setSpeed(255);
M3->setSpeed(255);
M4->setSpeed(255);
M1->run(RELEASE);
M2->run(RELEASE);
M3->run(RELEASE);
M4->run(RELEASE);
pinMode(BTState, INPUT);
Serial.begin(9600); // set up Serial library at 9600 bps
}
void loop()
{
//Stop car when connection lost or bluetooth disconnected
if (digitalRead(BTState) == LOW) {
state = 'S';
}
//Save income data to variable 'state'
if (Serial.available() > 0) {
state = Serial.read();
}
//If state is equal with letter 'F', car will go forward!
if (state == 'F') {
M1->run(FORWARD);
M2->run(FORWARD);
M3->run(FORWARD);
M4->run(FORWARD);
}
//If state is equal with letter 'B', car will go backward
else if (state == 'B') {
M1->run(BACKWARD);
M2->run(BACKWARD);
M3->run(BACKWARD);
M4->run(BACKWARD);
}
//If state is equal with letter 'S', stop the car
else if (state == 'S') {
M1->run(RELEASE);
M2->run(RELEASE);
M3->run(RELEASE);
M4->run(RELEASE);
}
}
以下是我设置的图片:
我在蓝牙时已正确连接所有内容,但我不明白为什么当我通过蓝牙启动我的手机或计算机的前进或后退命令时,我没有得到直流电机的全速。几乎看来我没有给他们足够的力量。
知道我做错了什么?
答案 0 :(得分:0)
好的,我弄清楚问题是什么。 它位于代码的这一部分:
//If state is equal with letter 'S', stop the car
else if (state == 'S') {
M1->run(RELEASE);
M2->run(RELEASE);
M3->run(RELEASE);
M4->run(RELEASE);
}
所以我的应用程序是通过蓝牙每隔50ms传输数据。因此,在这些时刻蓝牙正在执行上面的代码。所以我需要补充的是,如果蓝牙的状态是&#34; S&#34;比执行上面的代码要好1比1。 &#34; S&#34;状态表示没有按下应用程序上的按钮,这意味着停止机器人。 你能帮我写那行代码,说明S状态是否大于1,而不是执行if if语句?
谢谢, 斯拉夫