[X-NUCLEO-IKS01A2测评]有关蓝牙连接的滑翔机
1、评测计划中包括蓝牙连接和滑翔机的设计,但是测试过程中mbed读取的参数始终是初始值,没有变化,应该是没有启动传感器,所以读不出数据。因为时间有限,没有评测更换测试平台的的情况,按照例程都是可以顺利读出的,这个是mbed驱动设计的一些问题。2. 按照蓝牙连接控制blueNRG实现了蓝牙连接Nucleo板,采用的是cordova编程方式,源码如下,mbed设计部分
#include "mbed.h"
#include "ble/BLE.h"
#include "FlyService.h"
#define MOTOR_PERIOD 1 // 1ms in PWM period
#define PWM_FLY 50 // PWM start fly value in DC%
#define PWM_DELTA 1 // PWM Delta value in DC%
void initFlyWheels(void);
void flyWheelsInstance(int flycontrol);
PwmOut motorl(PA_0);
PwmOut motorr(PA_1);
uint16_t pwml,pwmr; // PWM value in DC% ranging 0~100
DigitalOut actuatedLED(LED1, 0);
Serial pc(USBTX, USBRX);
const static char DEVICE_NAME[] = "FLY";
static const uint16_t uuid16_list[] = {FLYService::FLY_SERVICE_UUID};
FLYService *flyServicePtr;
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
(void)params;
BLE::Instance().gap().startAdvertising(); // restart advertising
}
/**
* This callback allows the FLYService to receive updates to the ledState Characteristic.
*
* @param params
* Information about the characterisitc being updated.
*/
void onDataWrittenCallback(const GattWriteCallbackParams *params) {
if ((params->handle == flyServicePtr->getValueHandle()) && (params->len == 1)) {
actuatedLED = *(params->data);
//pc.printf("\r\n%d\r\n",*(params->data));
flyWheelsInstance(*(params->data));
}
}
/**
* This function is called when the ble initialization process has failled
*/
void onBleInitError(BLE &ble, ble_error_t error)
{
/* Initialization error handling should go here */
}
/**
* Callback triggered when the ble initialization process has finished
*/
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
BLE& ble = params->ble;
ble_error_t error = params->error;
initFlyWheels();
pc.printf("\r\nStarting ... ...\r\n");
if (error != BLE_ERROR_NONE) {
/* In case of error, forward the error handling to onBleInitError */
onBleInitError(ble, error);
return;
}
/* Ensure that it is the default instance of BLE */
if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
return;
}
ble.gap().onDisconnection(disconnectionCallback);
ble.gattServer().onDataWritten(onDataWrittenCallback);
bool initialValueForFLYCharacteristic = true;
flyServicePtr = new FLYService(ble, initialValueForFLYCharacteristic);
/* setup advertising */
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
ble.gap().startAdvertising();
while (true) {
ble.waitForEvent();
}
}
void initFlyWheels()
{
motorl.period_ms(MOTOR_PERIOD );
motorr.period_ms(MOTOR_PERIOD );
// Stop the motor as DC=0
pwml=0;
pwmr=0;
motorl=pwml/100;
motorr=pwmr/100;
}
void flyWheelsInstance(int flycontrol)
{
//flycontrol = 1;
pc.printf("\r\nflycontrol starting =%d\r\n",flycontrol);
switch (flycontrol)
{
case 0: // Stop
pwml=0;
pwmr=0;
pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
break;
case 1: //Fly
pwml=PWM_FLY;
pwmr=PWM_FLY;
pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
break;
case 2: //turn left
pwml=pwml-PWM_DELTA;
pwmr=pwmr+PWM_DELTA;
pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
break;
case '3': //turn right
pwml=pwml+PWM_DELTA;
pwmr=pwmr-PWM_DELTA;
pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
break;
case 4: //Speed up
pwml=pwml+PWM_DELTA;
pwmr=pwmr+PWM_DELTA;
pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
break;
case 6: //Speed down
pwml=pwml-PWM_DELTA;
pwmr=pwmr-PWM_DELTA;
pc.printf("\r\nflycontrol=%d\r\n",flycontrol);
break;
default: // Nothing change if not 1~6
pwml=pwml;
pwmr=pwmr;
break;
}
// Setting the pwm control for left motor and right motor in DC%
pc.printf("\r\npwml=%d--pwmr=%d\r\n",pwml,pwmr);
motorl=pwml/100;
motorr=pwmr/100;
}
int main(void)
{
BLE &ble = BLE::Instance();
ble.init(bleInitComplete);
}
采用的手机app截图
代码如下:
<!DOCTYPE html>
<html>
<!--
This is an app that demonstrates how to control an Arduino101 board
using BLE (Bluetooth Low Energy).
-->
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no
initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
<title>flywheels</title>
<style>
@import 'ui/css/evothings-app.css';
</style>
<script src="cordova.js"></script>
<script src="libs/jquery/jquery.js"></script>
<script src="libs/evothings/evothings.js"></script>
<script src="libs/evothings/ui/ui.js"></script>
<script src="libs/evothings/arduinoble/arduinoble.js"></script>
</head>
<body><!-- enables low-delay CSS transitions. -->
<header>
<button class="back">
<img src="ui/images/arrow-left.svg" >
</button>
<img class="logotype" src="ui/images/logo.jpg" alt="flywheels" />
<!--<button class="menu"><img src="ui/images/menu.svg" /></button>-->
</header>
<h1>flywheels</h1>
<p id="info">Initializing...</p>
<button class="yellow wide">CONNECT</button>
<br />
<div >
<table border="0" >
<tr>
<td> </td>
<td align="center"><button align="center"><img src="ui/images/button_up.png" ></button></td>
<td> </td>
</tr>
<tr>
<td align="center"><button align="center"><img src="ui/images/button_left.png" ></button></td>
<td align="center"><button id="flypause" align="center"><img src="ui/images/button_fly.png" ></button></td>
<td align="center"><button align="center"><img src="ui/images/button_right.png" ></button></td>
</tr>
<tr>
<td> </td>
<td align="center"><button align="center"><img src="ui/images/button_down.png" ></button></td>
<td> </td>
</tr>
</table>
</div>
<br />
<table><tr>
<td> </td>
<td align="center">4--up</td>
<td> </td>
</tr>
<tr>
<td align="center">2--left</td>
<td align="center">0/1 Pause/Fly </td>
<td align="center">3--right</td>
</tr>
<tr>
<td> </td>
<td align="center">6--down</td>
<td> </td>
</tr>
</table>
<br/>
<input type="text" id="txtag" value="Text-input(optional)">
<button class="green wide big">Send Command</button>
<br />
<button class="red wide big">Clear Command</button>
<script>
// Application object.
var app = {}
var flypause; // fly or pause status
// Connected device.
app.device = null;
// Turn on LED.
app.sentTX = function()
{
var txSent=new Uint8Array();
txSent=document.getElementById('txtag').value;
app.device && app.device.writeDataArray(new Uint8Array(), '0000a001-0000-1000-8000-00805f9b34fb');
}
// Turn off LED.
app.clearTX = function()
{
document.getElementById('txtag').value = " ";
app.device && app.device.writeDataArray(new Uint8Array(), '0000a001-0000-1000-8000-00805f9b34fb');
}
// Fly wheels fly '1'.
app.fly = function()
{
if (flypause==0) {
app.device && app.device.writeDataArray(new Uint8Array(), '0000a001-0000-1000-8000-00805f9b34fb');
document.getElementById('flypause').innerHTML="<img src='ui/images/button_pause.png' >";
flypause=1;
} else if (flypause==1) {
app.device && app.device.writeDataArray(new Uint8Array(), '0000a001-0000-1000-8000-00805f9b34fb');
document.getElementById('flypause').innerHTML="<img src='ui/images/button_fly.png' >";
flypause=0;
} else{
flypause=0;
}
}
// Fly wheels up '2' speed up
app.flyleft = function()
{
app.device && app.device.writeDataArray(new Uint8Array(), '0000a001-0000-1000-8000-00805f9b34fb');
}
// Fly wheels up '3' speed down
app.flyright = function()
{
app.device && app.device.writeDataArray(new Uint8Array(), '0000a001-0000-1000-8000-00805f9b34fb');
}
// Fly wheels turn left '4'
app.up = function()
{
app.device && app.device.writeDataArray(new Uint8Array(), '0000a001-0000-1000-8000-00805f9b34fb');
}
// Fly wheels turn right '6'
app.down = function()
{
app.device && app.device.writeDataArray(new Uint8Array(), '0000a001-0000-1000-8000-00805f9b34fb');
}
app.showMessage = function(info)
{
document.getElementById('info').innerHTML = info
};
// Called when BLE and other native functions are available.
app.onDeviceReady = function()
{
app.showMessage('Touch the connect button to begin.');
};
app.connect = function()
{
evothings.arduinoble.close();
app.showMessage('Connecting...');
evothings.arduinoble.connect(
'FLY', // Advertised name of BLE device.
function(device)
{
app.device = device;
app.showMessage('Connected! Touch buttons to turn LED on/off.');
},
function(errorCode)
{
app.showMessage('Connect error: ' + errorCode + '.');
});
};
document.addEventListener(
'deviceready',
function() { evothings.scriptsLoaded(app.onDeviceReady) },
false);
</script>
</body>
</html>
这个程序分别控制飞机的左右2个N30电机,驱动螺旋桨运动,控制左右方向。
基本能实现滑翔飞行的效果。不过因为电池选择的重量过大,不能在空中持续飞行。
汇总贴:X-NUCLEO-IKS01A2测评
https://bbs.eeworld.com.cn/thread-568428-1-1.html
个人汇总贴:
—by 北方
https://bbs.eeworld.com.cn/thread-567445-1-1.html
页:
[1]