James Lam / Mbed 2 deprecated zumy_mbed

Dependencies:   MPU6050IMU QEI RPCInterface mbed

Fork of zumy_mbed by David McPherson

Committer:
jlamyi
Date:
Thu Apr 07 23:31:21 2016 +0000
Revision:
15:3cee564f9d47
Parent:
14:a153ce7cc4c8
Achieved 70Hz data sending rate by two RPCFunction calls

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MichaelW 0:78952cd3935b 1 #include "mbed.h"
abuchan 3:8b5700499eb8 2 #include "SerialRPCInterface.h"
abuchan 2:2c0cd1aaae83 3 #include "MPU6050.h"
DavidMcP555 4:b8eeb59b62d4 4 #include "QEI.h"
abuchan 2:2c0cd1aaae83 5
jlamyi 13:af3e8a8189ed 6 //bool debug = false;
jlamyi 9:6bff048c3b49 7
jlamyi 8:1e47a6b49537 8 extern "C" void mbed_reset();
jlamyi 6:18183b8b08f1 9
abuchan 3:8b5700499eb8 10 SerialRPCInterface SerialRPC(USBTX, USBRX, 115200);
jlamyi 9:6bff048c3b49 11
jlamyi 15:3cee564f9d47 12 void GetIMUData(char* input, char* output);
jlamyi 15:3cee564f9d47 13 RPCFunction gid(&GetIMUData, "gid");
jlamyi 15:3cee564f9d47 14 void GetEncoderData(char* input, char* output);
jlamyi 15:3cee564f9d47 15 RPCFunction ged(&GetEncoderData, "ged");
jlamyi 8:1e47a6b49537 16 void Reset(char* input, char* output);
jlamyi 8:1e47a6b49537 17 RPCFunction rst(&Reset, "rst");
jlamyi 13:af3e8a8189ed 18 //void WD_init(char* input, char* output);
jlamyi 13:af3e8a8189ed 19 //RPCFunction wdinit(&WD_init, "wdinit");
abuchan 3:8b5700499eb8 20
jlamyi 8:1e47a6b49537 21 float accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z;
jlamyi 8:1e47a6b49537 22 int r_enc, l_enc;
jlamyi 6:18183b8b08f1 23
jlamyi 11:090042660f15 24
DavidMcP555 4:b8eeb59b62d4 25 QEI l_wheel (p29, p30, NC, 624);
DavidMcP555 4:b8eeb59b62d4 26 QEI r_wheel (p11, p12, NC, 624);
abuchan 2:2c0cd1aaae83 27
abuchan 2:2c0cd1aaae83 28 MPU6050 mpu6050;
abuchan 2:2c0cd1aaae83 29
abuchan 2:2c0cd1aaae83 30 DigitalOut init_done(LED1);
abuchan 2:2c0cd1aaae83 31 DigitalOut imu_good(LED2);
abuchan 2:2c0cd1aaae83 32 DigitalOut main_loop(LED3);
jlamyi 14:a153ce7cc4c8 33 DigitalOut main_loop2(LED4);
abuchan 2:2c0cd1aaae83 34
jlamyi 11:090042660f15 35 // Simon's Watchdog code from
jlamyi 11:090042660f15 36 // http://mbed.org/forum/mbed/topic/508/
jlamyi 13:af3e8a8189ed 37 /*
jlamyi 11:090042660f15 38 class Watchdog {
jlamyi 11:090042660f15 39 public:
jlamyi 11:090042660f15 40 // Load timeout value in watchdog timer and enable
jlamyi 11:090042660f15 41 void kick(float s) {
jlamyi 11:090042660f15 42 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
jlamyi 11:090042660f15 43 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
jlamyi 11:090042660f15 44 LPC_WDT->WDTC = s * (float)clk;
jlamyi 11:090042660f15 45 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
jlamyi 11:090042660f15 46 kick();
jlamyi 11:090042660f15 47 }
jlamyi 11:090042660f15 48 // "kick" or "feed" the dog - reset the watchdog timer
jlamyi 11:090042660f15 49 // by writing this required bit pattern
jlamyi 11:090042660f15 50 void kick() {
jlamyi 11:090042660f15 51 LPC_WDT->WDFEED = 0xAA;
jlamyi 11:090042660f15 52 LPC_WDT->WDFEED = 0x55;
jlamyi 11:090042660f15 53 }
jlamyi 11:090042660f15 54 };
jlamyi 13:af3e8a8189ed 55 */
jlamyi 11:090042660f15 56 // Setup the watchdog timer
jlamyi 13:af3e8a8189ed 57 // Watchdog wdt;
jlamyi 11:090042660f15 58
MichaelW 0:78952cd3935b 59 int main() {
abuchan 2:2c0cd1aaae83 60 init_done = 0;
abuchan 2:2c0cd1aaae83 61 imu_good = 0;
abuchan 2:2c0cd1aaae83 62 main_loop = 0;
jlamyi 11:090042660f15 63
abuchan 2:2c0cd1aaae83 64 //Set up I2C
abuchan 2:2c0cd1aaae83 65 i2c.frequency(400000); // use fast (400 kHz) I2C
abuchan 2:2c0cd1aaae83 66
abuchan 3:8b5700499eb8 67 volatile bool imu_ready = false;
abuchan 3:8b5700499eb8 68
abuchan 3:8b5700499eb8 69 wait_ms(100);
abuchan 2:2c0cd1aaae83 70
abuchan 2:2c0cd1aaae83 71 uint8_t whoami = mpu6050.readByte(MPU6050_ADDRESS, WHO_AM_I_MPU6050);
abuchan 2:2c0cd1aaae83 72
abuchan 2:2c0cd1aaae83 73 if (whoami == 0x68) // WHO_AM_I should always be 0x68
abuchan 2:2c0cd1aaae83 74 {
abuchan 2:2c0cd1aaae83 75 mpu6050.MPU6050SelfTest(SelfTest);
abuchan 2:2c0cd1aaae83 76 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) {
abuchan 2:2c0cd1aaae83 77 mpu6050.resetMPU6050(); // Reset registers to default in preparation for device calibration
abuchan 2:2c0cd1aaae83 78 mpu6050.calibrateMPU6050(gyroBias, accelBias); // Calibrate gyro and accelerometers, load biases in bias registers
abuchan 2:2c0cd1aaae83 79 mpu6050.initMPU6050();
abuchan 2:2c0cd1aaae83 80 mpu6050.getAres();
abuchan 2:2c0cd1aaae83 81 mpu6050.getGres();
abuchan 2:2c0cd1aaae83 82 imu_ready = true;
abuchan 2:2c0cd1aaae83 83 imu_good = 1;
abuchan 2:2c0cd1aaae83 84 }
abuchan 2:2c0cd1aaae83 85 }
abuchan 2:2c0cd1aaae83 86
abuchan 2:2c0cd1aaae83 87 init_done = 1;
abuchan 3:8b5700499eb8 88 uint8_t loop_count = 10;
MichaelW 0:78952cd3935b 89 while(1) {
abuchan 3:8b5700499eb8 90 wait_ms(10);
abuchan 2:2c0cd1aaae83 91
DavidMcP555 4:b8eeb59b62d4 92 // Handle the encoders
jlamyi 8:1e47a6b49537 93 r_enc = r_wheel.getPulses();
jlamyi 8:1e47a6b49537 94 l_enc = l_wheel.getPulses();
jlamyi 11:090042660f15 95 //if(debug) SerialRPC.pc.printf("Pulses are: %i, %i\r\n", l_enc,r_enc);
DavidMcP555 4:b8eeb59b62d4 96
jlamyi 14:a153ce7cc4c8 97
abuchan 3:8b5700499eb8 98 if (!(--loop_count)) {
abuchan 3:8b5700499eb8 99 loop_count = 10;
abuchan 3:8b5700499eb8 100 main_loop = !main_loop;
abuchan 3:8b5700499eb8 101 }
abuchan 2:2c0cd1aaae83 102
abuchan 3:8b5700499eb8 103 if (imu_ready) {
abuchan 2:2c0cd1aaae83 104
abuchan 2:2c0cd1aaae83 105 if(mpu6050.readByte(MPU6050_ADDRESS, INT_STATUS) & 0x01) { // check if data ready interrupt
abuchan 2:2c0cd1aaae83 106 mpu6050.readAccelData(accelCount); // Read the x/y/z adc values
abuchan 2:2c0cd1aaae83 107 mpu6050.readGyroData(gyroCount); // Read the x/y/z adc values
abuchan 2:2c0cd1aaae83 108
abuchan 2:2c0cd1aaae83 109 // Now we'll calculate the accleration value into actual g's
jlamyi 8:1e47a6b49537 110 accel_x = (float)accelCount[0]*aRes - accelBias[0]; // get actual g value, this depends on scale being set
jlamyi 8:1e47a6b49537 111 accel_y = (float)accelCount[1]*aRes - accelBias[1];
jlamyi 8:1e47a6b49537 112 accel_z = (float)accelCount[2]*aRes - accelBias[2];
jlamyi 6:18183b8b08f1 113
abuchan 2:2c0cd1aaae83 114 // Calculate the gyro value into actual degrees per second
jlamyi 8:1e47a6b49537 115 gyro_x = (float)gyroCount[0]*gRes - gyroBias[0]; // get actual gyro value, this depends on scale being set
jlamyi 8:1e47a6b49537 116 gyro_y = (float)gyroCount[1]*gRes - gyroBias[1];
jlamyi 8:1e47a6b49537 117 gyro_z = (float)gyroCount[2]*gRes - gyroBias[2];
abuchan 2:2c0cd1aaae83 118 }
abuchan 2:2c0cd1aaae83 119 }
jlamyi 14:a153ce7cc4c8 120
jlamyi 14:a153ce7cc4c8 121 // for testing where is the bug
jlamyi 15:3cee564f9d47 122 //main_loop2 = main_loop;
jlamyi 14:a153ce7cc4c8 123
MichaelW 0:78952cd3935b 124 }
MichaelW 0:78952cd3935b 125 }
jlamyi 6:18183b8b08f1 126
jlamyi 11:090042660f15 127
jlamyi 11:090042660f15 128 // RPCFunction
jlamyi 15:3cee564f9d47 129 void GetIMUData(char* input, char* output){
jlamyi 15:3cee564f9d47 130 sprintf(output, "%f,%f,%f,%f,%f,%f", accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z);
jlamyi 15:3cee564f9d47 131 }
jlamyi 15:3cee564f9d47 132
jlamyi 15:3cee564f9d47 133 void GetEncoderData(char* input, char* output){
jlamyi 15:3cee564f9d47 134 sprintf(output, "%d,%d", l_enc, r_enc);
jlamyi 6:18183b8b08f1 135 }
jlamyi 8:1e47a6b49537 136
jlamyi 8:1e47a6b49537 137 void Reset(char* input, char* output){
jlamyi 14:a153ce7cc4c8 138 mbed_reset();
jlamyi 8:1e47a6b49537 139 }
jlamyi 11:090042660f15 140
jlamyi 13:af3e8a8189ed 141 /*
jlamyi 11:090042660f15 142 void WD_init(char* input, char* output){
jlamyi 13:af3e8a8189ed 143 //wdt.kick(0.5);
jlamyi 11:090042660f15 144 }
jlamyi 13:af3e8a8189ed 145 */