【工业设备边缘智能化解决方案】Part 2一 低功耗3轴加速计+3轴陀螺仪+蓝牙调试
本帖最后由 吃了一鲸 于 2021-6-27 20:51 编辑<p><b>【工业设备边缘智能化解决方案】</b><strong>Part 2一 低功耗3 轴加速计3 轴陀螺仪+蓝牙调试</strong></p>
<p>分享贴:<b><a href="https://bbs.eeworld.com.cn/thread-1168959-1-1.html" rel="noopener noreferrer" target="_blank">【工业设备边缘智能化解决方案】Part 1一RSL10 开发环境搭建</a></b></p>
<h2><strong>一、传感器数据采集功能调试</strong></h2>
<p></p>
<p>根据<b><a href="https://bbs.eeworld.com.cn/thread-1168959-1-1.html" rel="noopener noreferrer" target="_blank">【工业设备边缘智能化解决方案】Part 1一RSL10 开发环境搭建</a></b>安装好例程包,如图,选择BHI160为基础demo。</p>
<h3><b>1.1 例程代码解析</b></h3>
<p><b>(1)初始化时钟</b></p>
<p style="margin-left: 40px;">BDK_Initialize();</p>
<p><b>(2)初始化I2C接口</b></p>
<p style="margin-left: 40px;">HAL_I2C_SetBusSpeed(<i>HAL_I2C_BUS_SPEED_FAST</i>);</p>
<p style="margin-left: 40px;">I2C速率有100kHz标准、400kHz快速和 1MHz超快,根据需求自己配置。</p>
<p><b>(3)初始化BHI160</b></p>
<pre>
<code class="language-cpp">{
printf("Initializing BHI160.\r\n");
struct SwTimer tim;
struct SwTimer_Duration tim_dur;
SwTimer_Initialize(&tim);
SwTimer_Start(&tim);
retval = BHI160_NDOF_Initialize();
ASSERT_DEBUG(retval == BHY_SUCCESS);
SwTimer_GetElapsed(&tim, &tim_dur);
SwTimer_Remove(&tim);
printf("BHI160 initialized in %lu seconds and %lu ns.\r\n",
tim_dur.seconds, tim_dur.nanoseconds);
}</code></pre>
<p>这里开定时器的原因在于I2C通信初始化BHI160过程中的延时定时。好像不加定时器也可以,但为了稳妥,我还是加上了。</p>
<p><b>(4)注册采集回调函数</b></p>
<pre>
<code class="language-cpp">retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_ORIENTATION, &ao_vector_cb, 5); //采样速率5
ASSERT_DEBUG(retval == BHY_SUCCESS);
retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_LINEAR_ACCELERATION, &la_vector_cb, 50);//采样速率50
ASSERT_DEBUG(retval == BHY_SUCCESS);
retval = BHI160_NDOF_EnableSensor(BHI160_NDOF_S_RATE_OF_ROTATION, &gyro_vector_cb, 50);//采样速率50
ASSERT_DEBUG(retval == BHY_SUCCESS);</code></pre>
<p><b>(5)开启任务调度和内核调度</b></p>
<pre>
<code class="language-cpp">BDK_Schedule();
SYS_WAIT_FOR_INTERRUPT;</code></pre>
<h3>1.2 调试结果</h3>
<p>打开J-Link RTT Viewer,选择USB,设备选择RSL10,SWD模式,点击OK</p>
<p></p>
<h2>二、增加蓝牙数据传输功能</h2>
<p></p>
<h3>2.1 配置</h3>
<p>打开sense_bhi160.retconfig,确保以上三个已经配置,第一个为电池监测服务,第二个用于传输传感器数据的服务,第三个用于蓝牙功能服务。</p>
<h3><b>2.2 初始化</b></h3>
<p>在main函数中添加蓝牙初始化函数:</p>
<pre>
<code class="language-cpp">BDK_BLE_Initialize();//蓝牙初始化
BLE_BASS_Initialize(50,1);//电池采用速率50,输入参数1
BLE_ICS_Initialize(&CustomService_WriteInd);//初始化数据传输服务,CustomService_WriteInd为接收数据回调函数,可以处理蓝牙接收的数据。
void CustomService_WriteInd(struct BLE_ICS_RxIndData *ind)
{
/* Print at most ind->data_len characters from ind->data array. */
printf("APP: Received message \'%.*s\'\r\n", ind->data_len, ind->data);
/* Echo received message back to TX characteristic. */
//BLE_ICS_Notify(ind->data, ind->data_len);
}</code></pre>
<h3>2.3 增加notify蓝牙数据服务</h3>
<p style="margin-left: 40px;">uint32_t BLE_ICS_Notify(uint8_t *data, uint16_t data_len)</p>
<p>该函数可以将设备的数据通过蓝牙传输到手机端,比如说我将改函数用于以下情况</p>
<pre>
<code class="language-cpp"> void la_vector_cb(bhy_data_generic_t *data, bhy_virtual_sensor_t sensor)
{
/* Linear Acceleration sensor values are scaled using dynamic range. */
uint16_t dyn_range = BHI160_NDOF_GetAccelDynamicRange();
float x = data->data_vector.x / 32768.0f * dyn_range;
float y = data->data_vector.y / 32768.0f * dyn_range;
float z = data->data_vector.z / 32768.0f * dyn_range;
if (fabsf(x) + fabsf(y) + fabsf(z) >= 1.0f)
{
LED_On(LED_RED);
}
else
{
LED_Off(LED_RED);
}
ble_data_tx = data->data_vector.x>>8;
ble_data_tx = data->data_vector.x;
ble_data_tx = data->data_vector.y>>8;
ble_data_tx = data->data_vector.y;
ble_data_tx = data->data_vector.z>>8;
ble_data_tx = data->data_vector.z;
ble_data_tx++;//第一个字节为包序,后面的为有效数据
BLE_ICS_Notify(ble_data_tx, 20);
printf("Linear Accel: x=%.2f g, y=%.2f g, z=%.2f g\r\n", x, y, z);
}</code></pre>
<h3>2.4 手机蓝牙联调</h3>
<p>后面会接入手机端联调,这里的演示我选用蓝牙调试工具lightblue。如图,连上设备后,可以看到蓝牙名称为BLE_DEMO,电池容量为83%,进入DATA_VALUE,选择listen for notifications,可以看到设备发过来的数据,经过测试,无丢包情况。</p>
<p></p>
<p> </p>
<p>恭喜恭喜 ,加油加油</p>
页:
[1]