36 #include <sys/types.h>
40 #include <haptic_force/Force.h>
42 #include <phantom_control/State.h>
47 phantom_control::State
g_SS;
52 void ForceCallBk (
const haptic_force::Force &force );
64 int main (
int argc,
char **argv )
74 HHD hHD = hdInitDevice(HD_DEFAULT_DEVICE);
76 if (HD_DEVICE_ERROR(error = hdGetError()))
78 hduPrintError(stderr, &error,
"Failed to initialize haptic device");
79 fprintf(stderr,
"\nPress any key to quit.\n");
83 ROS_INFO(
"Found %s.\n\n", hdGetString(HD_DEVICE_MODEL_TYPE));
89 hdEnable(HD_FORCE_OUTPUT);
92 if (HD_DEVICE_ERROR(error = hdGetError()))
94 hduPrintError(stderr, &error,
"Failed to start scheduler");
95 fprintf(stderr,
"\nPress any key to quit.\n");
114 ros::init( argc, argv,
"phantom_node" );
116 ros::NodeHandle n(
"~");
118 ros::Publisher pub_state= n.advertise< phantom_control::State >(
"/phantom_state", 1000 );
120 ros::Subscriber sub_force = n.subscribe (
"/force", 1000,
ForceCallBk );
122 ros::Rate loop_rate( 2.5*1300 );
126 int pid = getpid(), rpid;
128 boost::format fmt(
"sudo renice -10 %d");
131 rpid = std::system(fmt.str().c_str());
139 pub_state.publish(
g_SS );
163 ROS_INFO(
"Ending Session....\n");
166 hdDisableDevice(hHD);
215 g_SS.header.stamp = ros::Time::now ( );
226 state.
force[0]=force.force[0];
227 state.
force[1]=force.force[1];
228 state.
force[2]=force.force[2];
239 int calibrationStyle;
240 int supportedCalibrationStyles;
243 hdGetIntegerv(HD_CALIBRATION_STYLE, &supportedCalibrationStyles);
244 if (supportedCalibrationStyles & HD_CALIBRATION_ENCODER_RESET)
246 calibrationStyle = HD_CALIBRATION_ENCODER_RESET;
247 ROS_INFO(
"HD_CALIBRATION_ENCODER_RESE..\n\n");
249 if (supportedCalibrationStyles & HD_CALIBRATION_INKWELL)
251 calibrationStyle = HD_CALIBRATION_INKWELL;
252 ROS_INFO(
"HD_CALIBRATION_INKWELL..\n\n");
254 if (supportedCalibrationStyles & HD_CALIBRATION_AUTO)
256 calibrationStyle = HD_CALIBRATION_AUTO;
257 ROS_INFO(
"HD_CALIBRATION_AUTO..\n\n");
262 hdUpdateCalibration(calibrationStyle);
263 ROS_INFO(
"Calibrating.. (put stylus in well)\n");
264 if (HD_DEVICE_ERROR(error = hdGetError()))
266 hduPrintError(stderr, &error,
"Reset encoders reset failed.");
269 }
while (hdCheckCalibration() != HD_CALIBRATION_OK);
271 ROS_INFO(
"...\n...\nCalibration complete.\n");
288 hduVector3Dd position;
293 hdBeginFrame(hdGetCurrentDevice());
295 hdGetDoublev(HD_CURRENT_GIMBAL_ANGLES, omni_state->
rot);
296 hdGetDoublev(HD_CURRENT_POSITION, omni_state->
position);
297 hdGetDoublev(HD_CURRENT_JOINT_ANGLES, omni_state->
joints);
300 hdGetIntegerv(HD_CURRENT_BUTTONS, &nButtons);
301 omni_state->
buttons[0] = (nButtons & HD_DEVICE_BUTTON_1) ? 1 : 0;
302 omni_state->
buttons[1] = (nButtons & HD_DEVICE_BUTTON_2) ? 1 : 0;
305 hdGetDoublev(HD_MOTOR_TEMPERATURE, omni_state->
temp);
313 hdSetDoublev(HD_CURRENT_FORCE, omni_state->
force);
317 hdEndFrame(hdGetCurrentDevice());
319 if (HD_DEVICE_ERROR(error = hdGetError()))
321 hduPrintError(stderr, &error,
"Error during main scheduler callback\n");
322 if (hduIsSchedulerError(&error))
323 return HD_CALLBACK_DONE;
327 float t[7] = {0.0, omni_state->
joints[0], omni_state->
joints[1], omni_state->
joints[2]-omni_state->
joints[1], omni_state->
rot[0], omni_state->
rot[1], omni_state->
rot[2]};
329 for (
int i = 0; i < 7; i++) omni_state->
thetas[i] = t[i];
333 return HD_CALLBACK_CONTINUE;
346 hduVector3Dd position;
348 position[0]=
g_SS.position[0];
349 position[1]=
g_SS.position[1];
350 position[2]=
g_SS.position[2];
355 hduVector3Dd force2home(0, 0.49, 0);
357 hdSetDoublev(HD_CURRENT_FORCE, force2home);
361 hduVector3Dd force2home = (home_pos - position);
362 force2home.normalize();
365 hdSetDoublev(HD_CURRENT_FORCE, force2home);
hduVector3Dd g_home_pos(HOME_XX, HOME_YY, HOME_ZZ)
phantom_control::State g_SS
HDSchedulerHandle gCallbackHandle
void HHD_Auto_Calibration(void)
void ForceCallBk(const haptic_force::Force &force)
Header for the OpenHaptics lib and the haptic device state structure.
Structure for holding the state variables of the haptic device.
HDCallbackCode HDCALLBACK MonitorCallback(void *pUserData)
int main(int argc, char **argv)