Hi guys !
currently im working on Collision Avoidance drone using Sharp IR sensor. im using FC APM2.8. i put a limit for the distance, if the drone detect object <=30cm the drone will move opposite direction.
right now i got some problem when im trying to fly the drone. just 3 of brushless motor that spin. i wonder why just 3 not 4 (all of them), here's my source code :
#include <Servo.h>
#include <SharpIR.h>
// Define model and input pin:
#define IRPin A0
#define IR2 A1
#define IR3 A2
#define IR4 A3
// #define IR A1
#define model 20150 /* Model :
GP2Y0A02YK0F --> 20150
GP2Y0A21YK0F --> 1080
GP2Y0A710K0F --> 100500
GP2YA41SK0F --> 430
*/
byte lastch1, lastch2, lastch3, lastch4;
int rc1, rc2; // d8, d9, d10, d11
Servo outch1; //d11 roll
Servo outch2;//d12 pitch
unsigned long t1, t2, t3, t4;
int dist ;
int dist2 ;
int dist3 ;
int dist4 ;
SharpIR mySensor = SharpIR(IRPin, model);
SharpIR mySensor2 = SharpIR(IR2, model);
SharpIR mySensor3 = SharpIR(IR3, model);
SharpIR mySensor4 = SharpIR(IR4, model);
void setup(){
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT0);
PCMSK0 |= (1 << PCINT1);
PCMSK0 |= (1 << PCINT2);
outch1.attach(11); //roll
outch2.attach(12); // nick
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop(){
delay(250);
printsignal();
sensor();
}
// The Below Codes Reads the PWM value of Receiver
ISR(PCINT0_vect){
//ch1
if(lastch1 == 0 && PINB & B00000001){
lastch1 = 1;
t1 = micros();
}
else if(lastch1 == 1 && ! (PINB & B00000001)){
lastch1 = 0;
rc1 = micros() - t1;
}
//ch2
if(lastch2 == 0 && PINB & B00000010){
lastch2 = 1;
t2 = micros();
}
else if(lastch1 == 1 && ! (PINB & B00000010)){
lastch2 = 0;
rc2 = micros() - t2;
}
}
void printsignal(){
Serial.print("roll:");
if(dist <= 30){
outch1.write(rc1= 1020);
//Serial.print("object detected");
digitalWrite(13, HIGH);
}
else if(dist2 <=30){
outch1.write(rc1=1996);
digitalWrite(13,HIGH);
}
else {
outch1.write(rc1 = rc1);
digitalWrite(13, LOW);
}
if(rc1 - 1480 < 0) Serial.print("<<<");
else if(rc1 - 1520 >0) Serial.print(">>>");
else Serial.print("-+-");
// Serial.print(rc1);
Serial.print(rc1);
Serial.print("\t");
Serial.print("pitch:");
if(dist3 <= 30){
outch2.write(rc2 = 1024);
digitalWrite(13, HIGH);
}
else if(dist4 <=30){
outch2.write(rc2=1990);
digitalWrite(13,HIGH);
}
else {
outch2.write(rc2 = rc2);
digitalWrite(13, LOW);
}
if(rc2 - 1480 < 0) Serial.print("^^^");
else if(rc2 - 1520 >0) Serial.print("vvv");
else Serial.print("-+-");
Serial.print(rc2);
Serial.print("\t");
//Serial.print("\t");
Serial.print("\n");
}
void sensor(){
dist = mySensor.distance();
dist2 = mySensor2.distance();
dist3 = mySensor3.distance();
dist4 = mySensor4.distance();
// Print the measured distance to the serial monitor:
Serial.print("distance kanan: ");
Serial.print(dist);
Serial.println(" cm\t");
Serial.print("distance kiri: ");
Serial.print(dist2);
Serial.println(" cm");
Serial.print("distance depan: ");
Serial.print(dist3);
Serial.println(" cm\t");
Serial.print("distance belakang: ");
Serial.print(dist4);
Serial.println(" cm\t");
// Serial.print("\t");
delay(100);
}