forked from victor29z/haptics-chai3d-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-oring.cpp
732 lines (554 loc) · 22.6 KB
/
10-oring.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//===========================================================================
/*
Software License Agreement (BSD License)
Copyright (c) 2003-2012, CHAI3D.
(www.chai3d.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of CHAI3D nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
\author <http://www.chai3d.org>
\author Francois Conti
\version 3.0.0 $Rev: 839 $
*/
//===========================================================================
//---------------------------------------------------------------------------
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//---------------------------------------------------------------------------
#include "chai3d.h"
//---------------------------------------------------------------------------
#ifndef MACOSX
#include "GL/glut.h"
#else
#include "GLUT/glut.h"
#endif
// MINE***********************
#include "DeltaControl.h"
#pragma comment(lib, "DeltaUSB580.lib")
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// DECLARED CONSTANTS
//---------------------------------------------------------------------------
// initial size (width/height) in pixels of the window display
const int WINDOW_SIZE_W = 800;
const int WINDOW_SIZE_H = 800;
// mouse menu options (right button)
const int OPTION_FULLSCREEN = 1;
const int OPTION_WINDOWDISPLAY = 2;
// use stereo display
const bool USE_STEREO_DISPLAY = false;
//---------------------------------------------------------------------------
// DECLARED VARIABLES
//---------------------------------------------------------------------------
// a world that contains all objects of the virtual environment
cWorld* world;
// a camera that renders the world in the window display
cCamera* camera;
// a light source to illuminate the objects in the world
cSpotLight *light;
// width and height of the current window display
int displayW = 0;
int displayH = 0;
// a haptic device handler
cHapticDeviceHandler* handler;
// a pointer to the current haptic device
cGenericHapticDevice* hapticDevice;
// a virtual tool representing the haptic device in the scene
cToolCursor* tool;
// a virtual torus like object
cShapeTorus* object;
// angular velocity of object
cVector3d angVel(0,0,0);
// a label to display the rate [Hz] at which the simulation is running
cLabel* labelHapticRate;
// level widget to display interaction forces
cLevel* level;
// dial widget to display velocity of torus
cDial* dial;
// indicates if the haptic simulation currently running
bool simulationRunning = false;
// indicates if the haptic simulation has terminated
bool simulationFinished = false;
// frequency counter to measure the simulation haptic rate
cFrequencyCounter frequencyCounter;
// MINE******************
// my dev control and varibles
CDeltaUSBControl deltaCtrl;
double dF[3] ={0};
double dPos[3] = {0};
double dRad[6] = {0};
BYTE statu;
double dT[3]={0};
int nErrCode=0;
//---------------------------------------------------------------------------
// DECLARED MACROS
//---------------------------------------------------------------------------
// convert to resource path
#define RESOURCE_PATH(p) (char*)((resourceRoot+string(p)).c_str())
//---------------------------------------------------------------------------
// DECLARED FUNCTIONS
//---------------------------------------------------------------------------
// callback when the window display is resized
void resizeWindow(int w, int h);
// callback when a key from the representing is pressed
void keySelect(unsigned char key, int x, int y);
// callback when the right mouse button is pressed to select a menu item
void menuSelect(int value);
// function called before exiting the application
void close(void);
// main graphics timer and callback
void graphicsTimer(int data);
void updateGraphics(void);
// main haptics loop
void updateHaptics(void);
//===========================================================================
/*
DEMO: 10-oring.cpp
This demonstration illustrates the use of a simple 3D shape primitive
to create a virtual world. A virtual tool is used to represent the
haptic device. Haptic interaction between the tool and the object
is computed through the force models defined in the CHAI3D framework.
stiffness properties can be modified by adjusting the
parameters of the haptic effect classes.
In the main haptics loop function "updateHaptics()" , the position
of the haptic device is retrieved at each simulation iteration.
The interaction forces are then computed and sent to the device.
Finally, a simple dynamics model is used to simulate the behavior
of the o-ring like object.
*/
//===========================================================================
int main(int argc, char* argv[])
{
//-----------------------------------------------------------------------
// INITIALIZATION
//-----------------------------------------------------------------------
printf ("\n");
printf ("-----------------------------------\n");
printf ("CHAI3D\n");
printf ("Demo: 10-oring\n");
printf ("Copyright 2003-2012\n");
printf ("-----------------------------------\n");
printf ("\n\n");
printf ("Keyboard Options:\n\n");
printf ("[1] - Texture (ON/OFF)\n");
printf ("[2] - Wireframe (ON/OFF)\n");
printf ("[x] - Exit application\n");
printf ("\n\n>\r");
// parse first arg to try and locate resources
string resourceRoot = string(argv[0]).substr(0,string(argv[0]).find_last_of("/\\")+1);
//-----------------------------------------------------------------------
// WORLD - CAMERA - LIGHTING
//-----------------------------------------------------------------------
// create a new world.
world = new cWorld();
// set the background color of the environment
world->m_backgroundColor.setBlack();
// create a camera and insert it into the virtual world
camera = new cCamera(world);
world->addChild(camera);
// position and oriente the camera
// origin***
camera->set( cVector3d (3.0, 0.0, 0.0), // camera position (eye)
cVector3d (0.0, 0.0, 0.0), // lookat position (target)
cVector3d (0.0, 0.0, 1.0)); // direction of the (up) vector
//MINE frame direction*******************************
/* camera->set( cVector3d (0.0, 0.0, -3.0), // camera position (eye)
cVector3d (0.0, 0.0, 0.0), // lookat position (target)
cVector3d (-1.0, 0.0, 0.0)); // direction of the (up) vector*/
// my dev camera
// camera->set( cVector3d (0.0, 0.0,-0.5), // camera position (eye)
// cVector3d (0.0, 0.0, 0.0), // lookat position (target)
// cVector3d (-1.0, 0.0, 0.0)); // direction of the (up) vector
// set the near and far clipping planes of the camera
// anything in front/behind these clipping planes will not be rendered
camera->setClippingPlanes(0.01, 10.0);
// enable shadow casting
camera->setUseShadowCasting(true);
// create a light source
light = new cSpotLight(world);
// attach light to camera
camera->addChild(light);
// enable light source
light->setEnabled(true);
// position the light source
light->setLocalPos( 0.0, 0.5, 0.0);
// define the direction of the light beam
light->setDir(-3.0,-0.5, 0.0);
// enable this light source to generate shadows
light->setShadowMapEnabled(true);
//-----------------------------------------------------------------------
// HAPTIC DEVICES / TOOLS
//-----------------------------------------------------------------------
// create a haptic device handler
handler = new cHapticDeviceHandler();
// get access to the first available haptic device
handler->getDevice(hapticDevice, 0);
// retrieve information about the current haptic device
cHapticDeviceInfo info = hapticDevice->getSpecifications();
// create a 3D tool and add it to the world
tool = new cToolCursor(world);
world->addChild(tool);
// connect the haptic device to the tool
tool->setHapticDevice(hapticDevice); // Á¬½Óhaptic deviceµ½tool ÖÐ
// initialize tool by connecting to haptic device
tool->start();
// map the physical workspace of the haptic device to a larger virtual workspace.
tool->setWorkspaceRadius(1.0);
// define a radius for the tool
tool->setRadius(0.03);
// read the scale factor between the physical workspace of the haptic
// device and the virtual workspace defined for the tool
double workspaceScaleFactor = tool->getWorkspaceScaleFactor();
// define a maximum stiffness that can be handled by the current
// haptic device. The value is scaled to take into account the
// workspace scale factor
//double stiffnessMax = info.m_maxLinearStiffness / workspaceScaleFactor;
// high stiffness MINE**********
double stiffnessMax =300;
// my device open **********************MINE
deltaCtrl.ConnectDevice();
//-----------------------------------------------------------------------
// CREATING OBJECTS
//-----------------------------------------------------------------------
// create a torus object (o-ring)
object = new cShapeTorus(0.12, 0.25);
// add object to world
world->addChild(object);
// set the position of the object at the center of the world
object->setLocalPos(1.4, 0.0, 0); // position
// rotate object of 90 degrees around its y-axis so that it
// stands vertically
object->rotateAboutGlobalAxisDeg( cVector3d(0, 1, 0), 90);
// set the material stiffness to 100% of the maximum permitted stiffness
// of the haptic device
object->m_material->setStiffness(1.00 * stiffnessMax);
// set some environmental texture and material properties
object->m_texture = new cTexture2d();
// load texture file
bool fileload = object->m_texture->loadFromFile(RESOURCE_PATH("resources/images/spheremap-6.jpg"));
if (!fileload)
{
//fileload = object->m_texture->loadFromFile("../../../bin/resources/images/spheremap-6.jpg");
fileload = object->m_texture->loadFromFile("resources/images/spheremap-6.jpg");
}
if (!fileload)
{
printf("Error - Texture image failed to load correctly.\n");
close();
return (-1);
}
// setup texture and material properties
object->setUseTexture(true);
object->m_texture->setSphericalMappingEnabled(true);
object->m_material->m_ambient.set(0.9, 0.9, 0.9);
object->m_material->m_diffuse.set(0.9, 0.9, 0.9);
object->m_material->m_specular.set(1.0, 1.0, 1.0);
// create a haptic effect for this object so that the operator can
// feel its surface
cEffectSurface* newEffect = new cEffectSurface(object);
object->addEffect(newEffect);
//-----------------------------------------------------------------------
// WIDGETS
//-----------------------------------------------------------------------
// create a font
cFont *font = NEW_CFONTCALIBRI20();
// create a label to display the haptic rate of the simulation
labelHapticRate = new cLabel(font);
camera->m_frontLayer->addChild(labelHapticRate);
level = new cLevel();
camera->m_frontLayer->addChild(level);
level->setLocalPos(100,100,0);
level->setRange(0.0, 25.0);
dial = new cDial();
camera->m_frontLayer->addChild(dial);
dial->setSingleIncrementDisplay(false);
dial->setLocalPos(600,100,0);
dial->setRange(0.0, 5.0);
dial->setSize(60);
dial->setValue0(0.0);
//-----------------------------------------------------------------------
// OPEN GL - WINDOW DISPLAY
//-----------------------------------------------------------------------
// simulation in now running!
simulationRunning = true;
// initialize GLUT
glutInit(&argc, argv);
// retrieve the resolution of the computer display and estimate the position
// of the GLUT window so that it is located at the center of the screen
int screenW = glutGet(GLUT_SCREEN_WIDTH);
int screenH = glutGet(GLUT_SCREEN_HEIGHT);
int windowPosX = (screenW - WINDOW_SIZE_W) / 2;
int windowPosY = (screenH - WINDOW_SIZE_H) / 2;
// initialize the OpenGL GLUT window
glutInitWindowPosition(windowPosX, windowPosY);
glutInitWindowSize(WINDOW_SIZE_W, WINDOW_SIZE_H);
if (USE_STEREO_DISPLAY)
{
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_STEREO);
camera->setUseStereo(true);
}
else
{
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
camera->setUseStereo(false);
}
glutCreateWindow(argv[0]);
glutDisplayFunc(updateGraphics);
glutKeyboardFunc(keySelect);
glutReshapeFunc(resizeWindow);
glutSetWindowTitle("CHAI3D");
// create a mouse menu (right button)
glutCreateMenu(menuSelect);
glutAddMenuEntry("full screen", OPTION_FULLSCREEN);
glutAddMenuEntry("window display", OPTION_WINDOWDISPLAY);
glutAttachMenu(GLUT_RIGHT_BUTTON);
//-----------------------------------------------------------------------
// START SIMULATION
//-----------------------------------------------------------------------
// create a thread which starts the main haptics rendering loop
cThread* hapticsThread = new cThread();
hapticsThread->start(updateHaptics, CTHREAD_PRIORITY_HAPTICS);
// start the main graphics rendering loop
glutTimerFunc(30, graphicsTimer, 0);
glutMainLoop();
// close everything
close();
// exit
return (0);
}
//---------------------------------------------------------------------------
void resizeWindow(int w, int h)
{
// update the size of the viewport
displayW = w;
displayH = h;
glViewport(0, 0, displayW, displayH);
}
//---------------------------------------------------------------------------
void keySelect(unsigned char key, int x, int y)
{
// escape key
if ((key == 27) || (key == 'x'))
{
// close everything
close();
// exit application
exit(0);
}
// option 1:
if (key == '1')
{
bool useTexture = !object->getUseTexture();
object->setUseTexture(useTexture);
if (useTexture)
printf ("> Enable texture \r");
else
printf ("> Disable texture \r");
}
// option 2:
if (key == '2')
{
bool useWireMode = !object->getWireMode();
object->setWireMode(useWireMode);
if (useWireMode)
printf ("> Wire mode enabled \r");
else
printf ("> Wire mode disabled \r");
}
}
//---------------------------------------------------------------------------
void menuSelect(int value)
{
static int _wx, _wy, _ww, _wh;
switch (value)
{
// enable full screen display
case OPTION_FULLSCREEN:
_wx = glutGet(GLUT_WINDOW_X);
_wy = glutGet(GLUT_WINDOW_Y);
_ww = glutGet(GLUT_WINDOW_WIDTH);
_wh = glutGet(GLUT_WINDOW_HEIGHT);
glutFullScreen();
break;
// reshape window to original size
case OPTION_WINDOWDISPLAY:
glutPositionWindow(_wx, _wy);
glutReshapeWindow(_ww, _wh);
break;
}
}
//---------------------------------------------------------------------------
void close(void)
{
// stop the simulation
simulationRunning = false;
// wait for graphics and haptics loops to terminate
while (!simulationFinished) { cSleepMs(100); }
// close haptic device
tool->stop();
// my device close**************************MINE
memset(dF,0,sizeof(dF));
memset(dT,0,sizeof(dT));
//deltaCtrl.SetFGetPosStatues(dF,dPos,statu,dT,blOutRange); // clear force
deltaCtrl.SetTorqueGetPosStatus(dT,nErrCode); // clear the force
deltaCtrl.ReleaseDev();
}
//---------------------------------------------------------------------------
void graphicsTimer(int data)
{
// inform the GLUT window to call updateGraphics again (next frame)
if (simulationRunning)
{
glutPostRedisplay();
}
glutTimerFunc(30, graphicsTimer, 0);
}
//---------------------------------------------------------------------------
void updateGraphics(void)
{
int px, py;
// update haptic rate label
labelHapticRate->setString ("haptic rate: "+cStr(frequencyCounter.getFrequency(), 0) + " [Hz]");
px = (int)(0.5 * (displayW - labelHapticRate->getWidth()));
labelHapticRate->setLocalPos(px, 15);
// update other widgets
py = (int)(0.5 * (displayH - level->getHeight()));
level->setLocalPos(50, py);
level->setValue(tool->m_lastComputedGlobalForce.length());
px = displayW - 80;
py = (int)(0.5 * displayH);
dial->setLocalPos(px, py);
dial->setValue1(angVel.length());
// render world
camera->renderView(displayW, displayH);
// swap buffers
glutSwapBuffers();
// check for any OpenGL errors
GLenum err;
err = glGetError();
if (err != GL_NO_ERROR) printf("Error: %s\n", gluErrorString(err));
}
//---------------------------------------------------------------------------
void updateHaptics(void)
{
// reset clock
cPrecisionClock clock;
clock.reset();
// main haptic simulation loop
while(simulationRunning)
{
/////////////////////////////////////////////////////////////////////
// SIMULATION TIME
/////////////////////////////////////////////////////////////////////
// stop the simulation clock
clock.stop();
// read the time increment in seconds
double timeInterval = clock.getCurrentTimeSeconds();
// restart the simulation clock
clock.reset();
clock.start();
// update frequency counter
frequencyCounter.signal(1);
/////////////////////////////////////////////////////////////////////
// HAPTIC FORCE COMPUTATION
/////////////////////////////////////////////////////////////////////
// bool SetFGetPosStatues(double dF[3],double dPos[3], BYTE & statu,double dT[3],bool & blOutRange);
// MINE ***********************IO process
bool blIO = false;
//blIO = deltaCtrl.SetFGetPosStatues(dF,dPos,statu,dT, blOutRange); // changing point
blIO = deltaCtrl.SetFTGetPosStatus(dF,dT,nErrCode);
deltaCtrl.GetPos(dPos);
deltaCtrl.GetRad(dRad);
deltaCtrl.GetState(statu);
// compute global reference frames for each object
world->computeGlobalPositions(true);
// update position and orientation of tool
// tool->updatePose();
// update my pose************MINE***************** we get the force in
tool->updateMyPose(dPos);
//cVector3d posT;
// posT = tool->getDeviceLocalPos();
// compute interaction forces
tool->computeInteractionForces();
// we need to get the force out to our device**************** MINE
tool->getLastComputedForce(dF);
// send forces to device
tool->applyForces(); // ´Ë´¦Ó¦ÓÃÁ¦£¿
/////////////////////////////////////////////////////////////////////
// HAPTIC SIMULATION
/////////////////////////////////////////////////////////////////////
// get position of cursor in global coordinates
cVector3d toolPos = tool->getDeviceGlobalPos();
// get position of object in global coordinates
cVector3d objectPos = object->getGlobalPos();
// compute a vector from the center of mass of the object (point of rotation) to the tool
cVector3d v = cSub(toolPos, objectPos);
// compute angular acceleration based on the interaction forces
// between the tool and the object
cVector3d angAcc(0,0,0);
if (v.length() > 0.0)
{
// get the last force applied to the cursor in global coordinates
// we negate the result to obtain the opposite force that is applied on the
// object
cVector3d toolForce = cNegate(tool->m_lastComputedGlobalForce);
// compute the effective force that contributes to rotating the object.
cVector3d force = toolForce - cProject(toolForce, v);
// compute the resulting torque
cVector3d torque = cMul(v.length(), cCross( cNormalize(v), force));
// update rotational acceleration
const double INERTIA = 0.4;
angAcc = (1.0 / INERTIA) * torque;
}
// update rotational velocity
angVel.add(timeInterval * angAcc);
// set a threshold on the rotational velocity term
const double MAX_ANG_VEL = 10.0;
double vel = angVel.length();
if (vel > MAX_ANG_VEL)
{
angAcc.mul(MAX_ANG_VEL / vel);
}
// add some damping too
const double DAMPING = 0.1;
angVel.mul(1.0 - DAMPING * timeInterval);
// if user switch is pressed, set velocity to zero
if (tool->getUserSwitch(0) == 1)
{
angVel.zero();
}
// compute the next rotation configuration of the object
if (angVel.length() > C_SMALL)
{
object->rotateAboutGlobalAxisRad(cNormalize(angVel), timeInterval * angVel.length());
}
}
// exit haptics thread
simulationFinished = true;
}
//---------------------------------------------------------------------------