A hello world for inertial sensors
Dependencies: FXAS21000 FXOS8700Q mbed
Fork of FRDM-STBC-AGM01 by
northStarFuncs.cpp@3:123b546e4a5c, 2015-06-30 (annotated)
- Committer:
- Elecia
- Date:
- Tue Jun 30 16:06:47 2015 +0000
- Revision:
- 3:123b546e4a5c
Inertial sensor hello world project, code works though comments need some help.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Elecia | 3:123b546e4a5c | 1 | |
Elecia | 3:123b546e4a5c | 2 | #include <stdint.h> |
Elecia | 3:123b546e4a5c | 3 | #include <stdlib.h> |
Elecia | 3:123b546e4a5c | 4 | #include "northStarFuncs.h" |
Elecia | 3:123b546e4a5c | 5 | #include <math.h> |
Elecia | 3:123b546e4a5c | 6 | |
Elecia | 3:123b546e4a5c | 7 | #define UINT8_MAX 255 // not in stdint.h for some reason |
Elecia | 3:123b546e4a5c | 8 | |
Elecia | 3:123b546e4a5c | 9 | // red = abs(accel.x) scaled to 1G |
Elecia | 3:123b546e4a5c | 10 | // green = abs(accel.y) scaled to 1G |
Elecia | 3:123b546e4a5c | 11 | // blue = abs(accel.z) scaled to 1G |
Elecia | 3:123b546e4a5c | 12 | uint8_t conditionAccels(float reading) |
Elecia | 3:123b546e4a5c | 13 | { |
Elecia | 3:123b546e4a5c | 14 | uint8_t colorValue; |
Elecia | 3:123b546e4a5c | 15 | |
Elecia | 3:123b546e4a5c | 16 | // abs(reading) |
Elecia | 3:123b546e4a5c | 17 | if (reading < 0.0) {reading = -reading;} |
Elecia | 3:123b546e4a5c | 18 | |
Elecia | 3:123b546e4a5c | 19 | // scale from mg to 0 to 255 |
Elecia | 3:123b546e4a5c | 20 | reading = reading / 4; |
Elecia | 3:123b546e4a5c | 21 | |
Elecia | 3:123b546e4a5c | 22 | // limit to max |
Elecia | 3:123b546e4a5c | 23 | if (reading > UINT8_MAX ) { |
Elecia | 3:123b546e4a5c | 24 | colorValue = UINT8_MAX ; |
Elecia | 3:123b546e4a5c | 25 | } else { |
Elecia | 3:123b546e4a5c | 26 | colorValue = reading; |
Elecia | 3:123b546e4a5c | 27 | } |
Elecia | 3:123b546e4a5c | 28 | return colorValue; |
Elecia | 3:123b546e4a5c | 29 | } |
Elecia | 3:123b546e4a5c | 30 | |
Elecia | 3:123b546e4a5c | 31 | uint8_t conditionMags(float heading, float offset, float range) |
Elecia | 3:123b546e4a5c | 32 | { |
Elecia | 3:123b546e4a5c | 33 | float h = heading + offset; |
Elecia | 3:123b546e4a5c | 34 | float colorValFloat; |
Elecia | 3:123b546e4a5c | 35 | uint8_t colorVal; |
Elecia | 3:123b546e4a5c | 36 | |
Elecia | 3:123b546e4a5c | 37 | if (h < -180.0) { h = h + 360.0; } |
Elecia | 3:123b546e4a5c | 38 | if (h > 180.0) { h = h - 360.0; } |
Elecia | 3:123b546e4a5c | 39 | // now h is pointing in the direction of interest |
Elecia | 3:123b546e4a5c | 40 | |
Elecia | 3:123b546e4a5c | 41 | colorValFloat = 255.0 - abs(h)*255.0/range; |
Elecia | 3:123b546e4a5c | 42 | if (colorValFloat < 0.0) { colorValFloat = 0.0; } |
Elecia | 3:123b546e4a5c | 43 | colorVal = colorValFloat; |
Elecia | 3:123b546e4a5c | 44 | return colorVal; |
Elecia | 3:123b546e4a5c | 45 | } |
Elecia | 3:123b546e4a5c | 46 | |
Elecia | 3:123b546e4a5c | 47 | // arbitrary conversion: squares to reduces vibe (<1 rad/s) |
Elecia | 3:123b546e4a5c | 48 | // so as to highlight real movement |
Elecia | 3:123b546e4a5c | 49 | uint8_t conditionGyros(float reading) |
Elecia | 3:123b546e4a5c | 50 | { |
Elecia | 3:123b546e4a5c | 51 | uint8_t colorValue; |
Elecia | 3:123b546e4a5c | 52 | float colorValueFloat; |
Elecia | 3:123b546e4a5c | 53 | |
Elecia | 3:123b546e4a5c | 54 | colorValueFloat = (reading*reading)/1500.0; |
Elecia | 3:123b546e4a5c | 55 | if (colorValueFloat > 255) { |
Elecia | 3:123b546e4a5c | 56 | colorValue = 255; |
Elecia | 3:123b546e4a5c | 57 | } else { |
Elecia | 3:123b546e4a5c | 58 | colorValue = colorValueFloat; |
Elecia | 3:123b546e4a5c | 59 | } |
Elecia | 3:123b546e4a5c | 60 | |
Elecia | 3:123b546e4a5c | 61 | return colorValue; |
Elecia | 3:123b546e4a5c | 62 | } |
Elecia | 3:123b546e4a5c | 63 | |
Elecia | 3:123b546e4a5c | 64 | eModes changeMode(eModes current) |
Elecia | 3:123b546e4a5c | 65 | { |
Elecia | 3:123b546e4a5c | 66 | switch (current) { |
Elecia | 3:123b546e4a5c | 67 | case(OFF): return ACCEL; |
Elecia | 3:123b546e4a5c | 68 | case(ACCEL): return GYRO; |
Elecia | 3:123b546e4a5c | 69 | case(GYRO): return MAG; |
Elecia | 3:123b546e4a5c | 70 | case(MAG): return OFF; |
Elecia | 3:123b546e4a5c | 71 | } |
Elecia | 3:123b546e4a5c | 72 | return OFF; |
Elecia | 3:123b546e4a5c | 73 | } |