ESE350 project, Spring 2016, University of Pennsylvania

Dependencies:   Adafruit9-DOf Receiver mbed-rtos mbed

Revision:
39:fff0a72633ee
Parent:
38:14bf11115f9f
Child:
40:09a59d5b7944
--- a/quadcopter.cpp	Sun May 01 21:59:10 2016 +0000
+++ b/quadcopter.cpp	Sun May 01 22:13:01 2016 +0000
@@ -24,17 +24,13 @@
     kp_phi_   = 0.25 * (maxPwm - zeroVelPwm) * l_ / 0.5 * 4 / M_PI;
     kp_theta_ = 0.2 * (maxPwm - zeroVelPwm) * l_ / 0.5 * 4 / M_PI;
     kp_psi_   = 0;
-    // kp_phi_ = 0;
-//    kp_theta_ = 0;
 
     // derivative attitude control gains
-    kd_phi_   = 0.05 * (maxPwm - zeroVelPwm) * 2 / M_PI; //0.25 good
+    kd_phi_   = 0.05 * (maxPwm - zeroVelPwm) * 2 / M_PI; // 0.25 maybe a good
     kd_theta_ = 0.05 * (maxPwm - zeroVelPwm) * 2 / M_PI;
     kd_psi_   = 0.1;
-    // kd_phi_   = 0;
-    //kd_theta_ = 0;
 
-// incresae ki_phi
+    // incresae ki_phi
     ki_phi_ = 0 * (maxPwm - zeroVelPwm)/(2*M_PI/4); // full control signal after 2s at pi/4 error
     ki_theta_ = 0 *  (maxPwm - zeroVelPwm)/(2*M_PI/4);
 
@@ -83,12 +79,11 @@
         prev_kalman_time = controlTimer->read();
         return;
     }
-    
+
     accel_.getEvent(&accel_event_);
-//    mag_.getEvent(&mag_event_);
+    // mag_.getEvent(&mag_event_);
     dof_.accelGetOrientation(&accel_event_, &orientation_);
-//
-//    gyro_.getEvent(&gyro_event_);
+    // gyro_.getEvent(&gyro_event_);
 
     gyro_event_.gyro.x   -= initial_offsets_->gyro_x;
     gyro_event_.gyro.y   -= initial_offsets_->gyro_y;
@@ -97,8 +92,6 @@
     orientation_.pitch   -= initial_offsets_->pitch;
     orientation_.heading -= initial_offsets_->heading;
 
-
-
     static int current_filter = 0;
     filters_.p[current_filter]     = gyro_event_.gyro.x * M_PI / 180;
     filters_.q[current_filter]     = gyro_event_.gyro.y * M_PI / 180;
@@ -124,15 +117,15 @@
         psi_sum   += filters_.psi[i];
     }
 
-//    double radMaxAngle = M_PI / 180 * 20;
+    // double radMaxAngle = M_PI / 180 * 20;
     double phi_new = phi_sum / FILTER_SIZE;
-//    if (phi_new < radMaxAngle && phi_new > -radMaxAngle) {
-//        state_.phi = phi_new;
-//    };
+    // if (phi_new < radMaxAngle && phi_new > -radMaxAngle) {
+    //      state_.phi = phi_new;
+    // }
     double theta_new = theta_sum / FILTER_SIZE;
-//    if (theta_new < radMaxAngle && theta_new > -radMaxAngle) {
-//        state_.theta = theta_new;
-//    };
+    // if (theta_new < radMaxAngle && theta_new > -radMaxAngle) {
+    //  state_.theta = theta_new;
+    // }
 
     state_.phi = phi_new;
     state_.theta = theta_new;
@@ -154,21 +147,21 @@
     state_.theta = kalmanPitch.getAngle(state_.theta * 180 / M_PI, state_.q * 180 / M_PI, dt) * M_PI / 180;
     state_.p = kalmanRoll.getRate() * M_PI / 180;
     state_.q = kalmanPitch.getRate() * M_PI / 180;
-    
+
     double alphaX = 0.7;
     double alphaY = 0.7;
     compAngleX = (1 - alphaX) * (compAngleX + raw_p * dt) + alphaX * raw_phi; // Calculate the angle using a Complimentary filter
     compAngleY = (1 - alphaY) * (compAngleY + raw_q * dt) + alphaY * raw_theta;
-    
+
     //state_.phi = compAngleX;
     //state_.theta = compAngleY;
 
     prev_kalman_time = time;
-//    static int count = 0;
-//    if (count % 100 == 0) {
-//        pc_->printf("%d\r\n", count);
-//    }
-//    count++;
+    // static int count = 0;
+    // if (count % 100 == 0) {
+    //  pc_->printf("%d\r\n", count);
+    // }
+    // count++;
     //pc_->printf("%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\r\n", prev_kalman_time, F_des_, desiredState_.psi, desiredState_.theta, desiredState_.phi, state_.psi, state_.theta, state_.phi,state_.r, state_.p, state_.q, compAngleX, compAngleY, raw_phi, raw_theta);
 }
 
@@ -279,7 +272,6 @@
     //static int thrust_outliers = 0;
 
     receive = rf_receive_rssi(*mrf_, rxBuffer, rssi, rcLength_ + 1);
-    //pc_->printf("%s\r\n", rxBuffer);
     if (receive > 10) {
         int written = sscanf(rxBuffer, "%lld,%f,%f,%f,%f", &id, &thrust, &yaw, &pitch, &roll);
         // pc_->printf("%d\r\n", written);
@@ -291,9 +283,10 @@
         pc_->printf("Receive failure\r\n");
         return;
     }
-    //pc_->printf("thrust: %f\r\n", thrust);
-//    float temp_thrust = thrust - 0.5;
-//    
+
+// test for outliers (can remove when fixed for sure)
+// float temp_thrust = thrust - 0.5;
+//
 //    if (temp_thrust < -0.3) {
 //        thrust_outliers++;
 //        if (thrust_outliers < 3) {
@@ -303,16 +296,16 @@
 //        thrust_outliers = 0;
 //    }
 
-// TODO eliminate the zeros again after testing
     // convert to radians, range is = +-40° or +-0.698132 radians
     desiredState_.phi = 1 * (-(roll * 80) * M_PI / 180); // minus, because joystick to right should result in positive moment
     desiredState_.theta = 1 * (pitch * 80) * M_PI / 180;
     desiredState_.r = 1 * yaw; // number between 0 and 1 //((yaw - 0.5) * 80) * M_PI / 180;
     F_des_ = thrust; // number between 0 and 1 -> number between -0.5 and 0.5
-    // print id with thrust, yaw, pitch, and roll
-    
+
+    /* test for outliers (can remove when fixed for sure)
     if (abs(F_des_) > 0.01 || abs(desiredState_.psi) > 0.01 || abs(desiredState_.theta) > 0.02 || abs(desiredState_.phi) > 0.01) {
         pc_->printf("%lld: thrust: %f yaw: %f pitch: %f roll: %f\r\n", id, F_des_, desiredState_.psi, desiredState_.theta, desiredState_.phi); //, thrust_outliers);
     }
+    */
 }