PME.h

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

Generated on Thu Sep 17 23:14:16 2009 for CSL by  doxygen 1.5.8