openni_swipe.cpp
Go to the documentation of this file.
00001 // ros
00002 #include <ros/ros.h>
00003 #include <ros/package.h>
00004 #include <std_msgs/UInt8.h>
00005 #include <std_msgs/String.h>
00006 
00007 ros::Publisher swipe_pub;
00008 ros::Publisher swipe_status_pub;
00009 //
00010 
00011 //NITE
00012 #include <XnOpenNI.h>
00013 #include <XnCppWrapper.h>
00014 #include <XnVHandPointContext.h>
00015 #include <XnVSessionManager.h>
00016 #include <XnVSwipeDetector.h>
00017 #include <XnVSteadyDetector.h>
00018 #include <XnVPushDetector.h>
00019 #include <XnVWaveDetector.h>
00020 
00021 // NITE objects
00022 XnVSessionManager* g_pSessionManager = NULL;
00023 XnVSwipeDetector*  g_pSwipeDetector;
00024 XnVPushDetector*   g_pPushDetector;
00025 XnVWaveDetector*   g_pWaveDetector;
00026 XnVSteadyDetector* g_pSteadyDetector;
00027 
00028 typedef enum
00029 {
00030     IN_SESSION,
00031     NOT_IN_SESSION,
00032     QUICK_REFOCUS
00033 } SessionState;
00034 SessionState g_SessionState = NOT_IN_SESSION;
00035 
00036 xn::Context g_Context;
00037 
00038 void Publish_String(std::string str){
00039   std_msgs::String msg;
00040   msg.data = str;
00041   ROS_INFO((str + " published").c_str());
00042   swipe_pub.publish(msg);
00043 }
00044 
00045 // Swipe detector
00046 void XN_CALLBACK_TYPE Swipe_SwipeUp(XnFloat fVelocity, XnFloat fAngle, void* cxt)
00047 {
00048   Publish_String("Up");
00049 }
00050 
00051 void XN_CALLBACK_TYPE Swipe_SwipeDown(XnFloat fVelocity, XnFloat fAngle, void* cxt)
00052 {
00053   Publish_String("Down");
00054 }
00055 
00056 void XN_CALLBACK_TYPE Swipe_SwipeLeft(XnFloat fVelocity, XnFloat fAngle, void* cxt)
00057 {
00058   Publish_String("Left");
00059 }
00060 
00061 void XN_CALLBACK_TYPE Swipe_SwipeRight(XnFloat fVelocity, XnFloat fAngle, void* cxt)
00062 {
00063   Publish_String("Right");
00064 }
00065 
00066 // Steady detector
00067 void XN_CALLBACK_TYPE Steady_OnSteady(XnUInt32 nId, XnFloat fVelocity, void* cxt)
00068 {
00069   Publish_String("Standby");
00070 }
00071 
00072 // Push detector
00073 void XN_CALLBACK_TYPE Push_OnPush(XnFloat fVelocity, XnFloat fAngle, void* cxt)
00074 {
00075   Publish_String("Push");
00076 }
00077 
00078 void CleanupExit()
00079 {
00080   if (NULL != g_pSessionManager) {
00081     delete g_pSessionManager;
00082     g_pSessionManager = NULL;
00083   }
00084 
00085   delete g_pSwipeDetector;
00086   delete g_pSteadyDetector;
00087 
00088   g_Context.Shutdown();
00089 
00090   exit (1);
00091 }
00092 
00093 void XN_CALLBACK_TYPE SessionStart(const XnPoint3D& pFocus, void* UserCxt)
00094 {
00095   ROS_INFO("hand gesture detection start");
00096   g_SessionState = IN_SESSION;
00097 }
00098 
00099 void XN_CALLBACK_TYPE SessionEnd(void* UserCxt)
00100 {
00101   ROS_INFO("hand gesture detection end");
00102   g_SessionState = NOT_IN_SESSION;
00103 }
00104 
00105 void XN_CALLBACK_TYPE NoHands (void *UserCxt)
00106 {
00107   printf ("Quick refocus\n");
00108   g_SessionState = QUICK_REFOCUS;
00109 }
00110 
00111 #define CHECK_RC(rc, what)                                      \
00112   if (rc != XN_STATUS_OK)                                       \
00113     {                                                           \
00114       printf("%s failed: %s\n", what, xnGetStatusString(rc));   \
00115       return rc;                                                \
00116     }
00117 
00118 #define CHECK_ERRORS(rc, errors, what)          \
00119   if (rc == XN_STATUS_NO_NODE_PRESENT)          \
00120     {                                           \
00121       XnChar strError[1024];                    \
00122       errors.ToString(strError, 1024);          \
00123       printf("%s\n", strError);                 \
00124       return (rc);                              \
00125     }
00126 
00127 int main(int argc, char **argv)
00128 {
00129   ros::init (argc, argv, "openni_swipe");
00130   ros::NodeHandle nh;
00131 
00132   swipe_pub = nh.advertise<std_msgs::String>("swipe",10);
00133   swipe_status_pub = nh.advertise<std_msgs::UInt8>("swipe/status",10);
00134 
00135   XnStatus rc = XN_STATUS_OK;
00136   xn::EnumerationErrors errors;
00137   std::string configfile = ros::package::getPath ("openni_swipe") + "/openni_swipe.xml";
00138 
00139   rc = g_Context.InitFromXmlFile(configfile.c_str());
00140   CHECK_ERRORS(rc, errors, "InitFromXmlFile");
00141   CHECK_RC(rc, "InitFromXml");
00142 
00143   // Create and initialize point tracker
00144   g_pSessionManager = new XnVSessionManager;
00145   rc = g_pSessionManager->Initialize(&g_Context, "Wave", "RaiseHand");
00146   if (rc != XN_STATUS_OK)
00147         {
00148       printf("Couldn't initialize the Session Manager: %s\n", xnGetStatusString(rc));
00149       delete g_pSessionManager;
00150       return rc;
00151         }
00152   g_pSessionManager->RegisterSession(NULL, &SessionStart, &SessionEnd);
00153 
00154   g_pSwipeDetector = new XnVSwipeDetector;
00155   g_pSteadyDetector = new XnVSteadyDetector;
00156   g_pPushDetector = new XnVPushDetector;
00157   g_pWaveDetector = new XnVWaveDetector;
00158 
00159   // Swipe
00160   g_pSwipeDetector->RegisterSwipeUp(NULL, &Swipe_SwipeUp);
00161   g_pSwipeDetector->RegisterSwipeDown(NULL, &Swipe_SwipeDown);
00162   g_pSwipeDetector->RegisterSwipeLeft(NULL, &Swipe_SwipeLeft);
00163   g_pSwipeDetector->RegisterSwipeRight(NULL, &Swipe_SwipeRight);
00164   // Steady
00165   g_pSteadyDetector->RegisterSteady(NULL, &Steady_OnSteady);
00166   // Push
00167   g_pPushDetector->RegisterPush(NULL, &Push_OnPush);
00168 
00169   // Connect flow manager to the point tracker
00170   g_pSessionManager->AddListener(g_pSwipeDetector);
00171   g_pSessionManager->AddListener(g_pSteadyDetector);
00172   g_pSessionManager->AddListener(g_pPushDetector);
00173 
00174   g_Context.StartGeneratingAll();
00175   ros::Rate r (30);
00176   while (ros::ok ()){
00177     // Read next available data
00178     //g_Context.WaitOneUpdateAll(g_DepthGenerator);
00179     g_Context.WaitAndUpdateAll();
00180 
00181     // Process the data
00182     g_pSessionManager->Update(&g_Context);
00183 
00184     switch (g_SessionState)
00185     {
00186     case IN_SESSION:
00187         break;
00188     case NOT_IN_SESSION:
00189         ROS_INFO ("Perform wave gestures to track hand");
00190         break;
00191     case QUICK_REFOCUS:
00192         ROS_INFO ("Raise your hand for it to be identified");
00193         break;
00194     }
00195     std_msgs::UInt8 msg;
00196     msg.data = g_SessionState;
00197     swipe_status_pub.publish(msg);
00198 
00199     // ros sleep
00200     r.sleep();
00201   }
00202   CleanupExit();
00203 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


openni_swipe
Author(s): tsuda
autogenerated on Sat Mar 23 2013 17:54:06