CSL 5.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

PME.h
Go to the documentation of this file.
1 ///
2 /// PME.h -- Ventriloquist
3 /// See the copyright notice and acknowledgment of authors in the file COPYRIGHT
4 /// Doug McCoy, 2004.
5 ///
6
7 #ifndef INCLUDE_PME_H
8 #define INCLUDE_PME_H
9
10 #include "CPoint.h"
11 #include "VBAP.h"
12 #include "ThreadUtilities.h"
13 #include "PracticalSocket.h"
14 #include "CGestalt.h"
15
16 //#ifdef DO_TIMING // Here are the macros and globals for the timing code
17 #include <sys/time.h>
18 #define GET_TIME(val) if (gettimeofday(val, 0) != 0) cout << "Output: Error reading current time" << endl;
19 #define SUB_TIMES(t1, t2) (((t1->tv_sec - t2->tv_sec) * 1000000) + (t1->tv_usec - t2->tv_usec))
20 //#endif
21
22 using namespace csl;
23
24 // for use with sending over sockets
25
26 typedef struct {
27 int x;
28 int y;
29 int z;
30 int dx;
31 int dy;
32 int dz;
33 int glove_state;
34 } controller_str;
35
36 typedef enum {
37 kStopped,
38 kGrabbed,
39 kOrbit,
40 kDraw,
41 kBounce
42 } MovementType;
43
44 typedef enum {
45 kInvalid = -1,
46 kClosed = 0,
47 kPoint = 1,
48 kOpen = 15
49 } GloveState;
50
51 // for use with internal classes
52
53 class Controller {
54 protected:
55 CPoint _position;
56 CPoint _velocity;
57 GloveState glove_state;
58 csl::SynchPthread sync;
59 csl::ThreadPthread thread;
60 // unsigned short local_port;
61 unsigned short foreign_port;
62 string foreign_net_address;
63 UDPSocket sock;
64
65 public:
66 Controller(string remote_net_addr, unsigned short remote_port):
67 glove_state(kOpen), foreign_port(remote_port), foreign_net_address(remote_net_addr) { };
68 Controller(): glove_state(kOpen) { };
69 ~Controller(){ };
70 void set_remote_addr_and_port(string addr, unsigned short port ){ foreign_port=port; foreign_net_address=addr;};
71 void set_data(controller_str &str );
72 void get_data(CPoint &p, CPoint &v, GloveState &glove_st ); // get pos and vel and put result into p and v
73 void get_position(CPoint &p);
74 void * remote_read_func(void *data); // function to run in separate thread
75 void start_reader_thread();
76 void get_remote_data();
77 };
78
79 class Orbit {
80 protected:
81 double a; // semi-major axis
82 double n; // mean motion (radians per unit time)
83 double e; // eccentricity (0 <= e < 1)
84 double i; // inclination angle (0 <= i <= pi)
85 double omega; // longitude of ascending node (0 <= omega < 2pi)
86 double w; // argument of perigee (0 <= w < 2pi)
87 double nu; // true anomaly (0 <= v < 2pi)
88 CPoint e_vec; // eccentricity vector
89 CPoint n_vec; // ascending node vector
90 double mu; // gravity coonstant valid for individual instance
91
92 public:
93 Orbit(){ };
94 ~Orbit(){ };
95 void calculate_eccentricity(CPoint R, CPoint V);
96 void calculate_orbital_params(CPoint R, CPoint V);
97 void calculate_absolute_position(CPoint &new_position);
98 void calculate_new_position_in_orbit();
99 void dump();
100 };
101
102 #define MAX_TRACE_LENGTH (100 * 15) // 100 positions per second for max 15 seconds
103
104 class PMESource {
105 protected:
106 SpatialSource *source;
107 Orbit orbit;
108 MovementType current_move_type;
109 MovementType next_move_type;
110 CPoint trace[MAX_TRACE_LENGTH];
111 float bounce_distance;
112 CPoint bounce_velocity;
113 unsigned current_trace_index;
114 unsigned trace_length;
115
116 public:
117 void set_position(CPoint &P );
118 CPoint get_position();
119 void update_position();
120 void set_orbit(CPoint &R, CPoint &V);
121 MovementType get_current_move_type() { return current_move_type; };
122 MovementType get_next_move_type() { return next_move_type; };
123 void update_move_type(){ current_move_type = next_move_type; };
124 void set_next_move_type (MovementType mov_type);
125 void set_current_move_type (MovementType mov_type);
126 void push_trace(CPoint &pos );
127 void reset_trace(){ trace_length = current_trace_index = 0; };
128 void set_bounce_velocity(CPoint bv){ bounce_velocity = bv; };
129 void set_bounce_distance(float bd);
130
131 PMESource(SpatialSource & s );
132 PMESource();
133 ~PMESource(){ };
134 };
135
136 class PME {
137 protected:
138 // csl::SynchPthread sync;
139 csl::ThreadPthread management_thread;
140 Controller controller;
141 PMESource ** pme_source_list;
142 PMESource *grabbed_source;
143 unsigned short num_sources;
144 // MovementType pme_move_type;
145 void update_grabbed_position(CPoint &p);
146 bool check_for_grabbed_source(CPoint &p );
147 bool keep_processing_sources;
148
149 public:
150 bool add_pme_source(PMESource &s );
151 void remove_all_sources() {num_sources=0;};
152 void manage_sources();
153 // void set_movement_type(MovementType type ){ pme_move_type = type; };
154 void set_remote_addr_and_port(string addr, unsigned short port ){ controller.set_remote_addr_and_port(addr, port); };
155 static void * management_func(void *data); // function to run in separate thread
156 void start_management_thread();
157 void stop_management_thread() ;
158
159 PME(string remote_net_addr, unsigned short remote_port);
160 PME();
161 ~PME();
162 };
163
164 #endif
165