Speed sensor for tasdevelop

Dependencies:   BLE_API MMA8451Q_ss mbed

Fork of FRDM_MMA8451Q by Daniel K

Committer:
tasdevelop
Date:
Tue Mar 24 02:49:27 2015 +0000
Revision:
6:fbce7ee68c07
Parent:
5:973da26ee266
correct Floating-point calculation.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Daniel90 0:fa7bb836444d 1 #include "mbed.h"
Daniel90 0:fa7bb836444d 2 #include "MMA8451Q.h"
suzukimitsuhiro 1:6bd72ba7fa56 3 #include "BLEDevice.h"
Daniel90 0:fa7bb836444d 4
suzukimitsuhiro 1:6bd72ba7fa56 5 #define MMA8451_I2C_ADDRESS (0x1C<<1)
suzukimitsuhiro 1:6bd72ba7fa56 6
suzukimitsuhiro 2:9d566ea4f4fa 7 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
suzukimitsuhiro 1:6bd72ba7fa56 8 * it will have an impact on code-size and power consumption. */
suzukimitsuhiro 1:6bd72ba7fa56 9
suzukimitsuhiro 1:6bd72ba7fa56 10 #if NEED_CONSOLE_OUTPUT
suzukimitsuhiro 1:6bd72ba7fa56 11 Serial pc(USBTX, USBRX);
suzukimitsuhiro 1:6bd72ba7fa56 12 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
Daniel90 0:fa7bb836444d 13 #else
suzukimitsuhiro 1:6bd72ba7fa56 14 #define DEBUG(...) /* nothing */
suzukimitsuhiro 1:6bd72ba7fa56 15 #endif /* #if NEED_CONSOLE_OUTPUT */
suzukimitsuhiro 1:6bd72ba7fa56 16
suzukimitsuhiro 1:6bd72ba7fa56 17 PinName const SDA = p22;
suzukimitsuhiro 1:6bd72ba7fa56 18 PinName const SCL = p20;
suzukimitsuhiro 1:6bd72ba7fa56 19 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
suzukimitsuhiro 1:6bd72ba7fa56 20
suzukimitsuhiro 1:6bd72ba7fa56 21 //Serial pc(USBTX, USBRX);
suzukimitsuhiro 1:6bd72ba7fa56 22 Ticker ticker;
suzukimitsuhiro 1:6bd72ba7fa56 23 Ticker ticker2;
suzukimitsuhiro 1:6bd72ba7fa56 24
suzukimitsuhiro 1:6bd72ba7fa56 25 InterruptIn button(p1);
suzukimitsuhiro 1:6bd72ba7fa56 26
suzukimitsuhiro 1:6bd72ba7fa56 27 const static char DEVICE_NAME[] = "HRM1017_SPEED";
suzukimitsuhiro 1:6bd72ba7fa56 28 static volatile bool triggerSensorPolling = false;
suzukimitsuhiro 1:6bd72ba7fa56 29 BLEDevice ble;
suzukimitsuhiro 1:6bd72ba7fa56 30
suzukimitsuhiro 1:6bd72ba7fa56 31 /* Health Thermometer Service */
suzukimitsuhiro 1:6bd72ba7fa56 32 uint8_t thermTempPayload[5] = { 0, 0, 0, 0, 0 };
suzukimitsuhiro 1:6bd72ba7fa56 33 GattCharacteristic tempChar (GattCharacteristic::UUID_TEMPERATURE_MEASUREMENT_CHAR, thermTempPayload, 5, 5, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_INDICATE);
suzukimitsuhiro 1:6bd72ba7fa56 34 GattCharacteristic *htmChars[] = {&tempChar, };
suzukimitsuhiro 1:6bd72ba7fa56 35 GattService htmService(GattService::UUID_HEALTH_THERMOMETER_SERVICE, htmChars, sizeof(htmChars) / sizeof(GattCharacteristic *));
suzukimitsuhiro 1:6bd72ba7fa56 36 uint16_t uuid16_list[] = {GattService::UUID_HEALTH_THERMOMETER_SERVICE};
suzukimitsuhiro 1:6bd72ba7fa56 37
suzukimitsuhiro 1:6bd72ba7fa56 38 static Gap::ConnectionParams_t connectionParams;
suzukimitsuhiro 1:6bd72ba7fa56 39
suzukimitsuhiro 1:6bd72ba7fa56 40 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) // Mod
suzukimitsuhiro 1:6bd72ba7fa56 41 {
suzukimitsuhiro 1:6bd72ba7fa56 42 DEBUG("Disconnected handle %u, reason %u\n", handle, reason);
suzukimitsuhiro 1:6bd72ba7fa56 43 DEBUG("Restarting the advertising process\n\r");
suzukimitsuhiro 1:6bd72ba7fa56 44 ble.startAdvertising();
suzukimitsuhiro 1:6bd72ba7fa56 45 }
suzukimitsuhiro 1:6bd72ba7fa56 46
suzukimitsuhiro 1:6bd72ba7fa56 47 void onConnectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *params) //Mod
suzukimitsuhiro 1:6bd72ba7fa56 48 {
suzukimitsuhiro 1:6bd72ba7fa56 49 DEBUG("connected. Got handle %u\r\n", handle);
suzukimitsuhiro 1:6bd72ba7fa56 50 connectionParams.slaveLatency = 1;
suzukimitsuhiro 1:6bd72ba7fa56 51 if (ble.updateConnectionParams(handle, &connectionParams) != BLE_ERROR_NONE) {
suzukimitsuhiro 1:6bd72ba7fa56 52 DEBUG("failed to update connection paramter\r\n");
suzukimitsuhiro 1:6bd72ba7fa56 53 }
suzukimitsuhiro 1:6bd72ba7fa56 54 }
suzukimitsuhiro 1:6bd72ba7fa56 55 void GetAccelterData( void );
suzukimitsuhiro 1:6bd72ba7fa56 56 void periodicCallback(void)
suzukimitsuhiro 1:6bd72ba7fa56 57 {
suzukimitsuhiro 1:6bd72ba7fa56 58 GetAccelterData();
suzukimitsuhiro 1:6bd72ba7fa56 59 triggerSensorPolling = true;
suzukimitsuhiro 1:6bd72ba7fa56 60 }
suzukimitsuhiro 1:6bd72ba7fa56 61
suzukimitsuhiro 1:6bd72ba7fa56 62
suzukimitsuhiro 1:6bd72ba7fa56 63 void initBLE( void )
suzukimitsuhiro 1:6bd72ba7fa56 64 {
suzukimitsuhiro 1:6bd72ba7fa56 65 ticker.attach_us(periodicCallback, 10000); //10msecの周期タイマー
suzukimitsuhiro 1:6bd72ba7fa56 66
suzukimitsuhiro 1:6bd72ba7fa56 67 ble.init();
suzukimitsuhiro 1:6bd72ba7fa56 68 DEBUG("Init done\n");
suzukimitsuhiro 1:6bd72ba7fa56 69 ble.onDisconnection(disconnectionCallback);
suzukimitsuhiro 1:6bd72ba7fa56 70 ble.onConnection(onConnectionCallback);
suzukimitsuhiro 1:6bd72ba7fa56 71
suzukimitsuhiro 1:6bd72ba7fa56 72 ble.getPreferredConnectionParams(&connectionParams);
Daniel90 0:fa7bb836444d 73
suzukimitsuhiro 1:6bd72ba7fa56 74 /* setup advertising */
suzukimitsuhiro 1:6bd72ba7fa56 75 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
suzukimitsuhiro 1:6bd72ba7fa56 76 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
suzukimitsuhiro 2:9d566ea4f4fa 77
suzukimitsuhiro 1:6bd72ba7fa56 78 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
suzukimitsuhiro 1:6bd72ba7fa56 79 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
suzukimitsuhiro 1:6bd72ba7fa56 80 ble.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ //Advertisingの周期時間 0.625msec単位
suzukimitsuhiro 1:6bd72ba7fa56 81 ble.startAdvertising();
suzukimitsuhiro 2:9d566ea4f4fa 82
suzukimitsuhiro 1:6bd72ba7fa56 83 DEBUG("Start Advertising\n");
suzukimitsuhiro 1:6bd72ba7fa56 84
suzukimitsuhiro 1:6bd72ba7fa56 85 ble.addService(htmService);
suzukimitsuhiro 1:6bd72ba7fa56 86
suzukimitsuhiro 1:6bd72ba7fa56 87 }
suzukimitsuhiro 1:6bd72ba7fa56 88 //-------------------------------------
suzukimitsuhiro 1:6bd72ba7fa56 89
suzukimitsuhiro 2:9d566ea4f4fa 90 float oxyz = 0;
suzukimitsuhiro 2:9d566ea4f4fa 91 float dxyz = 0;
suzukimitsuhiro 2:9d566ea4f4fa 92
suzukimitsuhiro 1:6bd72ba7fa56 93 bool f=1;
suzukimitsuhiro 1:6bd72ba7fa56 94 float velocity = 0;
suzukimitsuhiro 1:6bd72ba7fa56 95 float maxvelocity = 0;
suzukimitsuhiro 1:6bd72ba7fa56 96 void GetAccelterData( void )
suzukimitsuhiro 1:6bd72ba7fa56 97 {
suzukimitsuhiro 2:9d566ea4f4fa 98 float x = acc.getAccX();
suzukimitsuhiro 2:9d566ea4f4fa 99 float y = acc.getAccY();
suzukimitsuhiro 2:9d566ea4f4fa 100 float z = acc.getAccZ();
suzukimitsuhiro 2:9d566ea4f4fa 101 float xyz = sqrt(x*x+y*y+z*z);
suzukimitsuhiro 1:6bd72ba7fa56 102
suzukimitsuhiro 1:6bd72ba7fa56 103 if(f==0){
suzukimitsuhiro 2:9d566ea4f4fa 104 dxyz += xyz - oxyz;
suzukimitsuhiro 1:6bd72ba7fa56 105 }
suzukimitsuhiro 1:6bd72ba7fa56 106 f=0;
tasdevelop 5:973da26ee266 107 velocity = dxyz*9.8;
tasdevelop 6:fbce7ee68c07 108 velocity *= (3600.0f/1000.0f/10.0f);
suzukimitsuhiro 2:9d566ea4f4fa 109 oxyz = xyz;
suzukimitsuhiro 1:6bd72ba7fa56 110
suzukimitsuhiro 2:9d566ea4f4fa 111 pc.printf("X: %7.2f, Y: %7.2f, Z: %7.2f v:%7.2f\r\n", x, y, z, velocity);
suzukimitsuhiro 1:6bd72ba7fa56 112
suzukimitsuhiro 1:6bd72ba7fa56 113 if (maxvelocity < velocity){
suzukimitsuhiro 1:6bd72ba7fa56 114 maxvelocity = velocity;
suzukimitsuhiro 1:6bd72ba7fa56 115 }
suzukimitsuhiro 1:6bd72ba7fa56 116 }
suzukimitsuhiro 1:6bd72ba7fa56 117 void InitAccelterData( void )
suzukimitsuhiro 1:6bd72ba7fa56 118 {
suzukimitsuhiro 1:6bd72ba7fa56 119 DEBUG("MMA8451 ID: %d\r\n", acc.getWhoAmI());
suzukimitsuhiro 2:9d566ea4f4fa 120 }
suzukimitsuhiro 1:6bd72ba7fa56 121 void onButton( void )
suzukimitsuhiro 1:6bd72ba7fa56 122 {
suzukimitsuhiro 1:6bd72ba7fa56 123 DEBUG("#####\r\n");
suzukimitsuhiro 2:9d566ea4f4fa 124 dxyz=0;
suzukimitsuhiro 1:6bd72ba7fa56 125 maxvelocity = 0;
suzukimitsuhiro 1:6bd72ba7fa56 126 }
suzukimitsuhiro 1:6bd72ba7fa56 127
suzukimitsuhiro 1:6bd72ba7fa56 128 void updateServiceValues(void);
Daniel90 0:fa7bb836444d 129
Daniel90 0:fa7bb836444d 130 int main(void) {
suzukimitsuhiro 1:6bd72ba7fa56 131 #if NEED_CONSOLE_OUTPUT
suzukimitsuhiro 1:6bd72ba7fa56 132 pc.baud(115200);
suzukimitsuhiro 1:6bd72ba7fa56 133 #endif
suzukimitsuhiro 1:6bd72ba7fa56 134
suzukimitsuhiro 1:6bd72ba7fa56 135 button.rise(&onButton);
suzukimitsuhiro 1:6bd72ba7fa56 136
suzukimitsuhiro 1:6bd72ba7fa56 137 InitAccelterData();
suzukimitsuhiro 1:6bd72ba7fa56 138 initBLE();
Daniel90 0:fa7bb836444d 139
Daniel90 0:fa7bb836444d 140 while (true) {
suzukimitsuhiro 1:6bd72ba7fa56 141 if (triggerSensorPolling) {
suzukimitsuhiro 1:6bd72ba7fa56 142 triggerSensorPolling = false;
suzukimitsuhiro 1:6bd72ba7fa56 143 updateServiceValues();
suzukimitsuhiro 1:6bd72ba7fa56 144 } else {
suzukimitsuhiro 1:6bd72ba7fa56 145 ble.waitForEvent();
suzukimitsuhiro 1:6bd72ba7fa56 146 }
Daniel90 0:fa7bb836444d 147 }
Daniel90 0:fa7bb836444d 148 }
suzukimitsuhiro 1:6bd72ba7fa56 149
suzukimitsuhiro 1:6bd72ba7fa56 150 uint32_t quick_ieee11073_from_float(float temperature)
suzukimitsuhiro 1:6bd72ba7fa56 151 {
suzukimitsuhiro 1:6bd72ba7fa56 152 uint8_t exponent = 0xFF; //exponent is -1
suzukimitsuhiro 1:6bd72ba7fa56 153 uint32_t mantissa = (uint32_t)(temperature*10);
suzukimitsuhiro 1:6bd72ba7fa56 154
suzukimitsuhiro 1:6bd72ba7fa56 155 return ( ((uint32_t)exponent) << 24) | mantissa;
suzukimitsuhiro 1:6bd72ba7fa56 156 }
suzukimitsuhiro 1:6bd72ba7fa56 157
suzukimitsuhiro 1:6bd72ba7fa56 158 void updateServiceValues(void)
suzukimitsuhiro 1:6bd72ba7fa56 159 {
suzukimitsuhiro 1:6bd72ba7fa56 160 uint32_t temp_ieee11073 = quick_ieee11073_from_float(maxvelocity);
suzukimitsuhiro 1:6bd72ba7fa56 161 memcpy(thermTempPayload+1, &temp_ieee11073, 4);
suzukimitsuhiro 1:6bd72ba7fa56 162 ble.updateCharacteristicValue(tempChar.getValueAttribute().getHandle(), thermTempPayload, sizeof(thermTempPayload)); //Mod
suzukimitsuhiro 1:6bd72ba7fa56 163 }
suzukimitsuhiro 1:6bd72ba7fa56 164
suzukimitsuhiro 1:6bd72ba7fa56 165
suzukimitsuhiro 1:6bd72ba7fa56 166
suzukimitsuhiro 1:6bd72ba7fa56 167