iOSのBLEコントローラアプリ「RCBController」とmbed HRM1017を接続し、RCサーボモータを操作するテストプログラムです。

Dependencies:   BLE_API_Native_IRC Servo mbed

Fork of BLE_RCBController by Junichi Katsu

  • 古いBLEライブラリを使っているのでプラットフォームは”Nordic nRF51822”を選択してください。
  • ライブラリ類はUpdateしないでください。コンパイルエラーになります。

うまく接続できない時は、iPhone/iPadのBluetoothをOFF->ONしてキャッシュをクリアしてみてください。

/media/uploads/robo8080/img_1560.jpg

Committer:
robo8080
Date:
Wed Jul 30 00:09:23 2014 +0000
Revision:
2:1a3fb1a40edf
Parent:
1:b806cc9ed05c
test2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:8c643bfe55b7 1 #include "mbed.h"
jksoft 0:8c643bfe55b7 2 #include "nRF51822n.h"
jksoft 0:8c643bfe55b7 3 #include "RCBController.h"
robo8080 1:b806cc9ed05c 4 #include "Servo.h"
jksoft 0:8c643bfe55b7 5
jksoft 0:8c643bfe55b7 6 #define DBG 0
jksoft 0:8c643bfe55b7 7
jksoft 0:8c643bfe55b7 8 nRF51822n nrf;
jksoft 0:8c643bfe55b7 9 Serial pc(USBTX, USBRX);
jksoft 0:8c643bfe55b7 10 /* LEDs for indication: */
jksoft 0:8c643bfe55b7 11 DigitalOut ConnectStateLed(LED1);
jksoft 0:8c643bfe55b7 12 PwmOut ControllerStateLed(LED2);
jksoft 0:8c643bfe55b7 13
robo8080 1:b806cc9ed05c 14 Servo servo1(P0_28), servo2(P0_29);
jksoft 0:8c643bfe55b7 15
jksoft 0:8c643bfe55b7 16 /* RCBController Service */
jksoft 0:8c643bfe55b7 17 static const uint16_t RCBController_service_uuid = 0xFFF0;
jksoft 0:8c643bfe55b7 18 static const uint16_t RCBController_Characteristic_uuid = 0xFFF1;
jksoft 0:8c643bfe55b7 19 GattService RCBControllerService (RCBController_service_uuid);
jksoft 0:8c643bfe55b7 20 GattCharacteristic Controller (RCBController_Characteristic_uuid,10, 10,
jksoft 0:8c643bfe55b7 21 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE |
jksoft 0:8c643bfe55b7 22 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
jksoft 0:8c643bfe55b7 23
jksoft 0:8c643bfe55b7 24 /* Advertising data and parameters */
jksoft 0:8c643bfe55b7 25 GapAdvertisingData advData;
jksoft 0:8c643bfe55b7 26 GapAdvertisingData scanResponse;
jksoft 0:8c643bfe55b7 27 GapAdvertisingParams advParams ( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
jksoft 0:8c643bfe55b7 28
jksoft 0:8c643bfe55b7 29 uint16_t uuid16_list[] = { RCBController_service_uuid };
jksoft 0:8c643bfe55b7 30
jksoft 0:8c643bfe55b7 31 RCBController controller;
jksoft 0:8c643bfe55b7 32
jksoft 0:8c643bfe55b7 33 // GapEvent
jksoft 0:8c643bfe55b7 34 class GapEventHandler : public GapEvents
jksoft 0:8c643bfe55b7 35 {
jksoft 0:8c643bfe55b7 36
jksoft 0:8c643bfe55b7 37 virtual void onConnected(void)
jksoft 0:8c643bfe55b7 38 {
jksoft 0:8c643bfe55b7 39 ConnectStateLed = 0;
jksoft 0:8c643bfe55b7 40 #if DBG
jksoft 0:8c643bfe55b7 41 pc.printf("Connected\n\r");
jksoft 0:8c643bfe55b7 42 #endif
jksoft 0:8c643bfe55b7 43 }
jksoft 0:8c643bfe55b7 44
jksoft 0:8c643bfe55b7 45 virtual void onDisconnected(void)
jksoft 0:8c643bfe55b7 46 {
jksoft 0:8c643bfe55b7 47 nrf.getGap().startAdvertising(advParams);
jksoft 0:8c643bfe55b7 48 ConnectStateLed = 1;
jksoft 0:8c643bfe55b7 49 #if DBG
jksoft 0:8c643bfe55b7 50 pc.printf("Disconnected\n\r");
jksoft 0:8c643bfe55b7 51 #endif
jksoft 0:8c643bfe55b7 52 }
jksoft 0:8c643bfe55b7 53 };
jksoft 0:8c643bfe55b7 54
jksoft 0:8c643bfe55b7 55 // GattEvent
jksoft 0:8c643bfe55b7 56 class GattServerEventHandler : public GattServerEvents
jksoft 0:8c643bfe55b7 57 {
jksoft 0:8c643bfe55b7 58 virtual void onDataWritten(uint16_t charHandle)
jksoft 0:8c643bfe55b7 59 {
jksoft 0:8c643bfe55b7 60 if (charHandle == Controller.handle) {
jksoft 0:8c643bfe55b7 61 nrf.getGattServer().readValue(Controller.handle, &controller.data[0], sizeof(controller));
jksoft 0:8c643bfe55b7 62 #if DBG
jksoft 0:8c643bfe55b7 63 pc.printf("DATA:%d %d %d %d %d %d %d %d %d %d\n\r",controller.data[0],controller.data[1],controller.data[2],controller.data[3],controller.data[4],
jksoft 0:8c643bfe55b7 64 controller.data[5],controller.data[6],controller.data[7],controller.data[8],controller.data[9]);
jksoft 0:8c643bfe55b7 65 #endif
robo8080 2:1a3fb1a40edf 66 ControllerStateLed = (float)controller.status.LeftAnalogLR / 255.0;
robo8080 1:b806cc9ed05c 67 servo1 = (float)controller.status.LeftAnalogUD / 255.0;
robo8080 1:b806cc9ed05c 68 servo2 = (float)controller.status.LeftAnalogLR / 255.0;
jksoft 0:8c643bfe55b7 69
jksoft 0:8c643bfe55b7 70 }
jksoft 0:8c643bfe55b7 71
jksoft 0:8c643bfe55b7 72 }
jksoft 0:8c643bfe55b7 73 };
jksoft 0:8c643bfe55b7 74
jksoft 0:8c643bfe55b7 75 /**************************************************************************/
jksoft 0:8c643bfe55b7 76 /*!
jksoft 0:8c643bfe55b7 77 @brief Program entry point
jksoft 0:8c643bfe55b7 78 */
jksoft 0:8c643bfe55b7 79 /**************************************************************************/
jksoft 0:8c643bfe55b7 80 int main(void)
jksoft 0:8c643bfe55b7 81 {
jksoft 0:8c643bfe55b7 82 #if DBG
jksoft 0:8c643bfe55b7 83 pc.printf("Start\n\r");
jksoft 0:8c643bfe55b7 84 #endif
robo8080 1:b806cc9ed05c 85 servo1.calibrate(0.0009,180.0);
robo8080 1:b806cc9ed05c 86 servo2.calibrate(0.0009,180.0);
robo8080 1:b806cc9ed05c 87 servo1 = 0.5;
robo8080 1:b806cc9ed05c 88 servo2 = 0.5;
jksoft 0:8c643bfe55b7 89 /* Setup an event handler for GAP events i.e. Client/Server connection events. */
jksoft 0:8c643bfe55b7 90 nrf.getGap().setEventHandler(new GapEventHandler());
jksoft 0:8c643bfe55b7 91
jksoft 0:8c643bfe55b7 92 /* Initialise the nRF51822 */
jksoft 0:8c643bfe55b7 93 nrf.init();
jksoft 0:8c643bfe55b7 94
jksoft 0:8c643bfe55b7 95 nrf.getGattServer().setEventHandler(new GattServerEventHandler());
jksoft 0:8c643bfe55b7 96
jksoft 0:8c643bfe55b7 97 /* Make sure we get a clean start */
jksoft 0:8c643bfe55b7 98 nrf.reset();
jksoft 0:8c643bfe55b7 99
jksoft 0:8c643bfe55b7 100 /* Add BLE-Only flag and complete service list to the advertising data */
jksoft 0:8c643bfe55b7 101 advData.addFlags(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jksoft 0:8c643bfe55b7 102 advData.addData(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
jksoft 0:8c643bfe55b7 103 (uint8_t*)uuid16_list, sizeof(uuid16_list));
jksoft 0:8c643bfe55b7 104 nrf.getGap().setAdvertisingData(advData, scanResponse);
jksoft 0:8c643bfe55b7 105
jksoft 0:8c643bfe55b7 106 /* RCBController Service */
jksoft 0:8c643bfe55b7 107 RCBControllerService.addCharacteristic(Controller);
jksoft 0:8c643bfe55b7 108 nrf.getGattServer().addService(RCBControllerService);
jksoft 0:8c643bfe55b7 109
jksoft 0:8c643bfe55b7 110 /* Start advertising (make sure you've added all your data first) */
jksoft 0:8c643bfe55b7 111 nrf.getGap().startAdvertising(advParams);
jksoft 0:8c643bfe55b7 112 ConnectStateLed = 1;
jksoft 0:8c643bfe55b7 113 ControllerStateLed = 1;
jksoft 0:8c643bfe55b7 114
jksoft 0:8c643bfe55b7 115 for (;;)
jksoft 0:8c643bfe55b7 116 {
jksoft 0:8c643bfe55b7 117 wait(1);
jksoft 0:8c643bfe55b7 118 }
jksoft 0:8c643bfe55b7 119 }
jksoft 0:8c643bfe55b7 120