Un Giroscopio: Materiales Drone
Un Giroscopio: Materiales Drone
Un Giroscopio: Materiales Drone
1. UN GIROSCOPIO
2. UN ARDUINO NANO
3. UN SOPORTE
4. 4 MOTORES MINI
5. UNA BATERIA LIPO
6. Cámara arduino
Código para arduino:
<p>#include "I2Cdev.h"<br>#include <servo.h>
#include <spi.h>
#include "RF24.h"
#include "MPU6050_6Axis_MotionApps20.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
//we need this to see nrf24l01 configuration in serial
#include "printf.h"</spi.h></servo.h></p><p>//end of libraries
###############################################</p><p>MPU6050 mpu;</p><p>float
x_rotation, y_rotation, z_rotation;
Servo motor1;
Servo motor2;
Servo motor3;
Servo motor4;
bool first_loop = true;
//radio
RF24 radio(9,10);
uint8_t data[6];
const uint64_t pipe = 0xE8E8F0F0E1LL;
long last_received;
int controll_number = 159;</p><p>//values = 5.2, 0.02, 1500
//variables for movement and positions ###########################################
//for my quadcopter this are the best settings for pid
float x_kp = 5, x_ki = 0.02, x_kd = 1100; //values for PID X axis
int max_pid = 300;
float x_p_out, x_i_out, x_d_out, x_output; //outputs for PID
float x_now, x_lasttime = 0, x_timechange;
float x_input, x_lastinput = 0, x_setpoint = 0;
float x_error, x_errorsum = 0, x_d_error, x_lasterror;</p><p>//values = 5.2, 0.02, 1500
float y_kp = 5, y_ki = 0.02, y_kd = 1100; //values for PID Y axis
float y_p_out, y_i_out, y_d_out, y_output; //outputs for PID
float y_now, y_lasttime = 0, y_timechange;
float y_input, y_lastinput = 0, y_setpoint = 0;
float y_error, y_errorsum = 0, y_d_error, y_lasterror;</p><p>float z_kp = 2, z_ki = 0, z_kd = 0;
//values for PID Z axis
float z_p_out, z_i_out, z_d_out, z_output; //outputs for PID
float z_now, z_lasttime = 0, z_timechange;
float z_input, z_lastinput = 0, z_setpoint = 0;
float z_error, z_errorsum = 0, z_d_error, z_lasterror;</p><p>//set it to 0 and see on serial port
what is the value for x and y rotation, use only if your flightcontroller board is not perfevtly
leveled. If your board is perfectly leveled set it to 0
float x_level_error = 0;
float y_level_error = 0;</p><p>/*
*
*
* JUNE 2016 - APRIL 2017
* C by Nikodem Bartnik
* [email protected]
* nikodembartnik.pl
*
*
*
*/</p><p>#define INTERRUPT_PIN 2 // use pin 2 on Arduino Uno & most boards
int motor1_power;
int motor2_power;
int motor3_power;
int motor4_power;</p><p>float allmotors_power = 600;</p><p>// MPU control/status vars
bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer</p><p>// orientation/motion vars
Quaternion q; // [w, x, y, z] quaternion container</p><p>VectorFloat gravity; // [x, y, z]
gravity vector</p><p>float rotation[3]; // [yaw, pitch, roll] yaw/pitch/roll container and
gravity vector
int safe_lock = 1;</p><p>volatile bool mpuInterrupt = false; // indicates whether MPU interrupt
pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}</p><p>void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation
difficulties
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif</p><p>printf_begin();</p><p> Serial.begin(115200);</p><p>
Serial.println("Initializing I2C devices...");
mpu.initialize();
pinMode(INTERRUPT_PIN, INPUT);</p><p> // verify connection
Serial.println("Testing device connections...");
Serial.println(mpu.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection
failed");
// bmp.begin();
radio.begin();
delay(1000);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_MAX);</p><p> radio.openReadingPipe(1,pipe);
radio.startListening();
}
}
// Serial.println(data[1]);
if(done == true){
last_received = millis();
}
}
}</p><p> if((millis()-last_received) > 3000 && allmotors_power > 0){
safe_lock = 0;
}
mpu.dmpGetQuaternion(&q, fifoBuffer);
mpu.dmpGetGravity(&gravity, &q);
mpu.dmpGetYawPitchRoll(rotation, &q, &gravity);</p><p> x_rotation = rotation[1] *
180/M_PI - x_level_error;
y_rotation = rotation[2] * 180/M_PI - y_level_error;
z_rotation = rotation[0] * 180/M_PI;</p><p>/*
if(pressure_loop_number == 10){
// Serial.print("Preasure: ");
//Serial.println(bmp.readAltitude());
pressure_loop_number = 0;
allmotors_power = 1000;
}
pressure_loop_number++;
*/</p><p> if(first_loop == true){
z_setpoint = z_rotation;
// current_altitude = bmp.readAltitude();
//set_altitude = current_altitude;
first_loop = false;
}
motor1_power = allmotors_power;
motor2_power = allmotors_power;
motor3_power = allmotors_power;
motor4_power = allmotors_power;
if(allmotors_power > 1500){
allmotors_power = 1500;
}</p><p>
x_output = calculatePID(0, x_rotation);
y_output = calculatePID(1, y_rotation);
z_output = calculatePID(2, z_rotation);</p><p> motor1_power += x_output/2;
motor1_power += z_output;
motor4_power -= x_output/2;
motor4_power += z_output;</p><p> motor2_power -= y_output/2;
motor2_power -= z_output;
motor3_power += y_output/2;
motor3_power -= z_output;
motor1.writeMicroseconds(motor1_power);
motor4.writeMicroseconds(motor4_power);
motor2.writeMicroseconds(motor2_power);
motor3.writeMicroseconds(motor3_power);
mpu.resetFIFO();
}else{
motor1.write(0);
motor2.write(0);
motor3.write(0);
motor4.write(0);
}
}
}</p><p> float calculatePID(int _axis, float _angel){</p><p> // X AXIS
if(_axis == 0){
x_now = millis();
x_timechange = x_now - x_lasttime;
x_error = x_setpoint - _angel;
x_p_out = (x_kp * x_error);
}</p>
<p>/*</p><p> *
*
* JUNE 2016 - APRIL 2017
* C by Nikodem Bartnik
* [email protected]
* nikodembartnik.pl
*
*
*