embedded software for tortuga bike
Dependencies: BLE_API BikeControl DataLogging X_NUCLEO_IDB0XA1 _24LCXXX mbed
main.cpp@2:7eb0bb74a8b6, 2016-07-18 (annotated)
- Committer:
- ptuytsch
- Date:
- Mon Jul 18 13:20:25 2016 +0000
- Revision:
- 2:7eb0bb74a8b6
- Parent:
- 1:ffdec767aa55
code merged in separate library, not the most beautiful way
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ptuytsch | 0:0803cdf04c32 | 1 | #include "mbed.h" |
ptuytsch | 0:0803cdf04c32 | 2 | #include "BikeData.h" |
ptuytsch | 0:0803cdf04c32 | 3 | #include "BatteryState.h" |
ptuytsch | 2:7eb0bb74a8b6 | 4 | #include "BikeControl.h" |
ptuytsch | 0:0803cdf04c32 | 5 | #include "ble/BLE.h" |
ptuytsch | 0:0803cdf04c32 | 6 | #include "ble/services/BikeService.h" |
ptuytsch | 0:0803cdf04c32 | 7 | #include "ble/services/BikeBatteryService.h" |
ptuytsch | 0:0803cdf04c32 | 8 | |
ptuytsch | 0:0803cdf04c32 | 9 | #define trailerBatteryPin PB_0 |
ptuytsch | 0:0803cdf04c32 | 10 | #define bikeBatteryPin PC_5 |
ptuytsch | 0:0803cdf04c32 | 11 | #define auxiliaryBatteryPin PC_4 |
ptuytsch | 0:0803cdf04c32 | 12 | |
ptuytsch | 0:0803cdf04c32 | 13 | /*************************************************************************** |
ptuytsch | 0:0803cdf04c32 | 14 | VARIABLES |
ptuytsch | 0:0803cdf04c32 | 15 | ****************************************************************************/ |
ptuytsch | 0:0803cdf04c32 | 16 | |
ptuytsch | 2:7eb0bb74a8b6 | 17 | /* |
ptuytsch | 1:ffdec767aa55 | 18 | //LIGHT |
ptuytsch | 1:ffdec767aa55 | 19 | DigitalOut lightFront(PB_14); |
ptuytsch | 1:ffdec767aa55 | 20 | DigitalOut lightBack(PA_11); |
ptuytsch | 1:ffdec767aa55 | 21 | DigitalOut lightLeft(PC_6); |
ptuytsch | 2:7eb0bb74a8b6 | 22 | DigitalOut lightRight(PC_8);*/ |
ptuytsch | 1:ffdec767aa55 | 23 | |
ptuytsch | 1:ffdec767aa55 | 24 | |
ptuytsch | 2:7eb0bb74a8b6 | 25 | static BikeControl *bc; |
ptuytsch | 2:7eb0bb74a8b6 | 26 | |
ptuytsch | 0:0803cdf04c32 | 27 | |
ptuytsch | 0:0803cdf04c32 | 28 | //3 battery states |
ptuytsch | 1:ffdec767aa55 | 29 | static BatteryState *trailerBattery; |
ptuytsch | 1:ffdec767aa55 | 30 | static BatteryState *bikeBattery; |
ptuytsch | 1:ffdec767aa55 | 31 | static BatteryState *auxiliaryBattery; |
ptuytsch | 0:0803cdf04c32 | 32 | |
ptuytsch | 0:0803cdf04c32 | 33 | //BLE VARIABLES |
ptuytsch | 2:7eb0bb74a8b6 | 34 | static char DEVICE_NAME[] = "NAME"; //Define default name of the BLE device |
ptuytsch | 0:0803cdf04c32 | 35 | static const uint16_t uuid16_list[] = {BikeService::BIKE_SERVICE_UUID, |
ptuytsch | 0:0803cdf04c32 | 36 | //GattService::UUID_DEVICE_INFORMATION_SERVICE, |
ptuytsch | 0:0803cdf04c32 | 37 | BatteryService::BATTERY_SERVICE_UUID, |
ptuytsch | 0:0803cdf04c32 | 38 | }; // List of the Service UUID's that are used. |
ptuytsch | 0:0803cdf04c32 | 39 | |
ptuytsch | 0:0803cdf04c32 | 40 | //Pointers to diferent objects and BLE services. |
ptuytsch | 0:0803cdf04c32 | 41 | static BikeData *bd; |
ptuytsch | 0:0803cdf04c32 | 42 | static BikeService *bikeServicePtr; |
ptuytsch | 0:0803cdf04c32 | 43 | //static DeviceInformationService *DISptr; |
ptuytsch | 0:0803cdf04c32 | 44 | static BatteryService *batSerPtr; |
ptuytsch | 0:0803cdf04c32 | 45 | |
ptuytsch | 0:0803cdf04c32 | 46 | //bool that is set to true each second. |
ptuytsch | 0:0803cdf04c32 | 47 | static bool update = false; |
ptuytsch | 0:0803cdf04c32 | 48 | static bool updateHalf = false; |
ptuytsch | 0:0803cdf04c32 | 49 | |
ptuytsch | 1:ffdec767aa55 | 50 | /*************************************************************************** |
ptuytsch | 1:ffdec767aa55 | 51 | CONTROL FUNCTIONS |
ptuytsch | 1:ffdec767aa55 | 52 | ****************************************************************************/ |
ptuytsch | 2:7eb0bb74a8b6 | 53 | /*void runTestLight() |
ptuytsch | 1:ffdec767aa55 | 54 | { |
ptuytsch | 1:ffdec767aa55 | 55 | printf("front\n"); |
ptuytsch | 1:ffdec767aa55 | 56 | lightFront =1; |
ptuytsch | 1:ffdec767aa55 | 57 | wait(1); |
ptuytsch | 1:ffdec767aa55 | 58 | lightFront = 0; |
ptuytsch | 1:ffdec767aa55 | 59 | printf("back\n"); |
ptuytsch | 1:ffdec767aa55 | 60 | lightBack = 1; |
ptuytsch | 1:ffdec767aa55 | 61 | wait(1); |
ptuytsch | 1:ffdec767aa55 | 62 | lightBack = 0; |
ptuytsch | 1:ffdec767aa55 | 63 | printf("left\n"); |
ptuytsch | 1:ffdec767aa55 | 64 | lightLeft = 1; |
ptuytsch | 1:ffdec767aa55 | 65 | wait(1); |
ptuytsch | 1:ffdec767aa55 | 66 | lightLeft = 0; |
ptuytsch | 1:ffdec767aa55 | 67 | printf("right\n"); |
ptuytsch | 1:ffdec767aa55 | 68 | lightRight = 1; |
ptuytsch | 1:ffdec767aa55 | 69 | wait(1); |
ptuytsch | 1:ffdec767aa55 | 70 | lightRight = 0; |
ptuytsch | 1:ffdec767aa55 | 71 | wait(1); |
ptuytsch | 2:7eb0bb74a8b6 | 72 | }*/ |
ptuytsch | 0:0803cdf04c32 | 73 | |
ptuytsch | 0:0803cdf04c32 | 74 | /*************************************************************************** |
ptuytsch | 0:0803cdf04c32 | 75 | BLE FUNCTIONS |
ptuytsch | 0:0803cdf04c32 | 76 | ****************************************************************************/ |
ptuytsch | 0:0803cdf04c32 | 77 | |
ptuytsch | 0:0803cdf04c32 | 78 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
ptuytsch | 0:0803cdf04c32 | 79 | { |
ptuytsch | 0:0803cdf04c32 | 80 | //restart advertising when disconnected |
ptuytsch | 0:0803cdf04c32 | 81 | BLE::Instance().gap().startAdvertising(); |
ptuytsch | 0:0803cdf04c32 | 82 | } |
ptuytsch | 0:0803cdf04c32 | 83 | |
ptuytsch | 0:0803cdf04c32 | 84 | void periodicCallback(void) |
ptuytsch | 0:0803cdf04c32 | 85 | { |
ptuytsch | 0:0803cdf04c32 | 86 | //Set update high to send new data over BLE |
ptuytsch | 0:0803cdf04c32 | 87 | update = true; |
ptuytsch | 0:0803cdf04c32 | 88 | } |
ptuytsch | 0:0803cdf04c32 | 89 | |
ptuytsch | 0:0803cdf04c32 | 90 | |
ptuytsch | 0:0803cdf04c32 | 91 | //This function is called when the ble initialization process has failled |
ptuytsch | 0:0803cdf04c32 | 92 | void onBleInitError(BLE &ble, ble_error_t error) |
ptuytsch | 0:0803cdf04c32 | 93 | { |
ptuytsch | 0:0803cdf04c32 | 94 | /* Initialization error handling should go here */ |
ptuytsch | 0:0803cdf04c32 | 95 | printf("initialization error!\n"); |
ptuytsch | 0:0803cdf04c32 | 96 | } |
ptuytsch | 0:0803cdf04c32 | 97 | |
ptuytsch | 0:0803cdf04c32 | 98 | |
ptuytsch | 0:0803cdf04c32 | 99 | //Callback triggered when the ble initialization process has finished |
ptuytsch | 0:0803cdf04c32 | 100 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
ptuytsch | 0:0803cdf04c32 | 101 | { |
ptuytsch | 0:0803cdf04c32 | 102 | BLE& ble = params->ble; |
ptuytsch | 0:0803cdf04c32 | 103 | ble_error_t error = params->error; |
ptuytsch | 0:0803cdf04c32 | 104 | |
ptuytsch | 0:0803cdf04c32 | 105 | if (error != BLE_ERROR_NONE) { /* In case of error, forward the error handling to onBleInitError */ |
ptuytsch | 0:0803cdf04c32 | 106 | printf("BLE error\n"); |
ptuytsch | 0:0803cdf04c32 | 107 | onBleInitError(ble, error); |
ptuytsch | 0:0803cdf04c32 | 108 | return; |
ptuytsch | 0:0803cdf04c32 | 109 | } |
ptuytsch | 0:0803cdf04c32 | 110 | |
ptuytsch | 0:0803cdf04c32 | 111 | /* Ensure that it is the default instance of BLE */ |
ptuytsch | 0:0803cdf04c32 | 112 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
ptuytsch | 0:0803cdf04c32 | 113 | return; |
ptuytsch | 0:0803cdf04c32 | 114 | } |
ptuytsch | 0:0803cdf04c32 | 115 | |
ptuytsch | 0:0803cdf04c32 | 116 | //set the disconnect function |
ptuytsch | 0:0803cdf04c32 | 117 | ble.gap().onDisconnection(disconnectionCallback); |
ptuytsch | 0:0803cdf04c32 | 118 | printf("BLE Disconnect callback set\n"); |
ptuytsch | 0:0803cdf04c32 | 119 | |
ptuytsch | 0:0803cdf04c32 | 120 | /* Setup primary services */ |
ptuytsch | 0:0803cdf04c32 | 121 | //DISptr = new DeviceInformationService(ble, "The Opportunity Factory", "1.0", "PJTMN06", "1.0", "0.9", "0.9"); |
ptuytsch | 0:0803cdf04c32 | 122 | bikeServicePtr = new BikeService(ble,bd); |
ptuytsch | 1:ffdec767aa55 | 123 | batSerPtr = new BatteryService(ble, |
ptuytsch | 1:ffdec767aa55 | 124 | trailerBattery->getBatteryPercentage(), |
ptuytsch | 1:ffdec767aa55 | 125 | bikeBattery->getBatteryPercentage(), |
ptuytsch | 1:ffdec767aa55 | 126 | auxiliaryBattery->getBatteryPercentage()); |
ptuytsch | 1:ffdec767aa55 | 127 | |
ptuytsch | 0:0803cdf04c32 | 128 | printf("Services set\n"); |
ptuytsch | 0:0803cdf04c32 | 129 | |
ptuytsch | 0:0803cdf04c32 | 130 | /* setup advertising */ |
ptuytsch | 0:0803cdf04c32 | 131 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_CYCLING);//set apperance |
ptuytsch | 0:0803cdf04c32 | 132 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); //different BLE options set |
ptuytsch | 0:0803cdf04c32 | 133 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));//Adding the list of UUID's |
ptuytsch | 0:0803cdf04c32 | 134 | //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));//passing the device name |
ptuytsch | 0:0803cdf04c32 | 135 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, bd->getBikeNameSize());//passing the device name |
ptuytsch | 0:0803cdf04c32 | 136 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);//setting connection type |
ptuytsch | 0:0803cdf04c32 | 137 | ble.gap().setAdvertisingInterval(100); /* 1000ms. */ |
ptuytsch | 0:0803cdf04c32 | 138 | ble.gap().startAdvertising(); |
ptuytsch | 0:0803cdf04c32 | 139 | printf("advertising\n"); |
ptuytsch | 0:0803cdf04c32 | 140 | } |
ptuytsch | 0:0803cdf04c32 | 141 | |
ptuytsch | 0:0803cdf04c32 | 142 | |
ptuytsch | 0:0803cdf04c32 | 143 | int main() { |
ptuytsch | 0:0803cdf04c32 | 144 | |
ptuytsch | 0:0803cdf04c32 | 145 | |
ptuytsch | 0:0803cdf04c32 | 146 | printf("\n======================\n\n"); |
ptuytsch | 0:0803cdf04c32 | 147 | printf("SETUP \n"); |
ptuytsch | 0:0803cdf04c32 | 148 | |
ptuytsch | 0:0803cdf04c32 | 149 | bd = new BikeData(PC_1); // creating BikeData object |
ptuytsch | 0:0803cdf04c32 | 150 | printf("BikeData object created\n"); |
ptuytsch | 0:0803cdf04c32 | 151 | |
ptuytsch | 0:0803cdf04c32 | 152 | |
ptuytsch | 1:ffdec767aa55 | 153 | trailerBattery = new BatteryState(trailerBatteryPin,BatteryState::Battery48V); |
ptuytsch | 1:ffdec767aa55 | 154 | bikeBattery = new BatteryState(bikeBatteryPin,BatteryState::Battery48V); |
ptuytsch | 1:ffdec767aa55 | 155 | auxiliaryBattery = new BatteryState(auxiliaryBatteryPin,BatteryState::Battery48V); |
ptuytsch | 1:ffdec767aa55 | 156 | |
ptuytsch | 2:7eb0bb74a8b6 | 157 | bc = new BikeControl(bd,trailerBattery, bikeBattery, auxiliaryBattery); |
ptuytsch | 2:7eb0bb74a8b6 | 158 | |
ptuytsch | 1:ffdec767aa55 | 159 | |
ptuytsch | 1:ffdec767aa55 | 160 | printf("run TestLight \n"); |
ptuytsch | 2:7eb0bb74a8b6 | 161 | //runTestLight(); |
ptuytsch | 2:7eb0bb74a8b6 | 162 | bc->runTestLight(); |
ptuytsch | 1:ffdec767aa55 | 163 | |
ptuytsch | 0:0803cdf04c32 | 164 | uint8_t size = bd->getBikeNameSize(); |
ptuytsch | 0:0803cdf04c32 | 165 | printf("name length: %i\n", size); |
ptuytsch | 0:0803cdf04c32 | 166 | if (size == 0){ |
ptuytsch | 0:0803cdf04c32 | 167 | printf("Setting Name First Time\n"); |
ptuytsch | 1:ffdec767aa55 | 168 | static char testName[] = "Tortuga"; |
ptuytsch | 0:0803cdf04c32 | 169 | bd -> setBikeName((char *)testName,sizeof(testName)); |
ptuytsch | 0:0803cdf04c32 | 170 | printf("SetFistTimeName\n"); |
ptuytsch | 0:0803cdf04c32 | 171 | //printf( "size: %i\n",bd -> getBikeNameSize()); |
ptuytsch | 0:0803cdf04c32 | 172 | //printf("first character: %c",bd->getBikeName()); |
ptuytsch | 0:0803cdf04c32 | 173 | } |
ptuytsch | 0:0803cdf04c32 | 174 | |
ptuytsch | 0:0803cdf04c32 | 175 | bd->getBikeName(DEVICE_NAME); |
ptuytsch | 0:0803cdf04c32 | 176 | printf("got device name\n"); |
ptuytsch | 0:0803cdf04c32 | 177 | BLE &ble = BLE::Instance(); // creating BLE object |
ptuytsch | 0:0803cdf04c32 | 178 | printf("BLE object created\n"); |
ptuytsch | 0:0803cdf04c32 | 179 | BLE::Instance().init(bleInitComplete); // call init function |
ptuytsch | 0:0803cdf04c32 | 180 | |
ptuytsch | 0:0803cdf04c32 | 181 | while (ble.hasInitialized() == false); // Wait until BLE is initialized |
ptuytsch | 0:0803cdf04c32 | 182 | printf("BLE object intitialized\n"); |
ptuytsch | 0:0803cdf04c32 | 183 | Ticker ticker; |
ptuytsch | 0:0803cdf04c32 | 184 | ticker.attach(periodicCallback, 1); //creating the ticker that wil update each second. |
ptuytsch | 0:0803cdf04c32 | 185 | printf("ticker 1 attached\n"); |
ptuytsch | 0:0803cdf04c32 | 186 | |
ptuytsch | 0:0803cdf04c32 | 187 | printf("START \n"); |
ptuytsch | 0:0803cdf04c32 | 188 | |
ptuytsch | 0:0803cdf04c32 | 189 | |
ptuytsch | 2:7eb0bb74a8b6 | 190 | bc->startControlLoop(); |
ptuytsch | 2:7eb0bb74a8b6 | 191 | |
ptuytsch | 0:0803cdf04c32 | 192 | while(true) |
ptuytsch | 0:0803cdf04c32 | 193 | { |
ptuytsch | 0:0803cdf04c32 | 194 | if (update){ |
ptuytsch | 1:ffdec767aa55 | 195 | printf("--------------\n"); |
ptuytsch | 1:ffdec767aa55 | 196 | float distance = (bd->getDataSet(BikeData::OVERALL))->getDistance(); |
ptuytsch | 1:ffdec767aa55 | 197 | printf("main distance: %f\n", distance); |
ptuytsch | 0:0803cdf04c32 | 198 | float speed = bd->getSpeed(); |
ptuytsch | 1:ffdec767aa55 | 199 | printf("main speed: %f\n", speed); |
ptuytsch | 0:0803cdf04c32 | 200 | |
ptuytsch | 0:0803cdf04c32 | 201 | //printf("update bikeService\n"); |
ptuytsch | 0:0803cdf04c32 | 202 | bikeServicePtr->update(); //update the bikeserice |
ptuytsch | 1:ffdec767aa55 | 203 | //printf("update battery1: %i\n", trailerBattery->getBatteryPercentage()); |
ptuytsch | 1:ffdec767aa55 | 204 | //batSerPtr->updateBatteryLevel1((++batLevel)%=101); |
ptuytsch | 1:ffdec767aa55 | 205 | //printf("battery level: %i\n", batLevel); |
ptuytsch | 0:0803cdf04c32 | 206 | batSerPtr->updateBatteryLevel1(trailerBattery->getBatteryPercentage()); |
ptuytsch | 1:ffdec767aa55 | 207 | //printf("update battery2: %i\n", bikeBattery->getBatteryPercentage()); |
ptuytsch | 0:0803cdf04c32 | 208 | batSerPtr->updateBatteryLevel2(bikeBattery->getBatteryPercentage()); |
ptuytsch | 1:ffdec767aa55 | 209 | //printf("update battery3: %i\n", auxiliaryBattery->getBatteryPercentage()); |
ptuytsch | 0:0803cdf04c32 | 210 | batSerPtr->updateBatteryLevel3(auxiliaryBattery->getBatteryPercentage()); |
ptuytsch | 0:0803cdf04c32 | 211 | update = false; |
ptuytsch | 0:0803cdf04c32 | 212 | } |
ptuytsch | 0:0803cdf04c32 | 213 | ble.waitForEvent(); //wait for an event when idle. |
ptuytsch | 2:7eb0bb74a8b6 | 214 | |
ptuytsch | 2:7eb0bb74a8b6 | 215 | bc->checkStatus(); |
ptuytsch | 0:0803cdf04c32 | 216 | } |
ptuytsch | 0:0803cdf04c32 | 217 | } |