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