1 /*---------------------------------------------------------------------------*
2  Project:  horizon
3  File:     hid_GyroscopeReader.h
4 
5  Copyright 2010 Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12 
13  $Rev: $
14  *---------------------------------------------------------------------------*/
15 
16 /* Please see man pages for details
17 
18 
19  */
20 
21 #ifndef NN_HID_CTR_GYROSCOPEREADER_H_
22 #define NN_HID_CTR_GYROSCOPEREADER_H_
23 
24 #include <nn/Handle.h>
25 #include <nn/Result.h>
26 #include <nn/types.h>
27 #include <nn/hid/CTR/hid_Result.h>
28 #include <nn/hid/CTR/hid_Api.h>
29 #include <nn/hid/CTR/hid_Gyroscope.h>
30 #include <nn/hid/CTR/hid_AccelerometerReader.h>
31 #include <nn/hid/CTR/hid_DeviceStatus.h>
32 #include <nn/util/util_SizedEnum.h>
33 #include <nn/util/util_NonCopyable.h>
34 
35 namespace nn
36 {
37 namespace hid
38 {
39 namespace CTR
40 {
41 
42 /* Please see man pages for details
43 
44 */
45 enum ZeroDriftMode
46 {
47     GYROSCOPE_ZERODRIFT_LOOSE, //
48     GYROSCOPE_ZERODRIFT_STANDARD,//
49     GYROSCOPE_ZERODRIFT_TIGHT,//
50     GYROSCOPE_ZERODRIFT_NUM
51 };
52 
53 
54 /* Please see man pages for details
55 
56 */
57 class GyroscopeReader: private nn::util::NonCopyable<GyroscopeReader>
58 {
59 public:
60 
61 
62     /* Please see man pages for details
63 
64 
65      */
66 
67     /* Please see man pages for details
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78     */
79     GyroscopeReader(AccelerometerReader* pAccelerometerReader = NULL, Gyroscope& gyroscope = GetGyroscope());
80 
81     /* Please see man pages for details
82 
83     */
84 
85     ~GyroscopeReader();
86 
87     /*
88 
89 */
90 
91     /* Please see man pages for details
92 
93 
94      */
95 
96     /* Please see man pages for details
97 
98 
99 
100 
101 
102 
103 
104     */
105     void Read(GyroscopeStatus* pBufs, s32* pReadLen, s32 bufLen);
106 
107     /* Please see man pages for details
108 
109 
110 
111 
112 
113 
114 
115     */
116     bool ReadLatest(GyroscopeStatus* pBuf);
117 
118     /*
119 
120 */
121 
122     /* Please see man pages for details
123 
124 
125      */
126 
127     /* Please see man pages for details
128 
129 
130 
131      */
132 
133     void Reset();
134 
135     /*
136 
137 */
138 
139     /* Please see man pages for details
140 
141 
142      */
143 
144 
145     /* Please see man pages for details
146 
147 
148 
149 
150 
151      */
152     void SetAngle(f32 ax, f32 ay, f32 az);
153 
154     /* Please see man pages for details
155 
156 
157 
158      */
159     void SetDirection(const Direction& direction);
160 
161     /* Please see man pages for details
162 
163 
164 
165 
166 
167      */
168     void SetAngleMagnification(f32 pitch, f32 yaw, f32 roll);
169 
170     /* Please see man pages for details
171 
172 
173 
174 
175 
176 
177      */
178     void SetDirectionMagnification(f32 magnification);
179 
180     /*
181 
182 */
183 
184     /* Please see man pages for details
185 
186 
187      */
188 
189     /* Please see man pages for details
190 
191 
192      */
193     void EnableZeroPlay();
194 
195     /* Please see man pages for details
196 
197 
198      */
199     void DisableZeroPlay();
200 
201     /* Please see man pages for details
202 
203 
204      */
205     bool IsEnableZeroPlay() const;
206 
207     /* Please see man pages for details
208 
209 
210 
211 
212 
213 
214 
215      */
216     f32 GetZeroPlayEffect() const;
217 
218 
219     /* Please see man pages for details
220 
221 
222 
223 
224 
225 
226 
227 
228      */
229     void SetZeroPlayParam(f32 radius);
230 
231     /* Please see man pages for details
232 
233 
234 
235 
236 
237 
238      */
239     void GetZeroPlayParam(f32& radius) const;
240 
241     /* Please see man pages for details
242 
243 
244 
245 
246 
247      */
248     void ResetZeroPlayParam();
249 
250     /*
251 
252 */
253 
254 
255     /* Please see man pages for details
256 
257 
258      */
259 
260     /* Please see man pages for details
261 
262 
263      */
264     void EnableZeroDrift();
265 
266     /* Please see man pages for details
267 
268 
269      */
270     void DisableZeroDrift();
271 
272     /* Please see man pages for details
273 
274 
275 
276 
277 
278      */
279     bool IsEnableZeroDrift() const;
280 
281     /* Please see man pages for details
282 
283 
284 
285 
286 
287      */
288     f32 GetZeroDriftEffect() const;
289 
290     /* Please see man pages for details
291 
292 
293 
294 
295 
296      */
297     void ResetZeroDriftMode();
298 
299     /* Please see man pages for details
300 
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312 
313      */
314     void SetZeroDriftMode(const ZeroDriftMode& mode);
315 
316     /* Please see man pages for details
317 
318 
319 
320      */
321     void GetZeroDriftMode(ZeroDriftMode& mode) const;
322 
323     /*
324 
325 */
326 
327 
328     /* Please see man pages for details
329 
330 
331      */
332 
333     /* Please see man pages for details
334 
335 
336 
337 
338 
339 
340 
341 
342 
343      */
344     void EnableAccRevise();
345 
346     /* Please see man pages for details
347 
348 
349      */
350     void DisableAccRevise();
351 
352     /* Please see man pages for details
353 
354 
355 
356 
357 
358      */
359     bool IsEnableAccRevise() const;
360 
361     /* Please see man pages for details
362 
363 
364 
365 
366      */
367     f32 GetAccReviseEffect() const;
368 
369 
370     /* Please see man pages for details
371 
372 
373 
374 
375 
376 
377 
378 
379 
380 
381 
382      */
383     void SetAccReviseParam(f32 revisePower, f32 reviseRange);
384 
385     /* Please see man pages for details
386 
387 
388 
389 
390      */
391     void GetAccReviseParam(f32& revisePower, f32& reviseRange) const;
392 
393     /* Please see man pages for details
394 
395 
396 
397 
398 
399      */
400     void ResetAccReviseParam();
401 
402     /*
403 
404 */
405 
406     /* Please see man pages for details
407 
408 
409      */
410 
411     /* Please see man pages for details
412 
413 
414 
415 
416 
417 
418 
419 
420 
421 
422 
423 
424 
425 
426 
427      */
428     void EnableAxisRotation();
429 
430     /* Please see man pages for details
431 
432 
433 
434      */
435     void DisableAxisRotation();
436 
437     /* Please see man pages for details
438 
439 
440      */
441     bool IsEnableAxisRotation() const;
442 
443     /* Please see man pages for details
444 
445 
446      */
447     void SetAxisRotationMatrix(const nn::math::MTX34& mtx);
448 
449     /* Please see man pages for details
450 
451 
452 
453 
454 
455 
456 
457      */
458     nn::math::MTX34 GetAxisRotationMatrix() const;
459 
460     /* Please see man pages for details
461 
462 
463 
464 
465 
466 
467      */
468     void GetAxisRotationMatrix(nn::math::MTX34* pMtx) const;
469 
470     /* Please see man pages for details
471 
472 
473      */
474     void ResetAxisRotationMatrix();
475 
476     /*
477 
478 */
479 
480 private :
481 
482     void ReadLocal(GyroscopeStatus* pBufs, s32* pReadLen, s32 bufLen);
483     void GetZeroDriftParam(f32& radius, s32& count, f32& power) const;
484     void SetZeroDriftParam(f32 radius, s32 count, f32 power);
485     f32 ReviseDirection_Acceleration(Direction& rev_dir, const nn::math::VEC3& acc);
486     void CalculateGyroscopeAxisStatus(
487         f32 *destSpeed,
488         s32 *nearSamplingNum,
489         f32 *zeroOffset,
490         s32 srcSpeed,
491         f32 speedScale,
492         s32* oldValueArray);
493     void CalculateDirection();
494     void InitializeCalibrationData();
495 
496 private :
497     /* Maximum number for the drift correction count*/
498     static const u32 GYROSCOPE_DRIFT_COUNT_MAX = 256; //Must be to a power of 2
499 
500 
501 private :
502     static const s32 GYROSCOPE_LOCAL_BUFFER_SIZE = 32;
503     s32              m_GyroscopeLocalBufferSize;
504     GyroscopeStatus m_GyroscopeStatusLocalBuffer[GYROSCOPE_LOCAL_BUFFER_SIZE];
505     bool            m_IsFirstRead;
506     util::SizedEnum1<ZeroDriftMode> m_ZeroDriftMode;
507     NN_PADDING2;
508 
509     AccelerometerReader m_DefaultAccelerometerReader;
510 
511     GyroscopeStatus m_CurrentStatus;
512     AccelerometerReader* m_pAccelerometerReader;
513 
514     nn::math::VEC3 m_SpeedOld;
515     nn::math::VEC3 m_SpeedVector;
516     nn::math::VEC3 m_SpeedScale;
517     f32         m_DirectionMagnification;           // Magnification when calculating attitude
518 
519     f32         m_Period;           // Coefficient for calculations
520     f32         m_FreqDegree;          // Coefficient for calculations
521     f32         m_FreqRadian;          // Coefficient for calculations
522 
523     bool        m_EnableZeroPlay;       // Enable zero point play?
524     bool        m_EnableZeroDrift;      // Use automatic zero point correction?
525     bool        m_EnableAccRevise;      // Correct attitude using acceleration?
526     bool        m_EnableRotate;
527 
528     f32         m_ZeroPlayRadius;   // Radius of zero point play (1 = 360deg/sec)
529     f32         m_ZeroDriftRadius;  // Drift count play radius (1 = 360deg/sec)
530     s32         m_ZeroDriftCount;      // Drift correction count
531     f32         m_ZeroDriftPower;      // Drift correction power
532     f32         m_AccRevisePower;      // Acceleration correction strength
533     f32         m_AccReviseRange;   // Acceleration correction enable radius
534 
535     f32         m_ZeroPlayEffect;        // Play circle inner status  0: outside - 1: center
536     f32         m_ZeroDriftEffect;        // Drift correction count continuation state (0-1)
537     f32         m_AccRevEffect;      // How much acceleration correction is applied
538 
539     nn::math::VEC3   m_CalibrationZero;  // Calibration value
540     f64         m_CalibrationScale[3];  // Calibration value
541 
542     nn::math::VEC3   m_CountZero;        // Count value indicating zero point
543     s32         m_CountIdx;         // Buffer index where the newest is stored
544     s32         m_CountT[3][GYROSCOPE_DRIFT_COUNT_MAX];      // XYZ hardware value
545     f32         m_DpsPitchMagnification;      // Magnification for calculating angular velocity in the pitch direction
546     f32         m_DpsYawMagnification;        // Magnification for calculating angular velocity in the yaw direction
547     f32         m_DpsRollMagnification;       // Magnification for calculating angular velocity in the roll direction
548     NN_PADDING4;
549 
550     /*from Low*/
551     Gyroscope&          m_Gyroscope;
552     s32                 m_IndexOfRead;
553     s64                 m_TickOfRead;
554 
555     /*rotate axis*/
556     nn::math::MTX34     m_RotateMtx;
557 
558 };
559 
560 } // namespace CTR {
561 } // namespace hid {
562 } // namespace nn {
563 
564 #endif // #ifndef NN_HID_CTR_GYROSCOPEREADER_H_
565