RaheeNew nonblocking

Dependencies:   RaheeNew LinkedList mbed-rpc mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "mbed_rpc.h"
00004 #include "string.h"
00005 #define _MBED_
00006 #include "Adafruit_9DOF.h"
00007 #include "Serial_base.h"
00008  
00009 Serial pc(USBTX, USBRX); // tx, rx
00010 Serial bluetooth(p9, p10);  // tx, rx
00011 Ticker compassTicker;
00012 float compassValue;
00013 float *ax, *ay, *az, *mx, *my, *mz;
00014 
00015 DigitalOut led1(LED1), led2(LED2), led3(LED3);
00016  
00017 //Use the RPC enabled wrapped class  - see RpcClasses.h for more info
00018 RpcDigitalOut leftTop(p20,"leftTop");
00019 RpcDigitalOut leftBottom(p12,"leftBottom");
00020 RpcDigitalOut rightTop(p22,"rightTop");
00021 RpcDigitalOut rightBottom(p29,"rightBottom");
00022  
00023 char buf[256], outbuf[256];
00024 
00025 bool compassTriggered = false;
00026 bool bluetoothTriggered = false;
00027 
00028 
00029 
00030 
00031 /* Assign a unique ID to the sensors */
00032 
00033 Adafruit_9DOF                 dof   = Adafruit_9DOF();
00034 Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
00035 Adafruit_LSM303_Mag_Unified   mag   = Adafruit_LSM303_Mag_Unified(30302);
00036 /* Update this with the correct SLP for accurate altitude measurements */
00037 float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA;
00038 /**************************************************************************
00039     @brief  Initialises all the sensors used by this example
00040 **************************************************************************/
00041 
00042 void initSensors(){
00043   if(!accel.begin()){
00044    /* There was a problem detecting the LSM303 ... check your connections */
00045     s_com->println(("Ooops, no LSM303 detected ... Check your wiring!"));
00046     while(1);
00047   }
00048   
00049   if(!mag.begin()){
00050     /* There was a problem detecting the LSM303 ... check your connections */
00051     s_com->println("Ooops, no LSM303 detected ... Check your wiring!");
00052     while(1);
00053   }
00054 }
00055 
00056 void setup(void){
00057     s_com->println(("Adafruit 9 DOF Pitch/Roll/Heading Example"));
00058     s_com->println("");
00059     
00060     /* Initialise the sensors */
00061     initSensors();
00062 }
00063 
00064 
00065 
00066 
00067  
00068 void compassTickerCallback() {
00069     compassTriggered = true;
00070 }
00071 
00072 void bluetoothCallback() {
00073     bluetoothTriggered = true;
00074 }
00075 
00076 
00077 
00078 void compassTickerHandler() {
00079     led1 = 1;
00080 
00081     sensors_event_t accel_event;
00082     sensors_event_t mag_event;
00083     sensors_vec_t   orientation;
00084 
00085     /* Calculate pitch and roll from the raw accelerometer data */
00086     accel.getEvent(&accel_event);
00087     if (dof.accelGetOrientation(&accel_event, &orientation)){
00088         /* 'orientation' should have valid .roll and .pitch fields */
00089         s_com->print(("Roll: "));
00090         s_com->print(orientation.roll);
00091         s_com->print(("; "));
00092         s_com->print(("Pitch: "));
00093         s_com->print(orientation.pitch);
00094         s_com->print(("; "));
00095     }
00096 
00097     /* Calculate the heading using the magnetometer */
00098     mag.getEvent(&mag_event);
00099     if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation)){
00100         /* 'orientation' should have valid .heading data now */
00101         s_com->print(("Heading: "));
00102         s_com->print(orientation.heading);
00103         s_com->print(("; "));
00104     }
00105     
00106     s_com->println((""));
00107 
00108     led1 = 0;
00109     compassTriggered =  false;
00110 }
00111 
00112 void bluetoothHandler() {
00113     bluetooth.gets(buf, 256);
00114     //pc.puts(buf);
00115     //Call the static call method on the RPC class
00116     RPC::call(buf, outbuf); 
00117     //bluetooth.printf("%s\n", outbuf);
00118     bluetoothTriggered = false;
00119 }
00120 
00121 
00122 int main() {
00123     
00124     pc.baud(115200);
00125     bluetooth.baud(115200);
00126     
00127     setup();
00128     compassTicker.attach(&compassTickerCallback, 5); // the address of the function to be attached (flip) and the interval (2 seconds)
00129     
00130     while(1) {
00131         if(pc.readable()) {
00132             pc.putc(pc.getc());
00133         }
00134         if(bluetooth.readable()) {
00135             bluetooth.gets(buf, 256);
00136             RPC::call(buf, outbuf); 
00137         }
00138         if(compassTriggered){
00139             compassTickerHandler();
00140         }
00141 
00142 
00143     }
00144 }