ワウペダルと加速度センサでBLE制御ラジコン!
以前スマホでコントロールするラジコンを作ったのですが。。。
やっぱり物理コントローラで動かしてみたくなりハンドルに加速度センサ、アクセルに大昔に買ったワウペダルを使ってコントローラつくりました♪コントローラと車の通信にはBLEを使用しました。
ワウペダルはウルフルズのガッツだぜをコピーしたくて買ったんだったなぁ。。もう20年前か。。。。
目次
構成
部品
- Redbearlab BLE Nano
- Redbear Duo
- 6DOF MPU-6050
- Motor Driver DRV8835
-
JIM DUNLOP wah pedal CRYBABY
車体
BLEとWiFiを搭載したマイコンRedBear Duoを使用しコントローラ情報を受信(BLE Central)し、モータを制御します。今回はWiFi使用しないのでBLE Nanoでも良かったのですがNanoはPWM出力できないのでPWM出力端子のあるDuoを贅沢に使用!
コントローラ
BLE搭載マイコンBLE Nanoを使用して車体と通信(BLE Peripheral)しています。
ハンドル(現状タッパのフタ。。。ww)の傾きを6軸モーションセンサMPU6050のy軸(水平方向)の加速度で検出しBLE Nanoに送信。
ワウペダル内の可変抵抗に配線引っ張ってBLE Nanoに接続してペダルの踏み込みを検知します。中ホコリだらけだった。。。
BLE NanoとDuoの詳細は以下にも記載しております。
動作
ワウペダルを踏み込むと加速します!
ハンドルを切ると曲がります!
Arduino IDE用コード
Redbear Duo (Central)
以下をベースに生成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
#if defined(ARDUINO) SYSTEM_MODE(SEMI_AUTOMATIC); #endif /* * BLE scan parameters: * - BLE_SCAN_TYPE * 0x00: Passive scanning, no scan request packets shall be sent.(default) * 0x01: Active scanning, scan request packets may be sent. * 0x02 - 0xFF: Reserved for future use. * - BLE_SCAN_INTERVAL: This is defined as the time interval from when the Controller started its last LE scan until it begins the subsequent LE scan. * Range: 0x0004 to 0x4000 * Default: 0x0010 (10 ms) * Time = N * 0.625 msec * Time Range: 2.5 msec to 10.24 seconds * - BLE_SCAN_WINDOW: The duration of the LE scan. The scan window shall be less than or equal to the scan interval. * Range: 0x0004 to 0x4000 * Default: 0x0010 (10 ms) * Time = N * 0.625 msec * Time Range: 2.5 msec to 10240 msec */ #define BLE_SCAN_TYPE 0x00 // Passive scanning #define BLE_SCAN_INTERVAL 0x0060 // 60 ms #define BLE_SCAN_WINDOW 0x0030 // 30 ms int mpuVol; uint8_t wpVol = 0; //Wah pedal値 int L = D8; int R = D9; /****************************************************** * Type Define ******************************************************/ typedef struct { uint16_t connected_handle; uint8_t addr_type; bd_addr_t addr; struct { gatt_client_service_t service; struct { gatt_client_characteristic_t chars; gatt_client_characteristic_descriptor_t descriptor[2]; // User_descriptor and client charactersitc configuration descriptor. } chars[2]; } service; // Service contains two characteristics and each characteristic contains two descriptors. } Device_t; /****************************************************** * Variable Definitions ******************************************************/ // Connect handle. static uint16_t connected_id = 0xFFFF; Device_t device; uint8_t chars_index = 0; uint8_t desc_index = 0; // The service uuid to be discovered. static uint8_t service1_uuid[16] = { 0x71,0x3d,0x00,0x00,0x50,0x3e,0x4c,0x75,0xba,0x94,0x31,0x48,0xf1,0x8d,0x94,0x1e }; static uint8_t gatt_notify_flag = 0; /****************************************************** * Function Definitions ******************************************************/ /** * @brief Find the data given the type in advertising data. * * @param[in] type The type of field data. * @param[in] advdata_len Length of advertising data. * @param[in] *p_advdata The pointer of advertising data. * @param[out] *len The length of found data. * @param[out] *p_field_data The pointer of buffer to store field data. * * @retval 0 Find the data * 1 Not find. */ uint32_t ble_advdata_decode(uint8_t type, uint8_t advdata_len, uint8_t *p_advdata, uint8_t *len, uint8_t *p_field_data) { uint8_t index = 0; uint8_t field_length, field_type; while (index < advdata_len) { field_length = p_advdata[index]; field_type = p_advdata[index + 1]; if (field_type == type) { memcpy(p_field_data, &p_advdata[index + 2], (field_length - 1)); *len = field_length - 1; return 0; } index += field_length + 1; } return 1; } /** * @brief Callback for scanning device. * * @param[in] *report * * @retval None */ void reportCallback(advertisementReport_t *report) { uint8_t index; Serial.println("reportCallback: "); Serial.print("The advEventType: "); Serial.println(report->advEventType, HEX); Serial.print("The peerAddrType: "); Serial.println(report->peerAddrType, HEX); Serial.print("The peerAddr: "); for (index = 0; index < 6; index++) { Serial.print(report->peerAddr[index], HEX); Serial.print(" "); } Serial.println(" "); Serial.print("The rssi: "); Serial.println(report->rssi, DEC); Serial.print("The ADV data: "); for (index = 0; index < report->advDataLen; index++) { Serial.print(report->advData[index], HEX); Serial.print(" "); } Serial.println(" "); Serial.println(" "); uint8_t len; uint8_t adv_name[31]; // Find short local name. if (0x00 == ble_advdata_decode(0x08, report->advDataLen, report->advData, &len, adv_name)) { Serial.print(" The length of Short Local Name : "); Serial.println(len, HEX); Serial.print(" The Short Local Name is : "); Serial.println((const char *)adv_name); if (0x00 == memcmp(adv_name, "TXRX", min(4, len))) { ble.stopScanning(); device.addr_type = report->peerAddrType; memcpy(device.addr, report->peerAddr, 6); //ble.connect(report->peerAddr, report->peerAddrType); //ble.connect(report->peerAddr, BD_ADDR_TYPE_LE_PUBLIC); ble.connect(report->peerAddr, BD_ADDR_TYPE_LE_RANDOM); } } } /** * @brief Connect handle. * * @param[in] status BLE_STATUS_CONNECTION_ERROR or BLE_STATUS_OK. * @param[in] handle Connect handle. * * @retval None */ void deviceConnectedCallback(BLEStatus_t status, uint16_t handle) { RGB.color(0, 60, 25); //BLE接続時にLED水色 switch (status) { case BLE_STATUS_OK: Serial.println("Device connected!"); // Connect to remote device, start to discover service. connected_id = handle; device.connected_handle = handle; // Start to discover service, will report result on discoveredServiceCallback. ble.discoverPrimaryServices(handle); break; default: break; } } /** * @brief Disconnect handle. * * @param[in] handle Connect handle. * * @retval None */ void deviceDisconnectedCallback(uint16_t handle){ RGB.color(100, 0, 0); //BLE非接続時にLED赤色 analogWrite(L, LOW); analogWrite(R, LOW); Serial.print("Disconnected handle:"); Serial.println(handle,HEX); if (connected_id == handle) { Serial.println("Restart scanning."); // Disconnect from remote device, restart to scanning. connected_id = 0xFFFF; ble.startScanning(); } } /** * @brief Callback for handling result of discovering service. * * @param[in] status BLE_STATUS_OK/BLE_STATUS_DONE * @param[in] con_handle * @param[in] *service Discoverable service. * * @retval None */ static void discoveredServiceCallback(BLEStatus_t status, uint16_t con_handle, gatt_client_service_t *service) { uint8_t index; if (status == BLE_STATUS_OK) { // Found a service. Serial.println(" "); Serial.print("Service start handle: "); Serial.println(service->start_group_handle, HEX); Serial.print("Service end handle: "); Serial.println(service->end_group_handle, HEX); Serial.print("Service uuid16: "); Serial.println(service->uuid16, HEX); Serial.print("The uuid128 : "); for (index = 0; index < 16; index++) { Serial.print(service->uuid128[index], HEX); Serial.print(" "); } Serial.println(" "); if (0x00 == memcmp(service->uuid128, service1_uuid, 16)) { Serial.println("Target uuid128"); device.service.service = *service; } } else if (status == BLE_STATUS_DONE) { Serial.println("Discovered service done"); // All sevice have been found, start to discover characteristics. // Result will be reported on discoveredCharsCallback. ble.discoverCharacteristics(device.connected_handle, &device.service.service); } } /** * @brief Callback for handling result of discovering characteristic. * * @param[in] status BLE_STATUS_OK/BLE_STATUS_DONE * @param[in] con_handle * @param[in] *characteristic Discoverable characteristic. * * @retval None */ static void discoveredCharsCallback(BLEStatus_t status, uint16_t con_handle, gatt_client_characteristic_t *characteristic) { uint8_t index; if (status == BLE_STATUS_OK) { // Found a characteristic. Serial.println(" "); Serial.print("characteristic start handle: "); Serial.println(characteristic->start_handle, HEX); Serial.print("characteristic value handle: "); Serial.println(characteristic->value_handle, HEX); Serial.print("characteristic end_handle: "); Serial.println(characteristic->end_handle, HEX); Serial.print("characteristic properties: "); Serial.println(characteristic->properties, HEX); Serial.print("characteristic uuid16: "); Serial.println(characteristic->uuid16, HEX); Serial.print("characteristic uuid128 : "); for (index = 0; index < 16; index++) { Serial.print(characteristic->uuid128[index], HEX); Serial.print(" "); } Serial.println(" "); if (chars_index < 2) { device.service.chars[chars_index].chars= *characteristic; chars_index++; } } else if (status == BLE_STATUS_DONE) { Serial.println("Discovered characteristic done"); chars_index = 0; // All characteristics have been found, start to discover descriptors. // Result will be reported on discoveredCharsDescriptorsCallback. ble.discoverCharacteristicDescriptors(device.connected_handle, &device.service.chars[chars_index].chars); } } /** * @brief Callback for handling result of discovering descriptor. * * @param[in] status BLE_STATUS_OK/BLE_STATUS_DONE * @param[in] con_handle * @param[in] *descriptor Discoverable descriptor. * * @retval None */ static void discoveredCharsDescriptorsCallback(BLEStatus_t status, uint16_t con_handle, gatt_client_characteristic_descriptor_t *descriptor) { uint8_t index; if (status == BLE_STATUS_OK) { // Found a descriptor. Serial.println(" "); Serial.print("descriptor handle: "); Serial.println(descriptor->handle, HEX); Serial.print("descriptor uuid16: "); Serial.println(descriptor->uuid16, HEX); Serial.print("descriptor uuid128 : "); for (index = 0; index < 16; index++) { Serial.print(descriptor->uuid128[index], HEX); Serial.print(" "); } Serial.println(" "); if (desc_index < 2) { device.service.chars[chars_index].descriptor[desc_index++] = *descriptor; } } else if (status == BLE_STATUS_DONE) { // finish. Serial.println("Discovered descriptor done"); chars_index++; if (chars_index < 2) { desc_index=0; ble.discoverCharacteristicDescriptors(device.connected_handle, &device.service.chars[chars_index].chars); } else { // Read value of characteristic, // Result will be reported on gattReadCallback. ble.readValue(device.connected_handle,&device.service.chars[1].chars); } } } /** * @brief Callback for handling result of reading. * * @param[in] status BLE_STATUS_OK/BLE_STATUS_DONE/BLE_STATUS_OTHER_ERROR * @param[in] con_handle * @param[in] value_handle * @param[in] *value * @param[in] length * * @retval None */ void gattReadCallback(BLEStatus_t status, uint16_t con_handle, uint16_t value_handle, uint8_t *value, uint16_t length) { uint8_t index; if (status == BLE_STATUS_OK) { //受信処理 mpuVol = value[0] - 127; //MPU6050 Acc-y値 wpVol = value[1]; //Wah pedal値 Serial.print(mpuVol); Serial.print("\t"); Serial.println(wpVol); } else if (status == BLE_STATUS_DONE) { //送信(ダミー) uint8_t data[]= {0}; // Result will be reported on gattWrittenCallback. // If use ble.writeValueWithoutResponse, will no response. ble.writeValue(device.connected_handle, device.service.chars[0].chars.value_handle, sizeof(data), data); } } /** * @brief Callback for handling result of writting. * * @param[in] status BLE_STATUS_DONE/BLE_STATUS_OTHER_ERROR * @param[in] con_handle * * @retval None */ void gattWrittenCallback(BLEStatus_t status, uint16_t con_handle) { if (status == BLE_STATUS_DONE) { //受信 ble.readValue(device.connected_handle,&device.service.chars[1].chars); } } /** * @brief Callback for handling result of reading descriptor. * * @param[in] status BLE_STATUS_DONE/BLE_STATUS_OTHER_ERROR * @param[in] con_handle * @param[in] value_handle * @param[in] *value * @param[in] length * * @retval None */ void gattReadDescriptorCallback(BLEStatus_t status, uint16_t con_handle, uint16_t value_handle, uint8_t *value, uint16_t length) { uint8_t index; if(status == BLE_STATUS_OK) { Serial.println(" "); Serial.println("Read descriptor ok"); Serial.print("conn handle: "); Serial.println(con_handle, HEX); Serial.print("value handle: "); Serial.println(value_handle, HEX); Serial.print("The value : "); for (index = 0; index < length; index++) { Serial.print(value[index], HEX); Serial.print(" "); } Serial.println(" "); } else if (status == BLE_STATUS_DONE) { // Enable notify. ble.writeClientCharsConfigDescritpor(device.connected_handle, &device.service.chars[0].chars, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); } } /** * @brief Callback for handling result of writting client characteristic configuration. * * @param[in] status BLE_STATUS_DONE/BLE_STATUS_OTHER_ERROR * @param[in] con_handle * * @retval None */ void gattWriteCCCDCallback(BLEStatus_t status, uint16_t con_handle) { if (status == BLE_STATUS_DONE) { Serial.println("gattWriteCCCDCallback done"); if (gatt_notify_flag == 0) { gatt_notify_flag = 1; ble.writeClientCharsConfigDescritpor(device.connected_handle, &device.service.chars[1].chars, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION); } else if (gatt_notify_flag == 1) { gatt_notify_flag = 2; } } } /** * @brief Callback for handling notify event from remote device. * * @param[in] status BLE_STATUS_OK * @param[in] con_handle * @param[in] value_handle * @param[in] *value * @param[in] length * * @retval None */ void gattNotifyUpdateCallback(BLEStatus_t status, uint16_t con_handle, uint16_t value_handle, uint8_t *value, uint16_t length) { uint8_t index; Serial.println(" "); Serial.println("Notify Update value "); Serial.print("conn handle: "); Serial.println(con_handle, HEX); Serial.print("value handle: "); Serial.println(value_handle, HEX); Serial.print("The value : "); for (index = 0; index < length; index++) { Serial.print(value[index], HEX); Serial.print(" "); } Serial.println(" "); } /** * @brief Setup. */ void setup() { Serial.begin(115200); delay(5000); RGB.control(true); RGB.color(100, 0, 0); pinMode(L, OUTPUT); pinMode(R, OUTPUT); // Open debugger, must befor init(). //ble.debugLogger(true); //ble.debugError(true); //ble.enablePacketLogger(); Serial.println("BLE central demo!"); // Initialize ble_stack. ble.init(); // Register callback functions. ble.onConnectedCallback(deviceConnectedCallback); ble.onDisconnectedCallback(deviceDisconnectedCallback); ble.onScanReportCallback(reportCallback); ble.onServiceDiscoveredCallback(discoveredServiceCallback); ble.onCharacteristicDiscoveredCallback(discoveredCharsCallback); ble.onDescriptorDiscoveredCallback(discoveredCharsDescriptorsCallback); ble.onGattCharacteristicReadCallback(gattReadCallback); ble.onGattCharacteristicWrittenCallback(gattWrittenCallback); ble.onGattDescriptorReadCallback(gattReadDescriptorCallback); ble.onGattWriteClientCharacteristicConfigCallback(gattWriteCCCDCallback); ble.onGattNotifyUpdateCallback(gattNotifyUpdateCallback); // Set scan parameters. ble.setScanParams(BLE_SCAN_TYPE, BLE_SCAN_INTERVAL, BLE_SCAN_WINDOW); // Start scanning. ble.startScanning(); Serial.println("Start scanning "); } /** * @brief Loop. */ void loop() { if(wpVol > 45){ if(mpuVol > 0){ analogWrite(L, wpVol - map(abs(mpuVol), 0, 127, 0, wpVol)); analogWrite(R, wpVol + map(abs(mpuVol), 0, 127, 0, wpVol)); }else{ analogWrite(L, wpVol + map(abs(mpuVol), 0, 127, 0, wpVol)); analogWrite(R, wpVol - map(abs(mpuVol), 0, 127, 0, wpVol)); } }else { analogWrite(L, LOW); analogWrite(R, LOW); } } |
BLE Nano (Peripheral)
以下をベースに生成
6軸モーションセンサMPU6050のArduino用ライブラリは以下にあります(MPU6050とI2Cdevを使用)。
https://github.com/jrowberg/i2cdevlib/tree/master/Arduino
https://github.com/jrowberg/i2cdevlib/tree/master/Arduino
BLE NanoでMPU6050を使用する場合にはArduino IDEボードマネージャのRedBearLab nRF51822 Boardsのパッケージバージョン1.0.5を使用してください。1.0.6以上では何かしらに変更あったらしくエラー出まくります。。。
さらにI2Cdev.hに ”#define BUFFER_LENGTH 32″ を追加で問題なく使用できるようになります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
#include <BLE_API.h> #include <Wire.h> #include "I2Cdev.h" #include "MPU6050.h" BLE ble; Timeout timeout; MPU6050 mpu; #define TXRX_BUF_LEN 20 static uint8_t tx_buf[TXRX_BUF_LEN]; // The Nordic UART Service static const uint8_t service1_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t service1_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t service1_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E}; static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71}; uint8_t tx_value[TXRX_BUF_LEN] = {0,}; uint8_t rx_value[TXRX_BUF_LEN] = {0,}; GattCharacteristic characteristic1(service1_tx_uuid, tx_value, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE ); GattCharacteristic characteristic2(service1_rx_uuid, rx_value, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); GattCharacteristic *uartChars[] = {&characteristic1, &characteristic2}; GattService uartService(service1_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *)); MPU6050 accelgyro; int16_t ax, ay, az; int16_t gx, gy, gz; static void disconnectionCallBack(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) { ble.startAdvertising(); } void writtenHandle(const GattWriteCallbackParams *Handler) { uint8_t buf[TXRX_BUF_LEN]; uint16_t bytesRead, index; if (Handler->handle == characteristic1.getValueAttribute().getHandle()) { ble.readCharacteristicValue(characteristic1.getValueAttribute().getHandle(), buf, &bytesRead); for(byte index=0; index<bytesRead; index++) { Serial.write(buf[index]); } } } void setup() { // join I2C bus (I2Cdev library doesn't do this automatically) #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE Wire.begin(); #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE Fastwire::setup(400, true); #endif Serial.begin(250000); // initialize device Serial.println("Initializing I2C devices..."); accelgyro.initialize(); // verify connection Serial.println("Testing device connections..."); Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); mpu.setXAccelOffset(-1020); mpu.setYAccelOffset(-1030); mpu.setZAccelOffset(1470); mpu.setXGyroOffset(80); mpu.setYGyroOffset(-45); mpu.setZGyroOffset(30); //BLE設定 ble.init(); ble.onDisconnection(disconnectionCallBack); ble.onDataWritten(writtenHandle); // setup adv_data and srp_data ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)"TXRX", sizeof("TXRX") - 1); ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid_rev)); // set adv_type ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // add service ble.addService(uartService); // set device name ble.setDeviceName((const uint8_t *)"Simple Chat"); // set tx power,valid values are -40, -20, -16, -12, -8, -4, 0, 4 ble.setTxPower(4); // set adv_interval, 100ms in multiples of 0.625ms. ble.setAdvertisingInterval(160); // set adv_timeout, in seconds ble.setAdvertisingTimeout(0); // start advertising ble.startAdvertising(); Serial.println("Advertising Start!"); } void loop() { // read raw accel/gyro measurements from device accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); /* Serial.print("a/g:\t"); Serial.print(ax); Serial.print("\t"); Serial.print(ay); Serial.print("\t"); Serial.print(az); Serial.print("\t"); Serial.print(gx); Serial.print("\t"); Serial.print(gy); Serial.print("\t"); Serial.println(gz); */ //BLEデータ送信 tx_buf[0] = map(ay, -18000, 18000, 0, 255); //MPU6050 tx_buf[1] = map(analogRead(A5), 960, 0, 0, 255); //Wah Pedal値 Serial.print(tx_buf[0]); Serial.print("\t"); Serial.println(tx_buf[1]); ble.updateCharacteristicValue(characteristic2.getValueAttribute().getHandle(), tx_buf, 2); } |
これでヘッドマウントデスプレイでFPVできたら面白いだろうなぁ
「ワウペダルと加速度センサでBLE制御ラジコン!」への1件のフィードバック