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 734 deletions
#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
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted