root/juggler/branches/2.2/modules/gadgeteer/test/dummyTrackd.cpp

Revision 19729, 8.1 kB (checked in by patrick, 2 years ago)

Copyright update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998-2007 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  *************** <auto-copyright.pl END do not edit this line> ***************/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <string.h>
30
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/ipc.h>
34 #include <sys/shm.h>
35 #include <limits.h>
36 #include <sys/prctl.h>
37 //#include <sys/schedctl.h>
38 #include <unistd.h>
39
40 #include <gadget/Devices/Open/Trackd/trackdmem.h>
41
42 #define TRACKD_MAX_SENSORS  30
43 #define CAVE_MAX_VALUATORS  20
44 #define CAVE_MAX_BUTTONS    20
45
46 struct TRACKD_TRACKING
47 {
48    CAVE_TRACKDTRACKER_HEADER  header;
49    CAVE_SENSOR_ST             sensor[TRACKD_MAX_SENSORS];
50 };
51
52 struct TRACKD_CONTROLLER
53 {
54    CAVE_TRACKDCONTROLLER_HEADER  header;
55    int                           button[CAVE_MAX_BUTTONS];
56    float                         valuator[CAVE_MAX_VALUATORS];
57 };
58
59 static int tracker_shmid=-1, controller_shmid=-1;
60 static TRACKD_TRACKING*    tracker =NULL;
61 static TRACKD_CONTROLLER*  controller=NULL;
62
63 #define PERMS 0666
64
65 // Allocate tracker mem
66 void allocateTrackerMem(int trackdKey)
67 {
68  tracker_shmid = shmget(trackdKey, sizeof(TRACKD_TRACKING), PERMS | IPC_CREAT);
69  if (tracker_shmid < 0)
70         {
71         fprintf(stderr,"can't get shared memory\n");
72         exit(-1);
73         }
74  tracker = (struct TRACKD_TRACKING *) shmat(tracker_shmid,(char *) 0, 0);
75  if (tracker == (struct TRACKD_TRACKING *) -1)
76         {
77         fprintf(stderr,"can't attach shared memory\n");
78         exit(-1);
79         }
80 }
81
82 // Allocate controller
83 void allocateControllerMem(int controllerKey)
84 {
85  controller_shmid = shmget(controllerKey,
86                         sizeof(struct TRACKD_CONTROLLER), PERMS | IPC_CREAT);
87  if (controller_shmid < 0)
88         {
89         fprintf(stderr,"can't get shared memory\n");
90         exit(-1);
91         }
92  controller = (struct TRACKD_CONTROLLER *)
93                         shmat(controller_shmid,(char *) 0, 0);
94  if (controller == (struct TRACKD_CONTROLLER *) -1)
95         {
96         fprintf(stderr,"can't attach shared memory\n");
97         exit(-1);
98         }
99 }
100
101 // Initialize tracker
102 void initTracker()
103 {
104  CAVE_SENSOR_ST sensor;
105
106  tracker->header.version = CAVELIB_2_6;
107  tracker->header.numSensors = 0;
108  tracker->header.sensorOffset = ((char *)&tracker->sensor[0]) - ((char *)tracker);
109  tracker->header.sensorSize = sizeof(CAVE_SENSOR_ST);
110  tracker->header.timestamp[0] = tracker->header.timestamp[1] = 0;
111  tracker->header.command = 0;
112
113  sensor.x = 0.0;
114  sensor.y = 0.0;
115  sensor.z = 0.0;
116  sensor.elev = 0.0;
117  sensor.azim = 0.0;
118  sensor.roll = 0.0;
119  sensor.calibrated = 0;
120
121  for (int loop=0; loop < TRACKD_MAX_SENSORS; ++loop)
122    tracker->sensor[loop] = sensor;
123 }
124
125
126 // Intiializae controller data
127 void initController()
128 {
129  controller->header.version = CAVELIB_2_6;
130  controller->header.buttonOffset = ((char *)&controller->button[0]) - ((char *)controller);
131  controller->header.valuatorOffset = ((char *)&controller->valuator[0]) - ((char *)controller);
132  controller->header.numButtons = 0;
133  controller->header.numValuators = 0;
134  controller->header.timestamp[0] = controller->header.timestamp[1] = 0;
135  controller->header.command = 0;
136
137  int loop;
138  for (loop=0; loop < CAVE_MAX_BUTTONS; ++loop)
139       controller->button[loop] = 0;
140  for (loop=0; loop < CAVE_MAX_VALUATORS; ++loop)
141       controller->valuator[loop] = 0.0;
142  //controller->num_buttons = 0;
143  //controller->num_valuators = 0;
144 }
145
146
147 void getTimeStaMp(uint32_t stamp[2])
148 {
149  struct timeval curtime;
150  gettimeofday(&curtime,NULL);
151  stamp[0] = curtime.tv_sec;
152  stamp[1] = curtime.tv_usec;
153 }
154
155
156 void setNewTrackerData(int numSensors)
157 {
158  static int nextSensor=0, first=1;
159  float t;
160  struct timeval curtime;
161  static struct timeval starttime;
162  if (first)
163  {
164    gettimeofday(&starttime,NULL);
165    first=0;
166  }
167  gettimeofday(&curtime,NULL);
168  t = curtime.tv_sec-starttime.tv_sec + (curtime.tv_usec-starttime.tv_usec)/1000000.0f;
169
170  // Translate head in circle around y axis radius of 4.0f
171  // Also rotate around z axis
172  // Head will bob up and down as well
173  // Head should also "nod" while spinning
174  if (nextSensor==0)
175  {
176    tracker->sensor[0].x = sin(t) * 2.0f;
177    tracker->sensor[0].y = 6 + cos(t/2.0f);
178    tracker->sensor[0].z = cos(t) * 3.0f;
179    tracker->sensor[0].elev = sin(t)*45.0f;      // sin(t/10.0f) * 45.0f;
180    tracker->sensor[0].azim = t*36.0f;   // sin(t/3.0f) * 120.0f;
181    tracker->sensor[0].roll = 0.0f;
182    tracker->sensor[0].calibrated = 0;
183  }
184  else
185  {
186    tracker->sensor[nextSensor].x = sin(t+nextSensor) + nextSensor*2.0f - 2.0f;
187    tracker->sensor[nextSensor].y = cos(t+nextSensor) + 4.0f;
188    tracker->sensor[nextSensor].z = -4.0f;
189    tracker->sensor[nextSensor].elev = sin(t/4.0f+nextSensor) * 180.0f;
190    tracker->sensor[nextSensor].azim = sin(t/2.0f+nextSensor) * 120.0f;
191    tracker->sensor[nextSensor].roll = sin(t/7.0f+nextSensor) * 90.0f;
192    tracker->sensor[nextSensor].calibrated = 0;
193  }
194  nextSensor = ((nextSensor+1) % numSensors);
195 }
196
197
198 int get_new_controller_data(int numButtons,int numValuators)
199 {
200  int i, buttonChange=0, t=time(NULL);
201  static int lastButtonChangeTime=-1;
202  //controller->num_buttons = numButtons;
203  //controller->num_valuators = numValuators;
204  if ((numButtons > 0) && (t > lastButtonChangeTime))
205  {
206    int b = random()%numButtons;
207    controller->button[b] = !(controller->button[b]);
208    lastButtonChangeTime = t;
209    buttonChange=1;
210  }
211
212  for (i=0; i < numValuators; i++)
213  {
214    controller->valuator[i] += (drand48()-0.5f)/20.0f;
215    if (controller->valuator[i] < -1.0f)
216       controller->valuator[i] = -1.0f;
217    else if (controller->valuator[i] > 1.0f)
218       controller->valuator[i] = 1.0f;
219  }
220
221  return ((numValuators > 0) || (buttonChange));
222 }
223
224
225
226 int main(int argc,char **argv)
227 {
228    int numSensors=0, numButtons=0, numValuators=0;
229    int trackerKey=0, controllerKey=0;
230
231    int i;      // sensornum;
232    //CAVE_CONFIG_ST config;
233    //CAVE_SENSOR_ST sensor;
234    //CAVE_CONTROLLER_ST controlvals;
235
236    if (argc < 5)
237    {
238       fprintf(stderr,"Usage: %s [-sensors <numsensors>] [-buttons <numbuttons>]"
239               " [-valuators <numvaluators>] [-trackerkey <keynum>] [-controllerkey <keynum>]\n", argv[0]);
240       exit(1);
241    }
242
243    // Get command line
244    for (i=1; i < argc; i++)
245    {
246       if (!strcmp(argv[i],"-sensors"))
247          numSensors = atoi(argv[++i]);
248       else if (!strcmp(argv[i],"-buttons"))
249          numButtons = atoi(argv[++i]);
250       else if (!strcmp(argv[i],"-valuators"))
251          numValuators = atoi(argv[++i]);
252       else if (!strcmp(argv[i],"-trackerkey"))
253          trackerKey = atoi(argv[++i]);
254       else if (!strcmp(argv[i],"-controllerkey"))
255          controllerKey = atoi(argv[++i]);
256       else
257          printf("Unknown option \"%s\"\n",argv[i]);
258    }
259
260    allocateTrackerMem(trackerKey);
261    allocateControllerMem(controllerKey);
262
263    initTracker();
264    initController();
265
266    tracker->header.numSensors = numSensors;
267    controller->header.numButtons = numButtons;
268    //controller->controller.num_buttons = numButtons;
269    controller->header.numValuators = numValuators;
270    //controller->controller.num_valuators = numValuators;
271
272    while (1)
273    {
274       setNewTrackerData(numSensors);
275       getTimeStaMp(tracker->header.timestamp);
276
277       if (get_new_controller_data(numButtons,numValuators))
278       {
279          getTimeStaMp(controller->header.timestamp);
280       }
281       usleep(100);
282    }
283
284    return 1;
285 }
Note: See TracBrowser for help on using the browser.