David McPherson / Mbed 2 deprecated zumy_mbed

Dependencies:   MPU6050IMU QEI RPCInterface mbed

Fork of zumy_mbed by Austin Buchan

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SerialRPCInterface.h"
00003 #include "MPU6050.h"
00004 #include "QEI.h"
00005 
00006 SerialRPCInterface SerialRPC(USBTX, USBRX, 115200);
00007 //Serial pc(USBTX, USBRX); // tx, rx
00008 
00009 float accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z;
00010 int r_enc, l_enc;
00011 
00012 RPCVariable<float> rpc_accel_x(&accel_x, "accel_x");
00013 RPCVariable<float> rpc_accel_y(&accel_y, "accel_y");
00014 RPCVariable<float> rpc_accel_z(&accel_z, "accel_z");
00015 RPCVariable<float> rpc_gryo_x(&gyro_x, "gyro_x");
00016 RPCVariable<float> rpc_gryo_y(&gyro_y, "gyro_y");
00017 RPCVariable<float> rpc_gryo_z(&gyro_z, "gyro_z");
00018 RPCVariable<int>   rpc_r_enc(&r_enc, "r_enc");
00019 RPCVariable<int>   rpc_l_enc(&l_enc, "l_enc");
00020 QEI l_wheel (p29, p30, NC, 624);
00021 QEI r_wheel (p11, p12, NC, 624);
00022 
00023 MPU6050 mpu6050;
00024 
00025 DigitalOut init_done(LED1);
00026 DigitalOut imu_good(LED2);
00027 DigitalOut main_loop(LED3);
00028 
00029 int main() {
00030     init_done = 0;
00031     imu_good = 0;
00032     main_loop = 0;
00033     
00034     //Set up I2C
00035     i2c.frequency(400000);  // use fast (400 kHz) I2C
00036     
00037     volatile bool imu_ready = false;
00038     
00039     wait_ms(100);
00040     
00041     uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050);
00042     
00043     if (whoami == 0x68) // WHO_AM_I should always be 0x68
00044     {
00045         mpu6050.MPU6050SelfTest(SelfTest);
00046         if(SelfTest[0] < 1.0f && SelfTest[1] < 1.0f && SelfTest[2] < 1.0f && SelfTest[3] < 1.0f && SelfTest[4] < 1.0f && SelfTest[5] < 1.0f) {
00047             mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration
00048             mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers  
00049             mpu6050.initMPU6050();
00050             mpu6050.getAres();
00051             mpu6050.getGres();
00052             imu_ready = true;
00053             imu_good = 1;
00054         }
00055     }
00056     
00057     init_done = 1;
00058     uint8_t loop_count = 10;
00059     while(1) {
00060         wait_ms(10);
00061         
00062         // Handle the encoders
00063         r_enc=r_wheel.getPulses();
00064         l_enc=l_wheel.getPulses();
00065         //pc.printf("Pulses are: %i, %i\r\n", l_enc,r_enc);
00066         
00067         if (!(--loop_count)) {
00068             loop_count = 10;
00069             main_loop = !main_loop;
00070         }
00071         
00072         if (imu_ready) {
00073             
00074             if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) {  // check if data ready interrupt
00075                 mpu6050.readAccelData(accelCount);  // Read the x/y/z adc values
00076                 mpu6050.readGyroData(gyroCount);  // Read the x/y/z adc values
00077 
00078                 // Now we'll calculate the accleration value into actual g's
00079                 accel_x = (float)accelCount[0]*aRes - accelBias[0];  // get actual g value, this depends on scale being set
00080                 accel_y = (float)accelCount[1]*aRes - accelBias[1];   
00081                 accel_z = (float)accelCount[2]*aRes - accelBias[2];  
00082                
00083                 // Calculate the gyro value into actual degrees per second
00084                 gyro_x = (float)gyroCount[0]*gRes - gyroBias[0];  // get actual gyro value, this depends on scale being set
00085                 gyro_y = (float)gyroCount[1]*gRes - gyroBias[1];  
00086                 gyro_z = (float)gyroCount[2]*gRes - gyroBias[2];
00087             }
00088         }
00089     }
00090 }