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.
Dependencies: SDFileSystem circular_buffer MPU6050 SoftSerial
source/main.cpp@1:2cc1c9a45be7, 2017-02-01 (annotated)
- Committer:
- OsmanKameric
- Date:
- Wed Feb 01 11:10:16 2017 +0000
- Revision:
- 1:2cc1c9a45be7
- Parent:
- 0:a4de55cab4e2
- Child:
- 3:b2087af18efe
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
OsmanKameric | 0:a4de55cab4e2 | 1 | #include <events/mbed_events.h> |
OsmanKameric | 0:a4de55cab4e2 | 2 | #include <mbed.h> |
OsmanKameric | 0:a4de55cab4e2 | 3 | #include "ble/BLE.h" |
OsmanKameric | 0:a4de55cab4e2 | 4 | #include "ble/Gap.h" |
OsmanKameric | 0:a4de55cab4e2 | 5 | #include "ble/services/BatteryService.h" |
OsmanKameric | 0:a4de55cab4e2 | 6 | #include "MPU6050.h" |
OsmanKameric | 0:a4de55cab4e2 | 7 | #include "MPUService.h" |
OsmanKameric | 1:2cc1c9a45be7 | 8 | #include <string> |
OsmanKameric | 1:2cc1c9a45be7 | 9 | #include <stdio.h> |
OsmanKameric | 1:2cc1c9a45be7 | 10 | Serial pc(p30, p31); |
OsmanKameric | 0:a4de55cab4e2 | 11 | MPU6050 mpu6050; |
OsmanKameric | 1:2cc1c9a45be7 | 12 | |
OsmanKameric | 1:2cc1c9a45be7 | 13 | InterruptIn pin(p10); |
OsmanKameric | 1:2cc1c9a45be7 | 14 | |
OsmanKameric | 1:2cc1c9a45be7 | 15 | |
OsmanKameric | 1:2cc1c9a45be7 | 16 | DigitalOut led(LED1, 1); |
OsmanKameric | 1:2cc1c9a45be7 | 17 | DigitalOut led2(LED2, 1); |
OsmanKameric | 1:2cc1c9a45be7 | 18 | uint16_t customServiceUUID = 0xA000; |
OsmanKameric | 1:2cc1c9a45be7 | 19 | uint16_t readCharUUID = 0xA001; |
OsmanKameric | 1:2cc1c9a45be7 | 20 | uint16_t writeCharUUID = 0xA002; |
OsmanKameric | 1:2cc1c9a45be7 | 21 | DigitalOut alivenessLED(LED1, 0); |
OsmanKameric | 0:a4de55cab4e2 | 22 | const static char DEVICE_NAME[] = "MPU"; |
OsmanKameric | 1:2cc1c9a45be7 | 23 | static const uint16_t uuid16_list[] = {0x0A0A}; |
OsmanKameric | 1:2cc1c9a45be7 | 24 | static int counter; |
OsmanKameric | 1:2cc1c9a45be7 | 25 | /* Set Up custom Characteristics */ |
OsmanKameric | 1:2cc1c9a45be7 | 26 | static float readValue[3] = {0}; |
OsmanKameric | 1:2cc1c9a45be7 | 27 | ReadOnlyArrayGattCharacteristic<float, sizeof(readValue)> readChar(readCharUUID, readValue, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); |
OsmanKameric | 1:2cc1c9a45be7 | 28 | |
OsmanKameric | 1:2cc1c9a45be7 | 29 | //static float writeValue[2] = {3,4}; |
OsmanKameric | 1:2cc1c9a45be7 | 30 | //WriteOnlyArrayGattCharacteristic<float, sizeof(writeValue)> writeChar(writeCharUUID, writeValue); |
OsmanKameric | 1:2cc1c9a45be7 | 31 | |
OsmanKameric | 1:2cc1c9a45be7 | 32 | /* Set up custom service */ |
OsmanKameric | 1:2cc1c9a45be7 | 33 | GattCharacteristic *characteristics[] = {&readChar/*, &writeChar*/}; |
OsmanKameric | 1:2cc1c9a45be7 | 34 | GattService customService(customServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); |
OsmanKameric | 1:2cc1c9a45be7 | 35 | |
OsmanKameric | 1:2cc1c9a45be7 | 36 | |
OsmanKameric | 1:2cc1c9a45be7 | 37 | |
OsmanKameric | 0:a4de55cab4e2 | 38 | static EventQueue eventQueue(/* event count */ 10 * /* event size */ 32); |
OsmanKameric | 0:a4de55cab4e2 | 39 | //InterruptIn button(BUTTON1); |
OsmanKameric | 1:2cc1c9a45be7 | 40 | void updateReadValue() { |
OsmanKameric | 1:2cc1c9a45be7 | 41 | alivenessLED = !alivenessLED; |
OsmanKameric | 1:2cc1c9a45be7 | 42 | |
OsmanKameric | 1:2cc1c9a45be7 | 43 | //mpu6050.readAccelData(accelData); |
OsmanKameric | 1:2cc1c9a45be7 | 44 | //mpu6050.getAres(); |
OsmanKameric | 1:2cc1c9a45be7 | 45 | //ax = accelData[0] * aRes - accelBias[0]; |
OsmanKameric | 1:2cc1c9a45be7 | 46 | //ay = accelData[1] * aRes - accelBias[1]; |
OsmanKameric | 1:2cc1c9a45be7 | 47 | //az = accelData[2] * aRes - accelBias[2]; |
OsmanKameric | 1:2cc1c9a45be7 | 48 | float newValue[3] = {ax,ay,az}; |
OsmanKameric | 1:2cc1c9a45be7 | 49 | //mpu6050.readGyroData(gyroData); |
OsmanKameric | 1:2cc1c9a45be7 | 50 | //mpu6050.getGres(); |
OsmanKameric | 1:2cc1c9a45be7 | 51 | |
OsmanKameric | 1:2cc1c9a45be7 | 52 | //gx = gyroData[0] * gRes; // - gyroBias[0]; |
OsmanKameric | 1:2cc1c9a45be7 | 53 | //gy = gyroData[1] * gRes; // - gyroBias[1]; |
OsmanKameric | 1:2cc1c9a45be7 | 54 | //gz = gyroData[2] * gRes; // - gyroBias[2]; |
OsmanKameric | 1:2cc1c9a45be7 | 55 | //float newValue[3] = {gx,gy,gz};*/ |
OsmanKameric | 1:2cc1c9a45be7 | 56 | //counter++; |
OsmanKameric | 1:2cc1c9a45be7 | 57 | //pc.printf("| Before | ax=%.3f | ay=%.3f | az=%.3f \r\n",gx,gy,gz); |
OsmanKameric | 1:2cc1c9a45be7 | 58 | //pc.printf("| Acce | ax=%.3f | ay=%.3f | az=%.3f | Gyro | gx=%.3f | gy=%.3f | gz=%.3f\r\n",ax,ay,az,gx,gy,gz); |
OsmanKameric | 1:2cc1c9a45be7 | 59 | //len_send = strlen((const char *)newValue); |
OsmanKameric | 1:2cc1c9a45be7 | 60 | BLE::Instance(BLE::DEFAULT_INSTANCE).gattServer().write(readChar.getValueHandle(), (uint8_t*)newValue, 12); |
OsmanKameric | 1:2cc1c9a45be7 | 61 | } |
OsmanKameric | 1:2cc1c9a45be7 | 62 | void periodicCallback(void) |
OsmanKameric | 1:2cc1c9a45be7 | 63 | { |
OsmanKameric | 1:2cc1c9a45be7 | 64 | if (BLE::Instance().getGapState().connected) { |
OsmanKameric | 1:2cc1c9a45be7 | 65 | eventQueue.call(updateReadValue); |
OsmanKameric | 1:2cc1c9a45be7 | 66 | } |
OsmanKameric | 1:2cc1c9a45be7 | 67 | } |
OsmanKameric | 0:a4de55cab4e2 | 68 | |
OsmanKameric | 0:a4de55cab4e2 | 69 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) |
OsmanKameric | 0:a4de55cab4e2 | 70 | { |
OsmanKameric | 0:a4de55cab4e2 | 71 | BLE& ble = BLE::Instance(); |
OsmanKameric | 0:a4de55cab4e2 | 72 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
OsmanKameric | 0:a4de55cab4e2 | 73 | } |
OsmanKameric | 0:a4de55cab4e2 | 74 | |
OsmanKameric | 0:a4de55cab4e2 | 75 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
OsmanKameric | 0:a4de55cab4e2 | 76 | { |
OsmanKameric | 0:a4de55cab4e2 | 77 | (void) params; |
OsmanKameric | 0:a4de55cab4e2 | 78 | BLE::Instance().gap().startAdvertising(); |
OsmanKameric | 0:a4de55cab4e2 | 79 | } |
OsmanKameric | 0:a4de55cab4e2 | 80 | void onBleInitError(BLE &ble, ble_error_t error) |
OsmanKameric | 0:a4de55cab4e2 | 81 | { |
OsmanKameric | 0:a4de55cab4e2 | 82 | /* Initialization error handling should go here */ |
OsmanKameric | 0:a4de55cab4e2 | 83 | } |
OsmanKameric | 1:2cc1c9a45be7 | 84 | |
OsmanKameric | 1:2cc1c9a45be7 | 85 | |
OsmanKameric | 0:a4de55cab4e2 | 86 | void bleInitComplete(BLE::InitializationCompleteCallbackContext* params) |
OsmanKameric | 0:a4de55cab4e2 | 87 | { |
OsmanKameric | 0:a4de55cab4e2 | 88 | BLE& ble = params->ble; |
OsmanKameric | 0:a4de55cab4e2 | 89 | ble_error_t error = params->error; |
OsmanKameric | 0:a4de55cab4e2 | 90 | |
OsmanKameric | 0:a4de55cab4e2 | 91 | if (error != BLE_ERROR_NONE) { |
OsmanKameric | 0:a4de55cab4e2 | 92 | /* In case of error, forward the error handling to onBleInitError */ |
OsmanKameric | 0:a4de55cab4e2 | 93 | onBleInitError(ble, error); |
OsmanKameric | 0:a4de55cab4e2 | 94 | return; |
OsmanKameric | 0:a4de55cab4e2 | 95 | } |
OsmanKameric | 0:a4de55cab4e2 | 96 | |
OsmanKameric | 0:a4de55cab4e2 | 97 | /* Ensure that it is the default instance of BLE */ |
OsmanKameric | 0:a4de55cab4e2 | 98 | if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
OsmanKameric | 0:a4de55cab4e2 | 99 | return; |
OsmanKameric | 0:a4de55cab4e2 | 100 | } |
OsmanKameric | 0:a4de55cab4e2 | 101 | ble.gap().onDisconnection(disconnectionCallback); |
OsmanKameric | 0:a4de55cab4e2 | 102 | |
OsmanKameric | 0:a4de55cab4e2 | 103 | /* setup advertising */ |
OsmanKameric | 0:a4de55cab4e2 | 104 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
OsmanKameric | 0:a4de55cab4e2 | 105 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); |
OsmanKameric | 0:a4de55cab4e2 | 106 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t*)DEVICE_NAME, sizeof(DEVICE_NAME)); |
OsmanKameric | 0:a4de55cab4e2 | 107 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
OsmanKameric | 1:2cc1c9a45be7 | 108 | |
OsmanKameric | 1:2cc1c9a45be7 | 109 | ble.gap().startAdvertising(); |
OsmanKameric | 1:2cc1c9a45be7 | 110 | ble.addService(customService); |
OsmanKameric | 0:a4de55cab4e2 | 111 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
OsmanKameric | 0:a4de55cab4e2 | 112 | |
OsmanKameric | 0:a4de55cab4e2 | 113 | |
OsmanKameric | 0:a4de55cab4e2 | 114 | } |
OsmanKameric | 1:2cc1c9a45be7 | 115 | void flip() { |
OsmanKameric | 1:2cc1c9a45be7 | 116 | pc.printf("Desio se interupt"); |
OsmanKameric | 1:2cc1c9a45be7 | 117 | } |
OsmanKameric | 1:2cc1c9a45be7 | 118 | bool XnegMD, XposMD, YnegMD, YposMD, ZnegMD, ZposMD; |
OsmanKameric | 0:a4de55cab4e2 | 119 | int main() |
OsmanKameric | 1:2cc1c9a45be7 | 120 | { |
OsmanKameric | 1:2cc1c9a45be7 | 121 | //mpu6050.whoAmI(); |
OsmanKameric | 1:2cc1c9a45be7 | 122 | //mpu6050.calibrate(accelBias, gyroBias); |
OsmanKameric | 1:2cc1c9a45be7 | 123 | //mpu6050.init(); |
OsmanKameric | 1:2cc1c9a45be7 | 124 | |
OsmanKameric | 1:2cc1c9a45be7 | 125 | |
OsmanKameric | 1:2cc1c9a45be7 | 126 | //setMotionDetectionThreshold |
OsmanKameric | 1:2cc1c9a45be7 | 127 | //char myChar = mpu6050.readByte(MPU6050_ADDRESS, 0x1F); |
OsmanKameric | 1:2cc1c9a45be7 | 128 | //pc.printf("Prije postavljanja\n"); |
OsmanKameric | 1:2cc1c9a45be7 | 129 | //pc.printf("MotionDetectionThreshold iznosi: %x\n",myChar); |
OsmanKameric | 1:2cc1c9a45be7 | 130 | //pc.printf("Poslije postavljanja\n"); |
OsmanKameric | 1:2cc1c9a45be7 | 131 | //mpu6050.writeByte(MPU6050_ADDRESS, 0x1F,0x02); |
OsmanKameric | 1:2cc1c9a45be7 | 132 | //myChar = mpu6050.readByte(MPU6050_ADDRESS, 0x1F); |
OsmanKameric | 1:2cc1c9a45be7 | 133 | //pc.printf("MotionDetectionThreshold iznosi: %x\n",myChar); |
OsmanKameric | 1:2cc1c9a45be7 | 134 | |
OsmanKameric | 1:2cc1c9a45be7 | 135 | |
OsmanKameric | 1:2cc1c9a45be7 | 136 | //setMotionDetectionDuration |
OsmanKameric | 1:2cc1c9a45be7 | 137 | //myChar = mpu6050.readByte(MPU6050_ADDRESS, 0x20); |
OsmanKameric | 1:2cc1c9a45be7 | 138 | //pc.printf("Prije postavljanja\n"); |
OsmanKameric | 1:2cc1c9a45be7 | 139 | //pc.printf("MotionDetectionDuration iznosi: %x\n",myChar); |
OsmanKameric | 1:2cc1c9a45be7 | 140 | //pc.printf("Poslije postavljanja\n"); |
OsmanKameric | 1:2cc1c9a45be7 | 141 | //mpu6050.writeByte(MPU6050_ADDRESS, 0x20,0x28); |
OsmanKameric | 1:2cc1c9a45be7 | 142 | //myChar = mpu6050.readByte(MPU6050_ADDRESS, 0x20); |
OsmanKameric | 1:2cc1c9a45be7 | 143 | //pc.printf("MotionDetectionDuration iznosi: %x\n",myChar); |
OsmanKameric | 1:2cc1c9a45be7 | 144 | |
OsmanKameric | 1:2cc1c9a45be7 | 145 | // XnegMD = mpu6050.getXNegMotionDetected(); |
OsmanKameric | 1:2cc1c9a45be7 | 146 | //mpu6050.whoAmI(); |
OsmanKameric | 1:2cc1c9a45be7 | 147 | //uint8_t *data; |
OsmanKameric | 1:2cc1c9a45be7 | 148 | //char byte = mpu6050.readByte(MPU6050_ADDRESS, 0x61); |
OsmanKameric | 1:2cc1c9a45be7 | 149 | //*data = byte & (1 << 7); |
OsmanKameric | 1:2cc1c9a45be7 | 150 | //pc.printf("Byte iznosi: %x\n",byte); |
OsmanKameric | 1:2cc1c9a45be7 | 151 | //int i = data[0]; |
OsmanKameric | 1:2cc1c9a45be7 | 152 | //pc.printf("7 bit iznosi: %d\n", i); |
OsmanKameric | 1:2cc1c9a45be7 | 153 | |
OsmanKameric | 1:2cc1c9a45be7 | 154 | //XposMD = mpu6050.getXPosMotionDetected(); |
OsmanKameric | 1:2cc1c9a45be7 | 155 | |
OsmanKameric | 1:2cc1c9a45be7 | 156 | |
OsmanKameric | 1:2cc1c9a45be7 | 157 | //pin.fall(&flip); |
OsmanKameric | 0:a4de55cab4e2 | 158 | eventQueue.call_every(500, periodicCallback); |
OsmanKameric | 0:a4de55cab4e2 | 159 | BLE& ble = BLE::Instance(); |
OsmanKameric | 0:a4de55cab4e2 | 160 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
OsmanKameric | 0:a4de55cab4e2 | 161 | ble.init(bleInitComplete); |
OsmanKameric | 0:a4de55cab4e2 | 162 | eventQueue.dispatch_forever(); |
OsmanKameric | 0:a4de55cab4e2 | 163 | return 0; |
OsmanKameric | 0:a4de55cab4e2 | 164 | } |