~hitmuri/vjpirate/trunk

« back to all changes in this revision

Viewing changes to os/win/include/vrpn_inertiamouse.h

  • Committer: Florent Berthaut
  • Date: 2014-07-26 18:53:16 UTC
  • mfrom: (5.1.12 mac)
  • Revision ID: flo@localhost.localdomain-20140726185316-c2ucnwmgm5kij4e2
Merged mac branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//-----------------------------------------------------------------------------------------
 
2
// Driver for the Bauhaus University Weimar "inertiamouse" device.  The class for this
 
3
// device is found at the end of the file, after two helper classes.
 
4
 
 
5
#ifndef VRPN_INERTIAMOUSE_H
 
6
#define VRPN_INERTIAMOUSE_H
 
7
 
 
8
#include "vrpn_Analog.h"
 
9
#include "vrpn_Button.h"
 
10
 
 
11
// Helper classes
 
12
class VRPN_API dcblocker {
 
13
public: // ctors, dtor
 
14
    dcblocker () 
 
15
        : in_ (0.0),
 
16
          out_ (0.0)
 
17
    {}
 
18
    dcblocker (dcblocker const& o) 
 
19
        : in_ (o.in_), 
 
20
          out_ (o.out_)
 
21
    {}
 
22
    ~dcblocker () throw () {}
 
23
public: // methods
 
24
    void swap (dcblocker& o) throw ()
 
25
    {
 
26
        double t;
 
27
        t = in_;  in_ = o.in_;    o.in_ = t;
 
28
        t = out_; out_ = o.out_;  o.out_ = t;
 
29
    }
 
30
    dcblocker& operator= (dcblocker const& o)
 
31
    {
 
32
        dcblocker tmp (o);
 
33
        swap (tmp);
 
34
        return *this;
 
35
    }
 
36
    double filter (double s)
 
37
    {
 
38
        out_ = s - in_ + (0.95 * out_);
 
39
        in_ = s;
 
40
        return out_; 
 
41
    }
 
42
    void reset ()
 
43
    {
 
44
        in_ = out_ = 0.0;
 
45
    }
 
46
private: // variables
 
47
    double in_;
 
48
    double out_;
 
49
};
 
50
 
 
51
// Helper classes
 
52
/*
 
53
 *  butterworth lowpass
 
54
 */
 
55
class VRPN_API lowpass {
 
56
public: // ctors, dtor
 
57
    lowpass ()
 
58
    {
 
59
        in_[0] = 0.0;
 
60
        in_[1] = 0.0;
 
61
        out_[0] = 0.0;
 
62
        out_[1] = 0.0;
 
63
    }
 
64
    lowpass (lowpass const& o)
 
65
    {
 
66
        in_[0] = o.in_[0];
 
67
        in_[1] = o.in_[1];
 
68
        out_[0] = o.out_[0];
 
69
        out_[1] = o.out_[1];
 
70
    }
 
71
    ~lowpass () throw () {}
 
72
public: // methods
 
73
    double filter (double s)
 
74
    {
 
75
        in_[0] = in_[1];
 
76
        in_[1] = s / 6.242183581;
 
77
        out_[0] = out_[1];
 
78
        out_[1] = in_[0] + in_[1] + (0.6795992982 * out_[0]);
 
79
        return out_[1];
 
80
    }
 
81
    void reset ()
 
82
    {
 
83
        in_[0] = in_[1] = out_[0] = out_[1] = 0.0;
 
84
    }
 
85
private: // variables
 
86
    double in_[2];
 
87
    double out_[2];
 
88
};
 
89
 
 
90
class VRPN_API vrpn_inertiamouse : public vrpn_Serial_Analog , public vrpn_Button {
 
91
public: // constants
 
92
 
 
93
    enum {
 
94
        Channels = 6,
 
95
        Buttons = 2,
 
96
        Update_Interval_Hz = 7372800 / 64 / 13 / Channels,
 
97
    };
 
98
    static const double Vel_Decay;
 
99
 
 
100
public: // construction/destruction
 
101
    // ctor
 
102
    vrpn_inertiamouse (const char* name, 
 
103
                       vrpn_Connection* c,
 
104
                       const char* port, 
 
105
                       int baud_rate);
 
106
 
 
107
    // factory method
 
108
    static vrpn_inertiamouse* create (const char* name, 
 
109
            vrpn_Connection* c,
 
110
            const char* port, 
 
111
            int baud_rate);
 
112
    // dtor
 
113
    ~vrpn_inertiamouse () { if (vel_) { delete [] vel_;} };
 
114
 
 
115
public: // virtual methods
 
116
 
 
117
    /// Called once through each main loop iteration to handle updates.
 
118
    virtual void mainloop ();
 
119
 
 
120
    virtual int reset(void);    //< Set device back to starting config
 
121
 
 
122
protected:
 
123
    int status_;                //< Used by mainloop() and get_report()
 
124
    int numbuttons_;            //< How many buttons to open
 
125
    int numchannels_;           //< How many analog channels to open
 
126
    
 
127
    int expected_chars_;        //< How many characters to expect in the report
 
128
    unsigned char buffer_[512]; //< Buffer of characters in report
 
129
    int bufcount_;              //< How many characters we have so far
 
130
    
 
131
    int null_radius_;           //< The range over which no motion should be 
 
132
                                //  reported
 
133
    
 
134
    struct timeval timestamp;   //< Time of the last report from the device
 
135
 
 
136
    double *vel_;               // velocity update
 
137
 
 
138
    dcblocker dcb_[Channels]; // dc blockers for all Channels
 
139
    lowpass lp_[Channels];    // lowpass filters for all Channels
 
140
 
 
141
    // Set all buttons, analogs and encoders back to 0
 
142
    virtual void clear_values(void);
 
143
    
 
144
    /// Try to read a report from the device.  Returns 1 if complete report received,
 
145
    /// 0 otherwise.  Sets _status to match current status.
 
146
    virtual int get_report(void);
 
147
    
 
148
    /// send report iff changed
 
149
    virtual void report_changes (vrpn_uint32 class_of_service
 
150
                                = vrpn_CONNECTION_LOW_LATENCY);
 
151
    /// send report whether or not changed
 
152
    virtual void report (vrpn_uint32 class_of_service
 
153
                         = vrpn_CONNECTION_LOW_LATENCY);
 
154
    
 
155
    // NOTE:  class_of_service is only applied to vrpn_Analog
 
156
    //  values, not vrpn_Button, which are always vrpn_RELIABLE
 
157
};
 
158
 
 
159
#endif