[工业类传感器] [X-NUCLEO-IKS01A2测评]有关蓝牙连接的滑翔机

北方   2017-11-20 13:14 楼主
1、评测计划中包括蓝牙连接和滑翔机的设计,但是测试过程中mbed读取的参数始终是初始值,没有变化,应该是没有启动传感器,所以读不出数据。因为时间有限,没有评测更换测试平台的的情况,按照例程都是可以顺利读出的,这个是mbed驱动设计的一些问题。2. 按照蓝牙连接控制blueNRG实现了蓝牙连接Nucleo板,采用的是cordova编程方式,源码如下, mbed设计部分
  1. #include "mbed.h"
  2. #include "ble/BLE.h"
  3. #include "FlyService.h"
  4. #define MOTOR_PERIOD 1 // 1ms in PWM period
  5. #define PWM_FLY 50 // PWM start fly value in DC%
  6. #define PWM_DELTA 1 // PWM Delta value in DC%
  7. void initFlyWheels(void);
  8. void flyWheelsInstance(int flycontrol);
  9. PwmOut motorl(PA_0);
  10. PwmOut motorr(PA_1);
  11. uint16_t pwml,pwmr; // PWM value in DC% ranging 0~100
  12. DigitalOut actuatedLED(LED1, 0);
  13. Serial pc(USBTX, USBRX);
  14. const static char DEVICE_NAME[] = "FLY";
  15. static const uint16_t uuid16_list[] = {FLYService::FLY_SERVICE_UUID};
  16. FLYService *flyServicePtr;
  17. void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
  18. {
  19. (void)params;
  20. BLE::Instance().gap().startAdvertising(); // restart advertising
  21. }
  22. /**
  23. * This callback allows the FLYService to receive updates to the ledState Characteristic.
  24. *
  25. * @param[in] params
  26. * Information about the characterisitc being updated.
  27. */
  28. void onDataWrittenCallback(const GattWriteCallbackParams *params) {
  29. if ((params->handle == flyServicePtr->getValueHandle()) && (params->len == 1)) {
  30. actuatedLED = *(params->data);
  31. //pc.printf("\r\n%d\r\n",*(params->data));
  32. flyWheelsInstance(*(params->data));
  33. }
  34. }
  35. /**
  36. * This function is called when the ble initialization process has failled
  37. */
  38. void onBleInitError(BLE &ble, ble_error_t error)
  39. {
  40. /* Initialization error handling should go here */
  41. }
  42. /**
  43. * Callback triggered when the ble initialization process has finished
  44. */
  45. void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
  46. {
  47. BLE& ble = params->ble;
  48. ble_error_t error = params->error;
  49. initFlyWheels();
  50. pc.printf("\r\nStarting ... ...\r\n");
  51. if (error != BLE_ERROR_NONE) {
  52. /* In case of error, forward the error handling to onBleInitError */
  53. onBleInitError(ble, error);
  54. return;
  55. }
  56. /* Ensure that it is the default instance of BLE */
  57. if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
  58. return;
  59. }
  60. ble.gap().onDisconnection(disconnectionCallback);
  61. ble.gattServer().onDataWritten(onDataWrittenCallback);
  62. bool initialValueForFLYCharacteristic = true;
  63. flyServicePtr = new FLYService(ble, initialValueForFLYCharacteristic);
  64. /* setup advertising */
  65. ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
  66. ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
  67. ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
  68. ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
  69. ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
  70. ble.gap().startAdvertising();
  71. while (true) {
  72. ble.waitForEvent();
  73. }
  74. }
  75. void initFlyWheels()
  76. {
  77. motorl.period_ms(MOTOR_PERIOD );
  78. motorr.period_ms(MOTOR_PERIOD );
  79. // Stop the motor as DC=0
  80. pwml=0;
  81. pwmr=0;
  82. motorl=pwml/100;
  83. motorr=pwmr/100;
  84. }
  85. void flyWheelsInstance(int flycontrol)
  86. {
  87. //flycontrol = 1;
  88. pc.printf("\r\nflycontrol starting =%d\r\n",flycontrol);
  89. switch (flycontrol)
  90. {
  91. case 0: // Stop
  92. pwml=0;
  93. pwmr=0;
  94. pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
  95. break;
  96. case 1: //Fly
  97. pwml=PWM_FLY;
  98. pwmr=PWM_FLY;
  99. pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
  100. break;
  101. case 2: //turn left
  102. pwml=pwml-PWM_DELTA;
  103. pwmr=pwmr+PWM_DELTA;
  104. pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
  105. break;
  106. case '3': //turn right
  107. pwml=pwml+PWM_DELTA;
  108. pwmr=pwmr-PWM_DELTA;
  109. pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
  110. break;
  111. case 4: //Speed up
  112. pwml=pwml+PWM_DELTA;
  113. pwmr=pwmr+PWM_DELTA;
  114. pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
  115. break;
  116. case 6: //Speed down
  117. pwml=pwml-PWM_DELTA;
  118. pwmr=pwmr-PWM_DELTA;
  119. pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
  120. break;
  121. default: // Nothing change if not 1~6
  122. pwml=pwml;
  123. pwmr=pwmr;
  124. break;
  125. }
  126. // Setting the pwm control for left motor and right motor in DC%
  127. pc.printf("\r\npwml=%d--pwmr=%d\r\n",pwml,pwmr);
  128. motorl=pwml/100;
  129. motorr=pwmr/100;
  130. }
  131. int main(void)
  132. {
  133. BLE &ble = BLE::Instance();
  134. ble.init(bleInitComplete);
  135. }
采用的手机app截图 875346149.jpg 代码如下:
  1. <!DOCTYPE html>
  2. <html>
  3. <!--
  4. This is an app that demonstrates how to control an Arduino101 board
  5. using BLE (Bluetooth Low Energy).
  6. -->
  7. <head>
  8. <meta charset="utf-8" />
  9. <meta name="viewport" content="width=device-width, user-scalable=no
  10. initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
  11. <title>flywheels</title>
  12. <style>
  13. [url=home.php?mod=space&uid=568594]@import[/url] 'ui/css/evothings-app.css';
  14. </style>
  15. <script src="cordova.js"></script>
  16. <script src="libs/jquery/jquery.js"></script>
  17. <script src="libs/evothings/evothings.js"></script>
  18. <script src="libs/evothings/ui/ui.js"></script>
  19. <script src="libs/evothings/arduinoble/arduinoble.js"></script>
  20. </head>
  21. <body><!-- enables low-delay CSS transitions. -->
  22. <header>
  23. <button class="back">
  24. <img src="ui/images/arrow-left.svg" >
  25. </button>
  26. <img class="logotype" src="ui/images/logo.jpg" alt="flywheels" />
  27. <!--<button class="menu"><img src="ui/images/menu.svg" /></button>-->
  28. </header>
  29. <h1>flywheels</h1>
  30. <p id="info">Initializing...</p>
  31. <button class="yellow wide">CONNECT</button>
  32. <br />
  33. <div >
  34. <table border="0" >
  35. <tr>
  36. <td> </td>
  37. <td align="center"><button align="center"><img src="ui/images/button_up.png" ></button></td>
  38. <td> </td>
  39. </tr>
  40. <tr>
  41. <td align="center"><button align="center"><img src="ui/images/button_left.png" ></button></td>
  42. <td align="center"><button id="flypause" align="center"><img src="ui/images/button_fly.png" ></button></td>
  43. <td align="center"><button align="center"><img src="ui/images/button_right.png" ></button></td>
  44. </tr>
  45. <tr>
  46. <td> </td>
  47. <td align="center"><button align="center"><img src="ui/images/button_down.png" ></button></td>
  48. <td> </td>
  49. </tr>
  50. </table>
  51. </div>
  52. <br />
  53. <table><tr>
  54. <td> </td>
  55. <td align="center">4--up</td>
  56. <td> </td>
  57. </tr>
  58. <tr>
  59. <td align="center">2--left</td>
  60. <td align="center">0/1 Pause/Fly </td>
  61. <td align="center">3--right</td>
  62. </tr>
  63. <tr>
  64. <td> </td>
  65. <td align="center">6--down</td>
  66. <td> </td>
  67. </tr>
  68. </table>
  69. <br/>
  70. <input type="text" id="txtag" value="Text-input(optional)">
  71. <button class="green wide big">Send Command</button>
  72. <br />
  73. <button class="red wide big">Clear Command</button>
  74. <script>
  75. // Application object.
  76. var app = {}
  77. var flypause; // fly or pause status
  78. // Connected device.
  79. app.device = null;
  80. // Turn on LED.
  81. app.sentTX = function()
  82. {
  83. var txSent=new Uint8Array();
  84. txSent=document.getElementById('txtag').value;
  85. app.device && app.device.writeDataArray(new Uint8Array([txSent]), '0000a001-0000-1000-8000-00805f9b34fb');
  86. }
  87. // Turn off LED.
  88. app.clearTX = function()
  89. {
  90. document.getElementById('txtag').value = " ";
  91. app.device && app.device.writeDataArray(new Uint8Array([0]), '0000a001-0000-1000-8000-00805f9b34fb');
  92. }
  93. // Fly wheels fly '1'.
  94. app.fly = function()
  95. {
  96. if (flypause==0) {
  97. app.device && app.device.writeDataArray(new Uint8Array([1]), '0000a001-0000-1000-8000-00805f9b34fb');
  98. document.getElementById('flypause').innerHTML="<img src='ui/images/button_pause.png' >";
  99. flypause=1;
  100. } else if (flypause==1) {
  101. app.device && app.device.writeDataArray(new Uint8Array([0]), '0000a001-0000-1000-8000-00805f9b34fb');
  102. document.getElementById('flypause').innerHTML="<img src='ui/images/button_fly.png' >";
  103. flypause=0;
  104. } else{
  105. flypause=0;
  106. }
  107. }
  108. // Fly wheels up '2' speed up
  109. app.flyleft = function()
  110. {
  111. app.device && app.device.writeDataArray(new Uint8Array([2]), '0000a001-0000-1000-8000-00805f9b34fb');
  112. }
  113. // Fly wheels up '3' speed down
  114. app.flyright = function()
  115. {
  116. app.device && app.device.writeDataArray(new Uint8Array([3]), '0000a001-0000-1000-8000-00805f9b34fb');
  117. }
  118. // Fly wheels turn left '4'
  119. app.up = function()
  120. {
  121. app.device && app.device.writeDataArray(new Uint8Array([4]), '0000a001-0000-1000-8000-00805f9b34fb');
  122. }
  123. // Fly wheels turn right '6'
  124. app.down = function()
  125. {
  126. app.device && app.device.writeDataArray(new Uint8Array([6]), '0000a001-0000-1000-8000-00805f9b34fb');
  127. }
  128. app.showMessage = function(info)
  129. {
  130. document.getElementById('info').innerHTML = info
  131. };
  132. // Called when BLE and other native functions are available.
  133. app.onDeviceReady = function()
  134. {
  135. app.showMessage('Touch the connect button to begin.');
  136. };
  137. app.connect = function()
  138. {
  139. evothings.arduinoble.close();
  140. app.showMessage('Connecting...');
  141. evothings.arduinoble.connect(
  142. 'FLY', // Advertised name of BLE device.
  143. function(device)
  144. {
  145. app.device = device;
  146. app.showMessage('Connected! Touch buttons to turn LED on/off.');
  147. },
  148. function(errorCode)
  149. {
  150. app.showMessage('Connect error: ' + errorCode + '.');
  151. });
  152. };
  153. document.addEventListener(
  154. 'deviceready',
  155. function() { evothings.scriptsLoaded(app.onDeviceReady) },
  156. false);
  157. </script>
  158. </body>
  159. </html>
这个程序分别控制飞机的左右2个N30电机,驱动螺旋桨运动,控制左右方向。 基本能实现滑翔飞行的效果。不过因为电池选择的重量过大,不能在空中持续飞行。

回复评论 (1)

汇总贴:X-NUCLEO-IKS01A2测评
https://bbs.eeworld.com.cn/thread-568428-1-1.html
个人汇总贴:
[X-NUCLEO-IKS01A2测评]—by 北方
https://bbs.eeworld.com.cn/thread-567445-1-1.html
玩板看这里: https://bbs.eeworld.com.cn/elecplay.html EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!
点赞  2017-11-23 11:28
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复