Hi,
I am working on a school project involving the installation of a “sense and avoid” system in a UAV.
So far i have managed to make my plane steer right when it detects an obstacle in front of it.
What i need help with:
1 . I need the plane to keep the ailerons and rudder in a right turn position for 0.3 seconds when an obstacle is detected.
-
After the 0.3 s have passed i need the plane to fly straight for another 3 seconds.
-
After the 3 seconds have passed i need the plane to steer left for 0,3 seconds so the flight path is parallel to the first heading.
-
After the 0,3 seconds of left steering have passed i need the plane to fly straigt for 6 seconds.
-
After the 6 seconds of parallel to first heading flight i need to make the plane steer left for 0,3 seconds.
6 and fly like that for another 3 seconds.
-
After the 3 seconds have passed i need the plane to steer right 0,3 seconds so the first heading and last heading coincide,
-
and fly like that until another obstacle appears.
In the attachment is a graphic explanation.
The code i am using is this:
#include <Servo.h>
#define GreenLED 2
#define YellowLED 3
#define RedLED 4
#define Buzzer1 5
#define Buzzer2 6
int SafetyDistanceLI = 100; // Li = inferior limit
int UncertaintyDistanceLS = 100; // Ls = superior limit
int ActionDistanceLS = 70;
int sound = 1500;
int AileronIdlePositioin = 60 ;
int RudderIdlePosition = 50 ;
int AileronSteeringUp = 20;
int RudderSteeringRight = 40;
int AileronSteeringDown = - 20; // i need to insert this based on time needs
int RudderSteeringLeft = - 40; // same as the above
Servo servoAileron;
Servo servoRudder;
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
servoAileron.attach(7);
servoRudder.attach(8);
}
void loop() {
int measuredDistance = readDistanceMediata(10, 0); // distance measured by IR sensor
Serial.print("cm:");
Serial.println(measuredDistance);
if (measuredDistance > SafetyDistanceLI)
{
digitalWrite(GreenLED, HIGH); // The green led lights up if the distance is greater than the minimum safety distance
}
else
{
digitalWrite(GreenLED, LOW); // the green led will be off only when the distance is lower than the safety distance
}
if ((measuredDistance <= UncertaintyDistanceLS) && (measuredDistance > ActionDistanceLS))
{
digitalWrite(YellowLED, HIGH); // the yellow led will be on if the measured distance is in the uncertainty distance interval
tone(Buzzer2, sound-800); // the buzzer starts to buzz
}
else
{
digitalWrite(YellowLED, LOW);
noTone(Buzzer2); // buzzer is of
}
if (measuredDistance <= ActionDistanceLS) //[b] i need to make this right turn last for 0,3 seconds, then the aircraft to fly with new heading for 3 seconds, then to turn left for 0,3 seconds, to fly another 6 seconds straight, then to turn left for 0,3 seconds and fly like that for 3 seconds, then to turn right for 0,3 seconds and keed flying until measuredDistance <= ActionDistanceLS[/b]
{
digitalWrite(RedLED, HIGH); // ledul Rosu se aprinde daca distanta scade sub limita superioara a distantei de actiune
tone(Buzzer1, sound); // difuzorul incepe sa atentioneze faptul ca distanta a scazut sub limita superioara a distantei de actiune
servoAileron.write(AileronIdlePosition + AileronPositionUp); // the ailron servo is turning with the AileronPositionUp value
servoRudder.write(RudderIdlePosition + RudderPositionRight); // the rudder servo is turning with the RudderPositionRight value
}
else
{
digitalWrite(RedLED, LOW);
noTone(Buzzer1);
servoAileron.write(AileronIdlePosition); // aileron servo comes back to idle position
servoRudder.write(RudderIdlePosition); // rudder servo comes back to idle position
}
delay(100);
}
int readDistanceMediata(int count, int pin)
{
int sum = 0;
for (int i = 0; i<count; i++)
{
float volts = analogRead(pin) * ((float) 5 / 1024);
float distance = 65 * pow(volts, -1.10);
sum = sum + distance;
delay(5);
}
return (int) (sum/count);
}
/code]