
For VNG botwar
Dependencies: BLE_API KalmanFilter mbed nRF51822
Revision 0:4a92d46bc788, committed 2016-08-02
- Comitter:
- longdh
- Date:
- Tue Aug 02 18:07:41 2016 +0000
- Commit message:
- First commit
Changed in this revision
diff -r 000000000000 -r 4a92d46bc788 BLE_API.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BLE_API.lib Tue Aug 02 18:07:41 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#66159681aa21
diff -r 000000000000 -r 4a92d46bc788 KalmanFilter.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/KalmanFilter.lib Tue Aug 02 18:07:41 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/longdh/code/KalmanFilter/#dd8a94919e2e
diff -r 000000000000 -r 4a92d46bc788 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Aug 02 18:07:41 2016 +0000 @@ -0,0 +1,79 @@ +#include "mbed.h" +#include "ble/BLE.h" +#include "KamalFilterRSSI.h" + +// q = very small, r= small, p=1 +static KamalFilterRSSI kalman(0.00001, 0.1, 1); + +// center beacons +static BLEProtocol::AddressBytes_t vngCenterBeacon = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +// distence to center +static int vngBeaconPower = 0x56; +static volatile double distanceToCenter = -1; + +static double calculateAccuracy(int txPower, double rssi) { + if (rssi == 0) { + return -1.0; // if we cannot determine accuracy, return -1. + } + + double ratio = rssi*1.0/txPower; + if (ratio < 1.0) { + return pow(ratio,10); + } + else { + double accuracy = (0.89976)* pow(ratio,7.7095) + 0.111; + return accuracy; + } +} + +void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { + + // if vng_beacons, filter data + if (0 == memcmp(params->peerAddr, vngCenterBeacon, sizeof (vngCenterBeacon))) { + double filterdRSSI = kalman.kalmanUpdate(params->rssi); + distanceToCenter = calculateAccuracy(vngBeaconPower, filterdRSSI); + } +} + +/** + * This function is called when the ble initialization process has failed + */ +void onBleInitError(BLE &ble, ble_error_t error) +{ + /* Initialization error handling should go here */ +} + +/** + * Callback triggered when the ble initialization process has finished + */ +void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) +{ + BLE& ble = params->ble; + ble_error_t error = params->error; + + if (error != BLE_ERROR_NONE) { + /* In case of error, forward the error handling to onBleInitError */ + onBleInitError(ble, error); + return; + } + + /* Ensure that it is the default instance of BLE */ + if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { + return; + } + + ble.gap().setScanParams(100 /* scan interval */, 100 /* scan window */); + ble.gap().startScan(advertisementCallback); +} + +int main(void) +{ + + BLE &ble = BLE::Instance(); + ble.init(bleInitComplete); + + while (true) { + ble.waitForEvent(); + } +}
diff -r 000000000000 -r 4a92d46bc788 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Aug 02 18:07:41 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/f9eeca106725 \ No newline at end of file
diff -r 000000000000 -r 4a92d46bc788 nRF51822.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nRF51822.lib Tue Aug 02 18:07:41 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#f7faad332abc