├── Chapter01 └── No_Code.txt ├── Chapter02 └── No_Code.txt ├── Chapter07 └── No_Code.txt ├── Chapter09 └── No_Code.txt ├── Chapter04 ├── test_gyr_acc_function │ └── test_gyr_acc_function.ino ├── mpu6050_init_function │ └── mpu6050_init_function.ino ├── All_Defining │ └── All_Defining.ino ├── accel_t_gyro_union_function │ └── accel_t_gyro_union_function.ino ├── motor_arm_ide_and_off_functions │ └── motor_arm_ide_and_off_functions.ino ├── reciever_to_value_function │ └── reciever_to_value_function.ino ├── stabilize_function │ └── stabilize_function.ino ├── void_setup_and_void_loop │ └── void_setup_and_void_loop.ino ├── MPU6050_Read_and_Write_Functions │ └── MPU6050_Read_and_Write_Functions.ino ├── radio_receive_and_signal │ └── radio_receive_and_signal.ino ├── GPS_Blynk │ └── GPS_Blynk.ino ├── gyro_read_function │ └── gyro_read_function.ino └── full_Code │ └── full_Code.ino ├── Chapter03 └── Blynk │ └── Blynk.ino ├── Chapter06 └── CameraGimbalCodeFull │ └── CameraGimbalCodeFull.ino ├── Chapter08 └── AvoideObstacleFullCode │ └── AvoideObstacleFullCode.ino ├── LICENSE ├── README.md └── Chapter05 └── BarometerCodeFull └── BarometerCodeFull.ino /Chapter01/No_Code.txt: -------------------------------------------------------------------------------- 1 | This chapter has no code -------------------------------------------------------------------------------- /Chapter02/No_Code.txt: -------------------------------------------------------------------------------- 1 | This chapter has no code -------------------------------------------------------------------------------- /Chapter07/No_Code.txt: -------------------------------------------------------------------------------- 1 | This chapter has no code -------------------------------------------------------------------------------- /Chapter09/No_Code.txt: -------------------------------------------------------------------------------- 1 | This chapter has no code -------------------------------------------------------------------------------- /Chapter04/test_gyr_acc_function/test_gyr_acc_function.ino: -------------------------------------------------------------------------------- 1 | void test_gyr_acc() { 2 | error = MPU6050_read (MPU6050_WHO_AM_I, &c, 1); 3 | if (error != 0) { 4 | while (true) { 5 | digitalWrite(13, HIGH); 6 | delay(300); 7 | digitalWrite(13, LOW); 8 | delay(300); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Chapter04/mpu6050_init_function/mpu6050_init_function.ino: -------------------------------------------------------------------------------- 1 | void mpu6050_init() { 2 | MPU6050_write_reg (MPU6050_GYRO_CONFIG, 0x08); // 0x00 = 250 deg/s _______ 0x08 = 500deg/s (dela med 65.5 för deg/s) ____________ ___ 0x10 = 1000deg/s ___ 0xC = 2500 deg/s 3 | MPU6050_write_reg (MPU6050_ACCEL_CONFIG, 0x08); // 0x00 = +-2g _____ 0x08 = +-4 g ___.... 4 | MPU6050_write_reg (MPU6050_CONFIG, 0x03); //LOW-pass filter 5 | 6 | } 7 | -------------------------------------------------------------------------------- /Chapter03/Blynk/Blynk.ino: -------------------------------------------------------------------------------- 1 | #define ESP8266_BAUD 9600 2 | #include 3 | #include 4 | #include 5 | char auth[] = "YourAuthToken"; 6 | char ssid[] = "YourNetworkName"; 7 | char pass[] = "YourPassword"; 8 | SoftwareSerial EspSerial(2, 3); 9 | 10 | ESP8266 wifi(&EspSerial); 11 | 12 | void setup() 13 | { 14 | Serial.begin(9600); 15 | EspSerial.begin(ESP8266_BAUD); 16 | delay(10); 17 | Blynk.begin(auth, wifi, ssid, pass); 18 | } 19 | void loop() 20 | { 21 | Blynk.run(); 22 | } 23 | -------------------------------------------------------------------------------- /Chapter04/All_Defining/All_Defining.ino: -------------------------------------------------------------------------------- 1 | typedef union accel_t_gyro_union 2 | { 3 | struct 4 | { 5 | uint8_t x_accel_h; 6 | uint8_t x_accel_l; 7 | uint8_t y_accel_h; 8 | uint8_t y_accel_l; 9 | uint8_t z_accel_h; 10 | uint8_t z_accel_l; 11 | uint8_t t_h; 12 | uint8_t t_l; 13 | uint8_t x_gyro_h; 14 | uint8_t x_gyro_l; 15 | uint8_t y_gyro_h; 16 | uint8_t y_gyro_l; 17 | uint8_t z_gyro_h; 18 | uint8_t z_gyro_l; 19 | } reg; 20 | struct 21 | { 22 | int x_accel; 23 | int y_accel; 24 | int z_accel; 25 | int temperature; 26 | int x_gyro; 27 | int y_gyro; 28 | int z_gyro; 29 | } value; 30 | }; 31 | -------------------------------------------------------------------------------- /Chapter06/CameraGimbalCodeFull/CameraGimbalCodeFull.ino: -------------------------------------------------------------------------------- 1 | 2 | #define BLYNK_PRINT Serial 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | char auth[] = "**********"; 9 | char ssid[] = "**********"; 10 | char pass[] = "**********"; 11 | 12 | Servo s1; 13 | Servo s2; 14 | 15 | void setup() 16 | { 17 | Serial.begin(9600); 18 | Blynk.begin(auth, ssid, pass); 19 | s1.attach(0); 20 | s2.attach(2); 21 | } 22 | BLYNK_WRITE(V1) 23 | { 24 | s1.write(param.asInt()); 25 | } 26 | BLYNK_WRITE(V6) 27 | { 28 | s2.write(param.asInt()); 29 | } 30 | void loop() 31 | { 32 | Blynk.run(); 33 | } 34 | -------------------------------------------------------------------------------- /Chapter04/accel_t_gyro_union_function/accel_t_gyro_union_function.ino: -------------------------------------------------------------------------------- 1 | typedef union accel_t_gyro_union 2 | { 3 | struct 4 | { 5 | uint8_t x_accel_h; 6 | uint8_t x_accel_l; 7 | uint8_t y_accel_h; 8 | uint8_t y_accel_l; 9 | uint8_t z_accel_h; 10 | uint8_t z_accel_l; 11 | uint8_t t_h; 12 | uint8_t t_l; 13 | uint8_t x_gyro_h; 14 | uint8_t x_gyro_l; 15 | uint8_t y_gyro_h; 16 | uint8_t y_gyro_l; 17 | uint8_t z_gyro_h; 18 | uint8_t z_gyro_l; 19 | } reg; 20 | struct 21 | { 22 | int x_accel; 23 | int y_accel; 24 | int z_accel; 25 | int temperature; 26 | int x_gyro; 27 | int y_gyro; 28 | int z_gyro; 29 | } value; 30 | }; 31 | -------------------------------------------------------------------------------- /Chapter08/AvoideObstacleFullCode/AvoideObstacleFullCode.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | char auth[] = "**********"; 4 | char ssid[] = "**********"; 5 | char pass[] = "**********"; 6 | 7 | void setup() { 8 | Blynk.begin(auth, ssid, pass); 9 | pinMode(4, OUTPUT); //for trigger 10 | pinMode(5, INPUT); //for echo 11 | } 12 | 13 | void loop() { 14 | long duration, distance; 15 | digitalWrite(4, LOW); //for trigger 16 | delayMicroseconds(2); 17 | digitalWrite(4, HIGH); 18 | delayMicroseconds(10); 19 | digitalWrite(4, LOW); 20 | duration = pulseIn(5, HIGH); //for echo 21 | distance = (duration / 2) / 29.1; 22 | Blynk.virtualWrite(V0, distance); 23 | delay(200); 24 | Blynk.run(); 25 | } 26 | -------------------------------------------------------------------------------- /Chapter04/motor_arm_ide_and_off_functions/motor_arm_ide_and_off_functions.ino: -------------------------------------------------------------------------------- 1 | 2 | 3 | void motor_arm() { 4 | delay(2000); 5 | myservoT.writeMicroseconds(800); //for degees 0-180 6 | myservoR.writeMicroseconds(800); //for degees 0-180 7 | myservoB.writeMicroseconds(800); //for degees 0-180 8 | myservoL.writeMicroseconds(800); //for degees 0-180 9 | delay(4700); 10 | } 11 | 12 | 13 | void motor_idle() { 14 | myservoT.write(57); //for degees 0-180 15 | myservoR.write(57); //for degees 0-180 16 | myservoB.write(57); //for degees 0-180 17 | myservoL.write(57); //for degees 0-180 18 | } 19 | 20 | 21 | void motor_off() { 22 | main_power = 19; 23 | myservoT.write(19); //for degees 0-180 24 | myservoR.write(19); //for degees 0-180 25 | myservoB.write(19); //for degees 0-180 26 | myservoL.write(19); //for degees 0-180 27 | } 28 | -------------------------------------------------------------------------------- /Chapter04/reciever_to_value_function/reciever_to_value_function.ino: -------------------------------------------------------------------------------- 1 | void reciever_to_value() { 2 | if (ESC > 900 && ESC < 2000) { 3 | main_power = map(ESC, 1080, 2000, 670, 2000); 4 | } else { 5 | main_power = 800; 6 | } 7 | 8 | yaw_temp = AILERON; 9 | 10 | if (AILERON > 900 && AILERON < 2000) { 11 | if (yaw_temp > 1550 || yaw_temp < 1450) { 12 | rad_rotate = map(yaw_temp, 970, 2000, 180, -180); 13 | } else { 14 | rad_rotate = 0; 15 | } 16 | } 17 | 18 | 19 | if (ppp > 900 && ppp < 2000) { 20 | rad_tilt_TB = map(ppp, 970, 2000, -44, 44); 21 | } else { 22 | rad_tilt_TB = 0; 23 | } 24 | 25 | 26 | if (ELEVATOR > 900 && ELEVATOR < 2000) { 27 | rad_tilt_LR = map(ELEVATOR, 1000, 2000, -44, 44); 28 | } else { 29 | rad_tilt_LR = 0; 30 | } 31 | 32 | PID_tune_2 = 0.1 * (map(ppp3, 1000, 2010, 4, 15)); 33 | PID_tune = 0.1 * (map(ppp2, 1000, 2010, 15, 50)); 34 | // gimbal_wanted_ang = map(ppp2,1000,2010,100,0); 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Chapter04/stabilize_function/stabilize_function.ino: -------------------------------------------------------------------------------- 1 | void stabilize() { 2 | 3 | 4 | 5 | P_x = (x_a + rad_tilt_LR) * 2.4; 6 | P_y = (y_a + rad_tilt_TB) * 2.4; 7 | I_x = I_x + (x_a + rad_tilt_LR) * dt_ * 3.7; 8 | I_y = I_y + (y_a + rad_tilt_TB) * dt_ * 3.7; 9 | D_x = x_vel * 0.7; 10 | D_y = y_vel * 0.7; 11 | //2.4 3.7 0.7 12 | //YAW 13 | 14 | P_z = (z_ang + wanted_z_ang) * 2.0; 15 | I_z = I_z + (z_ang + wanted_z_ang) * dt_ * 0.8; 16 | D_z = z_vel * 0.3; 17 | 18 | 19 | 20 | 21 | if (P_z > 160) { 22 | P_z = 160; 23 | } 24 | if (P_z < -160) { 25 | P_z = -160; 26 | } 27 | 28 | if (I_x > 30) { 29 | I_x = 30; 30 | } 31 | if (I_x < -30) { 32 | I_x = -30; 33 | } 34 | if (I_y > 30) { 35 | I_y = 30; 36 | } 37 | if (I_y < -30) { 38 | I_y = -30; 39 | } 40 | if (I_z > 30) { 41 | I_z = 30; 42 | } 43 | if (I_z < -30) { 44 | I_z = -30; 45 | } 46 | 47 | 48 | 49 | x_adder = P_x + I_x + D_x; 50 | y_adder = P_y + I_y + D_y; 51 | } 52 | 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Chapter04/void_setup_and_void_loop/void_setup_and_void_loop.ino: -------------------------------------------------------------------------------- 1 | void setup() 2 | { 3 | 4 | digitalWrite(13, HIGH); 5 | myservoT.attach(7); //7-TOP 6 | myservoR.attach(8); //8-Right 7 | myservoB.attach(9); //9 - BACK 8 | myservoL.attach(10); //10 LEFT 9 | Wire.begin(); 10 | PCintPort::attachInterrupt(ESC_IN, escRead, CHANGE); 11 | PCintPort::attachInterrupt(ELEVATOR_IN, elevatorRead, CHANGE); 12 | PCintPort::attachInterrupt(AILERON_IN, aileronRead, CHANGE); 13 | PCintPort::attachInterrupt(ppp_in, pppRead, CHANGE); 14 | PCintPort::attachInterrupt(ppp2_in, ppp2Read, CHANGE); 15 | PCintPort::attachInterrupt(ppp3_in, ppp3Read, CHANGE); 16 | test_gyr_acc(); 17 | test_radio_reciev(); 18 | 19 | mpu6050_init(); 20 | // Clear the 'sleep' bit to start the sensor. 21 | MPU6050_write_reg (MPU6050_PWR_MGMT_1, 0); 22 | // delay(1000); 23 | 24 | main_power = 0; 25 | motor_arm(); 26 | delay(800); 27 | digitalWrite(13, LOW); 28 | 29 | } 30 | 31 | 32 | void loop() 33 | { 34 | 35 | timer_counter++; 36 | if (timer_counter > 10000) { 37 | 38 | digitalWrite(13, HIGH); 39 | timer_counter = 0; 40 | } 41 | check_radio_signal(); 42 | reciever_to_value(); 43 | calc_wanted_z_ang(); 44 | read_acc_gyr(); 45 | if (main_power > 750) { 46 | stabilize(); 47 | } else { 48 | zero_on_zero_throttle(); 49 | } 50 | set_power(); 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Chapter04/MPU6050_Read_and_Write_Functions/MPU6050_Read_and_Write_Functions.ino: -------------------------------------------------------------------------------- 1 | int MPU6050_read(int start, uint8_t *buffer, int size) 2 | { 3 | int i, n, error; 4 | 5 | Wire.beginTransmission(MPU6050_I2C_ADDRESS); 6 | n = Wire.write(start); 7 | if (n != 1) 8 | return (-10); 9 | 10 | n = Wire.endTransmission(false); 11 | if (n != 0) 12 | return (n); 13 | 14 | // Third parameter is true: relase I2C-bus after data is read. 15 | Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true); 16 | i = 0; 17 | while (Wire.available() && i < size) 18 | { 19 | buffer[i++] = Wire.read(); 20 | } 21 | if ( i != size) 22 | return (-11); 23 | 24 | return (0); 25 | } 26 | 27 | 28 | 29 | int MPU6050_write(int start, const uint8_t *pData, int size) 30 | { 31 | int n, error; 32 | 33 | Wire.beginTransmission(MPU6050_I2C_ADDRESS); 34 | n = Wire.write(start); // write the start address 35 | if (n != 1) 36 | return (-20); 37 | 38 | n = Wire.write(pData, size); // write data bytes 39 | if (n != size) 40 | return (-21); 41 | 42 | error = Wire.endTransmission(true); // release the I2C-bus 43 | if (error != 0) 44 | return (error); 45 | 46 | return (0); // return : no error 47 | } 48 | 49 | 50 | int MPU6050_write_reg(int reg, uint8_t data) 51 | { 52 | int error; 53 | 54 | error = MPU6050_write(reg, &data, 1); 55 | 56 | return (error); 57 | } 58 | -------------------------------------------------------------------------------- /Chapter04/radio_receive_and_signal/radio_receive_and_signal.ino: -------------------------------------------------------------------------------- 1 | void test_radio_reciev() { 2 | while (ESC > 1120 || ESC < 1060) { 3 | 4 | //check shared flags to see if we have a new signal 5 | if (bUpdateFlagsShared) { 6 | noInterrupts(); 7 | bUpdateFlags = bUpdateFlagsShared; 8 | if (bUpdateFlags & ESC_FLAG) { 9 | ESC = ESC_IN_SHARED; 10 | } 11 | if (bUpdateFlags & ELEVATOR_FLAG) { 12 | ELEVATOR = ELEVATOR_IN_SHARED; 13 | } 14 | if (bUpdateFlags & AILERON_FLAG) { 15 | AILERON = AILERON_IN_SHARED; 16 | } 17 | if (bUpdateFlags & ppp_flag) { 18 | ppp = ppp_in_shared; 19 | } 20 | 21 | bUpdateFlagsShared = 0; 22 | interrupts(); 23 | 24 | } 25 | 26 | bUpdateFlags = 0; 27 | 28 | 29 | } 30 | } 31 | 32 | 33 | 34 | void check_radio_signal() { 35 | //check shared flags to see if we have a new signal 36 | if (bUpdateFlagsShared) { 37 | noInterrupts(); 38 | bUpdateFlags = bUpdateFlagsShared; 39 | if (bUpdateFlags & ESC_FLAG) { 40 | ESC = ESC_IN_SHARED; 41 | } 42 | if (bUpdateFlags & ELEVATOR_FLAG) { 43 | ELEVATOR = ELEVATOR_IN_SHARED; 44 | } 45 | if (bUpdateFlags & AILERON_FLAG) { 46 | AILERON = AILERON_IN_SHARED; 47 | } 48 | if (bUpdateFlags & ppp_flag) { 49 | ppp = ppp_in_shared; 50 | } 51 | if (bUpdateFlags & ppp2_flag) { 52 | ppp2 = ppp2_in_shared; 53 | } 54 | 55 | if (bUpdateFlags & ppp3_flag) { 56 | ppp3 = ppp3_in_shared; 57 | } 58 | 59 | 60 | bUpdateFlagsShared = 0; 61 | interrupts(); 62 | 63 | } 64 | 65 | bUpdateFlags = 0; 66 | 67 | 68 | 69 | 70 | } 71 | -------------------------------------------------------------------------------- /Chapter04/GPS_Blynk/GPS_Blynk.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define BLYNK_PRINT Serial 4 | #include 5 | #include 6 | 7 | static const int RX = 4, TX = 5; 8 | static const uint32_t GPSBaud = 9600; 9 | 10 | TinyGPSPlus mygps; 11 | WidgetMap myMap(V0); 12 | 13 | SoftwareSerial test_GPS(RX, TX); 14 | 15 | BlynkTimer timer; 16 | 17 | float noofsats; 18 | 19 | 20 | char auth[] = "********"; 21 | char ssid[] = "********"; 22 | char pass[] = "********"; 23 | 24 | void setup() 25 | { 26 | Serial.begin(115200); 27 | Serial.println(); 28 | test_GPS.begin(GPSBaud); 29 | Blynk.begin(auth, ssid, pass); 30 | timer.setInterval(5000L, checkGPS); 31 | } 32 | 33 | void checkGPS() { 34 | if (mygps.charsProcessed() < 10) 35 | { 36 | Serial.println(F("No GPS detected: check wiring.")); 37 | Blynk.virtualWrite(V4, "GPS ERROR"); 38 | } 39 | } 40 | 41 | void loop() 42 | { 43 | 44 | while (test_GPS.available() > 0) 45 | { 46 | if (mygps.encode(ss.read())) 47 | displayInfo(); 48 | } 49 | Blynk.run(); 50 | timer.run(); 51 | } 52 | 53 | void displayInfo() 54 | { 55 | 56 | if (mygps.location.isValid() ) 57 | { 58 | 59 | float latitude = (mygps.location.lat()); 60 | float longitude = (mygps.location.lng()); 61 | 62 | Serial.print("LAT: "); 63 | Serial.println(latitude, 6); 64 | Serial.print("LONG: "); 65 | Serial.println(longitude, 6); 66 | Blynk.virtualWrite(V1, String(latitude, 6)); 67 | Blynk.virtualWrite(V2, String(longitude, 6)); 68 | myMap.location(move_index, latitude, longitude, "GPS_Location"); 69 | sats = gps.satellites.value(); 70 | Blynk.virtualWrite(V3, sats); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Chapter04/gyro_read_function/gyro_read_function.ino: -------------------------------------------------------------------------------- 1 | void read_acc_gyr() { 2 | error = MPU6050_read (MPU6050_ACCEL_XOUT_H, (uint8_t *) &accel_t_gyro, sizeof(accel_t_gyro)); 3 | 4 | uint8_t swap; 5 | #define SWAP(x,y) swap = x; x = y; y = swap 6 | 7 | SWAP (accel_t_gyro.reg.x_accel_h, accel_t_gyro.reg.x_accel_l); 8 | SWAP (accel_t_gyro.reg.y_accel_h, accel_t_gyro.reg.y_accel_l); 9 | SWAP (accel_t_gyro.reg.z_accel_h, accel_t_gyro.reg.z_accel_l); 10 | SWAP (accel_t_gyro.reg.t_h, accel_t_gyro.reg.t_l); 11 | SWAP (accel_t_gyro.reg.x_gyro_h, accel_t_gyro.reg.x_gyro_l); 12 | SWAP (accel_t_gyro.reg.y_gyro_h, accel_t_gyro.reg.y_gyro_l); 13 | SWAP (accel_t_gyro.reg.z_gyro_h, accel_t_gyro.reg.z_gyro_l); 14 | 15 | gyroX_raw = accel_t_gyro.value.x_gyro; 16 | gyroY_raw = accel_t_gyro.value.y_gyro; 17 | gyroZ_raw = accel_t_gyro.value.z_gyro; 18 | double ang_x_raw = accel_t_gyro.value.y_accel; 19 | double ang_y_raw = accel_t_gyro.value.x_accel; 20 | double ang_z_raw = accel_t_gyro.value.z_accel; 21 | double ang_x_raw_sq = sq(ang_x_raw); 22 | double ang_y_raw_sq = sq(ang_y_raw); 23 | double ang_z_raw_sq = sq(ang_z_raw); 24 | accYangle_raw = (atan2(ang_y_raw, sqrt(ang_x_raw_sq + ang_z_raw_sq)) + PI) * RAD_TO_DEG - 180; 25 | accXangle_raw = (atan2(ang_x_raw, sqrt(ang_z_raw_sq + ang_y_raw_sq)) + PI) * RAD_TO_DEG - 180; 26 | 27 | x_ang = -accXangle_raw - ang_x_offset; 28 | y_ang = accYangle_raw - ang_y_offset; 29 | x_vel = (-gyroX_raw - gyr_x_raw_offset) / 65.5; 30 | y_vel = (-gyroY_raw - gyr_y_raw_offset) / 65.5; 31 | z_vel = (-gyroZ_raw - gyr_z_raw_offset) / 65.5; 32 | 33 | 34 | //komplementary filter 35 | dt_ = ((double)(micros() - timer) / 1000000); 36 | //if(dt_ > 0.1){ 37 | // dt_ = 0.003; 38 | //} 39 | x_a = 0.99 * (x_a + x_vel * dt_) + 0.01 * x_ang; 40 | y_a = 0.99 * (y_a + y_vel * dt_) + 0.01 * y_ang; 41 | z_ang = z_ang + z_vel * dt_; 42 | timer = micros(); 43 | } 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Building Smart Drones with ESP8266 and Arduino 5 | This is the code repository for [Building Smart Drones with ESP8266 and Arduino](https://www.packtpub.com/hardware-and-creative/building-smart-drones-esp8266-and-arduino?utm_source=repository&utm_medium=github&utm_campaign=repository&utm_term=9781788477512), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish. 6 | 7 | ## About the Book 8 | With this book, you will explore techniques for leveraging the tiny WiFi chip to enhance your drone and control it over a mobile phone. This book will start with teaching you how to solve problems while building your own wifi controlled Arduino based drone. You will also learn how to build a Quadcopter and a mission critical drone. Moving on you will learn how to build a prototype drone that will be given a mission to complete which it will do it itself. You will also learn to build various exciting projects such as gliding and racing drones. By the end of this book you will learn how to maintain and troubleshoot your drone. 9 | 10 | ## Instructions and Navigation 11 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter03. 12 | 13 | The code will look like the following: 14 | ``` 15 | if (bmp180.begin()) 16 | Serial.println("BMP180 initialization 17 | successful"); 18 | else 19 | { 20 | Serial.println("BMP180 initialization 21 | failed\n\n"); 22 | while(1); 23 | } 24 | ``` 25 | 26 | ## Related Products 27 | * [Designing Purpose-Built Drones for Ardupilot Pixhawk 2.1](https://www.packtpub.com/hardware-and-creative/designing-purpose-build-drones-ardupilotpixhawk-21?utm_source=repository&utm_medium=github&utm_campaign=repository&utm_term=9781786469168) 28 | 29 | * [Building Multicopter Video Drones](https://www.packtpub.com/hardware-and-creative/building-multicopter-video-drones?utm_source=repository&utm_medium=github&utm_campaign=repository&utm_term=9781782175438) 30 | 31 | * [Building a Quadcopter with Arduino](https://www.packtpub.com/hardware-and-creative/building-quadcopter-arduino?utm_source=repository&utm_medium=github&utm_campaign=repository&utm_term=9781785281846) 32 | 33 | ### Suggestions and Feedback 34 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSe5qwunkGf6PUvzPirPDtuy1Du5Rlzew23UBp2S-P3wB-GcwQ/viewform) if you have any feedback or suggestions. 35 | 36 | 37 | ### Download a free PDF 38 | 39 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to download a free PDF copy of this book.
40 |

https://packt.link/free-ebook/9781788477512

-------------------------------------------------------------------------------- /Chapter05/BarometerCodeFull/BarometerCodeFull.ino: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | #define ALTITUDE 142.0 // Altitude of the drone 8 | #define ESP_RESET_PIN 12 9 | 10 | #define MILS_IN_MIN 60000 11 | 12 | #define CHILD_ID_TEMP 0 13 | #define CHILD_ID_BARO 1 14 | 15 | int minuteCount = 0; 16 | double pressureSamples[9][6]; 17 | double pressureAvg[9]; 18 | double dP_dt; 19 | 20 | const char *weather[] = { 21 | "stable","sunny","cloudy","unstable","thunderstorm","unknown"}; 22 | 23 | int forecast = 5; 24 | 25 | unsigned long startTime; 26 | 27 | SFE_BMP180 bmp180; 28 | Esp8266EasyIoT esp; 29 | #define AP_USERNAME "******" 30 | #define AP_PASSWORD "******" 31 | #define INSTANCE_ID "******" 32 | const char* ssid = "The WiFi Name"; 33 | const char* password = "WiFI Password"; 34 | Esp8266EasyIoTMsg msgTemp(CHILD_ID_TEMP, V_TEMP); 35 | Esp8266EasyIoTMsg msgPress(CHILD_ID_BARO, V_PRESSURE); 36 | Esp8266EasyIoTMsg msgForec(CHILD_ID_BARO, V_FORECAST); 37 | 38 | void setup() 39 | { 40 | Serial1.begin(9600); // ESP 41 | Serial.begin(115200); // debug 42 | WiFi.begin(ssid, password); 43 | 44 | if (bmp180.begin()) 45 | Serial.println("BMP180 initialization successful"); 46 | else 47 | { 48 | Serial.println("BMP180 initialization failed\n\n"); 49 | while(1); 50 | } 51 | 52 | startTime = -1; 53 | esp.begin(NULL, ESP_RESET_PIN, &Serial1, &Serial); 54 | esp.present(CHILD_ID_TEMP, S_TEMP); 55 | esp.present(CHILD_ID_BARO, S_BARO); 56 | } 57 | 58 | 59 | void loop() 60 | { 61 | 62 | for(int i =0; i<10;i++) 63 | { 64 | if (esp.process()) 65 | break; 66 | } 67 | 68 | if (IsTimeout()) 69 | { 70 | char status; 71 | double T,P,p0,a; 72 | status = bmp180.startTemperature(); 73 | if (status != 0) 74 | { 75 | delay(status); 76 | status = bmp180.getTemperature(T); 77 | if (status != 0) 78 | { 79 | Serial.print("temperature: "); 80 | Serial.print(T,2); 81 | Serial.print(" deg C, "); 82 | Serial.print((9.0/5.0)*T+32.0,2); 83 | Serial.println(" deg F"); 84 | 85 | static int lastSendTempInt; 86 | int temp = round(T *10); 87 | 88 | if (temp != lastSendTempInt) 89 | { 90 | lastSendTempInt = temp; 91 | esp.send(msgTemp.set((float)T, 1)); 92 | } 93 | status = bmp180.startPressure(3); 94 | if (status != 0) 95 | { 96 | delay(status); 97 | status = bmp180.getPressure(P,T); 98 | if (status != 0) 99 | { 100 | p0 = bmp180.sealevel(P,ALTITUDE); 101 | Serial.print("relative (sea-level) pressure: "); 102 | Serial.print(p0,2); 103 | Serial.print(" mb, "); 104 | Serial.print(p0*0.0295333727,2); 105 | Serial.println(" inHg"); 106 | static int lastSendPresInt; 107 | int pres = round(p0 *10); 108 | 109 | if (pres != lastSendPresInt) 110 | { 111 | lastSendPresInt = pres; 112 | esp.send(msgPress.set((float)p0, 1)); 113 | } 114 | 115 | forecast = calculateForecast(p0); 116 | static int lastSendForeInt = -1; 117 | 118 | 119 | if (forecast != lastSendForeInt) 120 | { 121 | lastSendForeInt = forecast; 122 | esp.send(msgForec.set(weather[forecast])); 123 | } 124 | } 125 | else Serial.println("error retrieving pressure measurement\n"); 126 | } 127 | else Serial.println("error starting pressure measurement\n"); 128 | } 129 | else Serial.println("error retrieving temperature measurement\n"); 130 | } 131 | else Serial.println("error starting temperature measurement\n"); 132 | 133 | startTime = millis(); 134 | } 135 | } 136 | 137 | boolean IsTimeout() 138 | { 139 | unsigned long now = millis(); 140 | if (startTime <= now) 141 | { 142 | if ( (unsigned long)(now - startTime ) < MILS_IN_MIN ) 143 | return false; 144 | } 145 | else 146 | { 147 | if ( (unsigned long)(startTime - now) < MILS_IN_MIN ) 148 | return false; 149 | } 150 | 151 | return true; 152 | } 153 | 154 | 155 | int calculateForecast(double pressure) { 156 | //From 0 to 5 min. 157 | if (minuteCount <= 5){ 158 | pressureSamples[0][minuteCount] = pressure; 159 | } 160 | //From 30 to 35 min. 161 | else if ((minuteCount >= 30) && (minuteCount <= 35)){ 162 | pressureSamples[1][minuteCount - 30] = pressure; 163 | } 164 | //From 60 to 65 min. 165 | else if ((minuteCount >= 60) && (minuteCount <= 65)){ 166 | pressureSamples[2][minuteCount - 60] = pressure; 167 | } 168 | //From 90 to 95 min. 169 | else if ((minuteCount >= 90) && (minuteCount <= 95)){ 170 | pressureSamples[3][minuteCount - 90] = pressure; 171 | } 172 | //From 120 to 125 min. 173 | else if ((minuteCount >= 120) && (minuteCount <= 125)){ 174 | pressureSamples[4][minuteCount - 120] = pressure; 175 | } 176 | //From 150 to 155 min. 177 | else if ((minuteCount >= 150) && (minuteCount <= 155)){ 178 | pressureSamples[5][minuteCount - 150] = pressure; 179 | } 180 | //From 180 to 185 min. 181 | else if ((minuteCount >= 180) && (minuteCount <= 185)){ 182 | pressureSamples[6][minuteCount - 180] = pressure; 183 | } 184 | //From 210 to 215 min. 185 | else if ((minuteCount >= 210) && (minuteCount <= 215)){ 186 | pressureSamples[7][minuteCount - 210] = pressure; 187 | } 188 | //From 240 to 245 min. 189 | else if ((minuteCount >= 240) && (minuteCount <= 245)){ 190 | pressureSamples[8][minuteCount - 240] = pressure; 191 | } 192 | 193 | 194 | if (minuteCount == 5) { 195 | // Avg pressure in first 5 min, value averaged from 0 to 5 min. 196 | pressureAvg[0] = ((pressureSamples[0][0] + pressureSamples[0][1] 197 | + pressureSamples[0][2] + pressureSamples[0][3] 198 | + pressureSamples[0][4] + pressureSamples[0][5]) / 6); 199 | } 200 | else if (minuteCount == 35) { 201 | // Avg pressure in 30 min, value averaged from 0 to 5 min. 202 | pressureAvg[1] = ((pressureSamples[1][0] + pressureSamples[1][1] 203 | + pressureSamples[1][2] + pressureSamples[1][3] 204 | + pressureSamples[1][4] + pressureSamples[1][5]) / 6); 205 | float change = (pressureAvg[1] - pressureAvg[0]); 206 | dP_dt = change / 5; 207 | } 208 | else if (minuteCount == 65) { 209 | // Avg pressure at end of the hour, value averaged from 0 to 5 min. 210 | pressureAvg[2] = ((pressureSamples[2][0] + pressureSamples[2][1] 211 | + pressureSamples[2][2] + pressureSamples[2][3] 212 | + pressureSamples[2][4] + pressureSamples[2][5]) / 6); 213 | float change = (pressureAvg[2] - pressureAvg[0]); 214 | dP_dt = change / 10; 215 | } 216 | else if (minuteCount == 95) { 217 | // Avg pressure at end of the hour, value averaged from 0 to 5 min. 218 | pressureAvg[3] = ((pressureSamples[3][0] + pressureSamples[3][1] 219 | + pressureSamples[3][2] + pressureSamples[3][3] 220 | + pressureSamples[3][4] + pressureSamples[3][5]) / 6); 221 | float change = (pressureAvg[3] - pressureAvg[0]); 222 | dP_dt = change / 15; 223 | } 224 | else if (minuteCount == 125) { 225 | // Avg pressure at end of the hour, value averaged from 0 to 5 min. 226 | pressureAvg[4] = ((pressureSamples[4][0] + pressureSamples[4][1] 227 | + pressureSamples[4][2] + pressureSamples[4][3] 228 | + pressureSamples[4][4] + pressureSamples[4][5]) / 6); 229 | float change = (pressureAvg[4] - pressureAvg[0]); 230 | dP_dt = change / 20; 231 | } 232 | else if (minuteCount == 155) { 233 | // Avg pressure at end of the hour, value averaged from 0 to 5 min. 234 | pressureAvg[5] = ((pressureSamples[5][0] + pressureSamples[5][1] 235 | + pressureSamples[5][2] + pressureSamples[5][3] 236 | + pressureSamples[5][4] + pressureSamples[5][5]) / 6); 237 | float change = (pressureAvg[5] - pressureAvg[0]); 238 | dP_dt = change / 25; 239 | } 240 | else if (minuteCount == 185) { 241 | // Avg pressure at end of the hour, value averaged from 0 to 5 min. 242 | pressureAvg[6] = ((pressureSamples[6][0] + pressureSamples[6][1] 243 | + pressureSamples[6][2] + pressureSamples[6][3] 244 | + pressureSamples[6][4] + pressureSamples[6][5]) / 6); 245 | float change = (pressureAvg[6] - pressureAvg[0]); 246 | dP_dt = change / 30; 247 | } 248 | else if (minuteCount == 215) { 249 | // Avg pressure at end of the hour, value averaged from 0 to 5 min. 250 | pressureAvg[7] = ((pressureSamples[7][0] + pressureSamples[7][1] 251 | + pressureSamples[7][2] + pressureSamples[7][3] 252 | + pressureSamples[7][4] + pressureSamples[7][5]) / 6); 253 | float change = (pressureAvg[7] - pressureAvg[0]); 254 | dP_dt = change / 35; 255 | } 256 | else if (minuteCount == 245) { 257 | // Avg pressure at end of the hour, value averaged from 0 to 5 min. 258 | pressureAvg[8] = ((pressureSamples[8][0] + pressureSamples[8][1] 259 | + pressureSamples[8][2] + pressureSamples[8][3] 260 | + pressureSamples[8][4] + pressureSamples[8][5]) / 6); 261 | float change = (pressureAvg[8] - pressureAvg[0]); 262 | dP_dt = change / 40; // note this is for t = 4 hour 263 | 264 | minuteCount -= 30; 265 | pressureAvg[0] = pressureAvg[1]; 266 | pressureAvg[1] = pressureAvg[2]; 267 | pressureAvg[2] = pressureAvg[3]; 268 | pressureAvg[3] = pressureAvg[4]; 269 | pressureAvg[4] = pressureAvg[5]; 270 | pressureAvg[5] = pressureAvg[6]; 271 | pressureAvg[6] = pressureAvg[7]; 272 | pressureAvg[7] = pressureAvg[8]; 273 | } 274 | 275 | minuteCount++; 276 | 277 | if (minuteCount < 36) //if time is less than 35 min 278 | return 5; // Unknown, more time needed 279 | else if (dP_dt < (-0.25)) 280 | return 4; // Quickly falling LP, Thunderstorm, not stable 281 | else if (dP_dt > 0.25) 282 | return 3; // Quickly rising HP, not stable weather 283 | else if ((dP_dt > (-0.25)) && (dP_dt < (-0.05))) 284 | return 2; // Slowly falling Low Pressure System, stable rainy weather 285 | else if ((dP_dt > 0.05) && (dP_dt < 0.25)) 286 | return 1; // Slowly rising HP stable good weather 287 | else if ((dP_dt > (-0.05)) && (dP_dt < 0.05)) 288 | return 0; // Stable weather 289 | else 290 | return 5; // Unknown 291 | } 292 | -------------------------------------------------------------------------------- /Chapter04/full_Code/full_Code.ino: -------------------------------------------------------------------------------- 1 | #define MPU6050_AUX_VDDIO 0x01 // R/W 2 | #define MPU6050_SMPLRT_DIV 0x19 // R/W 3 | #define MPU6050_CONFIG 0x1A // R/W 4 | #define MPU6050_GYRO_CONFIG 0x1B // R/W 5 | #define MPU6050_ACCEL_CONFIG 0x1C // R/W 6 | #define MPU6050_FF_THR 0x1D // R/W 7 | #define MPU6050_FF_DUR 0x1E // R/W 8 | #define MPU6050_MOT_THR 0x1F // R/W 9 | #define MPU6050_MOT_DUR 0x20 // R/W 10 | #define MPU6050_ZRMOT_THR 0x21 // R/W 11 | #define MPU6050_ZRMOT_DUR 0x22 // R/W 12 | #define MPU6050_FIFO_EN 0x23 // R/W 13 | #define MPU6050_I2C_MST_CTRL 0x24 // R/W 14 | #define MPU6050_I2C_SLV0_ADDR 0x25 // R/W 15 | #define MPU6050_I2C_SLV0_REG 0x26 // R/W 16 | #define MPU6050_I2C_SLV0_CTRL 0x27 // R/W 17 | #define MPU6050_I2C_SLV1_ADDR 0x28 // R/W 18 | #define MPU6050_I2C_SLV1_REG 0x29 // R/W 19 | #define MPU6050_I2C_SLV1_CTRL 0x2A // R/W 20 | #define MPU6050_I2C_SLV2_ADDR 0x2B // R/W 21 | #define MPU6050_I2C_SLV2_REG 0x2C // R/W 22 | #define MPU6050_I2C_SLV2_CTRL 0x2D // R/W 23 | #define MPU6050_I2C_SLV3_ADDR 0x2E // R/W 24 | #define MPU6050_I2C_SLV3_REG 0x2F // R/W 25 | #define MPU6050_I2C_SLV3_CTRL 0x30 // R/W 26 | #define MPU6050_I2C_SLV4_ADDR 0x31 // R/W 27 | #define MPU6050_I2C_SLV4_REG 0x32 // R/W 28 | #define MPU6050_I2C_SLV4_DO 0x33 // R/W 29 | #define MPU6050_I2C_SLV4_CTRL 0x34 // R/W 30 | #define MPU6050_I2C_SLV4_DI 0x35 // R 31 | #define MPU6050_I2C_MST_STATUS 0x36 // R 32 | #define MPU6050_INT_PIN_CFG 0x37 // R/W 33 | #define MPU6050_INT_ENABLE 0x38 // R/W 34 | #define MPU6050_INT_STATUS 0x3A // R 35 | #define MPU6050_ACCEL_XOUT_H 0x3B // R 36 | #define MPU6050_ACCEL_XOUT_L 0x3C // R 37 | #define MPU6050_ACCEL_YOUT_H 0x3D // R 38 | #define MPU6050_ACCEL_YOUT_L 0x3E // R 39 | #define MPU6050_ACCEL_ZOUT_H 0x3F // R 40 | #define MPU6050_ACCEL_ZOUT_L 0x40 // R 41 | #define MPU6050_TEMP_OUT_H 0x41 // R 42 | #define MPU6050_TEMP_OUT_L 0x42 // R 43 | #define MPU6050_GYRO_XOUT_H 0x43 // R 44 | #define MPU6050_GYRO_XOUT_L 0x44 // R 45 | #define MPU6050_GYRO_YOUT_H 0x45 // R 46 | #define MPU6050_GYRO_YOUT_L 0x46 // R 47 | #define MPU6050_GYRO_ZOUT_H 0x47 // R 48 | #define MPU6050_GYRO_ZOUT_L 0x48 // R 49 | #define MPU6050_EXT_SENS_DATA_00 0x49 // R 50 | #define MPU6050_EXT_SENS_DATA_01 0x4A // R 51 | #define MPU6050_EXT_SENS_DATA_02 0x4B // R 52 | #define MPU6050_EXT_SENS_DATA_03 0x4C // R 53 | #define MPU6050_EXT_SENS_DATA_04 0x4D // R 54 | #define MPU6050_EXT_SENS_DATA_05 0x4E // R 55 | #define MPU6050_EXT_SENS_DATA_06 0x4F // R 56 | #define MPU6050_EXT_SENS_DATA_07 0x50 // R 57 | #define MPU6050_EXT_SENS_DATA_08 0x51 // R 58 | #define MPU6050_EXT_SENS_DATA_09 0x52 // R 59 | #define MPU6050_EXT_SENS_DATA_10 0x53 // R 60 | #define MPU6050_EXT_SENS_DATA_11 0x54 // R 61 | #define MPU6050_EXT_SENS_DATA_12 0x55 // R 62 | #define MPU6050_EXT_SENS_DATA_13 0x56 // R 63 | #define MPU6050_EXT_SENS_DATA_14 0x57 // R 64 | #define MPU6050_EXT_SENS_DATA_15 0x58 // R 65 | #define MPU6050_EXT_SENS_DATA_16 0x59 // R 66 | #define MPU6050_EXT_SENS_DATA_17 0x5A // R 67 | #define MPU6050_EXT_SENS_DATA_18 0x5B // R 68 | #define MPU6050_EXT_SENS_DATA_19 0x5C // R 69 | #define MPU6050_EXT_SENS_DATA_20 0x5D // R 70 | #define MPU6050_EXT_SENS_DATA_21 0x5E // R 71 | #define MPU6050_EXT_SENS_DATA_22 0x5F // R 72 | #define MPU6050_EXT_SENS_DATA_23 0x60 // R 73 | #define MPU6050_MOT_DETECT_STATUS 0x61 // R 74 | #define MPU6050_I2C_SLV0_DO 0x63 // R/W 75 | #define MPU6050_I2C_SLV1_DO 0x64 // R/W 76 | #define MPU6050_I2C_SLV2_DO 0x65 // R/W 77 | #define MPU6050_I2C_SLV3_DO 0x66 // R/W 78 | #define MPU6050_I2C_MST_DELAY_CTRL 0x67 // R/W 79 | #define MPU6050_SIGNAL_PATH_RESET 0x68 // R/W 80 | #define MPU6050_MOT_DETECT_CTRL 0x69 // R/W 81 | #define MPU6050_USER_CTRL 0x6A // R/W 82 | #define MPU6050_PWR_MGMT_1 0x6B // R/W 83 | #define MPU6050_PWR_MGMT_2 0x6C // R/W 84 | #define MPU6050_FIFO_COUNTH 0x72 // R/W 85 | #define MPU6050_FIFO_COUNTL 0x73 // R/W 86 | #define MPU6050_FIFO_R_W 0x74 // R/W 87 | #define MPU6050_WHO_AM_I 0x75 // R 88 | 89 | 90 | #define MPU6050_D0 0 91 | #define MPU6050_D1 1 92 | #define MPU6050_D2 2 93 | #define MPU6050_D3 3 94 | #define MPU6050_D4 4 95 | #define MPU6050_D5 5 96 | #define MPU6050_D6 6 97 | #define MPU6050_D7 7 98 | #define MPU6050_AUX_VDDIO MPU6050_D7 // I2C high: 1=VDD, 0=VLOGIC 99 | #define MPU6050_DLPF_CFG0 MPU6050_D0 100 | #define MPU6050_DLPF_CFG1 MPU6050_D1 101 | #define MPU6050_DLPF_CFG2 MPU6050_D2 102 | #define MPU6050_EXT_SYNC_SET0 MPU6050_D3 103 | #define MPU6050_EXT_SYNC_SET1 MPU6050_D4 104 | #define MPU6050_EXT_SYNC_SET2 MPU6050_D5 105 | // Combined definitions for the EXT_SYNC_SET values 106 | #define MPU6050_EXT_SYNC_SET_0 (0) 107 | #define MPU6050_EXT_SYNC_SET_1 (bit(MPU6050_EXT_SYNC_SET0)) 108 | #define MPU6050_EXT_SYNC_SET_2 (bit(MPU6050_EXT_SYNC_SET1)) 109 | #define MPU6050_EXT_SYNC_SET_3 (bit(MPU6050_EXT_SYNC_SET1)|bit(MPU6050_EXT_SYNC_SET0)) 110 | #define MPU6050_EXT_SYNC_SET_4 (bit(MPU6050_EXT_SYNC_SET2)) 111 | #define MPU6050_EXT_SYNC_SET_5 (bit(MPU6050_EXT_SYNC_SET2)|bit(MPU6050_EXT_SYNC_SET0)) 112 | #define MPU6050_EXT_SYNC_SET_6 (bit(MPU6050_EXT_SYNC_SET2)|bit(MPU6050_EXT_SYNC_SET1)) 113 | #define MPU6050_EXT_SYNC_SET_7 (bit(MPU6050_EXT_SYNC_SET2)|bit(MPU6050_EXT_SYNC_SET1)|bit(MPU6050_EXT_SYNC_SET0)) 114 | 115 | // Alternative names for the combined definitions. 116 | #define MPU6050_EXT_SYNC_DISABLED MPU6050_EXT_SYNC_SET_0 117 | #define MPU6050_EXT_SYNC_TEMP_OUT_L MPU6050_EXT_SYNC_SET_1 118 | #define MPU6050_EXT_SYNC_GYRO_XOUT_L MPU6050_EXT_SYNC_SET_2 119 | #define MPU6050_EXT_SYNC_GYRO_YOUT_L MPU6050_EXT_SYNC_SET_3 120 | #define MPU6050_EXT_SYNC_GYRO_ZOUT_L MPU6050_EXT_SYNC_SET_4 121 | #define MPU6050_EXT_SYNC_ACCEL_XOUT_L MPU6050_EXT_SYNC_SET_5 122 | #define MPU6050_EXT_SYNC_ACCEL_YOUT_L MPU6050_EXT_SYNC_SET_6 123 | #define MPU6050_EXT_SYNC_ACCEL_ZOUT_L MPU6050_EXT_SYNC_SET_7 124 | 125 | // Combined definitions for the DLPF_CFG values 126 | #define MPU6050_DLPF_CFG_0 (0) 127 | #define MPU6050_DLPF_CFG_1 (bit(MPU6050_DLPF_CFG0)) 128 | #define MPU6050_DLPF_CFG_2 (bit(MPU6050_DLPF_CFG1)) 129 | #define MPU6050_DLPF_CFG_3 (bit(MPU6050_DLPF_CFG1)|bit(MPU6050_DLPF_CFG0)) 130 | #define MPU6050_DLPF_CFG_4 (bit(MPU6050_DLPF_CFG2)) 131 | #define MPU6050_DLPF_CFG_5 (bit(MPU6050_DLPF_CFG2)|bit(MPU6050_DLPF_CFG0)) 132 | #define MPU6050_DLPF_CFG_6 (bit(MPU6050_DLPF_CFG2)|bit(MPU6050_DLPF_CFG1)) 133 | #define MPU6050_DLPF_CFG_7 (bit(MPU6050_DLPF_CFG2)|bit(MPU6050_DLPF_CFG1)|bit(MPU6050_DLPF_CFG0)) 134 | #define MPU6050_DLPF_260HZ MPU6050_DLPF_CFG_0 135 | #define MPU6050_DLPF_184HZ MPU6050_DLPF_CFG_1 136 | #define MPU6050_DLPF_94HZ MPU6050_DLPF_CFG_2 137 | #define MPU6050_DLPF_44HZ MPU6050_DLPF_CFG_3 138 | #define MPU6050_DLPF_21HZ MPU6050_DLPF_CFG_4 139 | #define MPU6050_DLPF_10HZ MPU6050_DLPF_CFG_5 140 | #define MPU6050_DLPF_5HZ MPU6050_DLPF_CFG_6 141 | #define MPU6050_DLPF_RESERVED MPU6050_DLPF_CFG_7 142 | 143 | 144 | // Use these only with the bit() macro. 145 | #define MPU6050_FS_SEL0 MPU6050_D3 146 | #define MPU6050_FS_SEL1 MPU6050_D4 147 | #define MPU6050_ZG_ST MPU6050_D5 148 | #define MPU6050_YG_ST MPU6050_D6 149 | #define MPU6050_XG_ST MPU6050_D7 150 | 151 | // Combined definitions for the FS_SEL values 152 | #define MPU6050_FS_SEL_0 (0) 153 | #define MPU6050_FS_SEL_1 (bit(MPU6050_FS_SEL0)) 154 | #define MPU6050_FS_SEL_2 (bit(MPU6050_FS_SEL1)) 155 | #define MPU6050_FS_SEL_3 (bit(MPU6050_FS_SEL1)|bit(MPU6050_FS_SEL0)) 156 | 157 | // Alternative names for the combined definitions 158 | // The name uses the range in degrees per second. 159 | #define MPU6050_FS_SEL_250 MPU6050_FS_SEL_0 160 | #define MPU6050_FS_SEL_500 MPU6050_FS_SEL_1 161 | #define MPU6050_FS_SEL_1000 MPU6050_FS_SEL_2 162 | #define MPU6050_FS_SEL_2000 MPU6050_FS_SEL_3 163 | #define MPU6050_ACCEL_HPF0 MPU6050_D0 164 | #define MPU6050_ACCEL_HPF1 MPU6050_D1 165 | #define MPU6050_ACCEL_HPF2 MPU6050_D2 166 | #define MPU6050_AFS_SEL0 MPU6050_D3 167 | #define MPU6050_AFS_SEL1 MPU6050_D4 168 | #define MPU6050_ZA_ST MPU6050_D5 169 | #define MPU6050_YA_ST MPU6050_D6 170 | #define MPU6050_XA_ST MPU6050_D7 171 | 172 | // Combined definitions for the ACCEL_HPF values 173 | #define MPU6050_ACCEL_HPF_0 (0) 174 | #define MPU6050_ACCEL_HPF_1 (bit(MPU6050_ACCEL_HPF0)) 175 | #define MPU6050_ACCEL_HPF_2 (bit(MPU6050_ACCEL_HPF1)) 176 | #define MPU6050_ACCEL_HPF_3 (bit(MPU6050_ACCEL_HPF1)|bit(MPU6050_ACCEL_HPF0)) 177 | #define MPU6050_ACCEL_HPF_4 (bit(MPU6050_ACCEL_HPF2)) 178 | #define MPU6050_ACCEL_HPF_7 (bit(MPU6050_ACCEL_HPF2)|bit(MPU6050_ACCEL_HPF1)|bit(MPU6050_ACCEL_HPF0)) 179 | 180 | // Alternative names for the combined definitions 181 | // The name uses the Cut-off frequency. 182 | #define MPU6050_ACCEL_HPF_RESET MPU6050_ACCEL_HPF_0 183 | #define MPU6050_ACCEL_HPF_5HZ MPU6050_ACCEL_HPF_1 184 | #define MPU6050_ACCEL_HPF_2_5HZ MPU6050_ACCEL_HPF_2 185 | #define MPU6050_ACCEL_HPF_1_25HZ MPU6050_ACCEL_HPF_3 186 | #define MPU6050_ACCEL_HPF_0_63HZ MPU6050_ACCEL_HPF_4 187 | #define MPU6050_ACCEL_HPF_HOLD MPU6050_ACCEL_HPF_7 188 | 189 | // Combined definitions for the AFS_SEL values 190 | #define MPU6050_AFS_SEL_0 (0) 191 | #define MPU6050_AFS_SEL_1 (bit(MPU6050_AFS_SEL0)) 192 | #define MPU6050_AFS_SEL_2 (bit(MPU6050_AFS_SEL1)) 193 | #define MPU6050_AFS_SEL_3 (bit(MPU6050_AFS_SEL1)|bit(MPU6050_AFS_SEL0)) 194 | 195 | // Alternative names for the combined definitions 196 | // The name uses the full scale range for the accelerometer. 197 | #define MPU6050_AFS_SEL_2G MPU6050_AFS_SEL_0 198 | #define MPU6050_AFS_SEL_4G MPU6050_AFS_SEL_1 199 | #define MPU6050_AFS_SEL_8G MPU6050_AFS_SEL_2 200 | #define MPU6050_AFS_SEL_16G MPU6050_AFS_SEL_3 201 | 202 | // FIFO_EN Register 203 | // These are the names for the bits. 204 | // Use these only with the bit() macro. 205 | #define MPU6050_SLV0_FIFO_EN MPU6050_D0 206 | #define MPU6050_SLV1_FIFO_EN MPU6050_D1 207 | #define MPU6050_SLV2_FIFO_EN MPU6050_D2 208 | #define MPU6050_ACCEL_FIFO_EN MPU6050_D3 209 | #define MPU6050_ZG_FIFO_EN MPU6050_D4 210 | #define MPU6050_YG_FIFO_EN MPU6050_D5 211 | #define MPU6050_XG_FIFO_EN MPU6050_D6 212 | #define MPU6050_TEMP_FIFO_EN MPU6050_D7 213 | 214 | // I2C_MST_CTRL Register 215 | // These are the names for the bits. 216 | // Use these only with the bit() macro. 217 | #define MPU6050_I2C_MST_CLK0 MPU6050_D0 218 | #define MPU6050_I2C_MST_CLK1 MPU6050_D1 219 | #define MPU6050_I2C_MST_CLK2 MPU6050_D2 220 | #define MPU6050_I2C_MST_CLK3 MPU6050_D3 221 | #define MPU6050_I2C_MST_P_NSR MPU6050_D4 222 | #define MPU6050_SLV_3_FIFO_EN MPU6050_D5 223 | #define MPU6050_WAIT_FOR_ES MPU6050_D6 224 | #define MPU6050_MULT_MST_EN MPU6050_D7 225 | 226 | // Combined definitions for the I2C_MST_CLK 227 | #define MPU6050_I2C_MST_CLK_0 (0) 228 | #define MPU6050_I2C_MST_CLK_1 (bit(MPU6050_I2C_MST_CLK0)) 229 | #define MPU6050_I2C_MST_CLK_2 (bit(MPU6050_I2C_MST_CLK1)) 230 | #define MPU6050_I2C_MST_CLK_3 (bit(MPU6050_I2C_MST_CLK1)|bit(MPU6050_I2C_MST_CLK0)) 231 | #define MPU6050_I2C_MST_CLK_4 (bit(MPU6050_I2C_MST_CLK2)) 232 | #define MPU6050_I2C_MST_CLK_5 (bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK0)) 233 | #define MPU6050_I2C_MST_CLK_6 (bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK1)) 234 | #define MPU6050_I2C_MST_CLK_7 (bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK1)|bit(MPU6050_I2C_MST_CLK0)) 235 | #define MPU6050_I2C_MST_CLK_8 (bit(MPU6050_I2C_MST_CLK3)) 236 | #define MPU6050_I2C_MST_CLK_9 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK0)) 237 | #define MPU6050_I2C_MST_CLK_10 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK1)) 238 | #define MPU6050_I2C_MST_CLK_11 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK1)|bit(MPU6050_I2C_MST_CLK0)) 239 | #define MPU6050_I2C_MST_CLK_12 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK2)) 240 | #define MPU6050_I2C_MST_CLK_13 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK0)) 241 | #define MPU6050_I2C_MST_CLK_14 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK1)) 242 | #define MPU6050_I2C_MST_CLK_15 (bit(MPU6050_I2C_MST_CLK3)|bit(MPU6050_I2C_MST_CLK2)|bit(MPU6050_I2C_MST_CLK1)|bit(MPU6050_I2C_MST_CLK0)) 243 | 244 | // Alternative names for the combined definitions 245 | // The names uses I2C Master Clock Speed in kHz. 246 | #define MPU6050_I2C_MST_CLK_348KHZ MPU6050_I2C_MST_CLK_0 247 | #define MPU6050_I2C_MST_CLK_333KHZ MPU6050_I2C_MST_CLK_1 248 | #define MPU6050_I2C_MST_CLK_320KHZ MPU6050_I2C_MST_CLK_2 249 | #define MPU6050_I2C_MST_CLK_308KHZ MPU6050_I2C_MST_CLK_3 250 | #define MPU6050_I2C_MST_CLK_296KHZ MPU6050_I2C_MST_CLK_4 251 | #define MPU6050_I2C_MST_CLK_286KHZ MPU6050_I2C_MST_CLK_5 252 | #define MPU6050_I2C_MST_CLK_276KHZ MPU6050_I2C_MST_CLK_6 253 | #define MPU6050_I2C_MST_CLK_267KHZ MPU6050_I2C_MST_CLK_7 254 | #define MPU6050_I2C_MST_CLK_258KHZ MPU6050_I2C_MST_CLK_8 255 | #define MPU6050_I2C_MST_CLK_500KHZ MPU6050_I2C_MST_CLK_9 256 | #define MPU6050_I2C_MST_CLK_471KHZ MPU6050_I2C_MST_CLK_10 257 | #define MPU6050_I2C_MST_CLK_444KHZ MPU6050_I2C_MST_CLK_11 258 | #define MPU6050_I2C_MST_CLK_421KHZ MPU6050_I2C_MST_CLK_12 259 | #define MPU6050_I2C_MST_CLK_400KHZ MPU6050_I2C_MST_CLK_13 260 | #define MPU6050_I2C_MST_CLK_381KHZ MPU6050_I2C_MST_CLK_14 261 | #define MPU6050_I2C_MST_CLK_364KHZ MPU6050_I2C_MST_CLK_15 262 | 263 | #define MPU6050_I2C_SLV0_RW MPU6050_D7 264 | 265 | // I2C_SLV0_CTRL Register 266 | // These are the names for the bits. 267 | // Use these only with the bit() macro. 268 | #define MPU6050_I2C_SLV0_LEN0 MPU6050_D0 269 | #define MPU6050_I2C_SLV0_LEN1 MPU6050_D1 270 | #define MPU6050_I2C_SLV0_LEN2 MPU6050_D2 271 | #define MPU6050_I2C_SLV0_LEN3 MPU6050_D3 272 | #define MPU6050_I2C_SLV0_GRP MPU6050_D4 273 | #define MPU6050_I2C_SLV0_REG_DIS MPU6050_D5 274 | #define MPU6050_I2C_SLV0_BYTE_SW MPU6050_D6 275 | #define MPU6050_I2C_SLV0_EN MPU6050_D7 276 | 277 | // A mask for the length 278 | #define MPU6050_I2C_SLV0_LEN_MASK 0x0F 279 | 280 | 281 | // Use these only with the bit() macro. 282 | #define MPU6050_I2C_SLV1_RW MPU6050_D7 283 | 284 | // I2C_SLV1_CTRL Register 285 | // These are the names for the bits. 286 | // Use these only with the bit() macro. 287 | #define MPU6050_I2C_SLV1_LEN0 MPU6050_D0 288 | #define MPU6050_I2C_SLV1_LEN1 MPU6050_D1 289 | #define MPU6050_I2C_SLV1_LEN2 MPU6050_D2 290 | #define MPU6050_I2C_SLV1_LEN3 MPU6050_D3 291 | #define MPU6050_I2C_SLV1_GRP MPU6050_D4 292 | #define MPU6050_I2C_SLV1_REG_DIS MPU6050_D5 293 | #define MPU6050_I2C_SLV1_BYTE_SW MPU6050_D6 294 | #define MPU6050_I2C_SLV1_EN MPU6050_D7 295 | 296 | // A mask for the length 297 | #define MPU6050_I2C_SLV1_LEN_MASK 0x0F 298 | 299 | 300 | #define MPU6050_I2C_SLV2_RW MPU6050_D7 301 | 302 | // I2C_SLV2_CTRL Register 303 | // These are the names for the bits. 304 | // Use these only with the bit() macro. 305 | #define MPU6050_I2C_SLV2_LEN0 MPU6050_D0 306 | #define MPU6050_I2C_SLV2_LEN1 MPU6050_D1 307 | #define MPU6050_I2C_SLV2_LEN2 MPU6050_D2 308 | #define MPU6050_I2C_SLV2_LEN3 MPU6050_D3 309 | #define MPU6050_I2C_SLV2_GRP MPU6050_D4 310 | #define MPU6050_I2C_SLV2_REG_DIS MPU6050_D5 311 | #define MPU6050_I2C_SLV2_BYTE_SW MPU6050_D6 312 | #define MPU6050_I2C_SLV2_EN MPU6050_D7 313 | 314 | // A mask for the length 315 | #define MPU6050_I2C_SLV2_LEN_MASK 0x0F 316 | 317 | // I2C_SLV3_ADDR Register 318 | // These are the names for the bits. 319 | // Use these only with the bit() macro. 320 | #define MPU6050_I2C_SLV3_RW MPU6050_D7 321 | 322 | // I2C_SLV3_CTRL Register 323 | // These are the names for the bits. 324 | // Use these only with the bit() macro. 325 | #define MPU6050_I2C_SLV3_LEN0 MPU6050_D0 326 | #define MPU6050_I2C_SLV3_LEN1 MPU6050_D1 327 | #define MPU6050_I2C_SLV3_LEN2 MPU6050_D2 328 | #define MPU6050_I2C_SLV3_LEN3 MPU6050_D3 329 | #define MPU6050_I2C_SLV3_GRP MPU6050_D4 330 | #define MPU6050_I2C_SLV3_REG_DIS MPU6050_D5 331 | #define MPU6050_I2C_SLV3_BYTE_SW MPU6050_D6 332 | #define MPU6050_I2C_SLV3_EN MPU6050_D7 333 | 334 | // A mask for the length 335 | #define MPU6050_I2C_SLV3_LEN_MASK 0x0F 336 | 337 | // I2C_SLV4_ADDR Register 338 | // These are the names for the bits. 339 | // Use these only with the bit() macro. 340 | #define MPU6050_I2C_SLV4_RW MPU6050_D7 341 | 342 | // I2C_SLV4_CTRL Register 343 | // These are the names for the bits. 344 | // Use these only with the bit() macro. 345 | #define MPU6050_I2C_MST_DLY0 MPU6050_D0 346 | #define MPU6050_I2C_MST_DLY1 MPU6050_D1 347 | #define MPU6050_I2C_MST_DLY2 MPU6050_D2 348 | #define MPU6050_I2C_MST_DLY3 MPU6050_D3 349 | #define MPU6050_I2C_MST_DLY4 MPU6050_D4 350 | #define MPU6050_I2C_SLV4_REG_DIS MPU6050_D5 351 | #define MPU6050_I2C_SLV4_INT_EN MPU6050_D6 352 | #define MPU6050_I2C_SLV4_EN MPU6050_D7 353 | 354 | // A mask for the delay 355 | #define MPU6050_I2C_MST_DLY_MASK 0x1F 356 | 357 | // I2C_MST_STATUS Register 358 | // These are the names for the bits. 359 | // Use these only with the bit() macro. 360 | #define MPU6050_I2C_SLV0_NACK MPU6050_D0 361 | #define MPU6050_I2C_SLV1_NACK MPU6050_D1 362 | #define MPU6050_I2C_SLV2_NACK MPU6050_D2 363 | #define MPU6050_I2C_SLV3_NACK MPU6050_D3 364 | #define MPU6050_I2C_SLV4_NACK MPU6050_D4 365 | #define MPU6050_I2C_LOST_ARB MPU6050_D5 366 | #define MPU6050_I2C_SLV4_DONE MPU6050_D6 367 | #define MPU6050_PASS_THROUGH MPU6050_D7 368 | 369 | // I2C_PIN_CFG Register 370 | // These are the names for the bits. 371 | // Use these only with the bit() macro. 372 | #define MPU6050_CLKOUT_EN MPU6050_D0 373 | #define MPU6050_I2C_BYPASS_EN MPU6050_D1 374 | #define MPU6050_FSYNC_INT_EN MPU6050_D2 375 | #define MPU6050_FSYNC_INT_LEVEL MPU6050_D3 376 | #define MPU6050_INT_RD_CLEAR MPU6050_D4 377 | #define MPU6050_LATCH_INT_EN MPU6050_D5 378 | #define MPU6050_INT_OPEN MPU6050_D6 379 | #define MPU6050_INT_LEVEL MPU6050_D7 380 | 381 | // INT_ENABLE Register 382 | // These are the names for the bits. 383 | // Use these only with the bit() macro. 384 | #define MPU6050_DATA_RDY_EN MPU6050_D0 385 | #define MPU6050_I2C_MST_INT_EN MPU6050_D3 386 | #define MPU6050_FIFO_OFLOW_EN MPU6050_D4 387 | #define MPU6050_ZMOT_EN MPU6050_D5 388 | #define MPU6050_MOT_EN MPU6050_D6 389 | #define MPU6050_FF_EN MPU6050_D7 390 | 391 | // INT_STATUS Register 392 | // These are the names for the bits. 393 | // Use these only with the bit() macro. 394 | #define MPU6050_DATA_RDY_INT MPU6050_D0 395 | #define MPU6050_I2C_MST_INT MPU6050_D3 396 | #define MPU6050_FIFO_OFLOW_INT MPU6050_D4 397 | #define MPU6050_ZMOT_INT MPU6050_D5 398 | #define MPU6050_MOT_INT MPU6050_D6 399 | #define MPU6050_FF_INT MPU6050_D7 400 | 401 | // MOT_DETECT_STATUS Register 402 | // These are the names for the bits. 403 | // Use these only with the bit() macro. 404 | #define MPU6050_MOT_ZRMOT MPU6050_D0 405 | #define MPU6050_MOT_ZPOS MPU6050_D2 406 | #define MPU6050_MOT_ZNEG MPU6050_D3 407 | #define MPU6050_MOT_YPOS MPU6050_D4 408 | #define MPU6050_MOT_YNEG MPU6050_D5 409 | #define MPU6050_MOT_XPOS MPU6050_D6 410 | #define MPU6050_MOT_XNEG MPU6050_D7 411 | 412 | // IC2_MST_DELAY_CTRL Register 413 | // These are the names for the bits. 414 | // Use these only with the bit() macro. 415 | #define MPU6050_I2C_SLV0_DLY_EN MPU6050_D0 416 | #define MPU6050_I2C_SLV1_DLY_EN MPU6050_D1 417 | #define MPU6050_I2C_SLV2_DLY_EN MPU6050_D2 418 | #define MPU6050_I2C_SLV3_DLY_EN MPU6050_D3 419 | #define MPU6050_I2C_SLV4_DLY_EN MPU6050_D4 420 | #define MPU6050_DELAY_ES_SHADOW MPU6050_D7 421 | 422 | // SIGNAL_PATH_RESET Register 423 | // These are the names for the bits. 424 | // Use these only with the bit() macro. 425 | #define MPU6050_TEMP_RESET MPU6050_D0 426 | #define MPU6050_ACCEL_RESET MPU6050_D1 427 | #define MPU6050_GYRO_RESET MPU6050_D2 428 | 429 | // MOT_DETECT_CTRL Register 430 | // These are the names for the bits. 431 | // Use these only with the bit() macro. 432 | #define MPU6050_MOT_COUNT0 MPU6050_D0 433 | #define MPU6050_MOT_COUNT1 MPU6050_D1 434 | #define MPU6050_FF_COUNT0 MPU6050_D2 435 | #define MPU6050_FF_COUNT1 MPU6050_D3 436 | #define MPU6050_ACCEL_ON_DELAY0 MPU6050_D4 437 | #define MPU6050_ACCEL_ON_DELAY1 MPU6050_D5 438 | 439 | // Combined definitions for the MOT_COUNT 440 | #define MPU6050_MOT_COUNT_0 (0) 441 | #define MPU6050_MOT_COUNT_1 (bit(MPU6050_MOT_COUNT0)) 442 | #define MPU6050_MOT_COUNT_2 (bit(MPU6050_MOT_COUNT1)) 443 | #define MPU6050_MOT_COUNT_3 (bit(MPU6050_MOT_COUNT1)|bit(MPU6050_MOT_COUNT0)) 444 | 445 | // Alternative names for the combined definitions 446 | #define MPU6050_MOT_COUNT_RESET MPU6050_MOT_COUNT_0 447 | 448 | // Combined definitions for the FF_COUNT 449 | #define MPU6050_FF_COUNT_0 (0) 450 | #define MPU6050_FF_COUNT_1 (bit(MPU6050_FF_COUNT0)) 451 | #define MPU6050_FF_COUNT_2 (bit(MPU6050_FF_COUNT1)) 452 | #define MPU6050_FF_COUNT_3 (bit(MPU6050_FF_COUNT1)|bit(MPU6050_FF_COUNT0)) 453 | 454 | // Alternative names for the combined definitions 455 | #define MPU6050_FF_COUNT_RESET MPU6050_FF_COUNT_0 456 | 457 | // Combined definitions for the ACCEL_ON_DELAY 458 | #define MPU6050_ACCEL_ON_DELAY_0 (0) 459 | #define MPU6050_ACCEL_ON_DELAY_1 (bit(MPU6050_ACCEL_ON_DELAY0)) 460 | #define MPU6050_ACCEL_ON_DELAY_2 (bit(MPU6050_ACCEL_ON_DELAY1)) 461 | #define MPU6050_ACCEL_ON_DELAY_3 (bit(MPU6050_ACCEL_ON_DELAY1)|bit(MPU6050_ACCEL_ON_DELAY0)) 462 | 463 | // Alternative names for the ACCEL_ON_DELAY 464 | #define MPU6050_ACCEL_ON_DELAY_0MS MPU6050_ACCEL_ON_DELAY_0 465 | #define MPU6050_ACCEL_ON_DELAY_1MS MPU6050_ACCEL_ON_DELAY_1 466 | #define MPU6050_ACCEL_ON_DELAY_2MS MPU6050_ACCEL_ON_DELAY_2 467 | #define MPU6050_ACCEL_ON_DELAY_3MS MPU6050_ACCEL_ON_DELAY_3 468 | 469 | // USER_CTRL Register 470 | // These are the names for the bits. 471 | // Use these only with the bit() macro. 472 | #define MPU6050_SIG_COND_RESET MPU6050_D0 473 | #define MPU6050_I2C_MST_RESET MPU6050_D1 474 | #define MPU6050_FIFO_RESET MPU6050_D2 475 | #define MPU6050_I2C_IF_DIS MPU6050_D4 // must be 0 for MPU-6050 476 | #define MPU6050_I2C_MST_EN MPU6050_D5 477 | #define MPU6050_FIFO_EN MPU6050_D6 478 | 479 | // PWR_MGMT_1 Register 480 | // These are the names for the bits. 481 | // Use these only with the bit() macro. 482 | #define MPU6050_CLKSEL0 MPU6050_D0 483 | #define MPU6050_CLKSEL1 MPU6050_D1 484 | #define MPU6050_CLKSEL2 MPU6050_D2 485 | #define MPU6050_TEMP_DIS MPU6050_D3 // 1: disable temperature sensor 486 | #define MPU6050_CYCLE MPU6050_D5 // 1: sample and sleep 487 | #define MPU6050_SLEEP MPU6050_D6 // 1: sleep mode 488 | #define MPU6050_DEVICE_RESET MPU6050_D7 // 1: reset to default values 489 | 490 | // Combined definitions for the CLKSEL 491 | #define MPU6050_CLKSEL_0 (0) 492 | #define MPU6050_CLKSEL_1 (bit(MPU6050_CLKSEL0)) 493 | #define MPU6050_CLKSEL_2 (bit(MPU6050_CLKSEL1)) 494 | #define MPU6050_CLKSEL_3 (bit(MPU6050_CLKSEL1)|bit(MPU6050_CLKSEL0)) 495 | #define MPU6050_CLKSEL_4 (bit(MPU6050_CLKSEL2)) 496 | #define MPU6050_CLKSEL_5 (bit(MPU6050_CLKSEL2)|bit(MPU6050_CLKSEL0)) 497 | #define MPU6050_CLKSEL_6 (bit(MPU6050_CLKSEL2)|bit(MPU6050_CLKSEL1)) 498 | #define MPU6050_CLKSEL_7 (bit(MPU6050_CLKSEL2)|bit(MPU6050_CLKSEL1)|bit(MPU6050_CLKSEL0)) 499 | 500 | // Alternative names for the combined definitions 501 | #define MPU6050_CLKSEL_INTERNAL MPU6050_CLKSEL_0 502 | #define MPU6050_CLKSEL_X MPU6050_CLKSEL_1 503 | #define MPU6050_CLKSEL_Y MPU6050_CLKSEL_2 504 | #define MPU6050_CLKSEL_Z MPU6050_CLKSEL_3 505 | #define MPU6050_CLKSEL_EXT_32KHZ MPU6050_CLKSEL_4 506 | #define MPU6050_CLKSEL_EXT_19_2MHZ MPU6050_CLKSEL_5 507 | #define MPU6050_CLKSEL_RESERVED MPU6050_CLKSEL_6 508 | #define MPU6050_CLKSEL_STOP MPU6050_CLKSEL_7 509 | 510 | // PWR_MGMT_2 Register 511 | // These are the names for the bits. 512 | // Use these only with the bit() macro. 513 | #define MPU6050_STBY_ZG MPU6050_D0 514 | #define MPU6050_STBY_YG MPU6050_D1 515 | #define MPU6050_STBY_XG MPU6050_D2 516 | #define MPU6050_STBY_ZA MPU6050_D3 517 | #define MPU6050_STBY_YA MPU6050_D4 518 | #define MPU6050_STBY_XA MPU6050_D5 519 | #define MPU6050_LP_WAKE_CTRL0 MPU6050_D6 520 | #define MPU6050_LP_WAKE_CTRL1 MPU6050_D7 521 | 522 | // Combined definitions for the LP_WAKE_CTRL 523 | #define MPU6050_LP_WAKE_CTRL_0 (0) 524 | #define MPU6050_LP_WAKE_CTRL_1 (bit(MPU6050_LP_WAKE_CTRL0)) 525 | #define MPU6050_LP_WAKE_CTRL_2 (bit(MPU6050_LP_WAKE_CTRL1)) 526 | #define MPU6050_LP_WAKE_CTRL_3 (bit(MPU6050_LP_WAKE_CTRL1)|bit(MPU6050_LP_WAKE_CTRL0)) 527 | 528 | // Alternative names for the combined definitions 529 | // The names uses the Wake-up Frequency. 530 | #define MPU6050_LP_WAKE_1_25HZ MPU6050_LP_WAKE_CTRL_0 531 | #define MPU6050_LP_WAKE_2_5HZ MPU6050_LP_WAKE_CTRL_1 532 | #define MPU6050_LP_WAKE_5HZ MPU6050_LP_WAKE_CTRL_2 533 | #define MPU6050_LP_WAKE_10HZ MPU6050_LP_WAKE_CTRL_3 534 | 535 | 536 | // Default I2C address for the MPU-6050 is 0x68. 537 | // But only if the AD0 pin is low. 538 | // Some sensor boards have AD0 high, and the 539 | // I2C address thus becomes 0x69. 540 | #define MPU6050_I2C_ADDRESS 0x68 541 | 542 | int in; 543 | int cou; 544 | 545 | typedef union accel_t_gyro_union 546 | { 547 | struct 548 | { 549 | uint8_t x_accel_h; 550 | uint8_t x_accel_l; 551 | uint8_t y_accel_h; 552 | uint8_t y_accel_l; 553 | uint8_t z_accel_h; 554 | uint8_t z_accel_l; 555 | uint8_t t_h; 556 | uint8_t t_l; 557 | uint8_t x_gyro_h; 558 | uint8_t x_gyro_l; 559 | uint8_t y_gyro_h; 560 | uint8_t y_gyro_l; 561 | uint8_t z_gyro_h; 562 | uint8_t z_gyro_l; 563 | } reg; 564 | struct 565 | { 566 | int x_accel; 567 | int y_accel; 568 | int z_accel; 569 | int temperature; 570 | int x_gyro; 571 | int y_gyro; 572 | int z_gyro; 573 | } value; 574 | }; 575 | 576 | 577 | int error = 1; 578 | accel_t_gyro_union accel_t_gyro; 579 | 580 | float accYangle_raw; 581 | float accXangle_raw; 582 | float gyroX_raw; 583 | float gyroY_raw; 584 | float gyroZ_raw; 585 | uint8_t c; 586 | 587 | 588 | 589 | 590 | 591 | #define ESC_IN 2 592 | #define ELEVATOR_IN 3 593 | #define AILERON_IN 4 594 | #define ppp_in 5 595 | #define ppp2_in 6 596 | #define ppp3_in 12 597 | //Declare bit-flags to indicate if which channels have new signals 598 | #define ESC_FLAG 1 599 | #define ELEVATOR_FLAG 2 600 | #define AILERON_FLAG 4 601 | #define ppp_flag 3 602 | #define ppp2_flag 5 603 | #define ppp3_flag 7 604 | //Holds update flags defined above 605 | volatile uint8_t bUpdateFlagsShared; 606 | //Shared variables are updated by the ISR 607 | volatile uint16_t ESC_IN_SHARED, ELEVATOR_IN_SHARED, AILERON_IN_SHARED, ppp_in_shared, ppp2_in_shared, ppp3_in_shared; 608 | //These are used to to to record rising edge of a pulse 609 | uint32_t ESC_START, ELEVATOR_START, AILERON_START, ppp_start, ppp2_start, ppp3_start; 610 | static uint16_t ESC, AILERON, ELEVATOR, ppp, ppp2, ppp3; 611 | //local updated flags 612 | static uint8_t bUpdateFlags; 613 | 614 | #include 615 | #include 616 | #include 617 | 618 | Servo myservoT; 619 | Servo myservoR; 620 | Servo myservoB; 621 | Servo myservoL; 622 | //Servo gimbal_ser; 623 | uint32_t timer; 624 | double dt_; 625 | 626 | int gyr_x_raw_offset = -30; 627 | int gyr_y_raw_offset = 32; 628 | int gyr_z_raw_offset = 27; 629 | float ang_x_offset = -0.15; 630 | float ang_y_offset = 3.5; 631 | 632 | float x_ang; 633 | float y_ang; 634 | float z_ang; 635 | float x_vel; 636 | float y_vel; 637 | float z_vel; 638 | int timer_counter = 0; 639 | 640 | int pref_x_ang; 641 | int pref_y_ang; 642 | 643 | float x_a; 644 | float y_a; 645 | float wanted_z_ang; 646 | 647 | 648 | float x_vel_mean; 649 | float y_vel_mean; 650 | float x_a_mean; 651 | float y_a_mean; 652 | 653 | double x_adder; 654 | double y_adder; 655 | //PID 656 | double P_x; 657 | double P_y; 658 | double P_z; 659 | double I_x; 660 | double I_y; 661 | double I_z; 662 | double D_x; 663 | double D_y; 664 | double D_z; 665 | 666 | double rot_x; // CW / CCW rotation 667 | double rot_y; 668 | 669 | int main_power; 670 | int power_T; //temps 671 | int power_B; //temps 672 | int power_R; //temps 673 | int power_L; //temps 674 | 675 | //int gimb_last = 0; 676 | //int gimbal_wanted_ang = 75; 677 | 678 | int rad_rotate; 679 | int rad_tilt_TB; 680 | int rad_tilt_LR; 681 | 682 | int yaw_temp; 683 | 684 | double PID_tune; 685 | double PID_tune_2; 686 | 687 | void setup() 688 | { 689 | 690 | digitalWrite(13, HIGH); 691 | myservoT.attach(7); //7-TOP 692 | myservoR.attach(8); //8-Right 693 | myservoB.attach(9); //9 - BACK 694 | myservoL.attach(10); //10 LEFT 695 | Wire.begin(); 696 | PCintPort::attachInterrupt(ESC_IN, escRead, CHANGE); 697 | PCintPort::attachInterrupt(ELEVATOR_IN, elevatorRead, CHANGE); 698 | PCintPort::attachInterrupt(AILERON_IN, aileronRead, CHANGE); 699 | PCintPort::attachInterrupt(ppp_in, pppRead, CHANGE); 700 | PCintPort::attachInterrupt(ppp2_in, ppp2Read, CHANGE); 701 | PCintPort::attachInterrupt(ppp3_in, ppp3Read, CHANGE); 702 | test_gyr_acc(); 703 | test_radio_reciev(); 704 | 705 | mpu6050_init(); 706 | // Clear the 'sleep' bit to start the sensor. 707 | MPU6050_write_reg (MPU6050_PWR_MGMT_1, 0); 708 | // delay(1000); 709 | 710 | main_power = 0; 711 | motor_arm(); 712 | delay(800); 713 | digitalWrite(13, LOW); 714 | 715 | } 716 | 717 | 718 | void loop() 719 | { 720 | 721 | timer_counter++; 722 | if (timer_counter > 10000) { 723 | 724 | digitalWrite(13, HIGH); 725 | timer_counter = 0; 726 | } 727 | check_radio_signal(); 728 | reciever_to_value(); 729 | calc_wanted_z_ang(); 730 | read_acc_gyr(); 731 | if (main_power > 750) { 732 | stabilize(); 733 | } else { 734 | zero_on_zero_throttle(); 735 | } 736 | set_power(); 737 | } 738 | 739 | 740 | void test_gyr_acc() { 741 | error = MPU6050_read (MPU6050_WHO_AM_I, &c, 1); 742 | if (error != 0) { 743 | while (true) { 744 | digitalWrite(13, HIGH); 745 | delay(300); 746 | digitalWrite(13, LOW); 747 | delay(300); 748 | } 749 | } 750 | } 751 | 752 | void mpu6050_init() { 753 | MPU6050_write_reg (MPU6050_GYRO_CONFIG, 0x08); // 0x00 = 250 deg/s _______ 0x08 = 500deg/s (dela med 65.5 för deg/s) ____________ ___ 0x10 = 1000deg/s ___ 0xC = 2500 deg/s 754 | MPU6050_write_reg (MPU6050_ACCEL_CONFIG, 0x08); // 0x00 = +-2g _____ 0x08 = +-4 g ___.... 755 | MPU6050_write_reg (MPU6050_CONFIG, 0x03); //LOW-pass filter 756 | 757 | } 758 | 759 | void print_angle() { 760 | 761 | } 762 | 763 | void motor_arm() { 764 | delay(2000); 765 | myservoT.writeMicroseconds(800); //for degees 0-180 766 | myservoR.writeMicroseconds(800); //for degees 0-180 767 | myservoB.writeMicroseconds(800); //for degees 0-180 768 | myservoL.writeMicroseconds(800); //for degees 0-180 769 | delay(4700); 770 | } 771 | 772 | void motor_idle() { 773 | myservoT.write(57); //for degees 0-180 774 | myservoR.write(57); //for degees 0-180 775 | myservoB.write(57); //for degees 0-180 776 | myservoL.write(57); //for degees 0-180 777 | } 778 | 779 | void motor_off() { 780 | main_power = 19; 781 | myservoT.write(19); //for degees 0-180 782 | myservoR.write(19); //for degees 0-180 783 | myservoB.write(19); //for degees 0-180 784 | myservoL.write(19); //for degees 0-180 785 | } 786 | 787 | void stabilize() { 788 | P_x = (x_a + rad_tilt_LR) * 2.4; 789 | P_y = (y_a + rad_tilt_TB) * 2.4; 790 | I_x = I_x + (x_a + rad_tilt_LR) * dt_ * 3.7; 791 | I_y = I_y + (y_a + rad_tilt_TB) * dt_ * 3.7; 792 | D_x = x_vel * 0.7; 793 | D_y = y_vel * 0.7; 794 | P_z = (z_ang + wanted_z_ang) * 2.0; 795 | I_z = I_z + (z_ang + wanted_z_ang) * dt_ * 0.8; 796 | D_z = z_vel * 0.3; 797 | 798 | if (P_z > 160) { 799 | P_z = 160; 800 | } 801 | if (P_z < -160) { 802 | P_z = -160; 803 | } 804 | if (I_x > 30) { 805 | I_x = 30; 806 | } 807 | if (I_x < -30) { 808 | I_x = -30; 809 | } 810 | if (I_y > 30) { 811 | I_y = 30; 812 | } 813 | if (I_y < -30) { 814 | I_y = -30; 815 | } 816 | if (I_z > 30) { 817 | I_z = 30; 818 | } 819 | if (I_z < -30) { 820 | I_z = -30; 821 | } 822 | x_adder = P_x + I_x + D_x; 823 | y_adder = P_y + I_y + D_y; 824 | } 825 | 826 | void set_power() { 827 | power_T = (int)(main_power - P_z - I_z - D_z - y_adder + x_adder); 828 | power_B = (int)(main_power - P_z - I_z - D_z + y_adder - x_adder); 829 | power_R = (int)(main_power + P_z + I_z + D_z - x_adder - y_adder); 830 | power_L = (int)(main_power + P_z + I_z + D_z + x_adder + y_adder); 831 | 832 | // if(power_T > 57){ 833 | myservoT.writeMicroseconds(power_T); //for degees 0-180 834 | // }else{myservoT.write(57);} 835 | 836 | // if(power_B > 57){ 837 | myservoB.writeMicroseconds(power_B); //for degees 0-180 838 | // }else{myservoB.write(57);} 839 | 840 | // if(power_R > 57){ 841 | myservoR.writeMicroseconds(power_R); //for degees 0-180 842 | // }else{myservoR.write(57);} 843 | 844 | // if(power_L > 57){ 845 | myservoL.writeMicroseconds(power_L); //for degees 0-180 846 | // }else{myservoL.write(57);} 847 | 848 | } 849 | 850 | void read_two_data() { 851 | x_a_mean = 0; 852 | y_a_mean = 0; 853 | x_vel_mean = 0; 854 | y_vel_mean = 0; 855 | 856 | read_acc_gyr(); 857 | x_a_mean += x_a; 858 | y_a_mean += y_a; 859 | x_vel_mean += x_vel; 860 | y_vel_mean += y_vel; 861 | read_acc_gyr(); 862 | x_a_mean += x_a; 863 | y_a_mean += y_a; 864 | x_vel_mean += x_vel; 865 | y_vel_mean += y_vel; 866 | 867 | x_a_mean /= 2; 868 | y_a_mean /= 2; 869 | x_vel_mean /= 2; 870 | y_vel_mean /= 2; 871 | } 872 | 873 | void calc_wanted_z_ang() { 874 | wanted_z_ang = wanted_z_ang + rad_rotate * dt_ * 2; 875 | 876 | } 877 | 878 | void zero_on_zero_throttle() { 879 | wanted_z_ang = 0; 880 | I_x = 0; 881 | I_y = 0; 882 | z_ang = 0; 883 | I_z = 0; 884 | // main_power = 800; 885 | } 886 | 887 | void escRead() { 888 | if (digitalRead(ESC_IN) == HIGH) { 889 | ESC_START = micros(); 890 | } else { 891 | ESC_IN_SHARED = (uint16_t) (micros() - ESC_START); 892 | bUpdateFlagsShared |= ESC_FLAG; 893 | } 894 | } 895 | 896 | void elevatorRead() { 897 | if (digitalRead(ELEVATOR_IN) == HIGH) { 898 | ELEVATOR_START = micros(); 899 | } else { 900 | ELEVATOR_IN_SHARED = (uint16_t) (micros() - ELEVATOR_START); 901 | bUpdateFlagsShared |= ELEVATOR_FLAG; 902 | } 903 | } 904 | 905 | void aileronRead() { 906 | if (digitalRead(AILERON_IN) == HIGH) { 907 | AILERON_START = micros(); 908 | } else { 909 | AILERON_IN_SHARED = (uint16_t) (micros() - AILERON_START); 910 | bUpdateFlagsShared |= AILERON_FLAG; 911 | } 912 | } 913 | void pppRead() { 914 | if (digitalRead(ppp_in) == HIGH) { 915 | ppp_start = micros(); 916 | } else { 917 | ppp_in_shared = (uint16_t) (micros() - ppp_start); 918 | bUpdateFlagsShared |= ppp_flag; 919 | } 920 | } 921 | 922 | void ppp2Read() { 923 | if (digitalRead(ppp2_in) == HIGH) { 924 | ppp2_start = micros(); 925 | } else { 926 | ppp2_in_shared = (uint16_t) (micros() - ppp2_start); 927 | bUpdateFlagsShared |= ppp2_flag; 928 | } 929 | } 930 | 931 | void ppp3Read() { 932 | if (digitalRead(ppp3_in) == HIGH) { 933 | ppp3_start = micros(); 934 | } else { 935 | ppp3_in_shared = (uint16_t) (micros() - ppp3_start); 936 | bUpdateFlagsShared |= ppp3_flag; 937 | } 938 | } 939 | void reciever_to_value() { 940 | if (ESC > 900 && ESC < 2000) { 941 | main_power = map(ESC, 1080, 2000, 670, 2000); 942 | } else { 943 | main_power = 800; 944 | } 945 | 946 | yaw_temp = AILERON; 947 | 948 | if (AILERON > 900 && AILERON < 2000) { 949 | if (yaw_temp > 1550 || yaw_temp < 1450) { 950 | rad_rotate = map(yaw_temp, 970, 2000, 180, -180); 951 | } else { 952 | rad_rotate = 0; 953 | } 954 | } 955 | 956 | 957 | if (ppp > 900 && ppp < 2000) { 958 | rad_tilt_TB = map(ppp, 970, 2000, -44, 44); 959 | } else { 960 | rad_tilt_TB = 0; 961 | } 962 | 963 | 964 | if (ELEVATOR > 900 && ELEVATOR < 2000) { 965 | rad_tilt_LR = map(ELEVATOR, 1000, 2000, -44, 44); 966 | } else { 967 | rad_tilt_LR = 0; 968 | } 969 | 970 | PID_tune_2 = 0.1 * (map(ppp3, 1000, 2010, 4, 15)); 971 | PID_tune = 0.1 * (map(ppp2, 1000, 2010, 15, 50)); 972 | } 973 | 974 | void test_radio_reciev() { 975 | while (ESC > 1120 || ESC < 1060) { 976 | 977 | //check shared flags to see if we have a new signal 978 | if (bUpdateFlagsShared) { 979 | noInterrupts(); 980 | bUpdateFlags = bUpdateFlagsShared; 981 | if (bUpdateFlags & ESC_FLAG) { 982 | ESC = ESC_IN_SHARED; 983 | } 984 | if (bUpdateFlags & ELEVATOR_FLAG) { 985 | ELEVATOR = ELEVATOR_IN_SHARED; 986 | } 987 | if (bUpdateFlags & AILERON_FLAG) { 988 | AILERON = AILERON_IN_SHARED; 989 | } 990 | if (bUpdateFlags & ppp_flag) { 991 | ppp = ppp_in_shared; 992 | } 993 | 994 | bUpdateFlagsShared = 0; 995 | interrupts(); 996 | 997 | } 998 | 999 | bUpdateFlags = 0; 1000 | 1001 | 1002 | } 1003 | } 1004 | 1005 | void check_radio_signal() { 1006 | //check shared flags to see if we have a new signal 1007 | if (bUpdateFlagsShared) { 1008 | noInterrupts(); 1009 | bUpdateFlags = bUpdateFlagsShared; 1010 | if (bUpdateFlags & ESC_FLAG) { 1011 | ESC = ESC_IN_SHARED; 1012 | } 1013 | if (bUpdateFlags & ELEVATOR_FLAG) { 1014 | ELEVATOR = ELEVATOR_IN_SHARED; 1015 | } 1016 | if (bUpdateFlags & AILERON_FLAG) { 1017 | AILERON = AILERON_IN_SHARED; 1018 | } 1019 | if (bUpdateFlags & ppp_flag) { 1020 | ppp = ppp_in_shared; 1021 | } 1022 | if (bUpdateFlags & ppp2_flag) { 1023 | ppp2 = ppp2_in_shared; 1024 | } 1025 | 1026 | if (bUpdateFlags & ppp3_flag) { 1027 | ppp3 = ppp3_in_shared; 1028 | } 1029 | 1030 | 1031 | bUpdateFlagsShared = 0; 1032 | interrupts(); 1033 | 1034 | } 1035 | 1036 | bUpdateFlags = 0; 1037 | } 1038 | 1039 | void read_acc_gyr() { 1040 | error = MPU6050_read (MPU6050_ACCEL_XOUT_H, (uint8_t *) &accel_t_gyro, sizeof(accel_t_gyro)); 1041 | 1042 | uint8_t swap; 1043 | #define SWAP(x,y) swap = x; x = y; y = swap 1044 | 1045 | SWAP (accel_t_gyro.reg.x_accel_h, accel_t_gyro.reg.x_accel_l); 1046 | SWAP (accel_t_gyro.reg.y_accel_h, accel_t_gyro.reg.y_accel_l); 1047 | SWAP (accel_t_gyro.reg.z_accel_h, accel_t_gyro.reg.z_accel_l); 1048 | SWAP (accel_t_gyro.reg.t_h, accel_t_gyro.reg.t_l); 1049 | SWAP (accel_t_gyro.reg.x_gyro_h, accel_t_gyro.reg.x_gyro_l); 1050 | SWAP (accel_t_gyro.reg.y_gyro_h, accel_t_gyro.reg.y_gyro_l); 1051 | SWAP (accel_t_gyro.reg.z_gyro_h, accel_t_gyro.reg.z_gyro_l); 1052 | 1053 | gyroX_raw = accel_t_gyro.value.x_gyro; 1054 | gyroY_raw = accel_t_gyro.value.y_gyro; 1055 | gyroZ_raw = accel_t_gyro.value.z_gyro; 1056 | double ang_x_raw = accel_t_gyro.value.y_accel; 1057 | double ang_y_raw = accel_t_gyro.value.x_accel; 1058 | double ang_z_raw = accel_t_gyro.value.z_accel; 1059 | double ang_x_raw_sq = sq(ang_x_raw); 1060 | double ang_y_raw_sq = sq(ang_y_raw); 1061 | double ang_z_raw_sq = sq(ang_z_raw); 1062 | accYangle_raw = (atan2(ang_y_raw, sqrt(ang_x_raw_sq + ang_z_raw_sq)) + PI) * RAD_TO_DEG - 180; 1063 | accXangle_raw = (atan2(ang_x_raw, sqrt(ang_z_raw_sq + ang_y_raw_sq)) + PI) * RAD_TO_DEG - 180; 1064 | 1065 | x_ang = -accXangle_raw - ang_x_offset; 1066 | y_ang = accYangle_raw - ang_y_offset; 1067 | x_vel = (-gyroX_raw - gyr_x_raw_offset) / 65.5; 1068 | y_vel = (-gyroY_raw - gyr_y_raw_offset) / 65.5; 1069 | z_vel = (-gyroZ_raw - gyr_z_raw_offset) / 65.5; 1070 | 1071 | 1072 | //komplementary filter 1073 | dt_ = ((double)(micros() - timer) / 1000000); 1074 | //if(dt_ > 0.1){ 1075 | // dt_ = 0.003; 1076 | //} 1077 | x_a = 0.99 * (x_a + x_vel * dt_) + 0.01 * x_ang; 1078 | y_a = 0.99 * (y_a + y_vel * dt_) + 0.01 * y_ang; 1079 | z_ang = z_ang + z_vel * dt_; 1080 | timer = micros(); 1081 | } 1082 | 1083 | 1084 | int MPU6050_read(int start, uint8_t *buffer, int size) 1085 | { 1086 | int i, n, error; 1087 | 1088 | Wire.beginTransmission(MPU6050_I2C_ADDRESS); 1089 | n = Wire.write(start); 1090 | if (n != 1) 1091 | return (-10); 1092 | 1093 | n = Wire.endTransmission(false); 1094 | if (n != 0) 1095 | return (n); 1096 | 1097 | // Third parameter is true: relase I2C-bus after data is read. 1098 | Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true); 1099 | i = 0; 1100 | while (Wire.available() && i < size) 1101 | { 1102 | buffer[i++] = Wire.read(); 1103 | } 1104 | if ( i != size) 1105 | return (-11); 1106 | 1107 | return (0); 1108 | } 1109 | 1110 | int MPU6050_write(int start, const uint8_t *pData, int size) 1111 | { 1112 | int n, error; 1113 | 1114 | Wire.beginTransmission(MPU6050_I2C_ADDRESS); 1115 | n = Wire.write(start); // write the start address 1116 | if (n != 1) 1117 | return (-20); 1118 | 1119 | n = Wire.write(pData, size); // write data bytes 1120 | if (n != size) 1121 | return (-21); 1122 | 1123 | error = Wire.endTransmission(true); // release the I2C-bus 1124 | if (error != 0) 1125 | return (error); 1126 | 1127 | return (0); // return : no error 1128 | } 1129 | 1130 | 1131 | int MPU6050_write_reg(int reg, uint8_t data) 1132 | { 1133 | int error; 1134 | 1135 | error = MPU6050_write(reg, &data, 1); 1136 | 1137 | return (error); 1138 | } 1139 | 1140 | --------------------------------------------------------------------------------