I'm writing a library to control a flying wing type airplane. When I upload this to the Arduino the rudder servo goes to 0 degrees but tries to keep pushing further. The left and right servos don't react in ant way. I tried to use FullControl in a regular function in an ino file and it worked completely fine. This is my code:
main Arduino code (.ino):
#include"FlyingWingRudder.h"
FlyingWingRudder plane(9,8,7); // declares the FlyingWingRudder object
void setup() {
}
void loop() {
plane.FullControl(15,15,15); // controls the Elevator, Ailerons and rudder
}
library (.h file)
#ifndef FlyingWingRudder_h
#define FlyingWingRudder_h
#include "Arduino.h"
class FlyingWingRudder{
public:
FlyingWingRudder(int leftServo , int rightServo , int rudderServo);
void FullControl(int PowerE, int PowerA, int PowerR);
private:
int _leftServo;
int _rightServo;
int _rudderServo;
int _Power;
int _PowerE;
int _PowerR;
int _PowerA;
};
#endif
library (.ccp) file
#include "Arduino.h"
#include "FlyingWingRudder.h"
#include <Servo.h>
Servo left; //left flap servo
Servo right; //right flap servo
Servo Rudder; // rudder servo
//float positionLeft = 90;
//float positionRight = 90;
//float positionRudder = 90;
FlyingWingRudder::FlyingWingRudder(int leftServo , int rightServo ,int rudderServo){ //attaches the servos to the pins specified in the program
left.attach(leftServo);
right.attach(rightServo);
Rudder.attach(rudderServo);
}
void FlyingWingRudder::FullControl(int PowerE , int PowerA , int PowerR ){ // power (in degrees) of the Elevator, Aileron and Rudder
if(PowerE > 0 && PowerA > 0){ //the elevator will tilt the plane upwards and the aileron will roll the plane anticlockwise
left.write(90 + PowerA + PowerE);
right.write(90 - PowerA + PowerE);
}
if(PowerE > 0 && PowerA < 0){ //the elevator will tilt the plane upwards and the aileron will roll the plane clockwise
left.write(90 - PowerA + PowerE);
right.write(90 + PowerA + PowerE);
}
if(PowerE > 0 && PowerA == 0){ //the elevator will tilt the plane upwards and the aileron is disabled
left.write(90 + PowerE);
right.write(90 + PowerE);
}
if(PowerE < 0 && PowerA > 0){ //the elevator will tilt the plane downwards and the aileron will roll the plane anticlockwise
left.write(90 + PowerA - PowerE);
right.write(90 - PowerA - PowerE);
}
if(PowerE < 0 && PowerA < 0){ //the elevator will tilt the plane downwards and the aileron will roll the plane clockwise
left.write(90 - PowerA - PowerE);
right.write(90 + PowerA - PowerE);
}
if(PowerE < 0 && PowerA == 0){ //the elevator will tilt the plane downwards and the aileron is disabled
left.write(90 - PowerE);
right.write(90 - PowerE);
}
Rudder.write(90 + PowerR); //turns the plane's rudder
}