Flight Controller
Flight Controller
Flight Controller
///////////////////
//Terms of use
////////////////////////////////////////////////////////////////////
///////////////////
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN
//THE SOFTWARE.
////////////////////////////////////////////////////////////////////
///////////////////
//Safety note
////////////////////////////////////////////////////////////////////
///////////////////
//Always remove the propellers and stay away from the motors unless
you
//are 100% certain of what you are doing.
////////////////////////////////////////////////////////////////////
///////////////////
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//PID gain and limit settings
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
float pid_p_gain_roll = 1.3; //Gain setting for the
roll P-controller
float pid_i_gain_roll = 0.04; //Gain setting for the
roll I-controller
float pid_d_gain_roll = 18.0; //Gain setting for the
roll D-controller
int pid_max_roll = 400; //Maximum output of the
PID-controller (+/-)
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//Declaring global variables
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
byte last_channel_1, last_channel_2, last_channel_3, last_channel_4;
byte eeprom_data[36];
byte highByte, lowByte;
volatile int receiver_input_channel_1, receiver_input_channel_2,
receiver_input_channel_3, receiver_input_channel_4;
int counter_channel_1, counter_channel_2, counter_channel_3,
counter_channel_4, loop_counter;
int esc_1, esc_2, esc_3, esc_4;
int throttle, battery_voltage;
int cal_int, start, gyro_address;
int receiver_input[5];
int temperature;
int acc_axis[4], gyro_axis[4];
float roll_level_adjust, pitch_level_adjust;
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//Setup routine
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void setup(){
//Serial.begin(57600);
//Copy the EEPROM data for fast access data.
for(start = 0; start <= 35; start++)eeprom_data[start] = EEPROM.
read(start);
start =
0;
//Set start back to zero.
gyro_address =
eeprom_data[32]; //Store
the gyro address in the variable.
Wire.
begin();
//Start the I2C as master.
TWBR =
12;
//Set the I2C clock speed to 400kHz.
//Check the EEPROM signature to make sure that the setup program
is executed.
while(eeprom_data[33] != 'J' || eeprom_data[34] != 'M' ||
eeprom_data[35] != 'B')delay(10);
set_gyro_registers();
//Set the specific gyro registers.
delayMicroseconds(1000);
//Wait 1000us.
PORTD &=
B00001111;
//Set digital poort 4, 5, 6 and 7 low.
delayMicroseconds(3000);
//Wait 3000us.
}
gyro_signalen();
//Read the gyro output.
gyro_axis_cal[1] +=
gyro_axis[1]; //Ad roll value
to gyro_roll_cal.
gyro_axis_cal[2] +=
gyro_axis[2]; //Ad pitch
value to gyro_pitch_cal.
gyro_axis_cal[3] +=
gyro_axis[3]; //Ad yaw value
to gyro_yaw_cal.
//We don't want the esc's to be beeping annoyingly. So let's
give them a 1000us puls while calibrating the gyro.
PORTD |=
B11110000;
//Set digital poort 4, 5, 6 and 7 high.
delayMicroseconds(1000);
//Wait 1000us.
PORTD &=
B00001111;
//Set digital poort 4, 5, 6 and 7 low.
delay(3);
//Wait 3 milliseconds before the next loop.
}
//Now that we have 2000 measures, we need to devide by 2000 to
get the average gyro offset.
gyro_axis_cal[1] /=
2000; //Divide the
roll total by 2000.
gyro_axis_cal[2] /=
2000; //Divide the
pitch total by 2000.
gyro_axis_cal[3] /=
2000; //Divide the
yaw total by 2000.
PCICR |= (1 <<
PCIE0); //Set
PCIE0 to enable PCMSK0 scan.
PCMSK0 |= (1 <<
PCINT0); //Set
PCINT0 (digital input 8) to trigger an interrupt on state change.
PCMSK0 |= (1 <<
PCINT1); //Set
PCINT1 (digital input 9)to trigger an interrupt on state change.
PCMSK0 |= (1 <<
PCINT2); //Set
PCINT2 (digital input 10)to trigger an interrupt on state change.
PCMSK0 |= (1 <<
PCINT3); //Set
PCINT3 (digital input 11)to trigger an interrupt on state change.
//Wait until the receiver is active and the throtle is set to the
lower position.
while(receiver_input_channel_3 < 990 || receiver_input_channel_3
> 1020 || receiver_input_channel_4 < 1400){
receiver_input_channel_3 =
convert_receiver_channel(3); //Convert the actual
receiver signals for throttle to the standard 1000 - 2000us
receiver_input_channel_4 =
convert_receiver_channel(4); //Convert the actual
receiver signals for yaw to the standard 1000 - 2000us
start
++;
//While waiting increment start whith every loop.
//We don't want the esc's to be beeping annoyingly. So let's
give them a 1000us puls while waiting for the receiver inputs.
PORTD |=
B11110000;
//Set digital poort 4, 5, 6 and 7 high.
delayMicroseconds(1000);
//Wait 1000us.
PORTD &=
B00001111;
//Set digital poort 4, 5, 6 and 7 low.
delay(3);
//Wait 3 milliseconds before the next loop.
if(start ==
125){ //Every
125 loops (500ms).
digitalWrite(12, !
digitalRead(12)); //Change the
led status.
start =
0;
//Start again at 0.
}
}
start =
0;
//Set start back to 0.
loop_timer =
micros(); //Set
the timer for the next loop.
////////////////////////////////////////////////////////////////////
////////////////////////////////
//This is the added IMU code from the videos:
//https://youtu.be/4BoIE8YQwM8
//https://youtu.be/j-kE0AMEWy4
////////////////////////////////////////////////////////////////////
////////////////////////////////
if(abs(acc_y) <
acc_total_vector){ //Prevent
the asin function to produce a NaN
angle_pitch_acc = asin((float)acc_y/acc_total_vector)* 57.
296; //Calculate the pitch angle.
}
if(abs(acc_x) <
acc_total_vector){ //Prevent
the asin function to produce a NaN
angle_roll_acc = asin((float)acc_x/acc_total_vector)* -57.
296; //Calculate the roll angle.
}
//Place the MPU-6050 spirit level and note the values in the
following two lines for calibration.
angle_pitch_acc -= 0.
0;
//Accelerometer calibration value for pitch.
angle_roll_acc -= 0.
0;
//Accelerometer calibration value for roll.
angle_pitch = angle_pitch * 0.9996 + angle_pitch_acc * 0.
0004; //Correct the drift of the gyro pitch angle with
the accelerometer pitch angle.
angle_roll = angle_roll * 0.9996 + angle_roll_acc * 0.
0004; //Correct the drift of the gyro roll angle with
the accelerometer roll angle.
pitch_level_adjust = angle_pitch *
15; //Calculate the pitch angle
correction
roll_level_adjust = angle_roll *
15; //Calculate the roll angle
correction
if(!
auto_level){
//If the quadcopter is not in auto-level mode
pitch_level_adjust =
0; //Set the pitch
angle correction to zero.
roll_level_adjust =
0; //Set the roll
angle correcion to zero.
}
//For starting the motors: throttle low and yaw left (step 1).
if(receiver_input_channel_3 < 1050 && receiver_input_channel_4 <
1050)start = 1;
//When yaw stick is back in the center position start the motors
(step 2).
if(start == 1 && receiver_input_channel_3 < 1050 &&
receiver_input_channel_4 > 1450){
start = 2;
angle_pitch =
angle_pitch_acc; //Set the
gyro pitch angle equal to the accelerometer pitch angle when the
quadcopter is started.
angle_roll =
angle_roll_acc; //Set
the gyro roll angle equal to the accelerometer roll angle when the
quadcopter is started.
gyro_angles_set =
true; //Set the IMU
started flag.
pid_roll_setpoint -=
roll_level_adjust; //Subtract the
angle correction from the standardized receiver roll input value.
pid_roll_setpoint /= 3.
0; //Divide the
setpoint for the PID roll controller by 3 to get angles in degrees.
pid_pitch_setpoint -=
pitch_level_adjust; //Subtract the
angle correction from the standardized receiver pitch input value.
pid_pitch_setpoint /= 3.
0; //Divide the
setpoint for the PID pitch controller by 3 to get angles in degrees.
throttle =
receiver_input_channel_3; //We
need the throttle signal as a base signal.
if (start ==
2){ //The
motors are started.
if (throttle > 1800) throttle =
1800; //We need some room to keep
full control at full throttle.
esc_1 = throttle - pid_output_pitch + pid_output_roll -
pid_output_yaw; //Calculate the pulse for esc 1 (front-right - CCW)
esc_2 = throttle + pid_output_pitch + pid_output_roll +
pid_output_yaw; //Calculate the pulse for esc 2 (rear-right - CW)
esc_3 = throttle + pid_output_pitch - pid_output_roll -
pid_output_yaw; //Calculate the pulse for esc 3 (rear-left - CCW)
esc_4 = throttle - pid_output_pitch - pid_output_roll +
pid_output_yaw; //Calculate the pulse for esc 4 (front-left - CW)
else{
esc_1 =
1000;
//If start is not 2 keep a 1000us pulse for ess-1.
esc_2 =
1000;
//If start is not 2 keep a 1000us pulse for ess-2.
esc_3 =
1000;
//If start is not 2 keep a 1000us pulse for ess-3.
esc_4 =
1000;
//If start is not 2 keep a 1000us pulse for ess-4.
}
////////////////////////////////////////////////////////////////////
////////////////////////////////
//Creating the pulses for the ESC's is explained in this video:
//https://youtu.be/fqEkVcqxtU8
////////////////////////////////////////////////////////////////////
////////////////////////////////
//! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
//Because of the angle calculation the loop time is getting very
important. If the loop time is
//longer or shorter than 4000us the angle calculation is off. If
you modify the code make sure
//that the loop time is still 4000us and no longer! More
information can be found on
//the Q&A page:
//! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !
if(micros() - loop_timer > 4050)digitalWrite(12,
HIGH); //Turn on the LED if the loop time exceeds
4050us.
PORTD |=
B11110000;
//Set digital outputs 4,5,6 and 7 high.
timer_channel_1 = esc_1 +
loop_timer; //Calculate the
time of the faling edge of the esc-1 pulse.
timer_channel_2 = esc_2 +
loop_timer; //Calculate the
time of the faling edge of the esc-2 pulse.
timer_channel_3 = esc_3 +
loop_timer; //Calculate the
time of the faling edge of the esc-3 pulse.
timer_channel_4 = esc_4 +
loop_timer; //Calculate the
time of the faling edge of the esc-4 pulse.
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//This routine is called every time input 8, 9, 10 or 11 changed
state. This is used to read the receiver signals.
//More information about this subroutine can be found in this video:
//https://youtu.be/bENjl1KQbvo
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
ISR(PCINT0_vect){
current_time = micros();
//Channel 1=========================================
if(PINB &
B00000001){
//Is input 8 high?
if(last_channel_1 ==
0){ //Input 8
changed from 0 to 1.
last_channel_1 =
1; //Remember
current input state.
timer_1 =
current_time; //Set
timer_1 to current_time.
}
}
else if(last_channel_1 ==
1){ //Input 8 is not
high and changed from 1 to 0.
last_channel_1 =
0; //Remember
current input state.
receiver_input[1] = current_time -
timer_1; //Channel 1 is current_time -
timer_1.
}
//Channel 2=========================================
if(PINB & B00000010
){ //Is input 9
high?
if(last_channel_2 ==
0){ //Input 9
changed from 0 to 1.
last_channel_2 =
1; //Remember
current input state.
timer_2 =
current_time; //Set
timer_2 to current_time.
}
}
else if(last_channel_2 ==
1){ //Input 9 is not
high and changed from 1 to 0.
last_channel_2 =
0; //Remember
current input state.
receiver_input[2] = current_time -
timer_2; //Channel 2 is current_time -
timer_2.
}
//Channel 3=========================================
if(PINB & B00000100
){ //Is input 10
high?
if(last_channel_3 ==
0){ //Input 10
changed from 0 to 1.
last_channel_3 =
1; //Remember
current input state.
timer_3 =
current_time; //Set
timer_3 to current_time.
}
}
else if(last_channel_3 ==
1){ //Input 10 is not
high and changed from 1 to 0.
last_channel_3 =
0; //Remember
current input state.
receiver_input[3] = current_time -
timer_3; //Channel 3 is current_time -
timer_3.
}
//Channel 4=========================================
if(PINB & B00001000
){ //Is input 11
high?
if(last_channel_4 ==
0){ //Input 11
changed from 0 to 1.
last_channel_4 =
1; //Remember
current input state.
timer_4 =
current_time; //Set
timer_4 to current_time.
}
}
else if(last_channel_4 ==
1){ //Input 11 is not
high and changed from 1 to 0.
last_channel_4 =
0; //Remember
current input state.
receiver_input[4] = current_time -
timer_4; //Channel 4 is current_time -
timer_4.
}
}
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//Subroutine for reading the gyro
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void gyro_signalen(){
//Read the MPU-6050
if(eeprom_data[31] == 1){
Wire.
beginTransmission(gyro_address);
//Start communication with the gyro.
Wire.
write(0x3B);
//Start reading @ register 43h and auto increment with every read.
Wire.
endTransmission();
//End the transmission.
Wire.requestFrom(gyro_address,
14); //Request 14 bytes from
the gyro.
receiver_input_channel_1 =
convert_receiver_channel(1); //Convert the actual
receiver signals for pitch to the standard 1000 - 2000us.
receiver_input_channel_2 =
convert_receiver_channel(2); //Convert the actual
receiver signals for roll to the standard 1000 - 2000us.
receiver_input_channel_3 =
convert_receiver_channel(3); //Convert the actual
receiver signals for throttle to the standard 1000 - 2000us.
receiver_input_channel_4 =
convert_receiver_channel(4); //Convert the actual
receiver signals for yaw to the standard 1000 - 2000us.
while(Wire.available() <
14); //Wait until the 14
bytes are received.
acc_axis[1] = Wire.read()<<8|Wire.
read(); //Add the low and high byte
to the acc_x variable.
acc_axis[2] = Wire.read()<<8|Wire.
read(); //Add the low and high byte
to the acc_y variable.
acc_axis[3] = Wire.read()<<8|Wire.
read(); //Add the low and high byte
to the acc_z variable.
temperature = Wire.read()<<8|Wire.
read(); //Add the low and high byte
to the temperature variable.
gyro_axis[1] = Wire.read()<<8|Wire.
read(); //Read high and low part of
the angular data.
gyro_axis[2] = Wire.read()<<8|Wire.
read(); //Read high and low part of
the angular data.
gyro_axis[3] = Wire.read()<<8|Wire.
read(); //Read high and low part of
the angular data.
}
if(cal_int == 2000){
gyro_axis[1] -=
gyro_axis_cal[1]; //Only
compensate after the calibration.
gyro_axis[2] -=
gyro_axis_cal[2]; //Only
compensate after the calibration.
gyro_axis[3] -=
gyro_axis_cal[3]; //Only
compensate after the calibration.
}
gyro_roll = gyro_axis[eeprom_data[28] &
0b00000011]; //Set gyro_roll to the correct
axis that was stored in the EEPROM.
if(eeprom_data[28] & 0b10000000)gyro_roll *=
-1; //Invert gyro_roll if the MSB of
EEPROM bit 28 is set.
gyro_pitch = gyro_axis[eeprom_data[29] &
0b00000011]; //Set gyro_pitch to the correct
axis that was stored in the EEPROM.
if(eeprom_data[29] & 0b10000000)gyro_pitch *=
-1; //Invert gyro_pitch if the MSB of
EEPROM bit 29 is set.
gyro_yaw = gyro_axis[eeprom_data[30] &
0b00000011]; //Set gyro_yaw to the correct
axis that was stored in the EEPROM.
if(eeprom_data[30] & 0b10000000)gyro_yaw *=
-1; //Invert gyro_yaw if the MSB of
EEPROM bit 30 is set.
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//Subroutine for calculating pid outputs
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//The PID controllers are explained in part 5 of the YMFC-3D video
session:
//https://youtu.be/JBvnB0279-Q
////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void calculate_pid(){
//Roll calculations
pid_error_temp = gyro_roll_input - pid_roll_setpoint;
pid_i_mem_roll += pid_i_gain_roll * pid_error_temp;
if(pid_i_mem_roll > pid_max_roll)pid_i_mem_roll = pid_max_roll;
else if(pid_i_mem_roll < pid_max_roll * -1)pid_i_mem_roll =
pid_max_roll * -1;
pid_last_roll_d_error = pid_error_temp;
//Pitch calculations
pid_error_temp = gyro_pitch_input - pid_pitch_setpoint;
pid_i_mem_pitch += pid_i_gain_pitch * pid_error_temp;
if(pid_i_mem_pitch > pid_max_pitch)pid_i_mem_pitch =
pid_max_pitch;
else if(pid_i_mem_pitch < pid_max_pitch * -1)pid_i_mem_pitch =
pid_max_pitch * -1;
pid_last_pitch_d_error = pid_error_temp;
//Yaw calculations
pid_error_temp = gyro_yaw_input - pid_yaw_setpoint;
pid_i_mem_yaw += pid_i_gain_yaw * pid_error_temp;
if(pid_i_mem_yaw > pid_max_yaw)pid_i_mem_yaw = pid_max_yaw;
else if(pid_i_mem_yaw < pid_max_yaw * -1)pid_i_mem_yaw =
pid_max_yaw * -1;
pid_output_yaw = pid_p_gain_yaw * pid_error_temp + pid_i_mem_yaw
+ pid_d_gain_yaw * (pid_error_temp - pid_last_yaw_d_error);
if(pid_output_yaw > pid_max_yaw)pid_output_yaw = pid_max_yaw;
else if(pid_output_yaw < pid_max_yaw * -1)pid_output_yaw =
pid_max_yaw * -1;
pid_last_yaw_d_error = pid_error_temp;
}
actual =
receiver_input[channel];
//Read the actual receiver value for the corresponding function
low = (eeprom_data[channel * 2 + 15] << 8) | eeprom_data[channel
* 2 + 14]; //Store the low value for the specific receiver input
channel
center = (eeprom_data[channel * 2 - 1] << 8) |
eeprom_data[channel * 2 - 2]; //Store the center value for the
specific receiver input channel
high = (eeprom_data[channel * 2 + 7] << 8) | eeprom_data[channel
* 2 + 6]; //Store the high value for the specific receiver input
channel
if(actual <
center){
//The actual receiver value is lower than the center value
if(actual < low)actual =
low; //Limit the
lowest value to the value that was detected during setup
difference = ((long)(center - actual) * (long)500) / (center -
low); //Calculate and scale the actual value to a 1000 -
2000us value
if(reverse == 1)return 1500 +
difference; //If the channel is
reversed
else return 1500 -
difference; //If the
channel is not reversed
}
else if(actual >
center){
//The actual receiver value is higher than the center
value
if(actual > high)actual =
high; //Limit the lowest
value to the value that was detected during setup
difference = ((long)(actual - center) * (long)500) / (high -
center); //Calculate and scale the actual value to a 1000 -
2000us value
if(reverse == 1)return 1500 -
difference; //If the channel is
reversed
else return 1500 +
difference; //If the
channel is not reversed
}
else return 1500;
}
void set_gyro_registers(){
//Setup the MPU-6050
if(eeprom_data[31] == 1){
Wire.
beginTransmission(gyro_address);
//Start communication with the address found during search.
Wire.
write(0x6B);
//We want to write to the PWR_MGMT_1 register (6B hex)
Wire.
write(0x00);
//Set the register bits as 00000000 to activate the gyro
Wire.
endTransmission();
//End the transmission with the gyro.
Wire.
beginTransmission(gyro_address);
//Start communication with the address found during search.
Wire.
write(0x1B);
//We want to write to the GYRO_CONFIG register (1B hex)
Wire.
write(0x08);
//Set the register bits as 00001000 (500dps full scale)
Wire.
endTransmission();
//End the transmission with the gyro
Wire.
beginTransmission(gyro_address);
//Start communication with the address found during search.
Wire.
write(0x1C);
//We want to write to the ACCEL_CONFIG register (1A hex)
Wire.
write(0x10);
//Set the register bits as 00010000 (+/- 8g full scale range)
Wire.
endTransmission();
//End the transmission with the gyro
while(1)delay(10);
//Stay in this loop for ever
}
Wire.
beginTransmission(gyro_address);
//Start communication with the address found during search
Wire.
write(0x1A);
//We want to write to the CONFIG register (1A hex)
Wire.
write(0x03);
//Set the register bits as 00000011 (Set Digital Low Pass Filter
to ~43Hz)
Wire.
endTransmission();
//End the transmission with the gyro
}
}