PME.h

Go to the documentation of this file.
00001 /*
00002  *  PME.h
00003  *  Ventriloquist
00004  *
00005  *  Created by Doug McCoy on 11/11/04.
00006  *  Copyright 2004 __MyCompanyName__. All rights reserved.
00007  *
00008  */
00009 #ifndef INCLUDE_PME_H
00010 #define INCLUDE_PME_H
00011 
00012 #include "CPoint.h"
00013 #include "VBAP.h"
00014 #include "ThreadUtilities.h"
00015 #include "PracticalSocket.h"
00016 #include "CGestalt.h"
00017 
00018 //#ifdef DO_TIMING                  // Here are the macros and globals for the timing code
00019 #include <sys/time.h>
00020 #define GET_TIME(val) if (gettimeofday(val, 0) != 0) cout << "Output: Error reading current time" << endl;
00021 #define SUB_TIMES(t1, t2) (((t1->tv_sec - t2->tv_sec) * 1000000) + (t1->tv_usec - t2->tv_usec))
00022 //#endif
00023 
00024 
00025 using namespace csl;
00026 
00027 // for use with sending over sockets
00028 typedef struct {            
00029     int x;
00030     int y;
00031     int z;
00032     int dx;
00033     int dy;
00034     int dz;
00035     int glove_state;
00036 } controller_str;
00037 
00038 typedef enum {
00039     kStopped,
00040     kGrabbed,
00041     kOrbit,
00042     kDraw, 
00043     kBounce
00044 } MovementType;
00045 
00046 typedef enum {
00047     kInvalid = -1,
00048     kClosed  =  0,
00049     kPoint   =  1,
00050     kOpen    = 15
00051 } GloveState;
00052 
00053 
00054 // for use with internal classes
00055 class Controller {
00056 protected:
00057     CPoint _position;
00058     CPoint _velocity;
00059     GloveState glove_state; 
00060     csl::SynchPthread sync;
00061     csl::ThreadPthread thread;
00062 //  unsigned short local_port;
00063     unsigned short foreign_port;
00064     string foreign_net_address;
00065     UDPSocket sock;
00066 
00067 public:
00068     Controller( string remote_net_addr, unsigned short remote_port): 
00069         glove_state(kOpen), foreign_port(remote_port), foreign_net_address(remote_net_addr) {};
00070     Controller(): glove_state(kOpen) {};
00071     ~Controller(){};
00072     void set_remote_addr_and_port( string addr, unsigned short port ){ foreign_port=port; foreign_net_address=addr;};
00073     void set_data( controller_str &str );
00074     void get_data( CPoint &p, CPoint &v, GloveState &glove_st );        // get pos and vel and put result into p and v
00075     void get_position(CPoint &p);
00076     void * remote_read_func(void *data);        // function to run in separate thread
00077     void start_reader_thread();
00078     void get_remote_data();
00079 };
00080 
00081 class Orbit {
00082 protected:
00083     double a;       // semi-major axis
00084     double n;       // mean motion (radians per unit time)
00085     double e;       // eccentricity         (0 <= e < 1)
00086     double i;       // inclination angle    (0 <= i <= pi)
00087     double omega;   // longitude of ascending node  (0 <= omega < 2pi)
00088     double w;       // argument of perigee  (0 <= w < 2pi)
00089     double nu;      // true anomaly         (0 <= v < 2pi)
00090     CPoint e_vec;   // eccentricity vector
00091     CPoint n_vec;   // ascending node vector
00092     double mu;      // gravity coonstant valid for individual instance
00093     
00094 public:
00095     Orbit(){};
00096     ~Orbit(){};
00097     void calculate_eccentricity( CPoint R,  CPoint V);
00098     void calculate_orbital_params( CPoint R,  CPoint V);
00099     void calculate_absolute_position(CPoint &new_position);
00100     void calculate_new_position_in_orbit();
00101     void dump();
00102 };
00103 
00104 #define MAX_TRACE_LENGTH (100 * 15)     // 100 positions per second for max 15 seconds
00105 
00106 class PMESource {
00107 protected:
00108     Source *source;
00109     Orbit orbit;
00110     MovementType current_move_type;
00111     MovementType next_move_type;
00112     CPoint trace[MAX_TRACE_LENGTH];
00113     float bounce_distance;
00114     CPoint bounce_velocity;
00115     unsigned current_trace_index;
00116     unsigned trace_length;
00117     
00118 public:
00119     void set_position( CPoint &P );
00120     CPoint get_position();
00121     void update_pos();
00122     void set_orbit(CPoint &R, CPoint &V);
00123     MovementType get_current_move_type() { return current_move_type; };
00124     MovementType get_next_move_type() { return next_move_type; };
00125     void update_move_type(){ current_move_type = next_move_type; };
00126     void set_next_move_type ( MovementType mov_type);
00127     void set_current_move_type ( MovementType mov_type);
00128     void push_trace( CPoint &pos );
00129     void reset_trace(){ trace_length = current_trace_index = 0; };
00130     void set_bounce_velocity( CPoint bv){ bounce_velocity = bv; };
00131     void set_bounce_distance(float bd);
00132     
00133     PMESource( Source &s );
00134     PMESource();
00135     ~PMESource(){};
00136 };
00137 
00138 class PME {
00139 protected:
00140 //  csl::SynchPthread sync;
00141     csl::ThreadPthread management_thread;
00142     Controller controller;
00143     PMESource ** pme_source_list;
00144     PMESource *grabbed_source;
00145     unsigned short num_sources;
00146 //  MovementType pme_move_type;
00147     void update_grabbed_position(CPoint &p);
00148     bool check_for_grabbed_source( CPoint &p );
00149     bool keep_processing_sources;
00150     
00151 public:
00152     bool add_pme_source( PMESource &s );
00153     void remove_all_sources() {num_sources=0;};
00154     void manage_sources();
00155 //  void set_movement_type( MovementType type ){ pme_move_type = type; };
00156     void set_remote_addr_and_port( string addr, unsigned short port ){ controller.set_remote_addr_and_port(addr, port); };
00157     static void * management_func(void *data);      // function to run in separate thread
00158     void start_management_thread();
00159     void stop_management_thread() ;
00160     
00161     PME( string remote_net_addr, unsigned short remote_port);
00162     PME();
00163     ~PME();
00164     
00165     
00166 };
00167 
00168 #endif
00169 

Generated on Fri Apr 6 20:18:14 2007 for CSL by  doxygen 1.4.5-20051010