BroBot Code for ESE350 Lab6 part 3 (Skeleton)

Dependencies:   MPU6050_V3 mbed-rtos mbed

Fork of BroBot_RTOS_ESE350 by Carter Sharer

Committer:
csharer
Date:
Sat Dec 17 22:46:59 2016 +0000
Revision:
6:62cdb7482b50
Parent:
4:2512939c10f0
Child:
8:8389c0a9339e
Working Stability and Position Controller

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csharer 4:2512939c10f0 1 //BroBot V3
csharer 4:2512939c10f0 2 //Author: Carter Sharer
csharer 4:2512939c10f0 3 //Date: 10/13/2016
csharer 6:62cdb7482b50 4 //Added communication protocol v1 (no type selection)
csharer 3:2f76ffbc5cef 5
csharer 4:2512939c10f0 6 //BroBot Begin
csharer 3:2f76ffbc5cef 7 #include "pin_assignments.h"
csharer 3:2f76ffbc5cef 8 #include "I2Cdev.h"
csharer 3:2f76ffbc5cef 9 #include "JJ_MPU6050_DMP_6Axis.h"
csharer 3:2f76ffbc5cef 10 #include "BroBot.h"
csharer 3:2f76ffbc5cef 11 #include "BroBot_IMU.h"
csharer 3:2f76ffbc5cef 12 #include "stepper_motors.h"
csharer 4:2512939c10f0 13 #include "MRF24J40.h"
csharer 4:2512939c10f0 14
csharer 6:62cdb7482b50 15 //Angle Offset is used to set the natural balance point of your robot.
csharer 6:62cdb7482b50 16 //You should adjust this offset so that your robots balance points is near 0
csharer 6:62cdb7482b50 17 #define ANGLE_OFFSET 107
csharer 6:62cdb7482b50 18
csharer 4:2512939c10f0 19 //For RF Communication
csharer 4:2512939c10f0 20 #define JSTICK_H 8
csharer 4:2512939c10f0 21 #define JSTICK_V 9
csharer 4:2512939c10f0 22 #define SPACE 10
csharer 4:2512939c10f0 23 #define KNOB1 11
csharer 4:2512939c10f0 24 #define KNOB2 12
csharer 4:2512939c10f0 25 #define KNOB3 13
csharer 4:2512939c10f0 26 #define KNOB4 14
csharer 4:2512939c10f0 27 #define ANGLE 15
csharer 4:2512939c10f0 28 #define BUTTON 16
csharer 4:2512939c10f0 29 #define JSTICK_OFFSET 100
csharer 4:2512939c10f0 30 #define TX_BUFFER_LEN 18
csharer 4:2512939c10f0 31 #define TX_ANGLE_OFFSET 100
csharer 4:2512939c10f0 32 //Knobs
csharer 4:2512939c10f0 33 #define POT1 p17
csharer 4:2512939c10f0 34 #define POT2 p18
csharer 4:2512939c10f0 35 #define POT3 p16
csharer 4:2512939c10f0 36 #define POT4 p15
csharer 4:2512939c10f0 37 //JoyStick
csharer 4:2512939c10f0 38 #define POTV p19
csharer 4:2512939c10f0 39 #define POTH p20
csharer 3:2f76ffbc5cef 40
csharer 3:2f76ffbc5cef 41 //PID
csharer 3:2f76ffbc5cef 42 #define MAX_THROTTLE 580
csharer 3:2f76ffbc5cef 43 #define MAX_STEERING 150
csharer 3:2f76ffbc5cef 44 #define MAX_TARGET_ANGLE 12
csharer 3:2f76ffbc5cef 45 #define KP 0.19
csharer 3:2f76ffbc5cef 46 #define KD 28
csharer 3:2f76ffbc5cef 47 #define KP_THROTTLE 0.01 //0.07
csharer 3:2f76ffbc5cef 48 #define KI_THROTTLE 0//0.04
csharer 3:2f76ffbc5cef 49 #define ITERM_MAX_ERROR 25 // Iterm windup constants for PI control //40
csharer 3:2f76ffbc5cef 50 #define ITERM_MAX 8000 // 5000
csharer 3:2f76ffbc5cef 51
csharer 4:2512939c10f0 52 //Controller Values
csharer 6:62cdb7482b50 53 float knob1, knob2, knob3, knob4;
csharer 6:62cdb7482b50 54 float jstick_h, jstick_v;
csharer 4:2512939c10f0 55
csharer 3:2f76ffbc5cef 56 //PID Default control values from constant definitions
csharer 6:62cdb7482b50 57 float Kp1 = KP;
csharer 6:62cdb7482b50 58 float Kd1 = KD;
csharer 6:62cdb7482b50 59 float Kp2 = KP_THROTTLE;
csharer 6:62cdb7482b50 60 float Ki2 = KI_THROTTLE;
csharer 6:62cdb7482b50 61 float Kd2; //Added for CS Pos contorl
csharer 3:2f76ffbc5cef 62 float PID_errorSum;
csharer 3:2f76ffbc5cef 63 float PID_errorOld = 0;
csharer 3:2f76ffbc5cef 64 float PID_errorOld2 = 0;
csharer 3:2f76ffbc5cef 65 float setPointOld = 0;
csharer 3:2f76ffbc5cef 66 float target_angle;
csharer 3:2f76ffbc5cef 67 float throttle = 0;
csharer 3:2f76ffbc5cef 68 float steering = 0;
csharer 3:2f76ffbc5cef 69 float max_throttle = MAX_THROTTLE;
csharer 3:2f76ffbc5cef 70 float max_steering = MAX_STEERING;
csharer 3:2f76ffbc5cef 71 float max_target_angle = MAX_TARGET_ANGLE;
csharer 3:2f76ffbc5cef 72 float control_output;
csharer 3:2f76ffbc5cef 73 int16_t actual_robot_speed; // overall robot speed (measured from steppers speed)
csharer 3:2f76ffbc5cef 74 int16_t actual_robot_speed_old;
csharer 3:2f76ffbc5cef 75 float estimated_speed_filtered; // Estimated robot speed
csharer 4:2512939c10f0 76 int robot_pos = 0;
csharer 3:2f76ffbc5cef 77
csharer 3:2f76ffbc5cef 78 Timer timer;
csharer 3:2f76ffbc5cef 79 int timer_value; //maybe make this a long
csharer 3:2f76ffbc5cef 80 int timer_old; //maybe make this a long
csharer 4:2512939c10f0 81 int dt;
csharer 3:2f76ffbc5cef 82
csharer 3:2f76ffbc5cef 83 uint8_t slow_loop_counter;
csharer 4:2512939c10f0 84 uint8_t medium_loop_counter;
csharer 3:2f76ffbc5cef 85 uint8_t loop_counter;
csharer 3:2f76ffbc5cef 86
csharer 3:2f76ffbc5cef 87 Serial pc(USBTX, USBRX);
csharer 3:2f76ffbc5cef 88
csharer 4:2512939c10f0 89 // LEDs
csharer 4:2512939c10f0 90 DigitalOut led1(LED1);
csharer 4:2512939c10f0 91 DigitalOut led2(LED2);
csharer 4:2512939c10f0 92 DigitalOut led3(LED3);
csharer 4:2512939c10f0 93 DigitalOut led4(LED4);
csharer 4:2512939c10f0 94
csharer 4:2512939c10f0 95 //Button
csharer 4:2512939c10f0 96 bool button;
csharer 6:62cdb7482b50 97 #include "communication.h"
csharer 4:2512939c10f0 98
csharer 3:2f76ffbc5cef 99 // ================================================================
csharer 3:2f76ffbc5cef 100 // === INITIAL SETUP ===
csharer 3:2f76ffbc5cef 101 // ================================================================
csharer 3:2f76ffbc5cef 102 void init_imu()
csharer 3:2f76ffbc5cef 103 {
csharer 3:2f76ffbc5cef 104 pc.printf("\r\r\n\n Start \r\n");
csharer 3:2f76ffbc5cef 105
csharer 3:2f76ffbc5cef 106 // Manual MPU initialization... accel=2G, gyro=2000º/s, filter=20Hz BW, output=200Hz
csharer 3:2f76ffbc5cef 107 mpu.setClockSource(MPU6050_CLOCK_PLL_ZGYRO);
csharer 3:2f76ffbc5cef 108 mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_2000);
csharer 3:2f76ffbc5cef 109 mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
csharer 3:2f76ffbc5cef 110 mpu.setDLPFMode(MPU6050_DLPF_BW_10); //10,20,42,98,188 // Default factor for BROBOT:10
csharer 4:2512939c10f0 111 mpu.setRate(4); // 0=1khz 1=500hz, 2=333hz, 3=250hz [4=200hz]default
csharer 3:2f76ffbc5cef 112 mpu.setSleepEnabled(false);
csharer 3:2f76ffbc5cef 113 wait_ms(500);
csharer 3:2f76ffbc5cef 114
csharer 3:2f76ffbc5cef 115 // load and configure the DMP
csharer 3:2f76ffbc5cef 116 devStatus = mpu.dmpInitialize();
csharer 3:2f76ffbc5cef 117 if(devStatus == 0) {
csharer 3:2f76ffbc5cef 118 mpu.setDMPEnabled(true);
csharer 3:2f76ffbc5cef 119 mpuIntStatus = mpu.getIntStatus();
csharer 3:2f76ffbc5cef 120 dmpReady = true;
csharer 4:2512939c10f0 121 } else {
csharer 3:2f76ffbc5cef 122 // 1 = initial memory load failed
csharer 3:2f76ffbc5cef 123 // 2 = DMP configuration updates failed
csharer 3:2f76ffbc5cef 124 pc.printf("DMP INIT error \r\n");
csharer 3:2f76ffbc5cef 125 }
csharer 3:2f76ffbc5cef 126
csharer 3:2f76ffbc5cef 127 //Gyro Calibration
csharer 3:2f76ffbc5cef 128 wait_ms(500);
csharer 3:2f76ffbc5cef 129 pc.printf("Gyro calibration!! Dont move the robot in 10 seconds... \r\n");
csharer 3:2f76ffbc5cef 130 wait_ms(500);
csharer 4:2512939c10f0 131
csharer 3:2f76ffbc5cef 132 // verify connection
csharer 3:2f76ffbc5cef 133 pc.printf(mpu.testConnection() ? "Connection Good \r\n" : "Connection Failed\r\n");
csharer 3:2f76ffbc5cef 134
csharer 3:2f76ffbc5cef 135 //Adjust Sensor Fusion Gain
csharer 3:2f76ffbc5cef 136 dmpSetSensorFusionAccelGain(0x20);
csharer 3:2f76ffbc5cef 137
csharer 3:2f76ffbc5cef 138 wait_ms(200);
csharer 3:2f76ffbc5cef 139 mpu.resetFIFO();
csharer 3:2f76ffbc5cef 140 }
csharer 3:2f76ffbc5cef 141
csharer 3:2f76ffbc5cef 142 // ================================================================
csharer 3:2f76ffbc5cef 143 // === MAIN PROGRAM LOOP ===
csharer 3:2f76ffbc5cef 144 // ================================================================
csharer 4:2512939c10f0 145 //CS PID CONTROLLER TEST
csharer 4:2512939c10f0 146 float target_angle_old = 0;
csharer 4:2512939c10f0 147 float change_in_target_angle = 0;
csharer 4:2512939c10f0 148 float change_in_angle = 0;
csharer 4:2512939c10f0 149 float angle_old1 = 0;
csharer 4:2512939c10f0 150 float angle_old2 = 0;
csharer 4:2512939c10f0 151 float kp_term = 0;
csharer 4:2512939c10f0 152 float kd_term = 0;
csharer 4:2512939c10f0 153 float error;
csharer 4:2512939c10f0 154 //For Position controller
csharer 4:2512939c10f0 155 float pos_error = 0;
csharer 4:2512939c10f0 156 float kp_pos_term = 0;
csharer 4:2512939c10f0 157 float kd_pos_term = 0;
csharer 4:2512939c10f0 158 float change_in_target_pos;
csharer 4:2512939c10f0 159 float target_pos, target_pos_old;
csharer 4:2512939c10f0 160 float change_in_pos;
csharer 4:2512939c10f0 161 float robot_pos_old, robot_pos_old1, robot_pos_old2;
csharer 4:2512939c10f0 162
csharer 6:62cdb7482b50 163 bool fallen = true;
csharer 3:2f76ffbc5cef 164 int main()
csharer 3:2f76ffbc5cef 165 {
csharer 6:62cdb7482b50 166 //Set the Channel. 0 is default, 15 is max
csharer 6:62cdb7482b50 167 uint8_t channel = 2;
csharer 6:62cdb7482b50 168 mrf.SetChannel(channel);
csharer 6:62cdb7482b50 169
csharer 6:62cdb7482b50 170 pc.baud(115200);
csharer 3:2f76ffbc5cef 171 pc.printf("Start\r\n");
csharer 3:2f76ffbc5cef 172 init_imu();
csharer 3:2f76ffbc5cef 173 timer.start();
csharer 3:2f76ffbc5cef 174 //timer
csharer 4:2512939c10f0 175 timer_value = timer.read_us();
syundo0730 0:8d2c753a96e7 176
csharer 3:2f76ffbc5cef 177 //Init Stepper Motors
csharer 3:2f76ffbc5cef 178 //Attach Timer Interupts (Tiker)
csharer 3:2f76ffbc5cef 179 timer_M1.attach_us(&ISR1, ZERO_SPEED);
csharer 3:2f76ffbc5cef 180 timer_M2.attach_us(&ISR2, ZERO_SPEED);
csharer 3:2f76ffbc5cef 181 step_M1 = 1;
csharer 3:2f76ffbc5cef 182 dir_M1 = 1;
csharer 6:62cdb7482b50 183 enable = DISABLE; //Disable Motors
csharer 4:2512939c10f0 184
csharer 4:2512939c10f0 185 //Attach Interupt for IMU
csharer 4:2512939c10f0 186 checkpin.rise(&dmpDataReady);
csharer 4:2512939c10f0 187
csharer 4:2512939c10f0 188 //Used to set angle upon startup, filter
csharer 4:2512939c10f0 189 bool FILTER_DISABLE = true;
csharer 4:2512939c10f0 190
csharer 6:62cdb7482b50 191 //Enable Motors
csharer 6:62cdb7482b50 192 enable = ENABLE;
csharer 6:62cdb7482b50 193
csharer 3:2f76ffbc5cef 194 while(1) {
csharer 6:62cdb7482b50 195 //led1 = led1^1;
csharer 6:62cdb7482b50 196 led4 = !fallen;
csharer 6:62cdb7482b50 197 led2 = button;
csharer 6:62cdb7482b50 198
csharer 6:62cdb7482b50 199 if(jstick_v > 80)
csharer 6:62cdb7482b50 200 led3 = 1;
csharer 6:62cdb7482b50 201 else
csharer 6:62cdb7482b50 202 led3 = 0;
csharer 6:62cdb7482b50 203
csharer 4:2512939c10f0 204 if(button) {
csharer 4:2512939c10f0 205 pos_M1 = 0;
csharer 4:2512939c10f0 206 pos_M2 = 0;
csharer 4:2512939c10f0 207 target_pos = 0;
csharer 6:62cdb7482b50 208 fallen = false;
csharer 4:2512939c10f0 209 }
csharer 4:2512939c10f0 210
csharer 6:62cdb7482b50 211 while(!mpuInterrupt) {
csharer 4:2512939c10f0 212 timer_value = timer.read_us();
csharer 4:2512939c10f0 213
csharer 4:2512939c10f0 214 //Set Gainz with knobs
csharer 6:62cdb7482b50 215 Kp1 = ((float)knob1) / 1000.0;
csharer 6:62cdb7482b50 216 Kd1 = ((float)knob2) / 1.0;
csharer 6:62cdb7482b50 217 Kp2 = ((float)knob3) / 1000.0;
csharer 6:62cdb7482b50 218 Kd2 = ((float)knob4) / 100.0;
csharer 4:2512939c10f0 219
csharer 4:2512939c10f0 220 //Joystick control
csharer 4:2512939c10f0 221 throttle = (float)jstick_v /10.0;
csharer 4:2512939c10f0 222 steering = (float)jstick_h / 10.0;
csharer 4:2512939c10f0 223
csharer 4:2512939c10f0 224 //Update Values
csharer 3:2f76ffbc5cef 225 loop_counter++;
csharer 3:2f76ffbc5cef 226 slow_loop_counter++;
csharer 4:2512939c10f0 227 medium_loop_counter++;
csharer 3:2f76ffbc5cef 228 dt = (timer_value - timer_old);
csharer 3:2f76ffbc5cef 229 timer_old = timer_value;
csharer 4:2512939c10f0 230 angle_old = angle;
csharer 3:2f76ffbc5cef 231
csharer 6:62cdb7482b50 232 // STANDING: Motor Control Enabled
csharer 6:62cdb7482b50 233 if(((angle < 45) && (angle > -45)) && (fallen == false)) {
csharer 3:2f76ffbc5cef 234
csharer 6:62cdb7482b50 235 //CS Pd Target Angle Contoller Goes Here
csharer 6:62cdb7482b50 236
csharer 6:62cdb7482b50 237 //Robot Position
csharer 6:62cdb7482b50 238 robot_pos = (pos_M1 + pos_M2) / 2;
csharer 6:62cdb7482b50 239 target_pos += throttle/2;
csharer 4:2512939c10f0 240
csharer 6:62cdb7482b50 241 //Position error
csharer 4:2512939c10f0 242 pos_error = robot_pos - target_pos; //robot_pos - target_pos;
csharer 6:62cdb7482b50 243
csharer 6:62cdb7482b50 244 //KP Term
csharer 6:62cdb7482b50 245 kp_pos_term = -Kp2 * pos_error;
csharer 4:2512939c10f0 246
csharer 4:2512939c10f0 247 //KD Term
csharer 4:2512939c10f0 248 change_in_target_pos = target_pos - target_pos_old;
csharer 4:2512939c10f0 249 change_in_pos = robot_pos - robot_pos_old2;
csharer 6:62cdb7482b50 250 kd_pos_term = ((-Kd2 * change_in_target_pos) + (-Kd2*change_in_pos)) /dt;
csharer 4:2512939c10f0 251 target_angle = kp_pos_term + kd_pos_term;
csharer 4:2512939c10f0 252 target_angle = CAP(target_angle, MAX_TARGET_ANGLE);
csharer 4:2512939c10f0 253
csharer 4:2512939c10f0 254 //Update values
csharer 4:2512939c10f0 255 target_pos_old = target_pos;
csharer 4:2512939c10f0 256 robot_pos_old2 = robot_pos_old1;
csharer 4:2512939c10f0 257 robot_pos_old1 = robot_pos_old;
csharer 4:2512939c10f0 258
csharer 4:2512939c10f0 259 //CS PD Stability CONTROLLER HERE
csharer 4:2512939c10f0 260 error = target_angle - angle;
csharer 6:62cdb7482b50 261 kp_term = Kp1 * error;
csharer 4:2512939c10f0 262
csharer 4:2512939c10f0 263 change_in_target_angle = target_angle - target_angle_old; //add
csharer 4:2512939c10f0 264 change_in_angle = angle - angle_old2; //add
csharer 6:62cdb7482b50 265 kd_term = ((Kd1 * change_in_target_angle) - Kd1*(change_in_angle)) / dt;
csharer 6:62cdb7482b50 266
csharer 6:62cdb7482b50 267 //pc.printf("dAngle:%f\r\n", angle-angle_old1);
csharer 6:62cdb7482b50 268
csharer 4:2512939c10f0 269 //Control Output
csharer 4:2512939c10f0 270 control_output += kp_term + kd_term;
csharer 4:2512939c10f0 271 control_output = CAP(control_output, MAX_CONTROL_OUTPUT); // Limit max output from control
csharer 6:62cdb7482b50 272 motor1 = (int16_t)(control_output + (steering));
csharer 6:62cdb7482b50 273 motor2 = (int16_t)(control_output - (steering));
csharer 4:2512939c10f0 274 motor1 = CAP(motor1, MAX_CONTROL_OUTPUT);
csharer 4:2512939c10f0 275 motor2 = CAP(motor2, MAX_CONTROL_OUTPUT);
csharer 4:2512939c10f0 276
csharer 4:2512939c10f0 277 //Update variables
csharer 4:2512939c10f0 278 target_angle_old = target_angle;
csharer 4:2512939c10f0 279 angle_old2 = angle_old1;
csharer 4:2512939c10f0 280 angle_old1 = angle;
csharer 4:2512939c10f0 281
csharer 4:2512939c10f0 282 //Enable Motors
csharer 4:2512939c10f0 283 enable = ENABLE;
csharer 4:2512939c10f0 284 setMotor1Speed(-motor1);
csharer 4:2512939c10f0 285 setMotor2Speed(-motor2);
csharer 4:2512939c10f0 286 robot_pos += (-motor1 + -motor2) / 2;
csharer 4:2512939c10f0 287 //pc.printf("m1: %d m2: %d angle: %0.1f, controlout: %f tAngle: %f dt: %f timer: %d \r\n", motor1, motor2, angle, control_output, target_angle, dt, timer_value);
csharer 6:62cdb7482b50 288 } else { //[FALLEN}
csharer 4:2512939c10f0 289 //Disable Motors
csharer 4:2512939c10f0 290 enable = DISABLE;
csharer 6:62cdb7482b50 291
csharer 6:62cdb7482b50 292 //Set fallen flag
csharer 6:62cdb7482b50 293 fallen = true;
csharer 4:2512939c10f0 294 }
csharer 4:2512939c10f0 295
csharer 6:62cdb7482b50 296 //Fast Loop
csharer 4:2512939c10f0 297 if(loop_counter >= 5) {
csharer 4:2512939c10f0 298 loop_counter = 0;
csharer 6:62cdb7482b50 299 pc.printf("angle:%d Kp1: %0.3f Kd1: %0.2f Kp2: %0.2f Kd2: %0.3f tang: %0.2f dt:%d pos_M1:%d pos_M2:%d rob_pos: %d\r\n", (int)angle, Kp1, Kd1, Kp2, Ki2, target_angle, dt, pos_M1, pos_M2, robot_pos);
csharer 6:62cdb7482b50 300 //pc.printf("Jstick_h: %d Jstick_v: %d Knob1 %d Knob2 %d Knob3 %d Knob4 %d Button: %d\r\n", jstick_h, jstick_v, knob1, knob2, knob3, knob4, button);
csharer 4:2512939c10f0 301 }
csharer 3:2f76ffbc5cef 302
csharer 4:2512939c10f0 303 //Meduim Loop
csharer 4:2512939c10f0 304 if (medium_loop_counter >= 10) {
csharer 4:2512939c10f0 305 medium_loop_counter = 0; // Read status
csharer 6:62cdb7482b50 306
csharer 4:2512939c10f0 307 //Recieve Data
csharer 6:62cdb7482b50 308 rxLen = rf_receive(rxBuffer, 128);
csharer 6:62cdb7482b50 309 if(rxLen > 0) {
csharer 6:62cdb7482b50 310 led1 = led1^1;
csharer 6:62cdb7482b50 311 //Process data with our protocal
csharer 6:62cdb7482b50 312 communication_protocal(rxLen);
csharer 4:2512939c10f0 313 }
csharer 6:62cdb7482b50 314
csharer 4:2512939c10f0 315 } // End of medium loop
csharer 4:2512939c10f0 316
csharer 4:2512939c10f0 317 //Slow Loop
csharer 4:2512939c10f0 318 if(slow_loop_counter >= 99) {
csharer 4:2512939c10f0 319 slow_loop_counter = 0;
csharer 6:62cdb7482b50 320
csharer 6:62cdb7482b50 321 /* Send Data To Controller goes here *
csharer 6:62cdb7482b50 322 * */
csharer 6:62cdb7482b50 323
csharer 4:2512939c10f0 324 } //End of Slow Loop
csharer 3:2f76ffbc5cef 325
csharer 4:2512939c10f0 326 //Reattach interupt
csharer 4:2512939c10f0 327 checkpin.rise(&dmpDataReady);
csharer 4:2512939c10f0 328 } //END WHILE
csharer 4:2512939c10f0 329
csharer 6:62cdb7482b50 330
csharer 6:62cdb7482b50 331 /********************* All IMU Handling DO NOT MODIFY *****************/
csharer 4:2512939c10f0 332 //Disable IRQ
csharer 4:2512939c10f0 333 checkpin.rise(NULL);
csharer 6:62cdb7482b50 334
csharer 6:62cdb7482b50 335 //reset interrupt flag and get INT_STATUS byte
csharer 3:2f76ffbc5cef 336 mpuInterrupt = false;
csharer 3:2f76ffbc5cef 337 mpuIntStatus = mpu.getIntStatus();
csharer 3:2f76ffbc5cef 338
csharer 6:62cdb7482b50 339 //get current FIFO count
csharer 3:2f76ffbc5cef 340 fifoCount = mpu.getFIFOCount();
csharer 3:2f76ffbc5cef 341
csharer 3:2f76ffbc5cef 342 // check for overflow (this should never happen unless our code is too inefficient)
csharer 3:2f76ffbc5cef 343 if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
csharer 3:2f76ffbc5cef 344 // reset so we can continue cleanly
csharer 3:2f76ffbc5cef 345 mpu.resetFIFO();
csharer 4:2512939c10f0 346 pc.printf("FIFO overflow!");
csharer 4:2512939c10f0 347
csharer 6:62cdb7482b50 348 //otherwise, check for DMP data ready interrupt (this should happen frequently)
csharer 3:2f76ffbc5cef 349 } else if (mpuIntStatus & 0x02) {
csharer 6:62cdb7482b50 350 //wait for correct available data length, should be a VERY short wait
csharer 4:2512939c10f0 351 while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
csharer 3:2f76ffbc5cef 352
csharer 6:62cdb7482b50 353 //read a packet from FIFO
csharer 3:2f76ffbc5cef 354 mpu.getFIFOBytes(fifoBuffer, packetSize);
csharer 3:2f76ffbc5cef 355
csharer 6:62cdb7482b50 356 //track FIFO count here in case there is > 1 packet available
csharer 6:62cdb7482b50 357 //(this lets us immediately read more without waiting for an interrupt)
csharer 3:2f76ffbc5cef 358 fifoCount -= packetSize;
csharer 3:2f76ffbc5cef 359
csharer 4:2512939c10f0 360 //Read new angle from IMU
csharer 4:2512939c10f0 361 new_angle = (float)(dmpGetPhi() - ANGLE_OFFSET);
csharer 4:2512939c10f0 362 dAngle = new_angle - angle;
csharer 3:2f76ffbc5cef 363
csharer 4:2512939c10f0 364 //Filter out angle readings larger then MAX_ANGLE_DELTA
csharer 4:2512939c10f0 365 if( ((dAngle < 15) && (dAngle > -15)) || FILTER_DISABLE) {
csharer 4:2512939c10f0 366 angle = new_angle;
csharer 4:2512939c10f0 367 FILTER_DISABLE = false; //turn of filter disabler
csharer 4:2512939c10f0 368 } else {
csharer 4:2512939c10f0 369 pc.printf("\t\t\t filtered angle \r\n");
csharer 4:2512939c10f0 370 }
csharer 3:2f76ffbc5cef 371 }
csharer 6:62cdb7482b50 372 /********************* All IMU Handling DO NOT MODIFY *****************/
csharer 6:62cdb7482b50 373
csharer 3:2f76ffbc5cef 374 } //end main loop
csharer 3:2f76ffbc5cef 375 } //End Main()