ROS库中本身带有kinect的驱动和应用包,但是很难应用到嵌入式系统当中,但是单独的openni库已经很完善了,很容易应用在PC机和嵌入式系统当中。为了在嵌入式的ARM系统中应用ROS和kinect,首先我在PC机上进行ROS链接openni库的实验。
一、安装openni库
参考链接:http://www.20papercups.net/programming/kinect-on-ubuntu-with-openni/
二、建立kinect_test包
首先建立一个ROS的package,然后在src文件夹下建立代码文件,结合ROS教程中WritingPublisherSubscriber(C++)一章中的编程方法和openni中samples里的NiSimpleRead例程代码,使用openni库中的功能函数获取kinect数据,并使用ROS进行消息发布。代码如下(代码包下载请见http://download.csdn.net/detail/hcx25909/5093853):
1、kinect_value_talker.cpp
/**************************************************************************** * * * OpenNI 1.x Alpha * * Copyright (C) 2011 PrimeSense Ltd. * * * * This file is part of OpenNI. * * * * OpenNI is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as published * * by the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * OpenNI is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public License * * along with OpenNI. If not, see <http://www.gnu.org/licenses/>. * * * ****************************************************************************/ //--------------------------------------------------------------------------- // Includes //--------------------------------------------------------------------------- #include <XnOpenNI.h> #include <XnLog.h> #include <XnCppWrapper.h> #include <XnFPSCalculator.h> #include "ros/ros.h" #include "std_msgs/String.h" #include <sstream> //--------------------------------------------------------------------------- // Defines //--------------------------------------------------------------------------- #define SAMPLE_XML_PATH "Data/SamplesConfig.xml" //--------------------------------------------------------------------------- // Macros //--------------------------------------------------------------------------- #define CHECK_RC(rc, what) \ if (rc != XN_STATUS_OK) \ { \ printf("%s failed: %s\n", what, xnGetStatusString(rc)); \ return rc; \ } //--------------------------------------------------------------------------- // Code //--------------------------------------------------------------------------- using namespace xn; XnBool fileExists(const char *fn) { XnBool exists; xnOSDoesFileExist(fn, &exists); return exists; } int main(int argc, char **argv) { XnStatus nRetVal = XN_STATUS_OK; Context context; ScriptNode scriptNode; EnumerationErrors errors; ros::init(argc, argv, "talker"); ros::NodeHandle n; ros::Publisher chatter_pub = n.advertise<std_msgs::String>("kinect_value", 1000); ros::Rate loop_rate(10); const char *fn = NULL; if (fileExists(SAMPLE_XML_PATH)) fn = SAMPLE_XML_PATH; else { printf("Could not find '%s'. Aborting.\n" , SAMPLE_XML_PATH); return XN_STATUS_ERROR; } printf("Reading config from: '%s'\n", fn); nRetVal = context.InitFromXmlFile(fn, scriptNode, &errors); if (nRetVal == XN_STATUS_NO_NODE_PRESENT) { XnChar strError[1024]; errors.ToString(strError, 1024); printf("%s\n", strError); return (nRetVal); } else if (nRetVal != XN_STATUS_OK) { printf("Open failed: %s\n", xnGetStatusString(nRetVal)); return (nRetVal); } DepthGenerator depth; nRetVal = context.FindExistingNode(XN_NODE_TYPE_DEPTH, depth); CHECK_RC(nRetVal, "Find depth generator"); XnFPSData xnFPS; nRetVal = xnFPSInit(&xnFPS, 180); CHECK_RC(nRetVal, "FPS Init"); DepthMetaData depthMD; while ((!xnOSWasKeyboardHit()) && (ros::ok())) { std_msgs::String msg; std::stringstream ss; nRetVal = context.WaitOneUpdateAll(depth); if (nRetVal != XN_STATUS_OK) { printf("UpdateData failed: %s\n", xnGetStatusString(nRetVal)); continue; } xnFPSMarkFrame(&xnFPS); depth.GetMetaData(depthMD); const XnDepthPixel* pDepthMap = depthMD.Data(); ss << "Frame " << depthMD.FrameID(); ss << " Middle point is: " << depthMD(depthMD.XRes() / 2, depthMD.YRes() / 2); ss << " FPS: " << xnFPSCalc(&xnFPS); msg.data = ss.str(); ROS_INFO("%s", msg.data.c_str()); chatter_pub.publish(msg); ros::spinOnce(); loop_rate.sleep(); } depth.Release(); scriptNode.Release(); context.Release(); return 0; }
#include "ros/ros.h"
#include "std_msgs/String.h"
/**
* This tutorial demonstrates simple receipt of messages over the ROS system.
*/
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.c_str());
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("kinect_value", 1000, chatterCallback);
ros::spin();
return 0;
}
参考链接:http://www.ros.org/wiki/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29
三、修改编译文件
首先在CMakeLists.txt中将程序和openni库进行链接。在文件中加入头文件路径:
include_directories ("/usr/include/ni/")
加入编译的文件和链接的库:
rosbuild_add_executable(kinect_value_talker src/kinect_value_talker.cpp)
rosbuild_add_executable(kinect_value_listener src/kinect_value_listener.cpp)
target_link_libraries(kinect_value_talker OpenNI)
然后进行rosmake编译。
实验结果:
参考链接:http://www.ros.org/wiki/openni
----------------------------------------------------------------
欢迎大家转载我的文章。
转载请注明:转自古-月
http://blog.csdn.net/hcx25909
欢迎继续关注我的博客