27 #include "sensor/ISensorDataSource.h" 28 #include "sensor/ISensorInfo.h" 29 #include "sensor/ISensorDataSubscriber.h" 30 #include "sensor/SensorDataSourceInfo.h" 31 #include "sensor/IProcessorInfo.h" 32 #include "time/TimestampedValue.h" 33 #include "time/TimestampInfo.h" 34 #include "orientation/Quaternion.h" 35 #include "quantity/Scalar.h" 36 #include "unit/Unit.h" 41 #include <forward_list> 47 vector<SensorDataSourceInfo *> sensor_data_source_infos;
49 long last_system_timestamp;
50 long last_sensor_timestamp;
52 forward_list<ISensorDataSubscriber *> tsq_subscribers;
53 bool navx_callback_registered;
54 vector<IQuantity*> active_sensor_data_quantities;
56 priority_mutex subscriber_mutex;
65 const static int QUANTITY_INDEX_QUATERNION = 0;
66 const static int QUANTITY_INDEX_YAW = 1;
67 const static int QUANTITY_INDEX_PITCH = 2;
68 const static int QUANTITY_INDEX_ROLL = 3;
70 navXSensor(AHRS* navx_sensor,
string sensor_name) :
71 navx_sensor(*navx_sensor),
72 navx_tsinfo(TimestampInfo::Scope::Sensor,
73 TimestampInfo::Basis::SinceLastReboot,
74 1.0 / Timestamp::MILLISECONDS_PER_SECOND,
75 1.0 / Timestamp::MILLISECONDS_PER_SECOND,
76 1.0 / (360 * Timestamp::MILLISECONDS_PER_SECOND),
77 1.0 / Timestamp::MILLISECONDS_PER_SECOND,
80 this->sensor_name = sensor_name;
81 this->navx_callback_registered =
false;
82 long default_timestamp_value = 0;
84 Timestamp::TimestampResolution::Millisecond);
85 vector<IUnit *>ms_units = {&milliseconds};
86 this->sensor_data_source_infos.push_back(
new SensorDataSourceInfo(
"Timestamp", ts, milliseconds));
87 vector<IUnit *> quaternion_units;
88 Quaternion::getUnits(quaternion_units);
94 SensorDataSourceInfo::getQuantityArray(this->sensor_data_source_infos,
95 active_sensor_data_quantities);
96 last_sensor_timestamp = 0;
97 last_system_timestamp = 0;
101 for (
auto p_sensor_data_source_info : sensor_data_source_infos) {
102 delete p_sensor_data_source_info;
108 std::unique_lock<priority_mutex> sync(subscriber_mutex);
109 bool existing =
false;
110 for (
auto tsq_subscriber : tsq_subscribers) {
111 if (tsq_subscriber == subscriber) {
120 if (!navx_callback_registered) {
121 navx_callback_registered = this->navx_sensor.RegisterCallback(
this,
124 if (navx_callback_registered) {
125 std::unique_lock<priority_mutex> sync(subscriber_mutex);
126 tsq_subscribers.push_front(subscriber);
133 std::unique_lock<priority_mutex> sync(subscriber_mutex);
135 bool existing =
false;
136 for (
auto tsq_subscriber : tsq_subscribers) {
137 if (tsq_subscriber == subscriber) {
145 tsq_subscribers.remove(subscriber);
146 if (tsq_subscribers.empty()) {
147 if (navx_callback_registered) {
148 navx_callback_registered =
149 this->navx_sensor.DeregisterCallback(
this);
155 void timestampedDataReceived(
long system_timestamp,
long sensor_timestamp,
156 AHRSProtocol::AHRSUpdateBase& data,
void * context) {
157 std::unique_lock<priority_mutex> sync(subscriber_mutex);
158 ((
Timestamp *) active_sensor_data_quantities[0])->setTimestamp(sensor_timestamp);
159 ((
Quaternion *) active_sensor_data_quantities[1])->set(data.quat_w, data.quat_x, data.quat_y, data.quat_z);
160 ((
Scalar *)active_sensor_data_quantities[2])->set(data.yaw);
161 ((
Scalar *)active_sensor_data_quantities[3])->set(data.pitch);
162 ((
Scalar *)active_sensor_data_quantities[4])->set(data.roll);
164 roborio.getProcessorTimestamp(roborio_timestamp);
166 subscriber->publish(active_sensor_data_quantities, roborio_timestamp);
186 void getSensorDataSourceInfos(vector<SensorDataSourceInfo*>& infos) {
187 infos = this->sensor_data_source_infos;
191 if (this->navx_sensor.IsConnected()) {
192 ((
Timestamp *) quantities[0])->setTimestamp(this->navx_sensor.GetLastSensorTimestamp());
193 ((
Quaternion *) quantities[1])->set(this->navx_sensor.GetQuaternionW(), this->navx_sensor.GetQuaternionX(),
194 this->navx_sensor.GetDisplacementY(), this->navx_sensor.GetQuaternionZ());
195 ((
Scalar *)quantities[2])->set(this->navx_sensor.GetYaw());
196 ((
Scalar *)quantities[3])->set(this->navx_sensor.GetPitch());
197 ((
Scalar *)quantities[4])->set(this->navx_sensor.GetRoll());
198 roborio.getProcessorTimestamp(curr_ts);
214 if (index == QUANTITY_INDEX_YAW) {
215 navx_sensor.ZeroYaw();
void setValid(bool valid)
Sets whether this TimestampedValue is valid or not.
Definition: TimestampedValue.h:184
Definition: SensorDataSourceInfo.h:37
Definition: TimestampInfo.h:28
bool subscribe(ISensorDataSubscriber *subscriber)
Subscribes the provided subscriber object for callbacks whenever new TimestampedQuaternion data is re...
Definition: navXSensor.h:106
The Quaternion class provides methods to operate on a quaternion.
Definition: Quaternion.h:76
Interface to be implemented by any subscriber of Sensor Data of type T.
Definition: ISensorDataSubscriber.h:40
bool getCurrent(vector< IQuantity * > &quantities, Timestamp &curr_ts)
Retrieves the most recently-received Sensor Data value.
Definition: navXSensor.h:190
Definition: IProcessorInfo.h:31
bool reset(int index)
For those quantities which can be reset to their default (e.g., an IMU Yaw, or an encoder counter)...
Definition: navXSensor.h:213
The ITimestampedQuaternionSensor interface should be implemented by any sensor which generates timest...
Definition: ISensorDataSource.h:42
Definition: Timestamp.h:30
Definition: ISensorInfo.h:34
bool unsubscribe(ISensorDataSubscriber *subscriber)
Unsubscribes the previously-registered subscriber object for callbacks whenever new Sensor Data is re...
Definition: navXSensor.h:132
Definition: navXSensor.h:44