|
A#2 的程序 和接线方法。
目前程序还没有融入任何高度控制, 这个过两天再考虑。
预计的效果是这样的:
1. 通道六开关,关闭。
2. 启动后 等半分钟,给GPS充分的时间启动。
3. 手动起飞。
4. 通道六开关, 打开。
5. 这时候 飞碟是混控的, 你可以强行控制飞碟。 如果你放开操纵杆, 飞机会自动飞回到你拨开关时候的位置。( 这样也是为了安全考虑。 之前的红外壁障功能也是样的。第四通道也是手动控制的。但是不管飞碟头朝哪个方向,都能自己飘回来)
如果关闭开关,飞碟又回到手动控制模式
代码:
#include <Servo.h>
String readString;
Servo roll;
Servo pitch;
Servo throttle;
int mode = 0;
int R;
int P;
long ch1, ch2, ch3, ch6;
int p = 1500;
int r = 1500;
void autopilot();
void output();
void setup(){
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
pinMode(6,INPUT);
pinMode(11,OUTPUT);
roll.attach(7);
pitch.attach(8);
throttle.attach(9);
ch1 = pulseIn(3,HIGH);
ch2 = pulseIn(4,HIGH);
Serial.begin(9600);
}
void loop(){
if(ch6 > 1500){
digitalWrite(11,HIGH);
ch1 = pulseIn(3,HIGH);
ch3 = pulseIn(5,HIGH);
p = ch1;
output();
autopilot();
/*------------------------------------------------------------------*/
ch2 = pulseIn(4,HIGH);
ch6 = pulseIn(6,HIGH);
r = ch2;
output();
autopilot();
}
else{ //(else) lasts 40ms
ch1 = pulseIn(3,HIGH);
ch3 = pulseIn(5,HIGH);
r = ch1;
output();
/*------------------------------------------------------------------*/
ch2 = pulseIn(4,HIGH);
ch6 = pulseIn(6,HIGH);
p = ch2;
output();
}
}
void autopilot(){
if (Serial.available()) {
delay(10);
if (Serial.available() >0) {
char c = Serial.read();
readString += c;
}
}
if (readString.length() == 6) {
Serial.println(readString);
R = (readString.charAt(1)-48)*10 + (readString.charAt(2)-48) - 50;
P = (readString.charAt(4)-48)*10 + (readString.charAt(5)-48) - 50;
r = ch1 + R;
p = ch2 + P;
readString="";
}
}
void output(){
roll.writeMicroseconds(p);
pitch.writeMicroseconds(r);
throttle.writeMicroseconds(ch3);
} |
|