我在尝试通过HC05蓝牙模块通过tx和rx向其他arduino发送一些串行数据时遇到问题。
整个项目正在开发一种混合动力卡丁车,并将arduino用作简单的ECU单元,并通过PID速度控制来控制DC电动机的PWM输出。我一直在逐步进行该项目,并尝试与arduino建立脚踏板并直接控制电子速度控制器(ESC)。我为此添加了一个简单的PID函数以及一个简单的霍尔传感器来检测速度,确实需要调整,但到目前为止效果很好。现在,当我尝试通过串行端口发送数据时,问题就来了。
我已经将蓝牙模块与单独的arduino连接,并成功地将数据从带有壶输入的一个arduino发送到另一个具有3.5英寸TFT屏幕的arduino。当我尝试将项目的主端集成到PID控制的直流电动机中时,系统冻结。从那以后,我删除了PID控制,回到直接控制,但仍然失败,我尝试注释掉编码器的中断序列,并为RPM设置了静态值,但仍然冻结。如果我不尝试使用任何数字输出,则发送序列有效。我真的很困惑。我已经尝试尝试调试的代码可以在下面找到。这不是完整的代码,并且已切成小段以尝试消除此故障。但是,在下面的这段代码中,如果我注释掉sendData函数,则系统将运行,并且电动机将以相对于踏板输入的相对速度旋转。一旦我尝试发送数据,系统就会运行一秒钟,然后冻结。数据仍在发送,静态读数仅显示数字输出处于工作状态。
#include <TimerOne.h>
int previous = 0;
int Tavg = 0; // the average
int Tout = 0;
int throttle = A0;
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int ESCPin = 5;
unsigned int counter=0;
int RPM;
long Time = 0;
long ReadInt = 0;
void docount() // counts from the speed sensor
{
counter++; // increase +1 the counter value
}
void timerIsr()
{
Timer1.detachInterrupt(); //stop the timer
Serial.print("Motor Speed: ");
RPM = (counter*75 ); // RPM= counterx10*60/8 (x10 for second, 8 counts in encoder, 60 minutes === 75x counter)
Serial.print(RPM);
Serial.println(" Rotation per min"); Serial.print(Tout);Serial.print("= "); Serial.print(Tout*0.01961);Serial.println("V");
counter=0; // reset counter to zero
Timer1.attachInterrupt( timerIsr ); //enable the timer
}
void ReadEnc (){
Timer1.initialize(100000); // set timer for 0.1sec
attachInterrupt(0, docount, RISING); // increase counter when speed sensor pin goes High
Timer1.attachInterrupt( timerIsr ); // enable the timer
}
void sendData(){
if (Serial.available()>0) {
if (Serial.read() == 0){
//Serial.println(sendChars);
int RPMout = RPM;
Serial.write("<");
//delay(2);
//Serial.write(sendChars);
Serial.write("Data is,");
//delay(2);
Serial.write( itoa (RPMout, 4,10));
//delay(2);
Serial.write(", 30, 48.35");
//delay(2);
Serial.write(">");
//delay(10);
Serial.println("");
}
}
}
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
pinMode(ESCPin, OUTPUT);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0; }
Time = millis();
ReadInt = -100;
}
void ReadSensor (){
// get the sensor value
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(throttle);
//Serial.println(readings[readIndex]);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
Tavg = total / numReadings;
}
void loop(){
ReadSensor();
ReadEnc();
RPM = 1800;
Tout = map(Tavg, 180, 860, 0, 200);
if (Tout>0){
analogWrite(ESCPin, Tout);
}
if (Time > ReadInt + 5000) {
sendData (); // when this is commented it works fine, tried moving it everywhere
ReadInt = Time;
}
Time = millis();
}
如果有人有任何想法,请告诉我,我知道我可能没有很好地解释我的问题,因此,如果他们有任何疑问或需要更多详细信息,请询问。