single_device.cpp

Simple example of single-device programming.

////////////////////////////////////////////////////////////////////////////////
//
// This example implements a simple gravity compensation loop for a single
// haptic device.
//
////////////////////////////////////////////////////////////////////////////////
// C++ library headers
#include <iostream>
#include <iomanip>
// project headers
#include "dhdc.h"
////////////////////////////////////////////////////////////////////////////////
int main(int argc,
char* argv[])
{
// get device count
if (dhdGetDeviceCount() <= 0)
{
std::cout << "error: " << dhdErrorGetLastStr() << std::endl;
return -1;
}
// open the first available device
if (dhdOpen() < 0)
{
std::cout << "error: " << dhdErrorGetLastStr() << std::endl;
return -1;
}
// haptic loop
while (true)
{
// apply a null force to put the device in gravity compensation
if (dhdSetForce(0.0, 0.0, 0.0) < 0)
{
std::cout << "error: " << dhdErrorGetLastStr() << std::endl;
break;
}
// exit the haptic loop on button press
if (dhdGetButton(0))
{
break;
}
}
// close the connection to the device
if (dhdClose() < 0)
{
std::cout << "error: " << dhdErrorGetLastStr() << std::endl;
}
return 0;
}