Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of tilt_angles by
main.cpp
00001 #include "mbed.h" 00002 #include <string> 00003 #include "C12832.h" 00004 #include "ENGO333_MMA7660.h" 00005 #include "ENGO333_MPU9150.h" 00006 #include "Tiltmeter.h" 00007 00008 00009 enum AccelType 00010 { 00011 MMA7660, // MMA7660 accelerometer on the mbed application board 00012 MPU9150 // MPU9150 IMU (external to mbed application board) 00013 }; 00014 00015 00016 // Function to prompt the user to select between the MMA7660 accelerometer or 00017 // the MPU9150 IMU. The user can select between these choices by moving the 00018 // joystick left or right; they make their selection by pressing the joystick 00019 // button. 00020 // 00021 // Arguments: 00022 // lcd = The LCD screen to use for display 00023 // joystick = Object linked to the joystick to be used 00024 // 00025 // Returns: 00026 // The function returns an enumeration identifying the sensor selected 00027 AccelType SelectAccelerometer( C12832& lcd, BusIn& joystick ); 00028 00029 00030 int main() 00031 { 00032 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00033 // START: Setup/variable declaration 00034 00035 00036 // Create LCD object for LCD screen operations 00037 C12832 lcd(p5, p7, p6, p8, p11); 00038 00039 // Create digital outputs for LED1 and LED2 that will be used to indicate 00040 // if things are working properly -- both values are to LOW (off) 00041 DigitalOut led1(LED1); 00042 DigitalOut led2(LED2); 00043 led1 = 0; 00044 led2 = 0; 00045 00046 // Create bus input for joystick 00047 BusIn joystick(p13, p16, p14); // Binary [ Centre | Right | Left ] joystick 00048 00049 00050 // END: Setup/variable declaration 00051 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00052 // START: Take Measurements 00053 00054 00055 // Create an accelerometer object 00056 ENGO333_MMA7660 accelerometer; 00057 00058 00059 // Detect sensor 00060 if( accelerometer.TestConnection() ) 00061 { 00062 // Turn on the first LED to show that the connection was detected 00063 led1 = 1; 00064 } 00065 else 00066 { 00067 lcd.locate(1, 11); 00068 lcd.printf("ERROR: Unknown sensor"); 00069 return 1; 00070 } 00071 00072 00073 // Prepare the screen 00074 lcd.cls(); 00075 lcd.locate(1, 1); 00076 lcd.printf("Press joystick to measure"); 00077 00078 00079 // Variable to store the value/status of the joystick. It is initialized 00080 // to represent pushing the joystick button. Since the loop below 00081 while( true ) 00082 { 00083 // Check if the joystick button is being pressed 00084 if (joystick.read() == 4) 00085 { 00086 // Turn on second LED to indicate a measurement is being made 00087 led2 = 1; 00088 00089 00090 // Get the tilt angles 00091 accelerometer.ComputeTiltAngles(); 00092 00093 00094 // Print the roll 00095 lcd.locate(1, 11); 00096 lcd.printf("Roll (deg)"); 00097 lcd.locate(50, 11); 00098 lcd.printf(": % 8.2f", accelerometer.GetRoll()); 00099 00100 // Print the pitch 00101 lcd.locate(1, 21); 00102 lcd.printf("Pitch (deg)"); 00103 lcd.locate(50, 21); 00104 lcd.printf(": % 8.2f", accelerometer.GetPitch()); 00105 00106 00107 // Short delay to avoid multiple measurements being displayed 00108 wait_ms(250); 00109 00110 00111 // Turn off second LED to indicate a measurement is complete 00112 led2 = 0; 00113 } 00114 00115 } 00116 00117 00118 // END: Take Measurements 00119 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00120 00121 } 00122 00123 00124 AccelType SelectAccelerometer( C12832& lcd, BusIn& joystick ) 00125 { 00126 // This is the current choice 00127 AccelType choice = MMA7660; 00128 00129 // This is the string to display the current choice (using asterisk) 00130 string sensorChoice = "MMA7660 * MPU9150"; 00131 00132 // Print the default choice 00133 lcd.cls(); 00134 lcd.locate(1, 1); 00135 lcd.printf("Move L/R to select sensor:"); 00136 lcd.locate(1, 18); 00137 lcd.printf("%s", sensorChoice.c_str()); 00138 00139 00140 // Create while-loop for user to select sensor 00141 while(1) 00142 { 00143 // read the joystick 00144 int joy = joystick.read(); 00145 00146 00147 if (joy == 2) // joystick pushed to the right 00148 { 00149 sensorChoice = "MMA7660 * MPU9150"; 00150 choice = MPU9150; 00151 lcd.locate(1, 18); 00152 lcd.printf("%s", sensorChoice.c_str()); 00153 } 00154 else if (joy == 1) // joystick pushed to the left 00155 { 00156 sensorChoice = "MMA7660 * MPU9150"; 00157 choice = MMA7660; 00158 lcd.locate(1, 18); 00159 lcd.printf("%s", sensorChoice.c_str()); 00160 } 00161 else if (joy == 4) // joystick "button" pushed (i.e., "select") 00162 { 00163 break; 00164 } 00165 } 00166 00167 // return the current choice 00168 return choice; 00169 }
Generated on Sat Jul 16 2022 15:16:04 by
1.7.2
