Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • danc/MicroCART
  • snawerdt/MicroCART_17-18
  • bbartels/MicroCART_17-18
  • jonahu/MicroCART
4 results
Show changes
Showing
with 0 additions and 1843 deletions
CC = gcc
CXX = g++
CFLAGS = -O
CCFLAGS = -O
VRPNDIR = $(HOME)/vrpn
VRPNINC = -I$(VRPNDIR)/include
VRPNLIB = -L$(VRPNDIR)/lib -lvrpn -lquat -lpthread
INCS = $(VRPNINC)
LIBS = $(VRPNLIB) -lm
OBJS = testvrpn.o phantom-field.o phantom-plane.o phantom-wave.o \
glwin.o glvrpn.o smdvrpn.o
BINS = testvrpn phantom-field phantom-plane phantom-wave glvrpn smdvrpn
all: $(BINS) $(OBJS)
.c.o :
$(CC) $(CCFLAGS) $(INCS) -c $*.c
.C.o :
$(CXX) $(CCFLAGS) $(INCS) -c $*.C
glwin.o : glwin.c glwin.h
$(CC) $(CCFLAGS) -DUSEOPENGL $(INCS) -c $*.c
testvrpn : testvrpn.o
$(CXX) $(CCFLAGS) $(INCS) testvrpn.o $(LIBS) -o $@
phantom-field : phantom-field.o
$(CXX) $(CCFLAGS) $(INCS) phantom-field.o $(LIBS) -o $@
phantom-plane : phantom-plane.o
$(CXX) $(CCFLAGS) $(INCS) phantom-plane.o $(LIBS) -o $@
phantom-wave : phantom-wave.o
$(CXX) $(CCFLAGS) $(INCS) phantom-wave.o $(LIBS) -o $@
glvrpn : glvrpn.o glwin.o
$(CXX) $(CCFLAGS) $(INCS) glvrpn.o glwin.o $(LIBS) -lGL -lGLU -lX11 -o $@
smdvrpn: smdvrpn.o glwin.o
$(CXX) $(CCFLAGS) $(INCS) smdvrpn.o glwin.o $(LIBS) -lGL -lGLU -lX11 -o $@
clean :
rm -f $(OBJS) $(BINS)
This directory contains test and example files for using a Phantom
force-feedback device with VRPN. They were written by John Stone
at the NIH Resource for Macromolecular Modeling and Bioinformatics at the
Beckman Institute at UIUC.
These programs are made to compile and run on an SGI. The VRPN portions of
these programs should be portable across platforms, but the openGL portions
(if any) have not been ported to Windows.
This README is made by Russ Taylor, so hopefully it is accurate.
testvrpn.C:
This opens the Phantom as a tracker, button, and forcedevice.
It prints out all tracker messages, button messages, amd force messages
(should there be force messages?)
glwin.[hc]:
Helper functions to create openGL windows quickly and easily on Unix.
glvrpn.C:
This opens and openGL window and displays the tracker as it moves
around (along with a line from the origin to the tracker). It is a good
example of how to use a VRPN tracker with openGL.
phantom-field.C:
Sends a forcefield to the Phantom when the button is held down.
phantom-plane.C:
Sends a plane to the Phantom that the user can feel, quits when the
button is pressed.
phantom-wave.c:
Sends a spatially-varying force field to the Phantom while the button
is held down.
smdvrpn.C:
Simple molecular dynamics simulation using VRPN, graphics, and haptics.
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vrpn_ForceDevice.h>
#include <vrpn_Tracker.h>
#include <vrpn_Button.h>
#define PHANTOM_SERVER "Tracker0@localhost"
/*****************************************************************************
*
Callback handler
*
*****************************************************************************/
void handle_force_change(void *userdata, const vrpn_FORCECB f)
{
static vrpn_FORCECB lr; // last report
static int first_report_done = 0;
if ((!first_report_done) ||
((f.force[0] != lr.force[0]) || (f.force[1] != lr.force[1])
|| (f.force[2] != lr.force[2]))) {
//printf("force is (%f,%f,%f)\n", f.force[0], f.force[1], f.force[2]);
}
first_report_done = 1;
lr = f;
}
void handle_tracker_change(void *userdata, const vrpn_TRACKERCB t)
{
static vrpn_TRACKERCB lr; // last report
static float dist_interval_sq = 0.004;
float *pos = (float *)userdata;
if ((lr.pos[0] - t.pos[0])*(lr.pos[0] - t.pos[0]) +
(lr.pos[1] - t.pos[1])*(lr.pos[1] - t.pos[1]) +
(lr.pos[2] - t.pos[2])*(lr.pos[2] - t.pos[2]) + dist_interval_sq){
//printf("Sensor %d is now at (%g,%g,%g)\n", t.sensor,
// t.pos[0], t.pos[1], t.pos[2]);
lr = t;
}
pos[0] = t.pos[0];
pos[1] = t.pos[1];
pos[2] = t.pos[2];
}
void handle_button_change(void *userdata, const vrpn_BUTTONCB b)
{
static int buttonstate = 1;
if (b.state != buttonstate) {
printf("button #%ld is in state %ld\n", b.button, b.state);
buttonstate = b.state;
}
*(int *)userdata = buttonstate;
}
int main(int argc, char *argv[])
{
int done = 0;
float pos[3];
int forceInEffect = 0;
int forceEnabled = 0;
vrpn_ForceDevice_Remote *forceDevice;
vrpn_Tracker_Remote *tracker;
vrpn_Button_Remote *button;
/* initialize the force device */
forceDevice = new vrpn_ForceDevice_Remote(PHANTOM_SERVER);
forceDevice->register_change_handler(NULL, handle_force_change);
/* initialize the tracker */
tracker = new vrpn_Tracker_Remote(PHANTOM_SERVER);
tracker->register_change_handler((void *)pos, handle_tracker_change);
/* initialize the button */
button = new vrpn_Button_Remote(PHANTOM_SERVER);
button->register_change_handler((void *)&forceEnabled, handle_button_change);
// main loop
while (! done )
{
// Let the forceDevice send its messages to remote force device
forceDevice->mainloop();
// Let tracker receive position information from remote tracker
tracker->mainloop();
// Let button receive button status from remote button
button->mainloop();
if (forceEnabled) {
forceDevice->setFF_Origin(0.1,0.1,0.0);
// units = dynes
forceDevice->setFF_Force(-10.0*pos[0],-10.0*pos[1],-10.0*pos[2]);
// set derivatives of force field:
// units = dynes/meter
forceDevice->setFF_Jacobian(-10,0, 0, 0,
-10, 0, 0, 0,
-10);
forceDevice->setFF_Radius(0.2); // 20cm radius of validity
forceDevice->sendForceField();
forceInEffect = 1;
}
else if (forceInEffect){
forceDevice->stopForceField();
forceInEffect = 0;
}
}
} /* main */
#include <stdio.h>
#include "vrpn_Tracker.h"
#include "vrpn_Button.h"
#include "vrpn_ForceDevice.h"
#include "quat.h"
#include "glwin.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
typedef struct {
float x; // position
float y;
float z;
double qx, qy, qz, qw; // quaternion orientation
} posquat;
typedef struct {
int b[100];
} buttons;
void handle_tracker(void *userdata, const vrpn_TRACKERCB t) {
posquat *pq = (posquat *) userdata;
pq->x = t.pos[0];
pq->y = t.pos[1];
pq->z = t.pos[2];
pq->qx=t.quat[Q_X];
pq->qy=t.quat[Q_Y];
pq->qz=t.quat[Q_Z];
pq->qw=t.quat[Q_W];
}
void handle_button(void *userdata, const vrpn_BUTTONCB b) {
buttons *bt = (buttons *) userdata;
if (b.button < 100)
bt->b[b.button] = b.state;
printf("\nButton %3d is in state: %d \n",
b.button, b.state);
fflush(stdout);
}
void handle_force(void *userdata, const vrpn_FORCECB f) {
#if 0
static vrpn_FORCECB lr; // last report
static int first_report_done = 0;
if ((!first_report_done) || ((f.force[0] != lr.force[0]) ||
(f.force[1] != lr.force[1]) || (f.force[2] != lr.force[2])))
printf("\nForce is (%11f, %11f, %11f) \n",
f.force[0], f.force[1], f.force[2]);
first_report_done = 1;
lr = f;
#endif
}
void init_graphics(void *myglwin) {
glwin_handle_events(myglwin); // let window to open and handle events once
glwin_handle_events(myglwin); // let window to open and handle events once
// setup OpenGL state...
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 0.01, 10.0);
gluLookAt( 0.0, 0.1, -5.0, // eye pos
0.0, 0.0, 0.0, // look at this point
0.0, 1.0, 0.0); // up direction
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
//
// Setup cute lighting parms..
//
glEnable(GL_LIGHTING);
glEnable(GL_LEQUAL);
GLfloat lpos[] = { 0.0, 0.0, -100.0, 1.0};
GLfloat lamb[] = { 0.05, 0.05, 0.05, 1.0};
GLfloat ldif[] = { 0.5, 0.5, 0.5, 1.0};
GLfloat lspc[] = { 0.5, 0.5, 0.5, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glLightfv(GL_LIGHT0, GL_AMBIENT, lamb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, ldif);
glLightfv(GL_LIGHT0, GL_SPECULAR, lspc);
glLightf(GL_LIGHT0, GL_SHININESS, 400.0);
glEnable(GL_LIGHT0);
GLfloat mdif[] = {0.9, 0.0, 0.2, 1.0};
GLfloat mspc[] = {0.2, 0.2, 0.2, 1.0};
glMaterialfv(GL_FRONT, GL_SPECULAR, mspc);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mdif);
glEnable(GL_COLOR_MATERIAL);
}
void draw_axes() {
//
// Draw Coordinate Axes
//
glDisable(GL_LIGHTING);
glBegin(GL_LINES);
float i;
glColor3f(0.0, 1.0, 0.0);
for (i=-10; i<10; i += 0.50) {
glVertex3f( -10.0, 0.0, i);
glVertex3f( 10.0, 0.0, i);
glVertex3f( i, 0.0, -10.0);
glVertex3f( i, 0.0, 10.0);
}
glEnd();
}
void draw_tracker(posquat *pq, GLUquadricObj *qobj1, GLUquadricObj *qobj2)
{
float x, y, z;
// Convert from haptic coordinates to our scene coordinates
x=-10.0*(pq->x);
y=10.0*(pq->y);
z=-10.0*(pq->z);
double q[4] = {-(pq->qx), (pq->qy), -(pq->qz), pq->qw};
// convert the orientation from quaternion to OpenGL matrix
double rotation[16];
q_to_ogl_matrix(rotation,q);
// Draw the tracker itself
//
// First draw line to object
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0 );
glVertex3f(x, y, z);
glEnd();
glEnable(GL_LIGHTING);
glColor3f(1.0, 0.0, 0.2);
glPushMatrix();
glTranslatef(x, y, z);
glMultMatrixd(rotation);
glTranslatef(-1.5, 0.0, 0.0);
gluSphere(qobj1, 0.2, 32, 32);
glTranslatef(3.0,0.0,0.0);
gluSphere(qobj2, 0.2, 32, 32);
glPopMatrix();
}
int main(int argc, char **argv) {
char * server;
posquat *tposquat = new posquat;
buttons bpos;
vrpn_Tracker_Remote *tkr;
vrpn_Button_Remote *btn;
vrpn_ForceDevice_Remote *fdv;
if (argc < 2) {
printf("%s: Invalid parms\n", argv[0]);
printf("usage: \n");
printf(" %s Tracker0@host\n", argv[0]);
return -1;
}
server = argv[1];
printf("Opening: %s ...\n", server);
tkr = new vrpn_Tracker_Remote(server);
tkr->register_change_handler(tposquat, handle_tracker);
btn = new vrpn_Button_Remote(server);
btn->register_change_handler(&bpos, handle_button);
fdv = new vrpn_ForceDevice_Remote(server);
fdv->register_force_change_handler(NULL, handle_force);
void * myglwin = glwin_create(700, 700);
if (myglwin == NULL) {
printf("Failed to open OpenGL window!!\n");
return -1;
}
init_graphics(myglwin);
GLUquadricObj *qobj1 = gluNewQuadric();
GLUquadricObj *qobj2 = gluNewQuadric();
/*
* main interactive loop
*/
while (1) {
// Let the tracker do its thing
tkr->mainloop();
btn->mainloop();
fdv->mainloop();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_axes();
draw_tracker(tposquat, qobj1, qobj2);
glwin_swap_buffers(myglwin);
}
} /* main */
/*
* glwin.c -- simple code for opening an OpenGL display window, and doing
* BitBlt operations or whatever...
*
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef USEOPENGL
#if !defined(WIN32) && !defined(_MSC_VER)
#include <X11/Xlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
#include "glwin.h"
typedef struct {
int scrnum;
Display *dpy;
Window root;
Window win;
GLXContext ctx;
int width;
int height;
} oglhandle;
void * glwin_create(int width, int height) {
oglhandle * handle;
XSetWindowAttributes attr;
unsigned long mask;
XVisualInfo *visinfo;
int attrib[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None };
handle = (oglhandle *) malloc(sizeof(oglhandle));
if (handle == NULL) {
printf("failed to allocate handle\n");
return NULL;
}
handle->width = width;
handle->height = height;
handle->dpy = XOpenDisplay(getenv("DISPLAY"));
if (handle->dpy == NULL) {
printf("failed to open Display!!\n");
free(handle);
return NULL;
}
handle->scrnum = DefaultScreen( handle->dpy );
handle->root = RootWindow( handle->dpy, handle->scrnum );
visinfo = glXChooseVisual( handle->dpy, handle->scrnum, attrib );
if (visinfo == NULL) {
printf("Error: couldn't get an RGB, Double-buffered visual\n");
free(handle);
return NULL;
}
/* window attributes */
attr.background_pixel = 0;
attr.border_pixel = 0;
attr.colormap = XCreateColormap(handle->dpy, handle->root,
visinfo->visual, AllocNone);
attr.event_mask = StructureNotifyMask | ExposureMask;
mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
handle->win = XCreateWindow(handle->dpy, handle->root, 0, 0, width, height,
0, visinfo->depth, InputOutput,
visinfo->visual, mask, &attr );
handle->ctx = glXCreateContext( handle->dpy, visinfo, NULL, True );
glXMakeCurrent( handle->dpy, handle->win, handle->ctx );
XStoreName(handle->dpy, handle->win,
"OpenGL Window");
XMapWindow(handle->dpy, handle->win);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glwin_swap_buffers(handle);
glClear(GL_COLOR_BUFFER_BIT);
glwin_swap_buffers(handle);
glwin_handle_events(handle);
glwin_handle_events(handle);
XFlush(handle->dpy);
return handle;
}
void glwin_destroy(void * voidhandle) {
oglhandle * handle = (oglhandle *) voidhandle;
if (handle == NULL)
return;
XUnmapWindow(handle->dpy, handle->win);
glXMakeCurrent(handle->dpy, None, NULL);
XDestroyWindow(handle->dpy, handle->win);
XCloseDisplay(handle->dpy);
}
void glwin_swap_buffers(void * voidhandle) {
oglhandle * handle = (oglhandle *) voidhandle;
if (handle != NULL)
glXSwapBuffers(handle->dpy, handle->win);
}
int glwin_handle_events(void * voidhandle) {
oglhandle * handle = (oglhandle *) voidhandle;
XEvent event;
if (handle == NULL)
return 0;
XNextEvent(handle->dpy, &event);
switch(event.type) {
case ConfigureNotify:
handle->width = event.xconfigure.width;
handle->height = event.xconfigure.height;
glShadeModel(GL_FLAT);
glViewport(0, 0, handle->width, handle->height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* this is upside-down for what most code thinks, but it is */
/* right side up for the ray tracer.. */
gluOrtho2D(0.0, handle->width, 0.0, handle->height);
glMatrixMode(GL_MODELVIEW);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelZoom(1.0, 1.0); /* for upside-down images that ray uses */
return 1;
}
return 0;
}
void glwin_draw_image(void * voidhandle, int xsize, int ysize,
unsigned char * img) {
glRasterPos2i(0, 0);
glDrawPixels(xsize, ysize, GL_RGB, GL_UNSIGNED_BYTE, img);
glwin_swap_buffers(voidhandle);
}
#else
/*
* WIN32 Version
*/
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "glwin.h"
typedef struct {
HWND hWnd;
HDC hDC;
HGLRC hRC;
long scrwidth;
long scrheight;
int width;
int height;
} oglhandle;
static char szAppName[] = "OpenGL";
static char szAppTitle[]="OpenGL Window";
/* XXX GET RID OF THIS!!!!! */
static oglhandle * globhandle; /* XXX GET RID OF THIS!!!!! */
/* XXX GET RID OF THIS!!!!! */
/*
* declaration of myWindowProc()
*/
LONG WINAPI myWindowProc( HWND, UINT, WPARAM, LPARAM );
static int OpenWin32Connection(oglhandle * handle) {
WNDCLASS wc;
HINSTANCE hInstance = GetModuleHandle(NULL);
/* Clear (important!) and then fill in the window class structure. */
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_OWNDC;
wc.lpfnWndProc = (WNDPROC) myWindowProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
wc.hbrBackground = NULL; /* Default color */
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
if(!RegisterClass(&wc)) {
printf("Cannot register window class.\n");
return -1;
}
handle->scrwidth = GetSystemMetrics(SM_CXSCREEN);
handle->scrheight = GetSystemMetrics(SM_CYSCREEN);
return 0;
}
static HGLRC SetupOpenGL(HWND hWnd) {
int nMyPixelFormatID;
HDC hDC;
HGLRC hRC;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof (PIXELFORMATDESCRIPTOR), /* struct size */
1, /* Version number */
PFD_DRAW_TO_WINDOW /* Flags, draw to a window, */
| PFD_SUPPORT_OPENGL, /* use OpenGL */
PFD_TYPE_RGBA, /* RGBA pixel values */
24, /* 24-bit color */
0, 0, 0, /* RGB bits & shift sizes. */
0, 0, 0, /* Don't care about them */
0, 0, /* No alpha buffer info */
0, 0, 0, 0, 0, /* No accumulation buffer */
32, /* 32-bit depth buffer */
0, /* No stencil buffer */
0, /* No auxiliary buffers */
PFD_MAIN_PLANE, /* Layer type */
0, /* Reserved (must be 0) */
0, /* No layer mask */
0, /* No visible mask */
0 /* No damage mask */
};
hDC = GetDC(hWnd);
nMyPixelFormatID = ChoosePixelFormat(hDC, &pfd);
/*
* catch errors here.
* If nMyPixelFormat is zero, then there's
* something wrong... most likely the window's
* style bits are incorrect (in CreateWindow() )
* or OpenGL isn't installed on this machine
*
*/
if (nMyPixelFormatID == 0) {
printf("Error selecting OpenGL Pixel Format!!\n");
return NULL;
}
SetPixelFormat( hDC, nMyPixelFormatID, &pfd );
hRC = wglCreateContext(hDC);
ReleaseDC(hWnd, hDC);
return hRC;
}
static int myCreateWindow(oglhandle * handle,
int xpos, int ypos, int xs, int ys) {
/* Create a main window for this application instance. */
handle->hWnd =
CreateWindow(
szAppName, /* app name */
szAppTitle, /* Text for window title bar */
WS_OVERLAPPEDWINDOW /* Window style */
| WS_CLIPCHILDREN
| WS_CLIPSIBLINGS, /* NEED THESE for OpenGL calls to work! */
xpos, ypos,
xs, ys,
NULL, /* no parent window */
NULL, /* Use the window class menu. */
GetModuleHandle(NULL), /* This instance owns this window */
handle /* We don't use any extra data */
);
if (!handle->hWnd) {
printf("Couldn't Open Window!!\n");
return -1;
}
handle->hDC = GetDC(handle->hWnd);
wglMakeCurrent(handle->hDC, handle->hRC);
/* Make the window visible & update its client area */
ShowWindow( handle->hWnd, SW_SHOW); /* Show the window */
UpdateWindow( handle->hWnd ); /* Sends WM_PAINT message */
return 0;
}
LONG WINAPI myWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps; /* Paint structure. */
switch(msg) {
case WM_CREATE:
globhandle->hRC = SetupOpenGL(hwnd);
return 0;
case WM_SIZE:
// hDC = GetDC(hwnd);
wglMakeCurrent(globhandle->hDC, globhandle->hRC);
glShadeModel(GL_FLAT);
glViewport(0, 0, (GLsizei) LOWORD(lParam), (GLsizei) HIWORD (lParam));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* this is upside-down for what most code thinks, but it is */
/* right side up for the ray tracer.. */
gluOrtho2D(0.0, (GLsizei) LOWORD(lParam), 0.0, (GLsizei) HIWORD (lParam));
glMatrixMode(GL_MODELVIEW);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelZoom(1.0, 1.0); /* for upside-down images that ray uses */
return 0;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_PAINT:
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
void * glwin_create(int width, int height) {
oglhandle * handle;
int rc;
handle = (oglhandle *) malloc(sizeof(oglhandle));
if (handle == NULL)
return NULL;
globhandle = handle;
rc = OpenWin32Connection(handle);
if (rc != 0) {
printf("OpenWin32Connection() returned an error!\n");
free(handle);
return NULL;
}
handle->width = width;
handle->height = height;
rc = myCreateWindow(handle, 0, 0, width, height);
if (rc != 0) {
printf("CreateWindow() returned an error!\n");
free(handle);
return NULL;
}
return handle;
}
void glwin_destroy(void * voidhandle) {
oglhandle * handle = (oglhandle *) voidhandle;
wglDeleteContext(handle->hRC);
PostQuitMessage( 0 );
/* glwin_handle_events(handle); */
}
void glwin_swap_buffers(void * voidhandle) {
oglhandle * handle = (oglhandle *) voidhandle;
glFlush();
}
int glwin_handle_events(void * voidhandle) {
oglhandle * handle = (oglhandle *) voidhandle;
MSG msg;
if (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
return 1;
}
return 0;
}
void glwin_draw_image(void * voidhandle, int xsize, int ysize, unsigned char * img) {
oglhandle * handle = (oglhandle *) voidhandle;
glRasterPos2i(0, 0);
glDrawPixels(xsize, ysize, GL_RGB, GL_UNSIGNED_BYTE, img);
glwin_swap_buffers(voidhandle);
return;
}
#endif
#else
void * glwin_create(int width, int height) {
return NULL;
}
void glwin_destroy(void * voidhandle) {
return;
}
void glwin_swap_buffers(void * voidhandle) {
return;
}
int glwin_handle_events(void * voidhandle) {
return 0;
}
void glwin_draw_image(void * voidhandle, int xsize, int ysize, unsigned char * img) {
return;
}
#endif
#ifdef __cplusplus
extern "C" {
#endif
void * glwin_create(int width, int height);
void glwin_destroy(void * voidhandle);
void glwin_swap_buffers(void * voidhandle);
int glwin_handle_events(void * voidhandle);
void glwin_draw_image(void * voidhandle, int xsize, int ysize, unsigned char * img);
#ifdef __cplusplus
}
#endif
#include <stdlib.h>
#include <stdio.h>
#include <vrpn_ForceDevice.h>
#include <vrpn_Tracker.h>
#include <vrpn_Button.h>
/*****************************************************************************
*
Callback handler
*
*****************************************************************************/
void handle_force_change(void *userdata, const vrpn_FORCECB f)
{
static vrpn_FORCECB lr; // last report
static int first_report_done = 0;
if ((!first_report_done) ||
((f.force[0] != lr.force[0]) || (f.force[1] != lr.force[1])
|| (f.force[2] != lr.force[2]))) {
//printf("force is (%f,%f,%f)\n", f.force[0], f.force[1], f.force[2]);
}
first_report_done = 1;
lr = f;
}
void handle_tracker_change(void *userdata, const vrpn_TRACKERCB t)
{
static vrpn_TRACKERCB lr; // last report
static float dist_interval_sq = 0.004;
float *pos = (float *)userdata;
if ((lr.pos[0] - t.pos[0])*(lr.pos[0] - t.pos[0]) +
(lr.pos[1] - t.pos[1])*(lr.pos[1] - t.pos[1]) +
(lr.pos[2] - t.pos[2])*(lr.pos[2] - t.pos[2]) + dist_interval_sq){
//printf("Sensor %d is now at (%g,%g,%g)\n", t.sensor,
// t.pos[0], t.pos[1], t.pos[2]);
lr = t;
}
pos[0] = t.pos[0];
pos[1] = t.pos[1];
pos[2] = t.pos[2];
}
void handle_button_change(void *userdata, const vrpn_BUTTONCB b)
{
static int buttonstate = 1;
if (b.state != buttonstate) {
printf("button #%ld is in state %ld\n", b.button, b.state);
buttonstate = b.state;
}
*(int *)userdata = buttonstate;
}
int main(int argc, char **argv) {
int done = 0;
float pos[3];
int forceInEffect = 0;
int forceEnabled = 0;
vrpn_ForceDevice_Remote *forceDevice;
vrpn_Tracker_Remote *tracker;
vrpn_Button_Remote *button;
if (argc < 2) {
printf("Usage: %s Phantom@machine\n", argv[0]);
exit(1);
}
char * servername = argv[1];
/* initialize the force device */
forceDevice = new vrpn_ForceDevice_Remote(servername);
forceDevice->register_force_change_handler(NULL, handle_force_change);
/* initialize the tracker */
tracker = new vrpn_Tracker_Remote(servername);
tracker->register_change_handler((void *)pos, handle_tracker_change);
/* initialize the button */
button = new vrpn_Button_Remote(servername);
button->register_change_handler((void *)&forceEnabled, handle_button_change);
// main loop
while (! done ) {
// Let the forceDevice send its messages to remote force device
forceDevice->mainloop();
// Let tracker receive position information from remote tracker
tracker->mainloop();
// Let button receive button status from remote button
button->mainloop();
if (forceEnabled) {
forceDevice->setFF_Origin(pos[0],pos[1],pos[2]);
// units = dynes
forceDevice->setFF_Force(0.02*cos(pos[0]*200.0*M_PI),
0.02*cos(pos[1]*200.0*M_PI),
0.02*cos(pos[2]*200.0*M_PI));
// set derivatives of force field:
// units = dynes/meter
forceDevice->setFF_Jacobian(-4.0*M_PI*sin(pos[0]*200.0*M_PI), 0, 0, 0,
-4.0*M_PI*sin(pos[1]*200.0*M_PI), 0, 0, 0,
-4.0*M_PI*sin(pos[2]*200.0*M_PI));
forceDevice->setFF_Radius(0.15); // radius of validity
forceDevice->sendForceField();
forceInEffect = 1;
}
else if (forceInEffect){
forceDevice->stopForceField();
forceInEffect = 0;
}
}
} /* main */
#include <stdlib.h>
#include <stdio.h>
#include <vrpn_ForceDevice.h>
#include <vrpn_Tracker.h>
#include <vrpn_Button.h>
#define PHANTOM_SERVER "Phantom@budapest"
/******************************************************************************
* Callback handler
*****************************************************************************/
void handle_force_change(void *userdata, vrpn_FORCECB f)
{
static vrpn_FORCECB lr; // last report
static int first_report_done = 0;
if ((!first_report_done) ||
((f.force[0] != lr.force[0]) || (f.force[1] != lr.force[1])
|| (f.force[2] != lr.force[2])))
printf("force is (%f,%f,%f)\n", f.force[0], f.force[1], f.force[2]);
first_report_done = 1;
lr = f;
}
void handle_tracker_change(void *userdata, const vrpn_TRACKERCB t)
{
static vrpn_TRACKERCB lr; // last report
static float dist_interval_sq = 0.004;
if ((lr.pos[0] - t.pos[0])*(lr.pos[0] - t.pos[0]) +
(lr.pos[1] - t.pos[1])*(lr.pos[1] - t.pos[1]) +
(lr.pos[2] - t.pos[2])*(lr.pos[2] - t.pos[2]) > dist_interval_sq){
printf("Sensor %d is now at (%g,%g,%g)\n", t.sensor,
t.pos[0], t.pos[1], t.pos[2]);
lr = t;
}
}
void handle_button_change(void *userdata, vrpn_BUTTONCB b)
{
static int count=0;
static int buttonstate = 1;
int done = 0;
if (b.state != buttonstate) {
printf("button #%ld is in state %ld\n", b.button, b.state);
buttonstate = b.state;
count++;
}
if (count > 4)
done = 1;
*(int *)userdata = done;
}
int main(int argc, char *argv[]) {
int done = 0;
vrpn_ForceDevice_Remote *forceDevice;
vrpn_Tracker_Remote *tracker;
vrpn_Button_Remote *button;
/* initialize the force device */
forceDevice = new vrpn_ForceDevice_Remote(PHANTOM_SERVER);
forceDevice->register_force_change_handler(NULL, handle_force_change);
/* initialize the tracker */
tracker = new vrpn_Tracker_Remote(PHANTOM_SERVER);
tracker->register_change_handler(NULL, handle_tracker_change);
/* initialize the button */
button = new vrpn_Button_Remote(PHANTOM_SERVER);
button->register_change_handler(&done, handle_button_change);
// Set plane and surface parameters
forceDevice->set_plane(0.0, 1.0, 0.0, 1.0);
forceDevice->setSurfaceKspring(0.1); // spring constant - units of
// dynes/cm
forceDevice->setSurfaceKdamping(0.002); // damping constant -
// units of dynes*sec/cm
forceDevice->setSurfaceFstatic(0.01); // set static friction
forceDevice->setSurfaceFdynamic(0.005); // set dynamic friction
forceDevice->setRecoveryTime(10); // recovery occurs over 10
// force update cycles
// enable force device and send first surface
forceDevice->startSurface();
// main loop
while (! done ){
// Let the forceDevice send its planes to remote force device
forceDevice->mainloop();
// Let tracker receive position information from remote tracker
tracker->mainloop();
// Let button receive button status from remote button
button->mainloop();
// we may call forceDevice->set_plane(...) followed by
// forceDevice->sendSurface() here to change the plane
// for example: using position information from a tracker we can
// compute a plane to approximate a complex surface at that
// position and send that approximation 15-30 times per
// second to simulate the complex surface
}
// shut off force device
forceDevice->stopSurface();
} /* main */
#include <stdlib.h>
#include <stdio.h>
#include <vrpn_ForceDevice.h>
#include <vrpn_Tracker.h>
#include <vrpn_Button.h>
#define PHANTOM_SERVER "Tracker0@localhost"
/*****************************************************************************
*
Callback handler
*
*****************************************************************************/
void handle_force_change(void *userdata, const vrpn_FORCECB f)
{
static vrpn_FORCECB lr; // last report
static int first_report_done = 0;
if ((!first_report_done) ||
((f.force[0] != lr.force[0]) || (f.force[1] != lr.force[1])
|| (f.force[2] != lr.force[2]))) {
//printf("force is (%f,%f,%f)\n", f.force[0], f.force[1], f.force[2]);
}
first_report_done = 1;
lr = f;
}
void handle_tracker_change(void *userdata, const vrpn_TRACKERCB t)
{
static vrpn_TRACKERCB lr; // last report
static float dist_interval_sq = 0.04;
float *pos = (float *)userdata;
if ((lr.pos[0] - t.pos[0])*(lr.pos[0] - t.pos[0]) +
(lr.pos[1] - t.pos[1])*(lr.pos[1] - t.pos[1]) +
(lr.pos[2] - t.pos[2])*(lr.pos[2] - t.pos[2]) + dist_interval_sq){
//printf("Sensor %d is now at (%g,%g,%g)\n", t.sensor,
// t.pos[0], t.pos[1], t.pos[2]);
lr = t;
}
pos[0] = t.pos[0];
pos[1] = t.pos[1];
pos[2] = t.pos[2];
}
void handle_button_change(void *userdata, const vrpn_BUTTONCB b)
{
static int buttonstate = 1;
if (b.state != buttonstate) {
printf("button #%ld is in state %ld\n", b.button, b.state);
buttonstate = b.state;
}
*(int *)userdata = buttonstate;
}
int main(int argc, char *argv[]) {
int done = 0;
float pos[3];
int forceInEffect = 0;
int forceEnabled = 0;
vrpn_ForceDevice_Remote *forceDevice;
vrpn_Tracker_Remote *tracker;
vrpn_Button_Remote *button;
float t;
/* initialize the force device */
forceDevice = new vrpn_ForceDevice_Remote(PHANTOM_SERVER);
forceDevice->register_force_change_handler(NULL, handle_force_change);
/* initialize the tracker */
tracker = new vrpn_Tracker_Remote(PHANTOM_SERVER);
tracker->register_change_handler((void *)pos, handle_tracker_change);
/* initialize the button */
button = new vrpn_Button_Remote(PHANTOM_SERVER);
button->register_change_handler((void *)&forceEnabled, handle_button_change);
t=0.0;
// main loop
while (!done) {
t+=0.001f;
// Let the forceDevice send its messages to remote force device
forceDevice->mainloop();
// Let tracker receive position information from remote tracker
tracker->mainloop();
// Let button receive button status from remote button
button->mainloop();
forceEnabled = 1;
if (forceEnabled) {
float py = (0.05 * sin(t + pos[0]*40.0*M_PI) + 0.05);
if (pos[1] > py) {
forceDevice->stopForceField();
forceInEffect = 0;
} else {
float fy;
forceDevice->setFF_Origin(pos[0],pos[1],pos[2]);
fy = (py - pos[1]) * 14.0;
// units = dynes
forceDevice->setFF_Force(0.20 * cos(t + pos[0]*200.0*M_PI),
fy,
0.0 * cos( pos[2]*200.0*M_PI));
// set derivatives of force field:
// units = dynes/meter
forceDevice->setFF_Jacobian(
-20.0*sin(t + pos[0]*200.0*M_PI), 0, 0,
0, -14.0, 0,
0, 0, -0.0*M_PI*sin(pos[2]*200.0*M_PI)
);
forceDevice->setFF_Radius(0.02); // 2cm radius of validity
forceDevice->sendForceField();
forceInEffect = 1;
}
}
else if (forceInEffect){
forceDevice->stopForceField();
forceInEffect = 0;
}
}
} /* main */
#include <stdio.h>
#include "vrpn_Tracker.h"
#include "vrpn_Button.h"
#include "vrpn_ForceDevice.h"
#include "quat.h"
#include "glwin.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>
typedef struct {
float x; // position
float y;
float z;
} pos;
typedef struct {
float fx;
float fy;
float fz;
} force;
typedef struct {
float x;
float y;
float z;
float vx;
float vy;
float vz;
float ax;
float ay;
float az;
float mass;
float charge;
} state;
void handle_tracker(void *userdata, const vrpn_TRACKERCB t) {
pos *p = (pos *) userdata;
p->x = -t.pos[0];
p->y = t.pos[1];
p->z = -t.pos[2];
}
void handle_force(void *userdata, const vrpn_FORCECB fcb) {
force *f = (force *) userdata;
f->fx = -fcb.force[0];
f->fy = fcb.force[1];
f->fz = -fcb.force[2];
}
void handle_button(void *userdata, const vrpn_BUTTONCB b) {
switch (b.button) {
case 0:
*(int *)userdata = b.state;
if (b.state == 1) printf("Force field enabled\n");
else printf("Force field disabled\n");
break;
default:
printf("Unknown button %3d was pressed\n",b.button);
}
fflush(stdout);
}
void init_graphics(void *myglwin)
{
// allw the window to open and do its stuff once
glwin_handle_events(myglwin);
// setup OpenGL state...
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 0.01, 10.0);
gluLookAt( 0.0, 0.1, -5.0, // eye pos
0.0, 0.0, 0.0, // look at this point
0.0, 1.0, 0.0); // up direction
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
//
// Setup cute lighting parms..
//
glEnable(GL_LIGHTING);
GLfloat lpos[] = { 0.0, 0.0, -100.0, 1.0};
GLfloat lamb[] = { 0.05, 0.05, 0.05, 1.0};
GLfloat ldif[] = { 0.5, 0.5, 0.5, 1.0};
GLfloat lspc[] = { 0.5, 0.5, 0.5, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, lpos);
glLightfv(GL_LIGHT0, GL_AMBIENT, lamb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, ldif);
glLightfv(GL_LIGHT0, GL_SPECULAR, lspc);
glLightf(GL_LIGHT0, GL_SHININESS, 400.0);
glEnable(GL_LIGHT0);
GLfloat mdif[] = {0.9, 0.0, 0.2, 1.0};
GLfloat mspc[] = {0.2, 0.2, 0.2, 1.0};
glMaterialfv(GL_FRONT, GL_DIFFUSE, mdif);
glMaterialfv(GL_FRONT, GL_SPECULAR, mspc);
glEnable(GL_COLOR_MATERIAL);
}
void draw_axes() {
//
// Draw Coordinate Axes
//
glDisable(GL_LIGHTING);
glBegin(GL_LINES);
float i;
glColor3f(0.0, 1.0, 0.0);
for (i=-10; i<10; i += 0.50) {
glVertex3f( -10.0, 0.0, i);
glVertex3f( 10.0, 0.0, i);
glVertex3f( i, 0.0, -10.0);
glVertex3f( i, 0.0, 10.0);
}
for (i=-10; i<10; i += 0.50) {
glVertex3f( -10.0, 1.0, i);
glVertex3f( 10.0, 1.0, i);
glVertex3f( i, 1.0, -10.0);
glVertex3f( i, 1.0, 10.0);
}
glEnd();
}
void draw_tracker_and_atom(force *p, GLUquadricObj *qobj,
state *atom, GLUquadricObj *qatom)
{
float x, y, z;
float atomx, atomy, atomz;
// Convert from atom coordinates to scene coordinates
atomx = atom->x;
atomy = atom->y;
atomz = atom->z;
// Convert from haptic coordinates to our scene coordinates
x=-(p->fx) + atomx;
y=-(p->fy) + atomy;
z=-(p->fz) + atomz;
// First draw line from tracker to atom
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex3f(x, y, z);
glVertex3f(atomx, atomy, atomz);
glEnd();
glEnable(GL_LIGHTING);
glColor3f(1.0, 0.0, 0.2);
glPushMatrix();
glTranslatef(x, y, z);
gluSphere(qobj, 0.2, 32, 32);
glPopMatrix();
glColor3f(0.0, 1.0, 0.2);
glPushMatrix();
glTranslatef(atomx, atomy, atomz);
gluSphere(qobj, 0.2, 32, 32);
glPopMatrix();
}
void apply_bc(state *atom)
{
const float floor_k = 20.0;
const float maxdim = 1.0;
const float nearclip = 3.99;
const float farclip = 5.0;
if (atom->y < 0) atom->ay -= floor_k*atom->y/atom->mass;
if (atom->y > maxdim) atom->ay -= floor_k*(atom->y-maxdim)/atom->mass;
if (atom->x > maxdim) atom->ax -= floor_k*(atom->x-maxdim)/atom->mass;
if (atom->x < -maxdim) atom->ax -= floor_k*(atom->x+maxdim)/atom->mass;
if (atom->z > farclip) atom->az -= floor_k*(atom->z-farclip)/atom->mass;
if (atom->z < -nearclip) atom->az -= floor_k*(atom->z+nearclip)/atom->mass;
}
void run_dynamics(state *atom, force *f)
{
const int N=1;
const float dt = 0.0004;
for (int i=0; i < N; i++) {
atom->x += dt*atom->vx + .5*dt*dt*atom->ax;
atom->y += dt*atom->vy + .5*dt*dt*atom->ay;
atom->z += dt*atom->vz + .5*dt*dt*atom->az;
atom->vx += 0.5*dt*(atom->ax);
atom->vy += 0.5*dt*(atom->ay);
atom->vz += 0.5*dt*(atom->az);
atom->ax = -f->fx/atom->mass;
atom->ay = -f->fy/atom->mass;
atom->az = -f->fz/atom->mass;
apply_bc(atom);
atom->vx += 0.5*dt*(atom->ax);
atom->vy += 0.5*dt*(atom->ay);
atom->vz += 0.5*dt*(atom->az);
}
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("%s: Invalid parms\n", argv[0]);
printf("usage: \n");
printf(" %s Tracker0@host\n", argv[0]);
return -1;
}
char *server = argv[1];
pos *phan_position = new pos;
pos *phan_offset = new pos;
force *phan_force = new force;
state *atom_state = new state;
int ff_enabled, ff_active;
double kspr = 500.0; // Units???
vrpn_Tracker_Remote *tkr;
vrpn_Button_Remote *btn;
vrpn_ForceDevice_Remote *fdv;
printf("Opening: %s ...\n", server);
tkr = new vrpn_Tracker_Remote(server);
tkr->register_change_handler(phan_position, handle_tracker);
btn = new vrpn_Button_Remote(server);
btn->register_change_handler(&ff_enabled, handle_button);
fdv = new vrpn_ForceDevice_Remote(server);
fdv->register_force_change_handler(phan_force, handle_force);
void * myglwin = glwin_create(700, 700);
if (myglwin == NULL) {
printf("Failed to open OpenGL window!!\n");
return -1;
}
atom_state->x = 0.0; atom_state->y = 0.0; atom_state->z = 0.0;
atom_state->vx = 0.0; atom_state->vy = 0.0; atom_state->vz = 0.0;
atom_state->ax = 0.0; atom_state->ay = 0.0; atom_state->az = 0.0;
atom_state->mass = .001;
ff_active = 0;
init_graphics(myglwin);
GLUquadricObj *qobj = gluNewQuadric();
GLUquadricObj *qatom = gluNewQuadric();
/*
* main interactive loop
*/
while (1) {
// Let the tracker do its thing
tkr->mainloop();
btn->mainloop();
fdv->mainloop();
run_dynamics(atom_state, phan_force);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (ff_enabled ) {
if (!ff_active) {
// Set up force field so initially the force is zero
phan_offset->x = phan_position->x - .2*atom_state->x;
phan_offset->y = phan_position->y - .2*atom_state->y;
phan_offset->z = phan_position->z - .2*atom_state->z;
}
// Now turn the force field on
// scene -> haptic: rotate 180 about the y axis
fdv->setConstraintMode(vrpn_ForceDevice::POINT_CONSTRAINT);
float cpos[3];
cpos[0] = -(.2*atom_state->x + phan_offset->x);
cpos[1] = (.2*atom_state->y + phan_offset->y);
cpos[2] = -(.2*atom_state->z + phan_offset->z);
fdv->setConstraintPoint(cpos);
fdv->setConstraintKSpring(kspr);
fdv->enableConstraint(1); // enable force field
ff_active = 1;
}
else if (ff_active) {
fdv->enableConstraint(0); // disable force field
ff_active = 0;
}
draw_axes();
draw_tracker_and_atom(phan_force, qobj, atom_state, qatom);
glwin_swap_buffers(myglwin);
}
} /* main */
#include <stdio.h>
#include "vrpn_Tracker.h"
#include "vrpn_Button.h"
#include "vrpn_ForceDevice.h"
void handle_tracker(void *userdata, const vrpn_TRACKERCB t) {
printf("Sensor %3d is now at (%11f, %11f, %11f, %11f, %11f, %11f, %11f) \r",
t.sensor, t.pos[0], t.pos[1], t.pos[2],
t.quat[0], t.quat[1], t.quat[2], t.quat[3]);
fflush(stdout);
}
void handle_button(void *userdata, const vrpn_BUTTONCB b) {
printf("\nButton %3d is in state: %d \n",
b.button, b.state);
fflush(stdout);
}
void handle_force(void *userdata, const vrpn_FORCECB f) {
static vrpn_FORCECB lr; // last report
static int first_report_done = 0;
if ((!first_report_done) || ((f.force[0] != lr.force[0]) ||
(f.force[1] != lr.force[1]) || (f.force[2] != lr.force[2])))
printf("\nForce is (%11f, %11f, %11f) \n",
f.force[0], f.force[1], f.force[2]);
first_report_done = 1;
lr = f;
}
int main(int argc, char **argv) {
char * server;
vrpn_Tracker_Remote *tkr;
vrpn_Button_Remote *btn;
vrpn_ForceDevice_Remote *fdv;
if (argc < 2) {
printf("%s: Invalid parms\n", argv[0]);
printf("usage: \n");
printf(" %s Tracker0@host\n", argv[0]);
return -1;
}
server = argv[1];
printf("Opening: %s ...\n", server);
tkr = new vrpn_Tracker_Remote(server);
tkr->register_change_handler(NULL, handle_tracker);
btn = new vrpn_Button_Remote(server);
btn->register_change_handler(NULL, handle_button);
fdv = new vrpn_ForceDevice_Remote(server);
fdv->register_force_change_handler(NULL, handle_force);
tkr->reset_origin();
/*
* main interactive loop
*/
while (1) {
// Let the tracker do its thing
tkr->mainloop();
btn->mainloop();
fdv->mainloop();
}
} /* main */
File deleted
#include <fcntl.h> // for open, O_RDONLY
#ifndef _WIN32
#include <netinet/in.h> // for ntohl
#include <unistd.h> // for close, read
#endif
#include <stdio.h> // for printf, fprintf, stderr
#include <stdlib.h> // for exit
#include <vrpn_Connection.h> // for vrpn_HANDLERPARAM, etc
#include "vrpn_Shared.h" // for timeval
const int buflen = 8000;
void Usage (const char * name) {
fprintf(stderr, "Usage: %s <filename>\n", name);
}
int main (int argc, char ** argv) {
char buffer [buflen];
vrpn_HANDLERPARAM header;
int file;
int retval;
if (argc != 2) {
Usage(argv[0]);
exit(0);
}
file = open(argv[1], O_RDONLY);
if (file == -1) {
fprintf(stderr, "Couldn't open \"%s\".\n", argv[1]);
exit(0);
}
while (1) {
int len;
struct timeval time;
long sender;
long type;
int len2;
retval = read(file, &header, sizeof(header));
if (retval < 0) { printf("ERROR\n"); close(file); exit(0); }
if (!retval) { printf("EOF\n"); close(file); exit(0); }
len = ntohl(header.payload_len);
time.tv_sec = ntohl(header.msg_time.tv_sec);
time.tv_usec = ntohl(header.msg_time.tv_usec);
sender = ntohl(header.sender);
type = ntohl(header.type);
//printf("Message type %d, sender %d, payload length %d\n",
//type, sender, len);
retval = read(file, buffer, len);
if (retval < 0) { printf("ERROR\n"); close(file); exit(0); }
if (!retval) { printf("EOF\n"); close(file); exit(0); }
//printf(" <%d bytes>\n", retval);
switch (type) {
case vrpn_CONNECTION_SENDER_DESCRIPTION:
len2 = ntohl(* ((int *) buffer));
buffer[len2 + sizeof(int)] = 0;
printf(" The name of sender #%ld is \"%s\".\n", sender, buffer + sizeof(int));
break;
case vrpn_CONNECTION_TYPE_DESCRIPTION:
len2 = ntohl(* ((int *) buffer));
buffer[len2 + sizeof(int)] = 0;
//printf(" The name of type #%d is \"%s\".\n", sender, buffer + sizeof(int));
break;
case vrpn_CONNECTION_UDP_DESCRIPTION:
buffer[len] = 0;
//printf(" UDP host is \"%s\", port %d.\n", buffer, sender);
break;
case vrpn_CONNECTION_LOG_DESCRIPTION:
buffer[len] = 0;
//printf(" Log to file \"%s\".\n", buffer);
break;
}
}
}
File deleted
#include <fcntl.h> // for open, O_RDONLY
#ifndef _WIN32
#include <netinet/in.h> // for ntohl
#include <unistd.h> // for close, read
#endif
#include <stdio.h> // for printf, fprintf, stderr
#include <stdlib.h> // for exit
#include <vrpn_Connection.h> // for vrpn_HANDLERPARAM, etc
#include "vrpn_Shared.h" // for timeval
const int buflen = 8000;
void Usage (const char * name) {
fprintf(stderr, "Usage: %s <filename>\n", name);
}
int main (int argc, char ** argv) {
char buffer [buflen];
vrpn_HANDLERPARAM header;
int file;
int retval;
if (argc != 2) {
Usage(argv[0]);
exit(0);
}
file = open(argv[1], O_RDONLY);
if (file == -1) {
fprintf(stderr, "Couldn't open \"%s\".\n", argv[1]);
exit(0);
}
while (1) {
int len;
struct timeval time;
long sender;
long type;
int len2;
retval = read(file, &header, sizeof(header));
if (retval < 0) { printf("ERROR\n"); close(file); exit(0); }
if (!retval) { printf("EOF\n"); close(file); exit(0); }
len = ntohl(header.payload_len);
time.tv_sec = ntohl(header.msg_time.tv_sec);
time.tv_usec = ntohl(header.msg_time.tv_usec);
sender = ntohl(header.sender);
type = ntohl(header.type);
//printf("Message type %d, sender %d, payload length %d\n",
//type, sender, len);
retval = read(file, buffer, len);
if (retval < 0) { printf("ERROR\n"); close(file); exit(0); }
if (!retval) { printf("EOF\n"); close(file); exit(0); }
//printf(" <%d bytes>\n", retval);
switch (type) {
case vrpn_CONNECTION_SENDER_DESCRIPTION:
len2 = ntohl(* ((int *) buffer));
buffer[len2 + sizeof(int)] = 0;
//printf(" The name of sender #%d is \"%s\".\n", sender, buffer + sizeof(int));
break;
case vrpn_CONNECTION_TYPE_DESCRIPTION:
len2 = ntohl(* ((int *) buffer));
buffer[len2 + sizeof(int)] = 0;
printf(" The name of type #%ld is \"%s\".\n", sender, buffer + sizeof(int));
break;
case vrpn_CONNECTION_UDP_DESCRIPTION:
buffer[len] = 0;
//printf(" UDP host is \"%s\", port %d.\n", buffer, sender);
break;
case vrpn_CONNECTION_LOG_DESCRIPTION:
buffer[len] = 0;
//printf(" Log to file \"%s\".\n", buffer);
break;
}
}
}
#include <unistd.h>
#include <stdio.h>
#include "vrpn_Sound.h"
char *SERVER = "Sound0@ioph100";
int main()
{
vrpn_Sound_Remote *remote;
remote = new vrpn_Sound_Remote(SERVER);
printf("Opened sound server %s\n",SERVER);
printf("Playing at level 0.24:\n");
remote->play_midi_sound(0.24);
remote->mainloop();
sleep (10);
printf("Playing at level 0.45:\n");
remote->play_midi_sound(0.45);
remote->mainloop();
sleep (10);
printf("Playing at level 0.74:\n");
remote->play_midi_sound(0.74);
remote->mainloop();
sleep (10);
printf("Playing at level 1.00:\n");
remote->play_midi_sound(1.00);
remote->mainloop();
sleep (10);
printf("...Done\n");
remote->play_stop(1);
remote->mainloop();
return 0;
}
File deleted
File deleted
File deleted
File deleted