|
我也来贴一版本吧,注意安全就好。2560的板子。
[mw_shl_code=c,true]#include<Servo.h>//The Arduino Mega has an additional four: numbers 2 (pin 21), 3 (pin 20), 4 (pin 19), and 5 (pin 18).
int ppm1 = 2;
int ppm2 = 3;
unsigned long rc1_PulseStartTicks, rc2_PulseStartTicks;
volatile int rc1_val, rc2_val;
int OUTAIL, OUTELE; //输出
////////////////////////////超声波值///////////////////
int distance1 = 300, distance2 = 300, distance3 = 300, distance4 = 300;
//////////////////////////超声波针脚定义///////////////
int input1 = 22, input2 = 24, input3 = 30, input4 = 32;
int output1 = 23, output2 = 25, output3 = 31, output4 = 33;
Servo AIL;
Servo ELE;
void setup()
{
// 电平变化即触发中断
attachInterrupt(0, rc1, CHANGE);
attachInterrupt(1, rc2, CHANGE);
////////////////////////////////
AIL.attach(8);
ELE.attach(9);
pinMode(ppm1, INPUT);
pinMode(ppm2, INPUT);
pinMode(input1, INPUT);
pinMode(output1, OUTPUT);
pinMode(input2, INPUT);
pinMode(output2, OUTPUT);
pinMode(input3, INPUT);
pinMode(output3, OUTPUT);
pinMode(input4, INPUT);
pinMode(output4, OUTPUT);
Serial.begin(9600);
}
void rc1()
{ // did the pin change to high or low?
if (digitalRead( ppm1 ) == HIGH)
rc1_PulseStartTicks = micros(); // store the current micros() value
else
rc1_val = micros() - rc1_PulseStartTicks;
}
void rc2()
{
// did the pin change to high or low?
if (digitalRead( ppm2 ) == HIGH)
rc2_PulseStartTicks = micros();
else
rc2_val = micros() - rc2_PulseStartTicks;
}
void loop() {
Sonar();//超声波函数
Avoidance (); //避障函数
OUTAIL = map(rc1_val, 1010, 2007, 47, 144);
OUTELE = map(rc2_val, 1010, 2007, 47, 144);
AIL.write(OUTAIL);
ELE.write(OUTELE);
Print();//串口输出
}
void Sonar ()
{
//第一个超声波/前
digitalWrite(output1, LOW);
delayMicroseconds(2);
digitalWrite(output1, HIGH);
delayMicroseconds(10);
distance1 = pulseIn(input1, HIGH);
distance1 = distance1 / 58;
distance1 = constrain(distance1, 10, 300);//限制量程
delay(2);
//第二个超声波/后
digitalWrite(output2, LOW);
delayMicroseconds(2);
digitalWrite(output2, HIGH);
delayMicroseconds(10);
distance2 = pulseIn(input2, HIGH);
distance2 = distance2 / 58;
distance2 = constrain(distance2, 10, 300);
delay(2);
//第三个超声波/左
digitalWrite(output3, LOW);
delayMicroseconds(2);
digitalWrite(output3, HIGH);
delayMicroseconds(10);
distance3 = pulseIn(input3, HIGH);
distance3 = distance3 / 58;
distance3 = constrain(distance3, 10, 300);
delay(2);
//第四个超声波/右
digitalWrite(output4, LOW);
delayMicroseconds(2);
digitalWrite(output4, HIGH);
delayMicroseconds(10);
distance4 = pulseIn(input4, HIGH);
distance4 = distance4 / 58;
distance4 = constrain(distance4, 10, 300);
delay(2);
}
void Avoidance ()
{
if (distance2 <= 80)
{ //前进,后有障碍物
if ((OUTELE <= 97) && (OUTELE >= 93))
{
ELE.write(102);
}
else
{ int PTELE1; int NOW_ELE1;
PTELE1 = 95.5 - OUTELE;
abs (PTELE1);
NOW_ELE1 = OUTELE + PTELE1;
ELE.write(NOW_ELE1);
}
}
//////////////////////////////////////////////////////////////////
if (distance1 <= 80)
{ //后退,前有障碍物
if ((OUTELE <= 97) && (OUTELE >= 93))
{
ELE.write(87);
}
else
{
int PTELE2;
int NOW_ELE2;
PTELE2 = 95.5 - OUTELE;
abs (PTELE2);
NOW_ELE2 = OUTELE - PTELE2;
ELE.write(NOW_ELE2);
}
}
//////////////////////////////////////////////////////////////////
if (distance4 <= 80)
{ //左飞,右有障碍物
if ((OUTAIL <= 97) && (OUTAIL >= 93))
{
AIL.write(102);
}
else
{ int PTAIL3;
int NOW_AIL3;
PTAIL3 = 95.5 - OUTAIL;
abs (PTAIL3);
NOW_AIL3 = OUTAIL + PTAIL3;
AIL.write(NOW_AIL3);
}
}
////////////////////////////////////////////////////////////////////
if (distance3 <= 80)
{ //右飞,左有障碍物
if ((OUTAIL <= 97) && (OUTAIL >= 93))
{
AIL.write(87);
}
else
{
int PTAIL4;
int NOW_AIL4;
PTAIL4 = 95.5 - OUTAIL;
abs (PTAIL4);
NOW_AIL4 = OUTAIL - PTAIL4;
AIL.write(NOW_AIL4);
}
}
}
void Print()
{
Serial.print("INAIL=");
Serial.print(rc1_val);
Serial.print(" INELE=");
Serial.print(rc2_val);
Serial.print(" OUTAIL=");
Serial.print(OUTAIL);
Serial.print(" OUTELE=");
Serial.print(OUTELE);
Serial.print(" distance1=");
Serial.print(distance1);
Serial.print(" distance2=");
Serial.print(distance2);
Serial.print(" distance3=");
Serial.print(distance3);
Serial.print(" distance4=");
Serial.print(distance4);
Serial.println();
delay(5);
}[/mw_shl_code] |
|