Commits

schlangen committed e0c0934

rotate camera by raising left or right hand.

Comments (0)

Files changed (4)

share/tilt_kinect.c

                 state.x = -deltaZ;
                 state.z = -deltaX;
 		
-		set_button(&state.U, button == 1);  // <- make this rotate the camera (needs further changes in main neverball code as rotate-buttons are not in state)
-		set_button(&state.D, button == 2);
-		
+		set_button(&state.minus, button == 1);
+		set_button(&state.plus, button == 2);		
             }
             SDL_mutexV(mutex);
     }
                 *b = config_get_d(CONFIG_JOYSTICK_DPAD_D);
                 *s = (ch == BUTTON_DN);
             }
+            else if ((ch = get_button(&state.plus)))
+            {
+                *b = config_get_d(CONFIG_JOYSTICK_BUTTON_R);
+                *s = (ch == BUTTON_DN);
+            }
+            else if ((ch = get_button(&state.minus)))
+            {
+                *b = config_get_d(CONFIG_JOYSTICK_BUTTON_L);
+                *s = (ch == BUTTON_DN);
+            }
         }
         SDL_mutexV(mutex);
     }

share/tilt_kinect.c~

-/*
- * Copyright (C) 2003 Robert Kooima
- * Copyright (C) 2012 Julian Habrock
- *
- * NEVERBALL is  free software; you can redistribute  it and/or modify
- * it under the  terms of the GNU General  Public License as published
- * by the Free  Software Foundation; either version 2  of the License,
- * or (at your option) any later version.
- *
- * This program 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
- * General Public License for more details.
- */
-
-#include <SDL.h>
-#include <SDL_thread.h>
-#include <math.h>
-#include <stdio.h>
-
-#include "config.h"
-
-#include <stdlib.h>
-
-
-
-/*---------------------------------------------------------------------------*/
-
-/* FIXME: I *did* copy paste this from tilt_loop.c, I swear! */
-
-/*
- * This data structure tracks button changes, counting transitions so that
- * none are missed if the event handling thread falls significantly behind
- * the device IO thread.
- */
-
-#define BUTTON_NC 0
-#define BUTTON_DN 1
-#define BUTTON_UP 2
-
-struct button_state
-{
-    unsigned char curr;
-    unsigned char last;
-    unsigned char upc;
-    unsigned char dnc;
-};
-
-static void set_button(struct button_state *B, int s)
-{
-    if ((B->curr == 0) != (s == 0))
-    {
-        if (B->curr)
-        {
-            B->upc++;
-            B->curr = 0;
-        }
-        else
-        {
-            B->dnc++;
-            B->curr = 1;
-        }
-    }
-}
-
-static int get_button(struct button_state *B)
-{
-    int ch = BUTTON_NC;
-
-    if      (B->last == 1 && B->upc > 0)
-    {
-        B->upc--;
-        B->last = 0;
-        ch = BUTTON_UP;
-    }
-    else if (B->last == 0 && B->dnc > 0)
-    {
-        B->dnc--;
-        B->last = 1;
-        ch = BUTTON_DN;
-    }
-
-    return ch;
-}
-
-/*---------------------------------------------------------------------------*/
-
-struct tilt_state
-{
-    int   status;
-    float x;
-    float z;
-    struct button_state A;
-    struct button_state B;
-    struct button_state plus;
-    struct button_state minus;
-    struct button_state home;
-    struct button_state L;
-    struct button_state R;
-    struct button_state U;
-    struct button_state D;
-};
-
-static struct tilt_state state;
-static SDL_mutex        *mutex  = NULL;
-static SDL_Thread       *thread = NULL;
-
-/*---------------------------------------------------------------------------*/
-
-/* kinect / OpenNI part */
-
-
-#include <XnOpenNI.h>
-
-
-
-// openni variables
-
-
-static XnContext *pContext;
-static XnNodeHandle hDepth;
-static XnDepthMetaData *pDepthMD;
-static XnNodeHandle hUserGenerator;
-
-static XnChar g_strPose[20] = "";
-
-// caches all joint coords
-static float allJointCoords[24][3];
-
-
-
-//  New user
-void XN_CALLBACK_TYPE new_user(XnNodeHandle generator,
-			       XnUserID nId, 
-			       void* pCookie) {
-	printf("New User %d\n", nId);
-
-	xnRequestSkeletonCalibration(generator, nId, TRUE);
-}
-
-// user lost
-void XN_CALLBACK_TYPE lost_user(XnNodeHandle generator,
-				XnUserID nId, 
-				void* pCookie) {
-	printf("Lost user %d\n", nId);
-	
-}
-
-
-// Callback: Detected a pose
-void XN_CALLBACK_TYPE pose_detected(XnNodeHandle capability, const XnChar* strPose, XnUserID nId, void* pCookie) {
-	printf("Pose %s detected for user %d\n", strPose, nId);
-	
-	xnStopPoseDetection(capability, nId);
-	xnRequestSkeletonCalibration(capability, nId, TRUE);
-}
-
-
-// Callback: Started calibration
-void XN_CALLBACK_TYPE calibration_started(XnNodeHandle capability, XnUserID nId, void* pCookie) {
-	printf("Calibration started for user %d\n", nId);
-}
-
-
-
-// Callback: Finished calibration
-void XN_CALLBACK_TYPE calibration_ended(XnNodeHandle capability, XnUserID nId, XnCalibrationStatus status, void* pCookie) {
-	if (status != XN_CALIBRATION_STATUS_NO_USER) {
-		printf("Calibration complete, start tracking user %d\n", nId);
-		xnStartSkeletonTracking(capability, nId);
-	}
-	else {
-		printf("Calibration failed for user %d\n", nId);
-		xnRequestSkeletonCalibration(capability, nId, TRUE);
-	}
-}
-
-
-
-int jointPos(XnUserID player, XnSkeletonJoint eJoint) {
-        
-	XnSkeletonJointTransformation jointTrans;
-
-	//userGenerator.GetSkeletonCap().GetSkeletonJoint(player, eJoint, jointTrans);
-	xnGetSkeletonJoint(hUserGenerator, player, eJoint, &jointTrans);
-
-
-	//if (jointTrans.position.fConfidence < 0.5) {
-	//	return 0;
-	//}
-
-	XnPoint3D realwordPoint;
-	realwordPoint.X = 0; realwordPoint.Y = 0; realwordPoint.Z = 0;
-	
-	//depth.ConvertProjectiveToRealWorld(1, &jointTrans.position.position, &realwordPoint); 
-	xnConvertProjectiveToRealWorld(hDepth, 1, &jointTrans.position.position, &realwordPoint); 
-	
-	allJointCoords[eJoint-1][0] = realwordPoint.X;
-	allJointCoords[eJoint-1][1] = realwordPoint.Y;
-	allJointCoords[eJoint-1][2] = realwordPoint.Z;
-	
-	return 0;
-}
-
-
-void get_body_positions(){
-  
-        int userCount = xnGetNumberOfUsers(hUserGenerator);
-	XnUserID *aUsers = malloc(userCount*sizeof(XnUserID));
-	XnUInt16 nUsers = userCount * sizeof(XnUserID);
-	for(int i = 0;i<userCount;i++){
-	  aUsers[i] = 0;
-	}
-	xnGetUsers(hUserGenerator, aUsers, &nUsers);
-	if (nUsers > 0){
-		//if (userGenerator.GetSkeletonCap().IsTracking(aUsers[0])) {
-		 if (xnIsSkeletonTracking(hUserGenerator,aUsers[0])){
-		  printf("is skeleton tracking");
-		  for(int j=1;j<25;j++){
-		    jointPos(aUsers[0], (XnSkeletonJoint) j);
-		  }		
-		}
-	}
-	free(aUsers);
-}
-
-void checkRetVal(XnStatus nRetVal) {
-	if (nRetVal != XN_STATUS_OK) {
-		printf("There was a problem initializing kinect... Make sure you have \
-connected both usb and power cables and that the driver and OpenNI framework \
-are correctly installed.\n\n");
-		printf("Error was: %s\n", xnGetStatusString(nRetVal));
-		exit(1);
-	}
-}
-
-void main_loop() {
-	// Read next available data
-	//context.WaitAnyUpdateAll();
-	xnWaitAnyUpdateAll(pContext);
-	
-	// Process the data
-	//depth.GetMetaData(depthMD);
-	xnGetDepthMetaData(hDepth, pDepthMD);
-	
-	get_body_positions();
-}
-
-
-void init_kinect() {
-	printf("Initializing...\n");
-	
-	XnStatus nRetVal = XN_STATUS_OK;
-	XnCallbackHandle hUserCallbacks, hCalibrationCallbacks, hPoseCallbacks;
-
-
-	//context.Init();
-	xnInit(&pContext);
-	
-	pDepthMD = xnAllocateDepthMetaData();
-
-
-	//depth.Create(context);
-	
-	nRetVal = xnCreateDepthGenerator(pContext, &hDepth, NULL, NULL);//&errors);
-	// TODO: error handling
-	if (nRetVal != (XnStatus)0){
-	  printf("error at depth generator create!");
-	}
-	
-	if (nRetVal == XN_STATUS_NO_NODE_PRESENT)
-	{
-	
-	}
-	else if (nRetVal != XN_STATUS_OK)
-	{
-	    printf("Create failed: %s\n", xnGetStatusString(nRetVal));
-	}
-
-
-	nRetVal = xnCreateUserGenerator(pContext, &hUserGenerator, NULL, NULL);
-
-	if (nRetVal != (XnStatus)0){
-	   printf("error at user generator create!");
-	}
-
-
-	//checkRetVal(userGenerator.RegisterUserCallbacks(new_user, lost_user, NULL, hUserCallbacks));
-	//checkRetVal(userGenerator.GetSkeletonCap().RegisterCalibrationCallbacks(calibration_started, calibration_ended, NULL, hCalibrationCallbacks));
-	//checkRetVal(userGenerator.GetPoseDetectionCap().RegisterToPoseCallbacks(pose_detected, NULL, NULL, hPoseCallbacks));
-	//checkRetVal(userGenerator.GetSkeletonCap().GetCalibrationPose(g_strPose));
-	//checkRetVal(userGenerator.GetSkeletonCap().SetSkeletonProfile(XN_SKEL_PROFILE_ALL));
-	
-	checkRetVal(xnRegisterUserCallbacks(hUserGenerator, &new_user, &lost_user, NULL, &hUserCallbacks));
-	checkRetVal(xnRegisterToCalibrationStart(hUserGenerator, &calibration_started, NULL, &hCalibrationCallbacks));
-	checkRetVal(xnRegisterToCalibrationComplete(hUserGenerator, &calibration_ended, NULL, &hCalibrationCallbacks));		
-	checkRetVal(xnRegisterToPoseDetected(hUserGenerator, &pose_detected, NULL, &hPoseCallbacks));
-	checkRetVal(xnGetSkeletonCalibrationPose(hUserGenerator, g_strPose));
-	checkRetVal(xnSetSkeletonProfile(hUserGenerator, XN_SKEL_PROFILE_ALL));
-	
-	printf("Initialized Kinect, looking for users...\n\n");
-	
-	//context.StartGeneratingAll();	
-	xnStartGeneratingAll(pContext);
-
-	
-}
-
-
-void free_kinect(){
-  //xnFreeDepthMetaData(pDepthMD);
-  xnContextRelease(pContext);
-}
-
-static int tilt_func(void *data)
-{
-       printf("hello kinect world! inited!");
-       
-    int running = 1;
-
-    SDL_mutexP(mutex);
-    state.status = running;
-    SDL_mutexV(mutex);
-
-    while (mutex && running)
-    {
-        SDL_mutexP(mutex);
-        running = state.status;
-        SDL_mutexV(mutex);
-
-	    main_loop();
-	    	    
-            SDL_mutexP(mutex);
-            {
-             float deltaX, deltaZ;
-  
-	      // difference in mm from neck to waist
-	      deltaX = (allJointCoords[XN_SKEL_NECK-1][0] - allJointCoords[XN_SKEL_WAIST-1][0]) / 20;//200;
-	      deltaZ = (allJointCoords[XN_SKEL_NECK-1][2] - allJointCoords[XN_SKEL_WAIST-1][2] - 10) / 5;//100;
-	      
-	   /*  if (deltaX > 1) deltaX = 1;
-	      else if (deltaX < -1) deltaX = -1;
-	      if (deltaZ > 1) deltaZ = 1;
-	      else if (deltaZ < -1) deltaZ = -1;*/
-	      
-	      int button = 0;
-	      
-	      if (allJointCoords[XN_SKEL_RIGHT_HAND][1] < allJointCoords[XN_SKEL_HEAD][1]){  // small y -> high object, ie if hand.y < head.y -> hand is above head
-		button = 1;
-		printf("button: 1!");
-	      }else if (allJointCoords[XN_SKEL_LEFT_HAND][1] < allJointCoords[XN_SKEL_HEAD][1]){
-		button = 2;
-		printf("button: 2!");
-	      }
-	      
-                state.x = -deltaZ;
-                state.z = -deltaX;
-		
-		set_button(&state.U, button == 1);  // <- make this rotate the camera (needs further changes in main neverball code as rotate-buttons are not in state)
-		set_button(&state.D, button == 2);
-		
-            }
-            SDL_mutexV(mutex);
-     //   printf("test");
-
-    }
-
-    //context.Shutdown();
-    free_kinect();
-    return 0;
-}
-
-
-
-void tilt_init(void)
-{
-    memset(&state, 0, sizeof (struct tilt_state));
-
-    init_kinect();
-
-    mutex  = SDL_CreateMutex();
-    thread = SDL_CreateThread(tilt_func, NULL);
-}
-
-void tilt_free(void)
-{
-    int b = 0;
-
-    if (mutex)
-    {
-        /* Get/set the status of the tilt sensor thread. */
-
-        SDL_mutexP(mutex);
-        b = state.status;
-        state.status = 0;
-        SDL_mutexV(mutex);
-
-        /* Kill the thread and destroy the mutex. */
-
-        SDL_WaitThread(thread, &b);
-        SDL_DestroyMutex(mutex);
-
-        mutex  = NULL;
-        thread = NULL;
-
-	free_kinect();
-    }
-}
-
-int tilt_get_button(int *b, int *s)
-{
-    int ch = BUTTON_NC;
-
-    if (mutex)
-    {
-        SDL_mutexP(mutex);
-        {
-            if      ((ch = get_button(&state.A)))
-            {
-                *b = config_get_d(CONFIG_JOYSTICK_BUTTON_A);
-                *s = (ch == BUTTON_DN);
-            }
-            else if ((ch = get_button(&state.B)))
-            {
-                *b = config_get_d(CONFIG_JOYSTICK_BUTTON_B);
-                *s = (ch == BUTTON_DN);
-            }
-            else if ((ch = get_button(&state.home)))
-            {
-                *b = config_get_d(CONFIG_JOYSTICK_BUTTON_EXIT);
-                *s = (ch == BUTTON_DN);
-            }
-            else if ((ch = get_button(&state.U)))
-            {
-                *b = config_get_d(CONFIG_JOYSTICK_DPAD_U);
-                *s = (ch == BUTTON_DN);
-            }
-            else if ((ch = get_button(&state.D)))
-            {
-                *b = config_get_d(CONFIG_JOYSTICK_DPAD_D);
-                *s = (ch == BUTTON_DN);
-            }
-        }
-        SDL_mutexV(mutex);
-    }
-    return ch;
-}
-
-float tilt_get_x(void)
-{
-    float x = 0.0f;
-
-    if (mutex)
-    {
-        SDL_mutexP(mutex);
-        x = state.x;
-        SDL_mutexV(mutex);
-    }
-
-    return x;
-}
-
-float tilt_get_z(void)
-{
-    float z = 0.0f;
-
-    if (mutex)
-    {
-        SDL_mutexP(mutex);
-        z = state.z;
-        SDL_mutexV(mutex);
-    }
-
-    return z;
-}
-
-int tilt_stat(void)
-{
-    int b = 0;
-
-    if (mutex)
-    {
-        SDL_mutexP(mutex);
-        b = state.status;
-        SDL_mutexV(mutex);
-    }
-    return b;
-}
-
-/*---------------------------------------------------------------------------*/

share/tilt_kinect.d~

Empty file removed.

share/tilt_kinect.o

Binary file removed.