PX4应用实例3:px4_simple_app
2017-04-02 09:38
197 查看
1. 打开src/examples/px4_simple_app/px4_simple_app.c
#include <px4_config.h>#include <px4_tasks.h>
#include <px4_posix.h>
#include <unistd.h>
#include <stdio.h>
#include <poll.h>
#include <string.h>
#include <math.h>
#include <uORB/uORB.h>
#include <uORB/topics/sensor_combined.h>
#include <uORB/topics/vehicle_attitude.h>
__EXPORT int px4_simple_app_main(int argc, char *argv[]);
int px4_simple_app_main(int argc, char *argv[])
{
PX4_INFO("Hello Sky!");
/* 订阅 sensor_combined主题*/
int sensor_sub_fd = orb_subscribe(ORB_ID(sensor_combined));
orb_set_interval(sensor_sub_fd, 200);//限制更新频率为5 Hz
/* 1.公告attitude主题 */
struct vehicle_attitude_s att;
memset(&att, 0, sizeof(att));
orb_advert_t att_pub = orb_advertise(ORB_ID(vehicle_attitude), &att);
/* 可以在此等待多个主题 */
px4_pollfd_struct_t fds[] =
{
{ .fd = sensor_sub_fd, .events = POLLIN },
};
int error_counter = 0;
for (int i = 0; i < 5; i++)
{
/* 等待1000ms获取数据 */
int poll_ret = px4_poll(fds, 1, 1000);
/* 处理结果*/
if (poll_ret == 0) //未得到数据
{
PX4_ERR("Got no data within a second");
}
else if (poll_ret < 0) //严重错误
{
if (error_counter < 10 || error_counter % 50 == 0)
{
/* use a counter to prevent flooding (and slowing us down) */
PX4_ERR("ERROR return value from poll(): %d", poll_ret);
}
error_counter++;
}
else //抓到数据
{
if (fds[0].revents & POLLIN)
{
/* 复制sensor_combined公告主题 */
struct sensor_combined_s raw;
orb_copy(ORB_ID(sensor_combined), sensor_sub_fd, &raw);
PX4_INFO("Accelerometer:\t%8.4f\t%8.4f\t%8.4f",
(double)raw.accelerometer_m_s2[0],
(double)raw.accelerometer_m_s2[1],
(double)raw.accelerometer_m_s2[2]);
/* q在.msg中定义*/
att.q[0] = raw.accelerometer_m_s2[0];
att.q[1] = raw.accelerometer_m_s2[1];
att.q[2] = raw.accelerometer_m_s2[2];
/*2. 发布attitude主题*/
orb_publish(ORB_ID(vehicle_attitude), att_pub, &att);
}
/* if (fds[1..n].revents & POLLIN) {} */
}
}
PX4_INFO("exiting");
return 0;
}
2. 修改Firmware\cmake\configs\nuttx_px4fmu-v2_default.cmake文件
将# examples/px4_simple_app中的#删除3.运行teraterm
相关文章推荐
- Kivy crash 中文教程 实例入门 1. 第1个应用 Kivy App (Making a simple App)
- Kivy crash 中文教程 实例入门 1. 第1个应用 Kivy App (Making a simple App)
- Kivy 中文教程 实例入门 简易画板 (Simple Paint App):3. 随机颜色及清除按钮
- phonegap整合调用dwr实例,web应用轻松变身移动APP
- Kivy 中文教程 实例入门 简易画板 (Simple Paint App):1. 自定义窗口部件 (widget)
- PX4应用实例2:创建uORB消息
- Kivy 中文教程 实例入门 简易画板 (Simple Paint App):0. 项目简介 & 成果展示
- PX4源码初学习(px4_simple_app.c)
- Yii框架文摘之二yii的入口与app应用实例(Yii::app())
- simple-spa 一个简单的单页应用实例
- simple-spa 一个简单的单页应用实例
- Kivy 中文教程 实例入门 简易画板 (Simple Paint App):1. 自定义窗口部件 (widget)
- PX4应用实例1:HelloWorld!
- 桌面组件Appwidget的应用实例以及导入第三方jar包的问题解决
- X5平台simpleSelect、gridSelect、treeSelect应用实例
- simple-spa 一个简单的单页应用实例
- Kivy 中文教程 实例入门 简易画板 (Simple Paint App):3. 随机颜色及清除按钮
- app.config自定义应用程序配置文件介绍与应用实例
- Kivy 中文教程 实例入门 简易画板 (Simple Paint App):2. 实现绘图功能
- zstack SimpleApp和GenericApp实例绑定程序流程