multiple_devices.cpp

Simple example of multiple-devices programming.

////////////////////////////////////////////////////////////////////////////////
//
// This example implements a simple gravity compensation loop for two devices.
// Note that a more efficient approach would require the creation of two haptic
// threads, one for each device. This implementation is OS specific however
// and is not implemented here.
//
////////////////////////////////////////////////////////////////////////////////
// C++ library headers
#include <iostream>
#include <iomanip>
// project headers
#include "dhdc.h"
////////////////////////////////////////////////////////////////////////////////
int main(int argc,
char* argv[])
{
// get device count
int deviceCount = dhdGetDeviceCount();
if (deviceCount < 0)
{
std::cout << "error: " << dhdErrorGetLastStr() << std::endl;
return -1;
}
else if (deviceCount < 1)
{
std::cout << "error: no device detected" << std::endl;
}
else if (deviceCount < 2)
{
std::cout << "error: singled device detected" << std::endl;
}
// open the first available device
int deviceID0 = dhdOpenID(0);
if (deviceID0 < 0)
{
std::cout << "error: " << dhdErrorGetLastStr() << std::endl;
return -1;
}
// open the second available device
int deviceID1 = dhdOpenID(1);
if (deviceID1 < 0)
{
std::cout << "error: " << dhdErrorGetLastStr() << std::endl;
return -1;
}
// haptic loop
while (true)
{
// apply a null force to put the first device in gravity compensation
if (dhdSetForce(0.0, 0.0, 0.0, deviceID0) < 0)
{
std::cout << "error: " << dhdErrorGetLastStr() << std::endl;
break;
}
// apply a null force to put the second device in gravity compensation
if (dhdSetForce(0.0, 0.0, 0.0, deviceID1) < 0)
{
std::cout << "error: " << dhdErrorGetLastStr() << std::endl;
break;
}
// exit the haptic loop on button press
if (dhdGetButton(0, deviceID0) || dhdGetButton(0, deviceID1))
{
break;
}
}
// close the connection to the first device
if (dhdClose(deviceID0) < 0)
{
std::cout << "error: " << dhdErrorGetLastStr() << std::endl;
}
// close the connection to the second device
if (dhdClose(deviceID1) < 0)
{
std::cout << "error: " << dhdErrorGetLastStr() << std::endl;
}
return 0;
}
DHD header file.
int __SDK dhdOpenID(char ID)
Definition: dhdc.cpp:411
int __SDK dhdClose(char ID=-1)
Definition: dhdc.cpp:443
int __SDK dhdGetDeviceCount()
Definition: dhdc.cpp:226
int __SDK dhdGetButton(int index, char ID=-1)
Definition: dhdc.cpp:1182
const char *__SDK dhdErrorGetLastStr()
Definition: dhdError.cpp:197
int __SDK dhdSetForce(double fx, double fy, double fz, char ID=-1)
Definition: dhdc.cpp:1697