|
发表于 2023-4-23 10:56:31
|
显示全部楼层
#include<Wire.h>
#include<Servo.h>
#include<SoftwareSerial.h>
#define T 2
#define R 3
#define P 4
#define Y 5
#define A1 6
#define A2 7
#define A3 10
#define A4 11
#define cx 15
#define cy 16
#define RX 8
#define TX 9
Servo tservo;//throttle
Servo rservo;//roll
Servo pservo;//pitch
Servo yservo;//yaw
Servo a1servo;//aux1
Servo a2servo;//aux2
Servo a3servo;//aux3
Servo a4servo;//au4
Servo cxservo;//camerax
Servo cyservo;//cameray
SoftwareSerial sserial(8, 9);
char infostore[26] = {0};
void setup() {
// put your setup code here, to run once:
pinMode(T, OUTPUT); //T
pinMode(R, OUTPUT); //R
pinMode(P, OUTPUT); //P
pinMode(Y, OUTPUT); //Y
pinMode(A1, OUTPUT); //A1
pinMode(A2, OUTPUT); //A2
pinMode(A3, OUTPUT); //A3
pinMode(A4, OUTPUT); //A4
pinMode(cx, OUTPUT); //X
pinMode(cy, OUTPUT); //Y
tservo.attach(T);
rservo.attach(R);
pservo.attach(P);
yservo.attach(Y);
a1servo.attach(A1);
a2servo.attach(A2);
a3servo.attach(A3);
a4servo.attach(A4);
Serial.begin(9600);
sserial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
char readbuffer[64];
char checkbuffer;
while(Serial.available()!=true)
{
;
}
delay(5);
//sserial.println("ready");
checkbuffer = Serial.read();
for (; checkbuffer != '#';)
{
checkbuffer = Serial.read();
}
Serial.println("get");
Serial.readBytesUntil('#', readbuffer, 64);
readbuffer[63] = 0;
Serial.println(readbuffer);
if (readbuffer[24] == 'E')
{
Serial.println("ture");
for (int i = 0; i <= 25; i++)
{
infostore = readbuffer;
}
memset( readbuffer, 0, sizeof(readbuffer) );
while (Serial.read() >= 0) {};
putout();
delay(2);
}
}
void putout()
{
char tpwm[4];
char rpwm[4];
char ppwm[4];
char ypwm[4];
char a1info;
char a2info;
char a3info;
char a4info;
char Xinfo;
char Yinfo;
int ti, ri, pi, yi;
int t, r, p, y;
int a1, a2, a3, a4;
tpwm[0] = infostore[1];
tpwm[1] = infostore[2];
tpwm[2] = infostore[3];
tpwm[3] = 0;
//Serial.println(tpwm);
rpwm[0] = infostore[5];
rpwm[1] = infostore[6];
rpwm[2] = infostore[7];
rpwm[3] = 0;
//Serial.println(rpwm);
ppwm[0] = infostore[9];
ppwm[1] = infostore[10];
ppwm[2] = infostore[11];
ppwm[3] = 0;
//Serial.println(ppwm);
ypwm[0] = infostore[13];
ypwm[1] = infostore[14];
ypwm[2] = infostore[15];
ypwm[3] = 0;
//Serial.println(ypwm);
a1info = infostore[17];
a2info = infostore[18];
a3info = infostore[19];
a4info = infostore[20];
ti = ctoi(tpwm);
ri = ctoi(rpwm);
pi = ctoi(ppwm);
yi = ctoi(ypwm);
t = ti * 10;
r = ri * 10;
p = pi * 10;
y = yi * 10;
// sserial.println(t);
// sserial.println(r);
// sserial.println(p);
// sserial.println(y);
// Serial.println(t);
// Serial.println(r);
// Serial.println(p);
// Serial.println(y);
a1 = 0;
a2 = 0;
a3 = 0;
a4 = 0;
tservo.writeMicroseconds(t);
rservo.writeMicroseconds(r);
pservo.writeMicroseconds(p);
yservo.writeMicroseconds(y);
a1servo.writeMicroseconds(a1);
a2servo.writeMicroseconds(a2);
a3servo.writeMicroseconds(a3);
a4servo.writeMicroseconds(a4);
delay(3);
}
int ctoi(char a[3])
{
if (a[0] != '0' || a[1] != '0' || a[2] != '0')
{
int b, c, d, e;
b = ((int)a[0]) - 48;
c = ((int)a[1]) - 48;
d = ((int)a[2]) - 48;
e = (b * 100) + (c * 10) + (d);
//Serial.println(e);
return e;
}
else {
return 0;
}
} |
|