PID

Dependencies:   BLE_API mbed nRF51822

Committer:
stoicancristi
Date:
Mon Oct 24 16:10:48 2016 +0000
Revision:
0:1f4d5c5491b8
Child:
1:d3e12393b71d
initial commit : implemented PID class;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stoicancristi 0:1f4d5c5491b8 1 /*
stoicancristi 0:1f4d5c5491b8 2
stoicancristi 0:1f4d5c5491b8 3 Copyright (c) 2012-2014 RedBearLab
stoicancristi 0:1f4d5c5491b8 4
stoicancristi 0:1f4d5c5491b8 5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software
stoicancristi 0:1f4d5c5491b8 6 and associated documentation files (the "Software"), to deal in the Software without restriction,
stoicancristi 0:1f4d5c5491b8 7 including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
stoicancristi 0:1f4d5c5491b8 8 and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
stoicancristi 0:1f4d5c5491b8 9 subject to the following conditions:
stoicancristi 0:1f4d5c5491b8 10 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
stoicancristi 0:1f4d5c5491b8 11
stoicancristi 0:1f4d5c5491b8 12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
stoicancristi 0:1f4d5c5491b8 13 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
stoicancristi 0:1f4d5c5491b8 14 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
stoicancristi 0:1f4d5c5491b8 15 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
stoicancristi 0:1f4d5c5491b8 16 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
stoicancristi 0:1f4d5c5491b8 17
stoicancristi 0:1f4d5c5491b8 18 */
stoicancristi 0:1f4d5c5491b8 19
stoicancristi 0:1f4d5c5491b8 20 /*
stoicancristi 0:1f4d5c5491b8 21 * The application works with the BLEController iOS/Android App.
stoicancristi 0:1f4d5c5491b8 22 * Type something from the Terminal to send
stoicancristi 0:1f4d5c5491b8 23 * to the BLEController App or vice verse.
stoicancristi 0:1f4d5c5491b8 24 * Characteristics received from App will print on Terminal.
stoicancristi 0:1f4d5c5491b8 25 */
stoicancristi 0:1f4d5c5491b8 26
stoicancristi 0:1f4d5c5491b8 27 //#include "mbed.h"
stoicancristi 0:1f4d5c5491b8 28 #include "PID.h"
stoicancristi 0:1f4d5c5491b8 29 #include "ble/BLE.h"
stoicancristi 0:1f4d5c5491b8 30
stoicancristi 0:1f4d5c5491b8 31
stoicancristi 0:1f4d5c5491b8 32 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
stoicancristi 0:1f4d5c5491b8 33 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
stoicancristi 0:1f4d5c5491b8 34 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
stoicancristi 0:1f4d5c5491b8 35
stoicancristi 0:1f4d5c5491b8 36 #define TXRX_BUF_LEN 20
stoicancristi 0:1f4d5c5491b8 37
stoicancristi 0:1f4d5c5491b8 38 BLE ble;
stoicancristi 0:1f4d5c5491b8 39
stoicancristi 0:1f4d5c5491b8 40 Serial pc(USBTX, USBRX);
stoicancristi 0:1f4d5c5491b8 41
stoicancristi 0:1f4d5c5491b8 42
stoicancristi 0:1f4d5c5491b8 43 // The Nordic UART Service
stoicancristi 0:1f4d5c5491b8 44 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
stoicancristi 0:1f4d5c5491b8 45 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
stoicancristi 0:1f4d5c5491b8 46 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
stoicancristi 0:1f4d5c5491b8 47 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
stoicancristi 0:1f4d5c5491b8 48
stoicancristi 0:1f4d5c5491b8 49
stoicancristi 0:1f4d5c5491b8 50 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
stoicancristi 0:1f4d5c5491b8 51 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
stoicancristi 0:1f4d5c5491b8 52
stoicancristi 0:1f4d5c5491b8 53 static uint8_t rx_buf[TXRX_BUF_LEN];
stoicancristi 0:1f4d5c5491b8 54 static uint8_t rx_len=0;
stoicancristi 0:1f4d5c5491b8 55
stoicancristi 0:1f4d5c5491b8 56
stoicancristi 0:1f4d5c5491b8 57 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
stoicancristi 0:1f4d5c5491b8 58
stoicancristi 0:1f4d5c5491b8 59 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
stoicancristi 0:1f4d5c5491b8 60
stoicancristi 0:1f4d5c5491b8 61 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
stoicancristi 0:1f4d5c5491b8 62
stoicancristi 0:1f4d5c5491b8 63 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
stoicancristi 0:1f4d5c5491b8 64
stoicancristi 0:1f4d5c5491b8 65
stoicancristi 0:1f4d5c5491b8 66
stoicancristi 0:1f4d5c5491b8 67 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
stoicancristi 0:1f4d5c5491b8 68 {
stoicancristi 0:1f4d5c5491b8 69 pc.printf("Disconnected \r\n");
stoicancristi 0:1f4d5c5491b8 70 pc.printf("Restart advertising \r\n");
stoicancristi 0:1f4d5c5491b8 71 ble.startAdvertising();
stoicancristi 0:1f4d5c5491b8 72 }
stoicancristi 0:1f4d5c5491b8 73
stoicancristi 0:1f4d5c5491b8 74 void WrittenHandler(const GattWriteCallbackParams *Handler)
stoicancristi 0:1f4d5c5491b8 75 {
stoicancristi 0:1f4d5c5491b8 76 uint8_t buf[TXRX_BUF_LEN];
stoicancristi 0:1f4d5c5491b8 77 uint16_t bytesRead, index;
stoicancristi 0:1f4d5c5491b8 78
stoicancristi 0:1f4d5c5491b8 79 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
stoicancristi 0:1f4d5c5491b8 80 {
stoicancristi 0:1f4d5c5491b8 81 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
stoicancristi 0:1f4d5c5491b8 82 memset(txPayload, 0, TXRX_BUF_LEN);
stoicancristi 0:1f4d5c5491b8 83 memcpy(txPayload, buf, TXRX_BUF_LEN);
stoicancristi 0:1f4d5c5491b8 84 pc.printf("WriteHandler \r\n");
stoicancristi 0:1f4d5c5491b8 85 pc.printf("Length: ");
stoicancristi 0:1f4d5c5491b8 86 pc.putc(bytesRead);
stoicancristi 0:1f4d5c5491b8 87 pc.printf("\r\n");
stoicancristi 0:1f4d5c5491b8 88 pc.printf("Data: ");
stoicancristi 0:1f4d5c5491b8 89 for(index=0; index<bytesRead; index++)
stoicancristi 0:1f4d5c5491b8 90 {
stoicancristi 0:1f4d5c5491b8 91 pc.putc(txPayload[index]);
stoicancristi 0:1f4d5c5491b8 92 }
stoicancristi 0:1f4d5c5491b8 93 pc.printf("\r\n");
stoicancristi 0:1f4d5c5491b8 94 }
stoicancristi 0:1f4d5c5491b8 95 }
stoicancristi 0:1f4d5c5491b8 96
stoicancristi 0:1f4d5c5491b8 97 void uartCB(void)
stoicancristi 0:1f4d5c5491b8 98 {
stoicancristi 0:1f4d5c5491b8 99 while(pc.readable())
stoicancristi 0:1f4d5c5491b8 100 {
stoicancristi 0:1f4d5c5491b8 101 rx_buf[rx_len++] = pc.getc();
stoicancristi 0:1f4d5c5491b8 102 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
stoicancristi 0:1f4d5c5491b8 103 {
stoicancristi 0:1f4d5c5491b8 104 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
stoicancristi 0:1f4d5c5491b8 105 pc.printf("RecHandler \r\n");
stoicancristi 0:1f4d5c5491b8 106 pc.printf("Length: ");
stoicancristi 0:1f4d5c5491b8 107 pc.putc(rx_len);
stoicancristi 0:1f4d5c5491b8 108 pc.printf("\r\n");
stoicancristi 0:1f4d5c5491b8 109 rx_len = 0;
stoicancristi 0:1f4d5c5491b8 110 break;
stoicancristi 0:1f4d5c5491b8 111 }
stoicancristi 0:1f4d5c5491b8 112 }
stoicancristi 0:1f4d5c5491b8 113 }
stoicancristi 0:1f4d5c5491b8 114
stoicancristi 0:1f4d5c5491b8 115 DigitalOut myled(LED1);
stoicancristi 0:1f4d5c5491b8 116 AnalogIn input(P0_4);
stoicancristi 0:1f4d5c5491b8 117 PwmOut output(D4);
stoicancristi 0:1f4d5c5491b8 118
stoicancristi 0:1f4d5c5491b8 119 int main(void)
stoicancristi 0:1f4d5c5491b8 120 {
stoicancristi 0:1f4d5c5491b8 121 ble.init();
stoicancristi 0:1f4d5c5491b8 122 ble.onDisconnection(disconnectionCallback);
stoicancristi 0:1f4d5c5491b8 123 ble.onDataWritten(WrittenHandler);
stoicancristi 0:1f4d5c5491b8 124
stoicancristi 0:1f4d5c5491b8 125 pc.baud(9600);
stoicancristi 0:1f4d5c5491b8 126 pc.printf("SimpleChat Init \r\n");
stoicancristi 0:1f4d5c5491b8 127
stoicancristi 0:1f4d5c5491b8 128 pc.attach( uartCB , pc.RxIrq);
stoicancristi 0:1f4d5c5491b8 129 // setup advertising
stoicancristi 0:1f4d5c5491b8 130 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
stoicancristi 0:1f4d5c5491b8 131 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
stoicancristi 0:1f4d5c5491b8 132 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
stoicancristi 0:1f4d5c5491b8 133 (const uint8_t *)"Gigel", sizeof("Gigel") - 1);
stoicancristi 0:1f4d5c5491b8 134 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
stoicancristi 0:1f4d5c5491b8 135 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
stoicancristi 0:1f4d5c5491b8 136 // 100ms; in multiples of 0.625ms.
stoicancristi 0:1f4d5c5491b8 137 ble.setAdvertisingInterval(160);
stoicancristi 0:1f4d5c5491b8 138
stoicancristi 0:1f4d5c5491b8 139 ble.addService(uartService);
stoicancristi 0:1f4d5c5491b8 140
stoicancristi 0:1f4d5c5491b8 141 ble.startAdvertising();
stoicancristi 0:1f4d5c5491b8 142 pc.printf("Advertising Start \r\n");
stoicancristi 0:1f4d5c5491b8 143 float out = 0;
stoicancristi 0:1f4d5c5491b8 144 PIDClass PID(5,2,1,100);
stoicancristi 0:1f4d5c5491b8 145 while(1)
stoicancristi 0:1f4d5c5491b8 146 {
stoicancristi 0:1f4d5c5491b8 147 myled = 1;
stoicancristi 0:1f4d5c5491b8 148 wait(1);
stoicancristi 0:1f4d5c5491b8 149 myled = 0;
stoicancristi 0:1f4d5c5491b8 150 wait(1);
stoicancristi 0:1f4d5c5491b8 151 out = PID.ComputeCommand(input.read());
stoicancristi 0:1f4d5c5491b8 152 output.write(out);
stoicancristi 0:1f4d5c5491b8 153 //ble.waitForEvent();
stoicancristi 0:1f4d5c5491b8 154 pc.printf("Gigel");
stoicancristi 0:1f4d5c5491b8 155 pc.putc(out);
stoicancristi 0:1f4d5c5491b8 156 }
stoicancristi 0:1f4d5c5491b8 157 }
stoicancristi 0:1f4d5c5491b8 158
stoicancristi 0:1f4d5c5491b8 159
stoicancristi 0:1f4d5c5491b8 160
stoicancristi 0:1f4d5c5491b8 161
stoicancristi 0:1f4d5c5491b8 162
stoicancristi 0:1f4d5c5491b8 163
stoicancristi 0:1f4d5c5491b8 164
stoicancristi 0:1f4d5c5491b8 165
stoicancristi 0:1f4d5c5491b8 166
stoicancristi 0:1f4d5c5491b8 167
stoicancristi 0:1f4d5c5491b8 168
stoicancristi 0:1f4d5c5491b8 169
stoicancristi 0:1f4d5c5491b8 170
stoicancristi 0:1f4d5c5491b8 171
stoicancristi 0:1f4d5c5491b8 172
stoicancristi 0:1f4d5c5491b8 173