Framework of classes and program to measure tilt angles using accelerometers
Fork of tilt_angles by
main.cpp@0:3bffc1862262, 2016-11-24 (annotated)
- Committer:
- mpetovello
- Date:
- Thu Nov 24 23:02:42 2016 +0000
- Revision:
- 0:3bffc1862262
Lab 10 draft, 2016
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mpetovello | 0:3bffc1862262 | 1 | #include "mbed.h" |
mpetovello | 0:3bffc1862262 | 2 | #include <string> |
mpetovello | 0:3bffc1862262 | 3 | #include "C12832.h" |
mpetovello | 0:3bffc1862262 | 4 | #include "ENGO333_MMA7660.h" |
mpetovello | 0:3bffc1862262 | 5 | #include "ENGO333_MPU9150.h" |
mpetovello | 0:3bffc1862262 | 6 | #include "Tiltmeter.h" |
mpetovello | 0:3bffc1862262 | 7 | |
mpetovello | 0:3bffc1862262 | 8 | |
mpetovello | 0:3bffc1862262 | 9 | enum AccelType |
mpetovello | 0:3bffc1862262 | 10 | { |
mpetovello | 0:3bffc1862262 | 11 | MMA7660, // MMA7660 accelerometer on the mbed application board |
mpetovello | 0:3bffc1862262 | 12 | MPU9150 // MPU9150 IMU (external to mbed application board) |
mpetovello | 0:3bffc1862262 | 13 | }; |
mpetovello | 0:3bffc1862262 | 14 | |
mpetovello | 0:3bffc1862262 | 15 | |
mpetovello | 0:3bffc1862262 | 16 | // Function to prompt the user to select between the MMA7660 accelerometer or |
mpetovello | 0:3bffc1862262 | 17 | // the MPU9150 IMU. The user can select between these choices by moving the |
mpetovello | 0:3bffc1862262 | 18 | // joystick left or right; they make their selection by pressing the joystick |
mpetovello | 0:3bffc1862262 | 19 | // button. |
mpetovello | 0:3bffc1862262 | 20 | // |
mpetovello | 0:3bffc1862262 | 21 | // Arguments: |
mpetovello | 0:3bffc1862262 | 22 | // lcd = The LCD screen to use for display |
mpetovello | 0:3bffc1862262 | 23 | // joystick = Object linked to the joystick to be used |
mpetovello | 0:3bffc1862262 | 24 | // |
mpetovello | 0:3bffc1862262 | 25 | // Returns: |
mpetovello | 0:3bffc1862262 | 26 | // The function returns an enumeration identifying the sensor selected |
mpetovello | 0:3bffc1862262 | 27 | AccelType SelectAccelerometer( C12832& lcd, BusIn& joystick ); |
mpetovello | 0:3bffc1862262 | 28 | |
mpetovello | 0:3bffc1862262 | 29 | |
mpetovello | 0:3bffc1862262 | 30 | int main() |
mpetovello | 0:3bffc1862262 | 31 | { |
mpetovello | 0:3bffc1862262 | 32 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
mpetovello | 0:3bffc1862262 | 33 | // START: Setup/variable declaration |
mpetovello | 0:3bffc1862262 | 34 | |
mpetovello | 0:3bffc1862262 | 35 | |
mpetovello | 0:3bffc1862262 | 36 | // Create LCD object for LCD screen operations |
mpetovello | 0:3bffc1862262 | 37 | C12832 lcd(p5, p7, p6, p8, p11); |
mpetovello | 0:3bffc1862262 | 38 | |
mpetovello | 0:3bffc1862262 | 39 | // Create digital outputs for LED1 and LED2 that will be used to indicate |
mpetovello | 0:3bffc1862262 | 40 | // if things are working properly -- both values are to LOW (off) |
mpetovello | 0:3bffc1862262 | 41 | DigitalOut led1(LED1); |
mpetovello | 0:3bffc1862262 | 42 | DigitalOut led2(LED2); |
mpetovello | 0:3bffc1862262 | 43 | led1 = 0; |
mpetovello | 0:3bffc1862262 | 44 | led2 = 0; |
mpetovello | 0:3bffc1862262 | 45 | |
mpetovello | 0:3bffc1862262 | 46 | // Create bus input for joystick |
mpetovello | 0:3bffc1862262 | 47 | BusIn joystick(p13, p16, p14); // Binary [ Centre | Right | Left ] joystick |
mpetovello | 0:3bffc1862262 | 48 | |
mpetovello | 0:3bffc1862262 | 49 | |
mpetovello | 0:3bffc1862262 | 50 | // END: Setup/variable declaration |
mpetovello | 0:3bffc1862262 | 51 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
mpetovello | 0:3bffc1862262 | 52 | // START: Take Measurements |
mpetovello | 0:3bffc1862262 | 53 | |
mpetovello | 0:3bffc1862262 | 54 | |
mpetovello | 0:3bffc1862262 | 55 | // Create an accelerometer object |
mpetovello | 0:3bffc1862262 | 56 | ENGO333_MMA7660 accelerometer; |
mpetovello | 0:3bffc1862262 | 57 | |
mpetovello | 0:3bffc1862262 | 58 | |
mpetovello | 0:3bffc1862262 | 59 | // Detect sensor |
mpetovello | 0:3bffc1862262 | 60 | if( accelerometer.TestConnection() ) |
mpetovello | 0:3bffc1862262 | 61 | { |
mpetovello | 0:3bffc1862262 | 62 | // Turn on the first LED to show that the connection was detected |
mpetovello | 0:3bffc1862262 | 63 | led1 = 1; |
mpetovello | 0:3bffc1862262 | 64 | } |
mpetovello | 0:3bffc1862262 | 65 | else |
mpetovello | 0:3bffc1862262 | 66 | { |
mpetovello | 0:3bffc1862262 | 67 | lcd.locate(1, 11); |
mpetovello | 0:3bffc1862262 | 68 | lcd.printf("ERROR: Unknown sensor"); |
mpetovello | 0:3bffc1862262 | 69 | return 1; |
mpetovello | 0:3bffc1862262 | 70 | } |
mpetovello | 0:3bffc1862262 | 71 | |
mpetovello | 0:3bffc1862262 | 72 | |
mpetovello | 0:3bffc1862262 | 73 | // Prepare the screen |
mpetovello | 0:3bffc1862262 | 74 | lcd.cls(); |
mpetovello | 0:3bffc1862262 | 75 | lcd.locate(1, 1); |
mpetovello | 0:3bffc1862262 | 76 | lcd.printf("Press joystick to measure"); |
mpetovello | 0:3bffc1862262 | 77 | |
mpetovello | 0:3bffc1862262 | 78 | |
mpetovello | 0:3bffc1862262 | 79 | // Variable to store the value/status of the joystick. It is initialized |
mpetovello | 0:3bffc1862262 | 80 | // to represent pushing the joystick button. Since the loop below |
mpetovello | 0:3bffc1862262 | 81 | while( true ) |
mpetovello | 0:3bffc1862262 | 82 | { |
mpetovello | 0:3bffc1862262 | 83 | // Check if the joystick button is being pressed |
mpetovello | 0:3bffc1862262 | 84 | if (joystick.read() == 4) |
mpetovello | 0:3bffc1862262 | 85 | { |
mpetovello | 0:3bffc1862262 | 86 | // Turn on second LED to indicate a measurement is being made |
mpetovello | 0:3bffc1862262 | 87 | led2 = 1; |
mpetovello | 0:3bffc1862262 | 88 | |
mpetovello | 0:3bffc1862262 | 89 | |
mpetovello | 0:3bffc1862262 | 90 | // Get the tilt angles |
mpetovello | 0:3bffc1862262 | 91 | accelerometer.ComputeTiltAngles(); |
mpetovello | 0:3bffc1862262 | 92 | |
mpetovello | 0:3bffc1862262 | 93 | |
mpetovello | 0:3bffc1862262 | 94 | // Print the roll |
mpetovello | 0:3bffc1862262 | 95 | lcd.locate(1, 11); |
mpetovello | 0:3bffc1862262 | 96 | lcd.printf("Roll (deg)"); |
mpetovello | 0:3bffc1862262 | 97 | lcd.locate(50, 11); |
mpetovello | 0:3bffc1862262 | 98 | lcd.printf(": % 8.2f", accelerometer.GetRoll()); |
mpetovello | 0:3bffc1862262 | 99 | |
mpetovello | 0:3bffc1862262 | 100 | // Print the pitch |
mpetovello | 0:3bffc1862262 | 101 | lcd.locate(1, 21); |
mpetovello | 0:3bffc1862262 | 102 | lcd.printf("Pitch (deg)"); |
mpetovello | 0:3bffc1862262 | 103 | lcd.locate(50, 21); |
mpetovello | 0:3bffc1862262 | 104 | lcd.printf(": % 8.2f", accelerometer.GetPitch()); |
mpetovello | 0:3bffc1862262 | 105 | |
mpetovello | 0:3bffc1862262 | 106 | |
mpetovello | 0:3bffc1862262 | 107 | // Short delay to avoid multiple measurements being displayed |
mpetovello | 0:3bffc1862262 | 108 | wait_ms(250); |
mpetovello | 0:3bffc1862262 | 109 | |
mpetovello | 0:3bffc1862262 | 110 | |
mpetovello | 0:3bffc1862262 | 111 | // Turn off second LED to indicate a measurement is complete |
mpetovello | 0:3bffc1862262 | 112 | led2 = 0; |
mpetovello | 0:3bffc1862262 | 113 | } |
mpetovello | 0:3bffc1862262 | 114 | |
mpetovello | 0:3bffc1862262 | 115 | } |
mpetovello | 0:3bffc1862262 | 116 | |
mpetovello | 0:3bffc1862262 | 117 | |
mpetovello | 0:3bffc1862262 | 118 | // END: Take Measurements |
mpetovello | 0:3bffc1862262 | 119 | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
mpetovello | 0:3bffc1862262 | 120 | |
mpetovello | 0:3bffc1862262 | 121 | } |
mpetovello | 0:3bffc1862262 | 122 | |
mpetovello | 0:3bffc1862262 | 123 | |
mpetovello | 0:3bffc1862262 | 124 | AccelType SelectAccelerometer( C12832& lcd, BusIn& joystick ) |
mpetovello | 0:3bffc1862262 | 125 | { |
mpetovello | 0:3bffc1862262 | 126 | // This is the current choice |
mpetovello | 0:3bffc1862262 | 127 | AccelType choice = MMA7660; |
mpetovello | 0:3bffc1862262 | 128 | |
mpetovello | 0:3bffc1862262 | 129 | // This is the string to display the current choice (using asterisk) |
mpetovello | 0:3bffc1862262 | 130 | string sensorChoice = "MMA7660 * MPU9150"; |
mpetovello | 0:3bffc1862262 | 131 | |
mpetovello | 0:3bffc1862262 | 132 | // Print the default choice |
mpetovello | 0:3bffc1862262 | 133 | lcd.cls(); |
mpetovello | 0:3bffc1862262 | 134 | lcd.locate(1, 1); |
mpetovello | 0:3bffc1862262 | 135 | lcd.printf("Move L/R to select sensor:"); |
mpetovello | 0:3bffc1862262 | 136 | lcd.locate(1, 18); |
mpetovello | 0:3bffc1862262 | 137 | lcd.printf("%s", sensorChoice.c_str()); |
mpetovello | 0:3bffc1862262 | 138 | |
mpetovello | 0:3bffc1862262 | 139 | |
mpetovello | 0:3bffc1862262 | 140 | // Create while-loop for user to select sensor |
mpetovello | 0:3bffc1862262 | 141 | while(1) |
mpetovello | 0:3bffc1862262 | 142 | { |
mpetovello | 0:3bffc1862262 | 143 | // read the joystick |
mpetovello | 0:3bffc1862262 | 144 | int joy = joystick.read(); |
mpetovello | 0:3bffc1862262 | 145 | |
mpetovello | 0:3bffc1862262 | 146 | |
mpetovello | 0:3bffc1862262 | 147 | if (joy == 2) // joystick pushed to the right |
mpetovello | 0:3bffc1862262 | 148 | { |
mpetovello | 0:3bffc1862262 | 149 | sensorChoice = "MMA7660 * MPU9150"; |
mpetovello | 0:3bffc1862262 | 150 | choice = MPU9150; |
mpetovello | 0:3bffc1862262 | 151 | lcd.locate(1, 18); |
mpetovello | 0:3bffc1862262 | 152 | lcd.printf("%s", sensorChoice.c_str()); |
mpetovello | 0:3bffc1862262 | 153 | } |
mpetovello | 0:3bffc1862262 | 154 | else if (joy == 1) // joystick pushed to the left |
mpetovello | 0:3bffc1862262 | 155 | { |
mpetovello | 0:3bffc1862262 | 156 | sensorChoice = "MMA7660 * MPU9150"; |
mpetovello | 0:3bffc1862262 | 157 | choice = MMA7660; |
mpetovello | 0:3bffc1862262 | 158 | lcd.locate(1, 18); |
mpetovello | 0:3bffc1862262 | 159 | lcd.printf("%s", sensorChoice.c_str()); |
mpetovello | 0:3bffc1862262 | 160 | } |
mpetovello | 0:3bffc1862262 | 161 | else if (joy == 4) // joystick "button" pushed (i.e., "select") |
mpetovello | 0:3bffc1862262 | 162 | { |
mpetovello | 0:3bffc1862262 | 163 | break; |
mpetovello | 0:3bffc1862262 | 164 | } |
mpetovello | 0:3bffc1862262 | 165 | } |
mpetovello | 0:3bffc1862262 | 166 | |
mpetovello | 0:3bffc1862262 | 167 | // return the current choice |
mpetovello | 0:3bffc1862262 | 168 | return choice; |
mpetovello | 0:3bffc1862262 | 169 | } |