本帖最后由 Crazy_GF 于 2014-8-7 09:31 编辑
ROS库中本身带有kinect的驱动和应用包,但是很难应用到嵌入式系统当中,但是单独的openni库已经很完善了,很容易应用在PC机和嵌入式系统当中。为了在嵌入式的ARM系统中应用ROS和kinect,首先我在PC机上进行ROS链接openni库的实验。 一、安装openni库
二、建立kinect_test包 1、kinect_value_talker.cpp
- <span style="font-size:14px;">/****************************************************************************
- * *
- * 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;
- }</span><span style="font-size: 18px;">
- </span>
复制代码
2、kinect_value_listener.cpp
- <span style="font-size:14px;">#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;
- }</span>
复制代码
参考链接: http://www.ros.org/wiki/ROS/Tuto ... criber%28c%2B%2B%29
三、修改编译文件 首先在CMakeLists.txt中将程序和openni库进行链接。在文件中加入头文件路径: [plain] view plaincopy 1. <span style="font-size:14px;">include_directories ("/usr/include/ni/")</span> 加入编译的文件和链接的库 - <span style="font-size:14px;">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)</span>
复制代码然后进行rosmake编译。 实验结果: |