Hello again,
Ive now come a bit further with the project.
- Values from sensors (GYRO AND ACC.)
- Values from receiver (using a libraries called PPMrcIn and Statistic)
- The sensor will only do its work when the transmitter is not sending any new values/directions
- So, if you are flying the plane manually and then put your transmitter away. The plane will stabilize itself (roll and pitch)
I will post a bit of the code, showing my algorithms. And here I need you guys to help me improve my code.
One thing I know that is not working well, is the automatic stabilizer.
Lets say:
- Plane is straightforward = roll and pitch has a value of 180 degrees.
- Plane is leaning = roll has a value of 160 (which means leaning 20 degrees to the left, I believe)
--> Divide value by 2 and insert it in servo.write() function.
The problem is that the indication of the servos gets to small.
Should I make it into several intervals instead, or will it take to much memory?
Servo YAW_SERVO;
Servo PITCH_SERVO;
Servo ROLL_SERVO;
int flag_roll = 0;
int flag_pitch = 0;
int flag_yaw = 0;
//////////////////////////////////////////////
////// INITIATION OF SERVOS ///////
//////////////////////////////////////////////
void init_servo_motor(){
YAW_SERVO.attach(SERVO_YAW_PIN);
PITCH_SERVO.attach(SERVO_PITCH_PIN);
ROLL_SERVO.attach(SERVO_ROLL_PIN);
}
//////////////////////////////////////////////
////// READING THE RECEIVER AND /////
////// MANIPULATING THE SERVOS THROUGH IT ///
//////////////////////////////////////////////
void read_receiver(){
YAW.readSignal();
PITCH.readSignal();
ROLL.readSignal();
// THROTTLE.readSignal();
//////////////////////////////////////////////
/// CONTROL SERVO THROUGH RECEIVER ////
//////////////////////////////////////////////
if(ROLL.getVersus() != 0){
ROLL_SERVO.write(map((ROLL.getSignal()), 1000,2000,0,179));
flag_roll = 0;
Serial.println("RECEIVER ROLL");
}
if(PITCH.getVersus() != 0){
PITCH_SERVO.write(map((PITCH.getSignal()), 1000,2000,0,179));
flag_pitch = 0;
Serial.println("RECEIVER PITCH");
}
if(YAW.getVersus() != 0){
YAW_SERVO.write(map((YAW.getSignal()), 1000,2000,0,179));
flag_yaw = 0;
Serial.println("RECEIVER YAW");
}
//CHECKING IF ALL OF THE LEVERS OF THE RECEIVER ARE OFF
if(ROLL.getVersus() == 0 && PITCH.getVersus() == 0 && YAW.getVersus() == 0){
control_servo_sensor();
}
}
void control_servo_sensor(){
//////////////////////////////////
/// CONTROLLING THE SERVOS ////
/// WITH SENSOR ////
//////////////////////////////////
get_sensor_values();
// THIS IS FOR THE ROLL SERVO //
if(kalAngleX < 177 || kalAngleX > 182){
Serial.println("SENSOR ROLL ");
Serial.print(kalAngleX);
int value_roll = kalAngleX/2;
ROLL_SERVO.write(value_roll);
flag_roll = 0;
}
// THIS IS FOR THE PITCH SERVO //
if(kalAngleY < 182 || kalAngleY > 185){
Serial.println("SENSOR PITCH ");
Serial.print(kalAngleY);
int value_pitch = kalAngleY/2;
PITCH_SERVO.write(value_pitch);
flag_pitch = 0;
}
///////////////////////////////////////////////////////////////////////
// RESET TO ZERO POSITION JUST FOR SAFETY (ROLL, PITCH, YAW SERVOS) //
///////////////////////////////////////////////////////////////////////
if(flag_roll == 0 && ROLL.getVersus() == 0 && kalAngleX > 177 && kalAngleX < 182){
Serial.println("RESETING ROLL");
ROLL_SERVO.write(90);
flag_roll = 1;
}
if(flag_pitch == 0 && PITCH.getVersus() == 0 && kalAngleY > 182 && kalAngleY < 185){
Serial.println("RESETING PTCH");
PITCH_SERVO.write(90);
flag_pitch = 1;
}
if(flag_yaw == 0 && YAW.getVersus() == 0){
Serial.println("RESETING PTCH");
YAW_SERVO.write(90);
flag_yaw = 1;
}
}
PS. Yes, I have the pinchangeint library installed. But lets forget about that since Ive found a quite good library now that satisfies me.
Cheers!