RSeries astromech firmware
Orientation.h
Go to the documentation of this file.
1 #ifndef Orientation_h
2 #define Orientation_h
3 
4 #include <Adafruit_BNO055.h>
5 #include "ReelTwo.h"
6 #include "core/SetupEvent.h"
7 #include "core/AnimatedEvent.h"
8 
19 class Orientation :
20  public SetupEvent, AnimatedEvent, Adafruit_BNO055
21 {
22 public:
26  Orientation(uint8_t id = 0) :
27  Adafruit_BNO055(55, BNO055_ADDRESS_A+id),
28  fID(id)
29  {
30  }
31 
35  virtual void setup()
36  {
37  if ((fReady = begin()) == true)
38  {
39  setExtCrystalUse(true);
40  }
41  else
42  {
43  /* There was a problem detecting the BNO055 ... check your connections */
44  DEBUG_PRINTLN("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR");
45  //Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
46  }
47  }
48 
52  float getYaw()
53  {
54  return int(fCurrentYaw);
55  }
56 
60  bool getYawChanged(float& yaw)
61  {
62  float prevYaw = fPreviousYaw;
63  fPreviousYaw = fCurrentYaw;
64  yaw = fCurrentYaw;
65  return (prevYaw != fCurrentYaw);
66  }
67 
71  float getRoll()
72  {
73  return int(fCurrentRoll);
74  }
75 
79  bool getRollChanged(float& roll)
80  {
81  float prevRoll = fPreviousRoll;
82  fPreviousRoll = fCurrentRoll;
83  roll = fCurrentRoll;
84  return (prevRoll != fCurrentRoll);
85  }
86 
90  float getPitch()
91  {
92  return int(fCurrentPitch);
93  }
94 
98  bool getPitchChanged(float& pitch)
99  {
100  float prevPitch = fPreviousPitch;
101  fPreviousPitch = fCurrentPitch;
102  pitch = fCurrentPitch;
103  return (prevPitch != fCurrentPitch);
104  }
105 
109  virtual void animate()
110  {
111  if (!fReady)
112  return;
113  sensors_event_t event;
114  getEvent(&event);
115 
116  if (event.orientation.x != fLastEvent.orientation.x ||
117  event.orientation.y != fLastEvent.orientation.y ||
118  event.orientation.z != fLastEvent.orientation.z)
119  {
120  float yaw = float(event.orientation.x);
121  float roll = float(event.orientation.y);
122  float pitch = float(event.orientation.z);
123  if (fCurrentYaw == -1)
124  {
125  fPreviousYaw = fCurrentYaw = yaw;
126  }
127  else
128  {
129  fPreviousYaw = fCurrentYaw;
130  fCurrentYaw = yaw;
131  }
132  if (fCurrentRoll == -1)
133  {
134  fPreviousRoll = fCurrentRoll = roll;
135  }
136  else
137  {
138  fPreviousRoll = fCurrentRoll;
139  fCurrentRoll = roll;
140  }
141  if (fCurrentPitch == -1)
142  {
143  fPreviousPitch = fCurrentPitch = roll;
144  }
145  else
146  {
147  fPreviousPitch = fCurrentPitch;
148  fCurrentPitch = pitch;
149  }
150  fLastEvent = event;
151  #ifdef USE_SMQ
152  if (fCurrentYaw != fPreviousYaw ||
153  fCurrentRoll != fPreviousRoll ||
154  fCurrentPitch != fPreviousPitch)
155  {
156  if (SMQ::sendTopic("Orientation"))
157  {
158  SMQ::send_uint8(F("id"), fID);
159  SMQ::send_float(F("yaw"), yaw);
160  SMQ::send_float(F("roll"), roll);
161  SMQ::send_float(F("pitch"), pitch);
162  SMQ::send_end();
163  }
164  }
165  #endif
166  }
167  }
168 
169 private:
170  bool fReady = false;
171  float fPreviousYaw = -1;
172  float fCurrentYaw = -1;
173  float fPreviousRoll = -1;
174  float fCurrentRoll = -1;
175  float fPreviousPitch = -1;
176  float fCurrentPitch = -1;
177  uint8_t fID;
178  sensors_event_t fLastEvent;
179 };
180 
181 #endif
ReelTwo.h
SetupEvent.h
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
SMQ::send_end
static void send_end()
Definition: ReelTwoSMQ.h:463
SetupEvent
Base class for all devices that require setup that cannot happen in the constructor....
Definition: SetupEvent.h:15
SMQ::sendTopic
static bool sendTopic(const smq_id id)
Definition: ReelTwoSMQ.h:155
Orientation::animate
virtual void animate()
Reads the IMU and publishes any change in heading.
Definition: Orientation.h:109
AnimatedEvent.h
DEBUG_PRINTLN
#define DEBUG_PRINTLN(s)
Definition: ReelTwo.h:188
Orientation::getYaw
float getYaw()
Returns the last recorded yaw.
Definition: Orientation.h:52
Orientation::getRollChanged
bool getRollChanged(float &roll)
Returns true if roll has changed since last calling this function.
Definition: Orientation.h:79
Orientation::getPitch
float getPitch()
Returns the last recorded pitch.
Definition: Orientation.h:90
SMQ::send_float
static void send_float(const msg_id id, float val)
Definition: ReelTwoSMQ.h:364
Orientation::getYawChanged
bool getYawChanged(float &yaw)
Returns true if yaw has changed since last calling this function.
Definition: Orientation.h:60
Orientation::Orientation
Orientation(uint8_t id=0)
Default Constructor.
Definition: Orientation.h:26
SMQ::send_uint8
static void send_uint8(const msg_id id, uint8_t val)
Definition: ReelTwoSMQ.h:292
Orientation::getPitchChanged
bool getPitchChanged(float &pitch)
Returns true if pitch has changed since last calling this function.
Definition: Orientation.h:98
Orientation::setup
virtual void setup()
Perform any initialzation not possible in the constructor.
Definition: Orientation.h:35
Orientation::getRoll
float getRoll()
Returns the last recorded roll.
Definition: Orientation.h:71
Orientation
Encapsulates an Adafruit BNO055 IMU.
Definition: Orientation.h:19