Broadcasts raw accelerometer values

Dependencies:   BLE_API MMA8452_tag_private mbed nRF51822

Fork of tag_final by Luis Bañuelos Chacon

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "BLEDevice.h"
00003 #include "MMA8452.h"
00004 
00005 // Configuration
00006 #define BLE_ADV_INTERVAL    1000        // Interval between each advertising packet (in MS)
00007 #define ACC_RATE            8          // Accelerometer sampling rate (in Hz)
00008 #define ACC_BUFFER_SIZE     8          // Accelerometer history size (DON'T CHANGE)
00009 
00010 // Objects
00011 BLEDevice   ble;                        // BLE
00012 DigitalOut  led(P0_2);                  // LED (Active Low)
00013 InterruptIn btn(P0_3);                  // Button Interrupt (Active Low)
00014 MMA8452     acc(P0_22, P0_20, 100000);  // Accelerometer
00015 Ticker      acc_tick;                   // Ticker for the accelerometer
00016 Ticker      led_tick;                   // Ticker for led
00017 Ticker      alg_tick;                   // Ticker for algorithm
00018 
00019 // Data
00020 struct acc_ring {
00021     double data[3][ACC_BUFFER_SIZE];
00022     int8_t data_int[3][ACC_BUFFER_SIZE];
00023     int head;
00024 } acc_ring;
00025 
00026 // Prototypes
00027 void onError();
00028 void onAccTick();
00029 void onButton();
00030 void onLedTick();
00031 void ledBlink(int, float);
00032 void ledStop();
00033 double getAccLast(int);
00034 double getAccMean(int);
00035 void setPayload(uint8_t*, uint8_t);
00036 
00037 // Events
00038 void onError() {
00039     led = 1;
00040     wait(0.25);
00041     led = 0;
00042     wait(0.25);
00043 }
00044 
00045 void onAccTick() {
00046     // Read accelerometer
00047     acc.readXGravity(&acc_ring.data[0][acc_ring.head]);
00048     acc.readYGravity(&acc_ring.data[1][acc_ring.head]);
00049     acc.readZGravity(&acc_ring.data[2][acc_ring.head]);
00050     
00051     // Convert to integer
00052     acc_ring.data_int[0][acc_ring.head] = (int8_t)(acc_ring.data[0][acc_ring.head] * 100);
00053     acc_ring.data_int[1][acc_ring.head] = (int8_t)(acc_ring.data[1][acc_ring.head] * 100);
00054     acc_ring.data_int[2][acc_ring.head] = (int8_t)(acc_ring.data[2][acc_ring.head] * 100);
00055     
00056     // Increment head
00057     acc_ring.head++;
00058     if (acc_ring.head >= ACC_BUFFER_SIZE) {
00059         acc_ring.head = 0;
00060         setPayload((uint8_t*)acc_ring.data_int, sizeof(acc_ring.data_int));
00061     }
00062 }
00063 
00064 void onButton() {
00065     ledBlink(3, 1);
00066 }
00067 
00068 int led_blink;
00069 void onLedTick() {
00070     led = !led;         // Invert LED state
00071     
00072     if (led == 1)       // If led was turned off
00073         led_blink--;    // Decrement blink counter
00074         
00075     if (led_blink == 0)
00076         led_tick.detach();
00077 }
00078 
00079 // Functions
00080 void setPayload(uint8_t * data, uint8_t size) {
00081     ble.clearScanResponse();
00082     ble.accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, data, size);
00083     ble.setAdvertisingPayload();
00084 }
00085 
00086 void ledBlink(int num, float period) {
00087     led = 0;
00088     led_blink = num;
00089     led_tick.attach(&onLedTick, period/2);
00090 }
00091 
00092 void ledStop() {
00093     led_blink = 0;
00094     led_tick.detach();
00095 }
00096 
00097 double getAccMean(int axis) {
00098     double mean = 0;
00099     for (int i = 0; i < ACC_BUFFER_SIZE; i++) {
00100         mean += acc_ring.data[axis][i];
00101     }
00102     return (mean / (float)ACC_BUFFER_SIZE);
00103 }
00104 
00105 
00106 int main(void) {
00107     // Initialize LED and Button
00108     led = 1;
00109     btn.mode(PullUp);
00110     btn.fall(&onButton);
00111     
00112     // Initialize BLE
00113     uint8_t tagAddress[6];
00114     uint8_t tagName[8];
00115     ble.init();                                             // Initialize BLE stack
00116     ble.setTxPower(4);                                      // Set power level (in dB)
00117     ble.setAddress(Gap::ADDR_TYPE_RANDOM_STATIC, NULL);     // Static random address
00118     ble.getAddress(NULL, tagAddress);                       // Get random address from stack
00119     sprintf((char*)tagName, "TAG_%2X%2X", tagAddress[1], tagAddress[0]);
00120     ble.accumulateAdvertisingPayload(                       // Advertise as BLE
00121         GapAdvertisingData::BREDR_NOT_SUPPORTED |
00122         GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00123     ble.accumulateAdvertisingPayload(                       // Set name
00124         GapAdvertisingData::COMPLETE_LOCAL_NAME,
00125         tagName,
00126         sizeof(tagName));
00127     ble.setAdvertisingInterval((uint16_t)((float)BLE_ADV_INTERVAL / 0.625));    // Advertising interval
00128     ble.startAdvertising();                                 // Start advertising
00129     
00130     // Initialize accelerometer
00131     char acc_id;
00132     acc.getDeviceID(&acc_id);                               // Check if accelerometer succesfully comunicates
00133     if (acc_id != 0x2A) onError();
00134     acc.standby();                                          // Put into standby mode before configuration
00135     acc.setDataRate(acc.RATE_12_5);                         // Set hardware data rate to 12.5Hz
00136     acc.setDynamicRange(acc.DYNAMIC_RANGE_2G);              // Set dynamic range up to 2G
00137     acc.setBitDepth(acc.BIT_DEPTH_12);                      // Set bit depth to 12bits for resolution
00138     acc.activate();                                         // Activate accelerometer
00139     acc_tick.attach(&onAccTick, (1.0 / (float)ACC_RATE));   // Setup periodic reads
00140     
00141     while (1) {
00142         ble.waitForEvent();
00143     }
00144 }