示例#1
0
HAPIForceEffect::EffectOutput 
OpenHapticsRenderer::renderHapticsOneStep( 
                     HAPIHapticsDevice *hd,
                     const HapticShapeVector &shapes ) {
  PhantomHapticsDevice *pd = dynamic_cast< PhantomHapticsDevice * >( hd );
  if( !pd ) {
    AnyHapticsDevice *d = dynamic_cast< AnyHapticsDevice *>( hd );
    if( d ) {
      pd = dynamic_cast< PhantomHapticsDevice * >( d->getActualHapticsDevice() );
    }
    if( !pd ) return HAPIForceEffect::EffectOutput();
  }

  hdMakeCurrentDevice( pd->getDeviceHandle() );

  // add the resulting force and torque to the rendered force.
  HDdouble force[3];
  hdGetDoublev( HD_CURRENT_FORCE, force );
  
  HDdouble torque[3];
  hdGetDoublev( HD_CURRENT_TORQUE, torque );

  // add the resulting force and torque to the rendered force.
  // The force and torque from the haptics device is in local coordinates.
  // We want it in global coordinates.
  Matrix3 pos_matrix_rotation = hd->getPositionCalibration().getRotationPart();
  return HAPIForceEffect::EffectOutput( 
    pos_matrix_rotation * Vec3( force[0], force[1], force[2] ),
    pos_matrix_rotation * Vec3( torque[0], torque[1], torque[2] ) );
}
int main(int argc, char* argv[])
{
	HAPIFloat x=0, y=0, z=0;

	AnyHapticsDevice hd;

	//initialize. return error and exit if there is a failure
	if (hd.initDevice() !=HAPIHapticsDevice::SUCCESS)
	{
		cerr <<hd.getLastErrorMsg() <<endl;
		system("PAUSE");
		return 0;
	}

	hd.enableDevice(); //enable

	string thePosition;
	string theSpringConst;
	bool continue_trying = true;

	while(continue_trying)
	{
		cout<<"Enter position 0<n<10 for x, y, z (separate by spaces)"<<endl;
		getline(cin, thePosition);

		char *pEnd;
		x = strtod(thePosition.c_str(), &pEnd);
		y = strtod(pEnd, &pEnd);
		z = strtod(pEnd, NULL);

		x = x/200;
		y = y/200;
		z = z/200;

		cout<<"Enter spring constant:"<<endl;
		cout<<"100 is relatively weak, 1000 is strong"<<endl;
		getline(cin, theSpringConst);
		HAPIFloat spring_constant = strtod(theSpringConst.c_str(), NULL);

		//stringstream stm_x, stm_y, stm_z, stm_const;
		//stm_x << x;
		//stm_y << y;
		//stm_z << z;
		//stm_const << spring_constant;

		//add spring to haptics
		HapticSpring *spring = new HapticSpring(Vec3(x,y,z), spring_constant);
		hd.addEffect(spring);
		hd.transferObjects();

		cout<<"Press enter to change position and spring constant"<<endl;
		getline(cin, thePosition);
	hd.removeEffect(spring);
	hd.transferObjects();
}
hd.disableDevice();
hd.releaseDevice();
}
示例#3
0
HHLRC OpenHapticsRenderer::initHLLayer( HAPIHapticsDevice *hd ) {
  PhantomHapticsDevice *pd = dynamic_cast< PhantomHapticsDevice * >( hd );
  if( !pd ) {
    AnyHapticsDevice *d = dynamic_cast< AnyHapticsDevice *>( hd );
    if( d ) {
      pd = dynamic_cast< PhantomHapticsDevice * >( d->getActualHapticsDevice() );
    }
    if( !pd ) return NULL;
  }

  if( pd->getDeviceState() != HAPIHapticsDevice::UNINITIALIZED ) {
    if( context_map.find( pd ) == context_map.end() ) {
      // Create a haptic context for the device. The haptic context maintains 
      // the state that persists between frame intervals and is used for
      // haptic rendering.
      HHD jj = pd->getDeviceHandle();
      // The check of no_context and scheduler started is used to get
      // around a problem with openhaptics if the scheduler is started
      // before a hlcontext is created. Has to be made this way (only once)
      // in order to not get strange behaviour when using dual device setup.
      bool restart = false;
      if( PhantomHapticsDevice::isSchedulerStarted() && nr_of_context == 0 ) {
        PhantomHapticsDevice::stopScheduler();
        restart = true;
      }
      context_map[ pd ] = hlCreateContext( jj );
      if( restart ) {
        PhantomHapticsDevice::startScheduler();
      }
      ++nr_of_context;

      if( !dummy_context ) {
        dummy_context = hlCreateContext( jj );
        ++nr_of_context;
      }
      hlMakeCurrent( context_map[ pd ] );

      hlEnable(HL_HAPTIC_CAMERA_VIEW);
      hlEnable(HL_ADAPTIVE_VIEWPORT );
      return context_map[ pd ];
    }
  }
  return NULL;
}
示例#4
0
int main(int argc, char* argv[]) {
  // Get a connected device.
  AnyHapticsDevice hd;
  // Init the device.
  if( hd.initDevice() != HAPIHapticsDevice::SUCCESS ) {
    cerr << hd.getLastErrorMsg() << endl;
    return 0;
  }
  // Enable the device
  hd.enableDevice();

  // used to loop the program until user wants to exit.
  bool continue_trying = true;

  while( continue_trying ) {
    // Get info from user.
    cout << "Enter a position as x y z (in m) then press ENTER" << endl;
    string thePosition;
    getline( cin, thePosition );

    char * pEnd;
    HAPIFloat x, y, z;
    x = strtod( thePosition.c_str(), &pEnd );
    y = strtod( pEnd, &pEnd );
    z = strtod( pEnd, NULL );

    cout << "Enter the value for a spring constant then press ENTER." << endl;
    cout << "A good value is 100 since HAPI uses m." << endl;
    cout << "Keep a firm grip of the device before pressing ENTER." << endl;
    getline( cin, thePosition );
    HAPIFloat spring_constant = strtod( thePosition.c_str(), NULL );

    cout << "The haptic device will be pulled towards position ";
    cout << x << " ";
    cout << y << " " << z << endl;
    cout << "The spring constant used is: " << spring_constant << endl;
    cout << "Any faulty input number will be replaced by 0" << endl << endl;

    // The spring effect with a position and spring_constant input by the user.
    HapticSpring *spring_effect = new HapticSpring( Vec3( x, y, z ),
                                                    spring_constant );
    // Add the effect to the haptics device.
    hd.addEffect( spring_effect );
    // Send the effect to the haptics loop and from now on it will be used to
    // send forces to the device.
    hd.transferObjects();

    cout << "Type q and then ENTER to exit, any other input and then ";
    cout << "ENTER will allow you to a new point and a new springconstant"
      << endl;
    getline( cin, thePosition );
    if( thePosition == "q" ) {
      continue_trying = false;
    }
    // Remove the effect from the haptics device.
    hd.removeEffect( spring_effect );
    // Transfer changes to the haptic loop. Now the haptic effect will stop
    // affecting the haptics device since it is removed from the haptics loop.
    hd.transferObjects();
  }
  // Disable device.
  hd.disableDevice();
  // Release device.
  hd.releaseDevice();
}