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