Hey vinceherman! Sure let me tell you some more details.
The quadcopter is using Ardupilot 2.8 which is a quite old. It is not autonomous but I am trying to make it autonomous in 1 DOF. This means that I will control with my trasmiter Aileron, Elevator, Rudder and the Throttle will be controlled by the Ultrasonic Sensor. Yes it was OK before that, more than OK.
I am using Turnigy 9XR Pro as my transmitter.
Ardupilot: https://www.ardupilot.co.uk/
Exactly I am trying to control the throttle. Let me post some code. I am not posting the entire file caz its a huge one with details that you dont need.
#define THROTTLE_IN_PIN 4
Servo THROTTLE_TO_FC;
unsigned long thrDuration;
const byte thrInPin = THROTTLE_IN_PIN;
I read the receiver with
thrDuration = pulseIn(thrInPin, HIGH);
I write to the Flight Controller with:
value = thrDuration+distance;
THROTTLE_TO_FC.writeMicroseconds(value);
My setup() function:
pinMode(thrInPin, INPUT);
THROTTLE_TO_FC.attach(8);
So, my algorithm is to read from the ultrasonic distance and I read correctly using this example:
#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
if (distance >= maximumRange || distance <= minimumRange){
/* Send a negative number to computer and Turn LED ON
to indicate "out of range" */
Serial.println("-1");
digitalWrite(LEDPin, HIGH);
}
else {
/* Send the distance to the computer using Serial protocol, and
turn LED OFF to indicate successful reading. */
Serial.println(distance);
digitalWrite(LEDPin, LOW);
}
//Delay 50ms before next reading.
delay(50);
}
Then I need to write the distance multiplied with a gain factor so I will end up in a range of 1400 to 1600. I have done this too. My last print before the writeMicroseconds outputs the correct values. The problem occur when I writeMicroseconds to the FC for some reason.
Thank you for your time.