FXOS8700CQ class modified for el17yfk
Revision 4:5a6ef0d027c4, committed 2019-04-18
- Comitter:
- yfkwok
- Date:
- Thu Apr 18 04:52:31 2019 +0000
- Parent:
- 3:eedcae2bd4aa
- Commit message:
- 18/04/2019 - Added functions for getting direction and magnitude
Changed in this revision
FXOS8700CQ.cpp | Show annotated file Show diff for this revision Revisions of this file |
FXOS8700CQ.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/FXOS8700CQ.cpp Wed Apr 17 17:53:33 2019 +0000 +++ b/FXOS8700CQ.cpp Thu Apr 18 04:52:31 2019 +0000 @@ -176,4 +176,41 @@ float roll_angle = atan2(values.ay,values.az)*180.0/3.1416; return roll_angle; +} + +Direction FXOS8700CQ::get_direction() +{ + float pitch_angle = get_pitch_angle(); // -180 to 180 degree + float roll_angle = get_roll_angle(); //-180 to 180 degree + + Direction d; + // partition 360 into segments and check which segment the angle is in + if ((pitch_angle <= 10) && (roll_angle <= 10) && (pitch_angle >= -10) && (roll_angle >= -10)) { + d = CENTRE; // check for -1.0 angle + } else if ((pitch_angle > 10) && (roll_angle < 10) && (roll_angle > -10)) { // then keep going in 45 degree increments + d = N; + } else if ((pitch_angle > 10) && (roll_angle < -10)) { + d = NE; + } else if ((pitch_angle < 10) && (roll_angle < -10) && (pitch_angle > -10)) { + d = E; + } else if ((pitch_angle < -10) && (roll_angle < -10)) { + d = SE; + } else if ((pitch_angle < -10) && (roll_angle < 10) && (roll_angle > -10)) { + d = S; + } else if ((pitch_angle < -10) && (roll_angle > 10)) { + d = SW; + } else if ((pitch_angle < 10) && (pitch_angle > -10) && (roll_angle > 10)) { + d = W; + } else if ((pitch_angle > 10) && (roll_angle > 10)) { + d = NW; + } + + return d; +} + +float FXOS8700CQ::get_mag() +{ + Data values = get_values(); + float mag = sqrt(values.ay*values.ay + values.az*values.az + values.ax*values.ax); + return mag; } \ No newline at end of file
--- a/FXOS8700CQ.h Wed Apr 17 17:53:33 2019 +0000 +++ b/FXOS8700CQ.h Thu Apr 18 04:52:31 2019 +0000 @@ -40,6 +40,7 @@ #define FXOS8700CQ_H #include "mbed.h" +#include "Gamepad.h" // mbed API uses 8-bit addresses so need to left-shift 7-bit addresses by 1 #define FXOS8700CQ_ADDR (0x1D << 1) // for K64F board @@ -75,6 +76,8 @@ Data get_values(); float get_pitch_angle(); float get_roll_angle(); + Direction get_direction(); + float get_mag(); private: I2C* i2c;