Arduino串行通信
由于我改变了我的电机控制器,每当一个电机进入反向,它不会刹车,当它应该 向前看,很好,只要我按住控制台应用程序上的按钮,它就会运行。 向左/向右转动,正在反转的电机将不停止运行。 向后移动时,两者都将不停止运行Arduino串行通信,arduino,Arduino,由于我改变了我的电机控制器,每当一个电机进入反向,它不会刹车,当它应该 向前看,很好,只要我按住控制台应用程序上的按钮,它就会运行。 向左/向右转动,正在反转的电机将不停止运行。 向后移动时,两者都将不停止运行 // Firmware for the Android Shield Board for tank robots // Pan/tilt servos now work: 0 pin off, 255 pin on, 1~254 8 bit granularity servo m
// Firmware for the Android Shield Board for tank robots
// Pan/tilt servos now work: 0 pin off, 255 pin on, 1~254 8 bit granularity servo movement (5 microseconds).
#define PwmPinMotorA 11
#define PwmPinMotorB 6
#define DirectionPinMotorA 3
#define DirectionPinMotorB 5
#define ServoPin1 0
#define ServoPin2 0
#define ServoFlip1 false
#define ServoFlip2 false
#define mySerialSpeed 9600 // arduino 2009: 4800 or lower!
#define debugSerialSpeed 9600 // arduino 2009: 4800 or lower!
#define BufferLength 16
#define LineEnd1 13
#define LineEnd2 10
#define ServoTimingStep 5
#define ServoCenter 1500
#define ServoTimingFloor ServoCenter-(127*ServoTimingStep)
//#define serialout
#define debugout
#include <SoftwareSerial.h>
SoftwareSerial mySerial(12, 255); // rx only
char charin = 80;
char inputBuffer[BufferLength];
int value = 128;
int speed = 128;
int timer = 15;
int timermax = 15;
int inputLength = 0;
int servoval1 = 127;
int servoval2 = 127;
int tempval1, tempval2;
void setup()
{
// motor pins must be outputs
pinMode(PwmPinMotorA, OUTPUT);
pinMode(PwmPinMotorB, OUTPUT);
pinMode(DirectionPinMotorA, OUTPUT);
pinMode(DirectionPinMotorB, OUTPUT);
mySerial.begin(mySerialSpeed);
#ifdef debugout
Serial.begin(debugSerialSpeed);
#endif
}
// process a command string
void HandleCommand(char* input, int length)
{
#ifdef debugout
Serial.print(">");
Serial.print(input);
Serial.print("<");
Serial.print(length);
Serial.println("|");
#endif
if (length < 1) { // not a valid command
return;
}
// calculate number following command (d10~d255)
if (length > 1) {
value = atoi(&input[1]);
if (value > 255)
value = 255;
if (value < 0)
value = 0;
switch(input[0])
{
case 'd':
case 'D':
if (value > 127)
value = 127;
speed = value*2;
break;
case '/':
timermax = value;
break;
case 'c':
case 'C':
#ifdef ServoFlip1
servoval1 = 256 - value;
#else
servoval1 = value;
#endif
break;
case 'v':
case 'V':
#ifdef ServoFlip2
servoval2 = 256 - value;
#else
servoval2 = value;
#endif
break;
default:
break;
}
}
timer = timermax;
int* command = (int*)input;
// check commands
// note that the two bytes are swapped, ie 'RA' means command AR
switch(*command) {
case '2':
case '2j':
case '2J':
analogWrite(PwmPinMotorB, speed);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorA, speed);
digitalWrite(DirectionPinMotorA, HIGH);
break;
case '8':
case '8j':
case '8J':
analogWrite(PwmPinMotorB, speed);
digitalWrite(DirectionPinMotorB, HIGH);
analogWrite(PwmPinMotorA, speed);
digitalWrite(DirectionPinMotorA, LOW);
break;
case '6':
case '6j':
case '6J':
analogWrite(PwmPinMotorB, speed);
digitalWrite(DirectionPinMotorB, HIGH);
analogWrite(PwmPinMotorA, speed);
digitalWrite(DirectionPinMotorA, HIGH);
break;
case '4':
case '4j':
case '4J':
analogWrite(PwmPinMotorB, speed);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorA, speed);
digitalWrite(DirectionPinMotorA, LOW);
break;
case '9':
case '9j':
case '9J':
analogWrite(PwmPinMotorB, speed);
digitalWrite(DirectionPinMotorB, HIGH);
break;
case '1':
case '1j':
case '1J':
analogWrite(PwmPinMotorB, speed);
digitalWrite(DirectionPinMotorB, LOW);
break;
case '3':
case '3j':
case '3J':
analogWrite(PwmPinMotorA, speed);
digitalWrite(DirectionPinMotorA, HIGH);
break;
case '7':
case '7j':
case '7J':
analogWrite(PwmPinMotorA, speed);
digitalWrite(DirectionPinMotorA, LOW);
break;
default: // stop, just to be safe
analogWrite(PwmPinMotorA, 0);
digitalWrite(DirectionPinMotorA, LOW);
analogWrite(PwmPinMotorB, 0);
digitalWrite(DirectionPinMotorB, LOW);
break;
}
}
void loop()
{
// get a command string form the mySerial port
inputLength = 0;
do {
while (!mySerial.available()){
// note: arduino cannot handle fullduplex on myserial so no output here!
// do servos here
tempval1 = (servoval1*ServoTimingStep) + ServoTimingFloor;
tempval2 = (servoval2*ServoTimingStep) + ServoTimingFloor;
if (servoval1 > 0)
digitalWrite(ServoPin1,HIGH);
delayMicroseconds(tempval1);
if (servoval1 < 255)
digitalWrite(ServoPin1,LOW);
if (servoval2 > 0)
digitalWrite(ServoPin2,HIGH);
delayMicroseconds(tempval2);
if (servoval2 < 255)
digitalWrite(ServoPin2,LOW);
delayMicroseconds(5000 - tempval1 - tempval2);
delay(15); // reduce/remove if we're doing more things here
// decrease the timer
if (--timer < 0)
{
timer=0;
analogWrite(PwmPinMotorA, 0);
analogWrite(PwmPinMotorB, 0);
}
};
// wait for input
{
charin = mySerial.read(); // read it in
#ifdef debugout
Serial.print(charin);
tempval1 = charin;
Serial.println(tempval1);
#endif
if ((charin > 46 && charin < 58) || (charin=='d') || (charin=='j') || (charin=='c') || (charin=='v'))
{
inputBuffer[inputLength]=charin;
inputLength++;
#ifdef serialout
mySerial.print("$PD,11,");
mySerial.print(timer);
mySerial.print(",");
mySerial.print(value);
mySerial.println("*");
#endif
}
}
}
while (charin>46 && charin<119 && charin != LineEnd1 && charin != LineEnd2 && inputLength < BufferLength);
inputBuffer[inputLength] = 0; // add null terminator
HandleCommand(inputBuffer, inputLength);
}
//坦克机器人Android屏蔽板固件
//平移/倾斜伺服现在工作:0针关闭,255针打开,1~254 8位粒度伺服移动(5微秒)。
#定义PwmPinMotorA 11
#定义PwmPinMotorB 6
#定义方向PinMotora 3
#定义方向PinMotorB 5
#定义伺服销1 0
#定义伺服销2 0
#将ServoFlip1定义为false
#将ServoFlip2定义为false
#定义mySerialSpeed 9600//arduino 2009:4800或更低!
#定义debugSerialSpeed 9600//arduino 2009:4800或更低!
#定义缓冲区长度16
#定义LineEnd1 13
#定义LineEnd2 10
#定义伺服定时步骤5
#定义伺服中心1500
#定义伺服正时楼层伺服中心-(127*伺服正时步骤)
//#定义串行输出
#定义调试输出
#包括
SoftwareSerial mySerial(12255);//仅限rx
char-charin=80;
字符输入缓冲区[BufferLength];
int值=128;
整数速度=128;
int定时器=15;
int-timermax=15;
int inputLength=0;
int=1=127;
int servoval2=127;
int tempval1、tempval2;
无效设置()
{
//电机引脚必须为输出端
pinMode(PwmPinMotorA,输出);
pinMode(PwmPinMotorB,输出);
pinMode(方向PINMotora,输出);
pinMode(方向PinMotorB,输出);
mySerial.begin(mySerialSpeed);
#ifdef调试
串行。开始(调试串行速度);
#恩迪夫
}
//处理命令字符串
void HandleCommand(字符*输入,整数长度)
{
#ifdef调试
序列号。打印(“>”);
串行打印(输入);
Serial.print(“我在Arduino 1.0中编译了您的代码,并在我的Pro Mini 16MHz 328上运行了这些修改:
//#define debugout
HardwareSerial mySerial = Serial;
#define PwmPinMotorA 7
//#define debugout
HardwareSerial mySerial = Serial;
并在我的Mega 16MHz 2560上进行了以下修改:
//#define debugout
HardwareSerial mySerial = Serial;
#define PwmPinMotorA 7
//#define debugout
HardwareSerial mySerial = Serial;
而且它似乎工作得很好(从代码和示波器上的结果判断)
我一直在使用SoftwareSerial与我的GPS通话,我注意到如果我以9600波特运行它,它会出现奇怪的错误。我必须以4800波特运行才能实现无缝通信
显然,这并不能解释为什么您的--timer(0)
数字写入(伺服引脚1,高);
延迟微秒(tempval1);
如果(servoval1<255)
数字写入(伺服引脚1,低电平);
如果(servoval2>0)
数字写入(伺服引脚2,高);
延迟微秒(tempval2);
如果(servoval2<255)
数字写入(伺服引脚2,低电平);
延迟微秒(5000-tempval1-tempval2);
延迟(15);//如果我们在这里做更多的事情,减少/删除
//处理超时
if(timecheck&&millis()>clrtime)
{
timecheck=false;
模拟写入(PwmPinMotorA,0);
模拟写入(PwmPinMotorB,0);
}
};
//等待输入
{
charin=mySerial.read();//读入
如果((charin>46&&charin<58)| |(charin=='d')| |
(查林=='j')| |(查林=='c')| |(查林=='v'))
{
inputBuffer[inputLength]=charin;
inputLength++;
}
}
}
while(charin>46&&charin 1){
值=atoi(&input[1]);
如果(值>255)
数值=255;
如果(值<0)
数值=0;
开关(输入[0])
{
案例“d”:
案例“D”:
如果(值>127)
数值=127;
速度=值*2;
打破
案例“/”:
超时=100*值;
打破
案例“c”:
案例“C”:
#ifdef伺服翻转1
servoval1=256-值;
#否则
servoval1=值;
#恩迪夫
打破
案例“v”:
案例“V”:
#ifdef伺服翻转2
servoval2=256-值;
#否则
servoval2=值;
#恩迪夫
打破
违约:
打破
}
}
clrtime=millis()+超时;
timecheck=true;
int*命令=(int*)输入;
//检查命令
//请注意,这两个字节是交换的,即“RA”表示命令AR
开关(*命令){
案例“2”:
案例“2j”:
案例“2J”:
模拟写入(PwmPinMotorB,速度);
数字写入(方向PINMOTORB,低电平);
模拟写入(PwmPinMotorA,速度);
digitalWrite(方向Pinmotora,高);
打破
案例“8”:
案例“8j”:
案例“8J”:
模拟写入(PwmPinMotorB,速度);
数字写入(方向PINMOTORB,高);
模拟写入(PwmPinMotorA,速度);
数字写入(方向pinmotora,低电平);
打破
案例“6”:
案例“6j”:
案例“6J”:
模拟写入(PwmPinMotorB,速度);
数字写入(方向PINMOTORB,高);
模拟写入(PwmPinMotorA,速度);
digitalWrite(方向Pinmotora,高);
打破
案例“4”:
案例“4j”:
案例“4J”:
模拟写入(PwmPinMotorB,速度);
数字写入(方向PINMOTORB,低电平);
模拟写入(PwmPinMotorA,速度);
数字写入(方向pinmotora,低电平);
打破
案例“9”:
案例“9j”:
案例“9J”:
模拟写入(PwmPinMotorB,速度);
数字写入(方向PINMOTORB,高);
打破
案例“1”:
案例“1j”:
案例“1J”:
模拟写入(PwmPinMotorB,速度);
数字写入(方向PINMOTORB,低电平);
打破
案例“3”:
案例“3j”:
案例“3J”:
模拟写入(PwmPinMotorA,速度);
digitalWrite(方向Pinmotora,高);
打破
案例“7”:
案例“7j”:
案例“7J”:
模拟写入(PwmPinMotorA,速度);
数字写入(方向pinmotora,低电平);
打破
默认值://停止,只是为了安全
模拟写入(PwmPinMotorA,0);
数字写入(方向pinmotora,低电平);
模拟写入(PwmPinMotorB,0);
数字写入(方向PINMOTORB,低电平);
打破
}