Example #1
0
/*
 	Function:	InitHL
 	Usage:		InitHL();
 	---------------------------------------------------------------------------
 	Initializes haptic device and haptic rendering library.
  		
 */
void InitHL()
{
    HDErrorInfo error;
    hHD = hdInitDevice(HD_DEFAULT_DEVICE);
    if (HD_DEVICE_ERROR(error = hdGetError()))
    {
        hduPrintError(stderr, &error, "Failed to initialize haptic device");
        fprintf(stderr, "Press any key to exit");
        getchar();
        exit(1);
    }   
    
    // Create a haptic context for the device. The haptic context maintains 
    // the state that persists between frame intervals and is used for
    // haptic rendering.
    hHLRC = hlCreateContext(hHD);
    hlMakeCurrent(hHLRC); 

    // generate a shape id to hold the haptic surface rep
    mSurfaceShapeId = hlGenShapes(1);

    // add a callback to handle motion while touching surface
    hlAddEventCallback(HL_EVENT_TOUCH, mSurfaceShapeId, HL_CLIENT_THREAD, 
                       OnTouchSurface, NULL);
    hlAddEventCallback(HL_EVENT_UNTOUCH, mSurfaceShapeId, HL_CLIENT_THREAD, 
                       OnTouchSurface, NULL);
    // set motion tolerance to 0 so we get motion event when
    // when not moving.
    hlEventd(HL_EVENT_MOTION_LINEAR_TOLERANCE, 0);
    hlAddEventCallback(HL_EVENT_MOTION, mSurfaceShapeId, HL_CLIENT_THREAD, 
                       OnDragSurface, NULL);

}
void CHapticViewerView::initHL()
{
    HDErrorInfo error;
    m_hHD = hdInitDevice(HD_DEFAULT_DEVICE);
    if (HD_DEVICE_ERROR(error = hdGetError()))
    {
        char errorMsg[500];
        sprintf(errorMsg,
                "HD error %s\nHHD: %X\nError Code: %X\nInternalErrorCode: %d\n%s\n",
                hdGetErrorString(error.errorCode),
                error.hHD,
                error.errorCode,
                error.internalErrorCode,
                "Failed to initialize haptic device");
        MessageBox(errorMsg);
        getchar();
        exit(1);
    }   
    
    // Create a haptic context for the device. The haptic context maintains 
    // the state that persists between frame intervals and is used for
    // haptic rendering.
    m_hHLRC = hlCreateContext(m_hHD);
    hlMakeCurrent(m_hHLRC); 

    hlTouchableFace(HL_FRONT);
}
Example #3
0
	void CybPhantom::exitHL()
	{
		if(shapeIdListIscreated){
			for(int i = 0; i < numHapticLayers; i++)
				hlDeleteShapes(shapeId[i], 1);
			delete [] properties;
		}
	
	
		hlMakeCurrent(NULL);
		if(hapticContext != NULL)
		{
			hlDeleteContext(hapticContext);
		}
	
		if(hapticDevice != HD_INVALID_HANDLE)
		{
			hdDisableDevice(hapticDevice);
			hapticDevice = HD_INVALID_HANDLE;
		}
	
		if(ambientPropertyIsEnable){
			CybViscosity *viscosity = CybViscosity::getInstance();
			viscosity->stopAmbientProperty();
			delete viscosity;
		}
	
	}
Example #4
0
	int CybPhantom::initHL()
	{
		//TODO HABILITAR O OUTPUT FORCE NA HD.

		HDErrorInfo error;
	
		hapticDevice = hdInitDevice(HD_DEFAULT_DEVICE);
		if(HD_DEVICE_ERROR(error = hdGetError()))
		{
			//Falta tratar o erro!
			hduPrintError(stderr, &error, "Failed to initialize haptic device");
			return -1;
		}

		checkCalibrationStatus(&stateCalibration);	
	
		hapticContext = hlCreateContext(hapticDevice);
		hlMakeCurrent(hapticContext);
	
		hlTouchableFace(HL_FRONT_AND_BACK);
	
		hlEnable(HL_HAPTIC_CAMERA_VIEW);

		//hlEnable(HL_ADAPTIVE_VIEWPORT);
	
		//hdEnable(HD_FORCE_OUTPUT);

		return 0;
	}
Example #5
0
void init(void)
{
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

   // Enable depth buffering to provide depth information for OpenHaptics.
   glDepthFunc(GL_LEQUAL);
   glEnable(GL_DEPTH_TEST);

   // OpenHaptics setup follows:
   
   // Create a haptic device instance.
   HDErrorInfo error;
   HHD hHD = hdInitDevice(HD_DEFAULT_DEVICE);
   if (HD_DEVICE_ERROR(error = hdGetError()))
   {
       hduPrintError(stderr, &error, "Failed to initialize haptic device");
       fprintf(stderr, "Press any key to exit");
       getchar();
       exit(-1);
   }

   if (HD_SUCCESS != hdGetError().errorCode)
   {
       fprintf(stderr, "Erorr initializing haptic device.\nPress any key to exit");
       getchar();
       exit(-1);
   }
   
   // Create a haptic rendering context and activate it.
   HHLRC hHLRC = hlCreateContext(hHD);
   hlMakeCurrent(hHLRC);

   // Reserve an id for the shape
   gMyShapeId = hlGenShapes(1);

   // Specify the boundaries for the workspace of the haptic device
   // in millimeters in the cordinates of the haptic device.
   // The haptics engine will map the view volume to this workspace
   hlWorkspace (-80, -80, -70, 80, 80, 20);

   // Specify the haptic view volume (in this case it will be
   // the same as the graphic view volume).
   hlMatrixMode(HL_TOUCHWORKSPACE);
   hlLoadIdentity();
   hlOrtho (0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
Example #6
0
/*******************************************************************************
 Cleanup
*******************************************************************************/
void exitHandler()
{
    // free up the haptic rendering context
    hlMakeCurrent(NULL);
    if (hHLRC != NULL)
    {
        hlDeleteContext(hHLRC);
    }

    // free up the haptic device
    if (hHD != HD_INVALID_HANDLE)
    {
        hdDisableDevice(hHD);
    }
}
Example #7
0
/*******************************************************************************
 Initialize the HDAPI.  This involves initing a device configuration, enabling
 forces, and scheduling a haptic thread callback for servicing the device.
*******************************************************************************/
void initHL()
{
    HDErrorInfo error;
    ghHD = hdInitDevice( HD_DEFAULT_DEVICE );

    if (HD_DEVICE_ERROR(error = hdGetError()))
    {
        hduPrintError(stderr, &error, "Failed to initialize haptic device");
        fprintf(stderr, "Press any key to exit");
        getchar();
        exit(-1);
    }

/*************************************************************
ADDED
*************************************************************/
    hdScheduleAsynchronous(hdBeginCB, 0, HD_MAX_SCHEDULER_PRIORITY);
    hdScheduleAsynchronous(hdEndCB, 0, HD_MIN_SCHEDULER_PRIORITY);

/*************************************************************
END ADDED
*************************************************************/
    ghHLRC = hlCreateContext(ghHD);
    hlMakeCurrent(ghHLRC);

// Enable optimization of the viewing parameters when rendering
// geometry for OpenHaptics.
    hlEnable(HL_HAPTIC_CAMERA_VIEW);

// Generate id for the shape.
    gShapeId = hlGenShapes(1);
    gPointId = hlGenShapes(1);
    hlTouchableFace(HL_FRONT);

    hlAddEventCallback(HL_EVENT_TOUCH,gPointId , HL_COLLISION_THREAD,
        hlTouchCB, 0);

    hlAddEventCallback(HL_EVENT_UNTOUCH,gPointId , HL_COLLISION_THREAD,
        hlUnTouchCB, 0);
}
Example #8
0
/*******************************************************************************
 This handler is called when the application is exiting.  Deallocates any state 
 and cleans up.
*******************************************************************************/
void exitHandler()
{
    // Remove event callbacks.
    hlRemoveEventCallback(HL_EVENT_TOUCH, gSphereShapeId, 
        HL_CLIENT_THREAD, touchSphereCallback);
    hlRemoveEventCallback(HL_EVENT_UNTOUCH, gSphereShapeId, 
        HL_CLIENT_THREAD, untouchSphereCallback);
    hlRemoveEventCallback(HL_EVENT_TOUCH, gTorusShapeId, 
        HL_CLIENT_THREAD, touchTorusCallback);
    hlRemoveEventCallback(HL_EVENT_UNTOUCH, gTorusShapeId, 
        HL_CLIENT_THREAD, untouchTorusCallback);
    hlRemoveEventCallback(HL_EVENT_TOUCH, gTeapotShapeId, 
        HL_CLIENT_THREAD, touchTeapotCallback);
    hlRemoveEventCallback(HL_EVENT_UNTOUCH, gTeapotShapeId, 
        HL_CLIENT_THREAD, untouchTeapotCallback);
    hlRemoveEventCallback(HL_EVENT_CALIBRATION_UPDATE, HL_OBJECT_ANY,
        HL_CLIENT_THREAD, calibrationCallback);                       
    hlRemoveEventCallback(HL_EVENT_CALIBRATION_INPUT, HL_OBJECT_ANY,
        HL_CLIENT_THREAD, calibrationCallback);                       

    // Deallocate the shape ids we reserved in initHL.
    hlDeleteShapes(gSphereShapeId, 1);
    hlDeleteShapes(gTorusShapeId, 1);
    hlDeleteShapes(gSphereShapeId, 1);

    // Free up the haptic rendering context.
    hlMakeCurrent(NULL);
    if (hHLRC != NULL)
    {
        hlDeleteContext(hHLRC);
    }

    // Free up the haptic device.
    if (hHD != HD_INVALID_HANDLE)
    {
        hdDisableDevice(hHD);
    }
}
Example #9
0
/*******************************************************************************
 This handler is called when the application is exiting.  Deallocates any state
 and cleans up.
*******************************************************************************/
void exitHandler()
{
// Deallocate the sphere shape id we reserved in initHL.
    if (objmodel)
    {
        hlDeleteShapes(gShapeId, 1);
        hlDeleteShapes(gPointId, 1);
    }

// Free up the haptic rendering context.
    hlMakeCurrent(NULL);

    if (ghHLRC != NULL)
    {
        hlDeleteContext(ghHLRC);
    }

// Free up the haptic device.
    if (ghHD != HD_INVALID_HANDLE)
    {
        hdDisableDevice(ghHD);
    }
}
void CProtoHapticDoc::SimulationStep()
{
	int elapsed= clock() - m_lastSimStep;
	int steps= (int)((((float)elapsed)*m_simSpeed)*0.1f);

	if(steps<1) {
		m_lastSimStep= clock();
		return;
	}

	// collision detection
	for(int i= 0; i<m_shapeCount; i++) {
		if(m_shapes[i]->isCollisionDynamic())
			dGeomSetBody (m_geoms[i], bodies[i]);
		else
			dGeomSetBody (m_geoms[i], 0);
	}

	dSpaceCollide (m_spaceID,this,&nearCallbackStatic);

	dWorldQuickStep (m_worldID, steps);
	dJointGroupEmpty (m_jointGroup);
	for( int i= 0; i<m_shapeCount; i++) {
			//air resistance
			if(m_shapes[i]->isCollisionDynamic()||m_shapes[i]->isProxyDynamic()) {
				const dReal *vel;
				const dReal *angvel;
				
				vel= dBodyGetLinearVel (bodies[i]);
				dBodyAddForce (bodies[i], -vel[0]*m_airResistance, -vel[1]*m_airResistance, -vel[2]*m_airResistance);

				angvel= dBodyGetAngularVel (bodies[i]);
				dBodyAddTorque (bodies[i], -angvel[0]*m_airResistance, -angvel[1]*m_airResistance, -angvel[2]*m_airResistance);
			}

			HHLRC rc= hlGetCurrentContext();

			// proxy0
			hlMakeCurrent(m_context);
			if(m_shapes[i]->isProxyDynamic()&&m_shapes[i]->touching()) {
				HLdouble force[3];
				HLdouble pp[3];
				hlGetDoublev(HL_DEVICE_FORCE,force);
				hlGetDoublev(HL_PROXY_POSITION,pp);

				dBodyAddForceAtPos (bodies[i], -force[0], -force[1], -force[2],
											   pp[0],    pp[1],    pp[2]);
			}

			if(((CProtoHapticApp*)AfxGetApp())->isTwoDevices()) {
				// proxy1
				hlMakeCurrent(m_context_1);
				if(m_shapes[i]->isProxyDynamic()&&m_shapes[i]->touching1()) {
					HLdouble force[3];
					HLdouble pp[3];
					hlGetDoublev(HL_DEVICE_FORCE,force);
					hlGetDoublev(HL_PROXY_POSITION,pp);

					dBodyAddForceAtPos (bodies[i], -force[0], -force[1], -force[2],
												   pp[0],    pp[1],    pp[2]);
				}
			}

			hlMakeCurrent(rc);

			// gravity
			if(m_shapes[i]->isCollisionDynamic())
				dBodyAddForce(bodies[i], 0.0, -m_shapes[i]->getMass()*(m_gravity/10.0f), 0.0);

			const dReal *pos;
			const dReal *rot;
			
			pos= dBodyGetPosition (bodies[i]);

			rot= dBodyGetRotation (bodies[i]);

			float rotation[16];
		
			rotation[0]= rot[0];  rotation[4]= rot[1];   rotation[8]= rot[2];    rotation[12]= rot[3];
			rotation[1]= rot[4];  rotation[5]= rot[5];   rotation[9]= rot[6];    rotation[13]= rot[7];
			rotation[2]= rot[8];  rotation[6]= rot[9];   rotation[10]= rot[10];  rotation[14]= rot[11];
			rotation[3]= 0.0;     rotation[7]= 0.0;      rotation[11]= 0.0;      rotation[15]= 1.0;
			m_shapes[i]->setRotation(rotation);
			m_shapes[i]->setLocation(pos[0], pos[1], pos[2]);
	}

	m_lastSimStep= clock();
}
Example #11
0
/*******************************************************************************
 Initialize the HDAPI.  This involves initing a device configuration, enabling
 forces, and scheduling a haptic thread callback for servicing the device.
*******************************************************************************/
void initHL()
{
    HDErrorInfo error;

    hHD = hdInitDevice(HD_DEFAULT_DEVICE);
    if (HD_DEVICE_ERROR(error = hdGetError()))
    {
        hduPrintError(stderr, &error, "Failed to initialize haptic device");
        fprintf(stderr, "Press any key to exit");
        getchar();
        exit(-1);
    }
    
    hHLRC = hlCreateContext(hHD);
    hlMakeCurrent(hHLRC);

    // Enable optimization of the viewing parameters when rendering
    // geometry for OpenHaptics.
    hlEnable(HL_HAPTIC_CAMERA_VIEW);

    // Generate id's for the three shapes.
    gSphereShapeId = hlGenShapes(1);
    gTorusShapeId = hlGenShapes(1);
    gTeapotShapeId = hlGenShapes(1);

    // Setup event callbacks.
    hlAddEventCallback(HL_EVENT_TOUCH, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       &touchShapeCallback, NULL);
    hlAddEventCallback(HL_EVENT_UNTOUCH, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       &untouchShapeCallback, NULL);
    hlAddEventCallback(HL_EVENT_TOUCH, gSphereShapeId, HL_CLIENT_THREAD, 
                       &touchSphereCallback, NULL);
    hlAddEventCallback(HL_EVENT_UNTOUCH, gSphereShapeId, HL_CLIENT_THREAD, 
                       &untouchSphereCallback, NULL);
    hlAddEventCallback(HL_EVENT_TOUCH, gTorusShapeId, HL_CLIENT_THREAD, 
                       &touchTorusCallback, NULL);
    hlAddEventCallback(HL_EVENT_UNTOUCH, gTorusShapeId, HL_CLIENT_THREAD, 
                       &untouchTorusCallback, NULL);
    hlAddEventCallback(HL_EVENT_TOUCH, gTeapotShapeId, HL_CLIENT_THREAD, 
                       &touchTeapotCallback, NULL);
    hlAddEventCallback(HL_EVENT_UNTOUCH, gTeapotShapeId, HL_CLIENT_THREAD, 
                       &untouchTeapotCallback, NULL);
    hlAddEventCallback(HL_EVENT_1BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       &button1DownCallback, NULL);
    hlAddEventCallback(HL_EVENT_1BUTTONUP, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       &button1UpCallback, NULL);
    hlAddEventCallback(HL_EVENT_2BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       &button2DownCallback, NULL);
    hlAddEventCallback(HL_EVENT_2BUTTONUP, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       &button2UpCallback, NULL);
    hlAddEventCallback(HL_EVENT_3BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       &button3DownCallback, NULL);
    hlAddEventCallback(HL_EVENT_3BUTTONUP, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       &button3UpCallback, NULL);
    hlAddEventCallback(HL_EVENT_1BUTTONDOWN, gSphereShapeId, HL_CLIENT_THREAD, 
                       &button1DownSphereCallback, NULL);
    hlAddEventCallback(HL_EVENT_1BUTTONUP, gSphereShapeId, HL_CLIENT_THREAD, 
                       &button1UpSphereCallback, NULL);
    hlAddEventCallback(HL_EVENT_MOTION, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       &motionCallback, NULL);
    hlAddEventCallback(HL_EVENT_MOTION, gSphereShapeId, HL_CLIENT_THREAD, 
                       &motionOnSphereCallback, NULL);
    hlAddEventCallback(HL_EVENT_CALIBRATION_UPDATE, HL_OBJECT_ANY,
                       HL_CLIENT_THREAD, &calibrationCallback, NULL);                       
    hlAddEventCallback(HL_EVENT_CALIBRATION_INPUT, HL_OBJECT_ANY,
                       HL_CLIENT_THREAD, &calibrationCallback, NULL);                       
}
Example #12
0
/*******************************************************************************
 Main function.
*******************************************************************************/
int main(int argc, char *argv[])
{
    HHD hHD;
    HHLRC hHLRC;
    HDErrorInfo error;
    HLuint spring;

    /* The code snippet provided to you by SensAble should be executed near 
       application startup.  Once a deployment license has been validated, 
       it will remain in effect until the application is shutdown.
    
       Be sure to place the HD deployment license code before the call 
       to hdInitDevice, and the HL deployment license code before the call 
       to hlCreateContext.
     
       NOTE THAT THE FOLLOWING IS FOR DEMONSTRATION ONLY. THE LICENSES 
       ARE NOT VALID. */

    hdDeploymentLicense(
        "ABC Software, Inc.",
        "HapticsGold",
        "F1312D97ECCC754D5BE4BEE7E831BC27ACF809E9B850D9576F1A856AF70DD3A879B4D3DC7F922BDB2C639DA4A565CA5FC598D8AF34EA010B13A8C232B78F22C");

    hlDeploymentLicense(
        "ABC Software, Inc.",
        "HapticsGold",
        "A653A1EEAFF7B87C952754672FDB1AA16A9035ADE1CFCA6394FE869BAFECE0B7A32251502DDF220B7BA27979695041AE59DCEA007605027D471801F4BF26C24");

    hHD = hdInitDevice(HD_DEFAULT_DEVICE);
    if (HD_DEVICE_ERROR(error = hdGetError())) 
    {
        hduPrintError(stderr, &error, "Failed to initialize haptic device");
        fprintf(stderr, "\nPress any key to quit.\n");
        getch();
        return -1;
    }
    hdMakeCurrentDevice(hHD);    

    hHLRC = hlCreateContext(hHD);
    hlMakeCurrent(hHLRC);
    
    hlDisable(HL_USE_GL_MODELVIEW);

    spring = hlGenEffects(1);

    // Add a callback to handle button down in the collision thread.
    hlAddEventCallback(HL_EVENT_1BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       buttonCB, &spring);
    hlAddEventCallback(HL_EVENT_1BUTTONUP, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       buttonCB, &spring);

    printf("Press and hold the primary stylus button to feel the spring effect.\n");
    printf("Press any key to quit.\n\n");

    // Run the main loop.
    while (!_kbhit())
    {
        hlBeginFrame();

        // Poll for events. Note that client thread event callbacks get
        // dispatched from within a frame here, so we can safely start/stop
        // effects from the event callback directly.
        hlCheckEvents();

        hlEndFrame();
    }

    hlDeleteEffects(spring, 1);

    hlDeleteContext(hHLRC);
    hdDisableDevice(hHD);

    return 0;
}
/*******************************************************************************
 Main function.
*******************************************************************************/
int main(int argc, char *argv[])
{


    HHD hHD;
    HHLRC hHLRC;
    HDErrorInfo error;
    HLuint friction, spring;
    HLerror frameError;

    hHD = hdInitDevice(HD_DEFAULT_DEVICE);
    if (HD_DEVICE_ERROR(error = hdGetError())) 
    {
        hduPrintError(stderr, &error, "Failed to initialize haptic device");
        fprintf(stderr, "\nPress any key to quit.\n");
        getch();
        return -1;
    }
    hdMakeCurrentDevice(hHD);    

    hHLRC = hlCreateContext(hHD);
    hlMakeCurrent(hHLRC);
    
    hlDisable(HL_USE_GL_MODELVIEW);

    spring = hlGenEffects(1);

    /* Add a callback to handle button down in the collision thread. */
    hlAddEventCallback(HL_EVENT_1BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       buttonCB, &spring);
    hlAddEventCallback(HL_EVENT_1BUTTONUP, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       buttonCB, &spring);
    hlAddEventCallback(HL_EVENT_2BUTTONDOWN, HL_OBJECT_ANY, HL_CLIENT_THREAD, 
                       buttonCB, 0);

    /* Start an ambient friction effect. */
    friction = hlGenEffects(1);

    hlBeginFrame();
    hlEffectd(HL_EFFECT_PROPERTY_GAIN, 0.2);
    hlEffectd(HL_EFFECT_PROPERTY_MAGNITUDE, 0.5);
    hlStartEffect(HL_EFFECT_FRICTION, friction);
    hlEndFrame();

    printf("Move around to feel the ambient stick-slip friction.\n\n");
    printf("Press and hold the primary stylus button to feel the spring effect.\n\n");
    printf("Press the second stylus button to trigger an impulse.\n\n");

    /* Run the main loop. */
    while (!_kbhit())
    {
        hlBeginFrame();

        /* Poll for events.  Note that client thread event callbacks get
           dispatched from within a frame here, so we can safely start/stop
           effects from the event callback directly */
        hlCheckEvents();

        hlEndFrame();

        /* Check for any errors. */
        while (HL_ERROR(frameError = hlGetError()))
        {
            fprintf(stderr, "HL Error: %s\n", frameError.errorCode);
            
            if (frameError.errorCode == HL_DEVICE_ERROR)
            {
                hduPrintError(stderr, &frameError.errorInfo,
                    "Error during haptic rendering\n");
            }
        }
    }

    /* Stop the friction effect. */
    hlBeginFrame();
    hlStopEffect(friction);
    hlEndFrame();

    hlDeleteEffects(friction, 1);
    hlDeleteEffects(spring, 1);

    hlDeleteContext(hHLRC);
    hdDisableDevice(hHD);

    return 0;
}