SF2 C++ FRC Class Library
Sensor Fusion Framework (SF2) for FRC
TimestampedValue.h
1 /* ============================================
2  SF2 source code is placed under the MIT license
3  Copyright (c) 2017 Kauai Labs
4 
5  Permission is hereby granted, free of charge, to any person obtaining a copy
6  of this software and associated documentation files (the "Software"), to deal
7  in the Software without restriction, including without limitation the rights
8  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  copies of the Software, and to permit persons to whom the Software is
10  furnished to do so, subject to the following conditions:
11 
12  The above copyright notice and this permission notice shall be included in
13  all copies or substantial portions of the Software.
14 
15  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  THE SOFTWARE.
22  ===============================================
23  */
24 
25 #ifndef SRC_TIME_TIMESTAMPEDVALUE_H_
26 #define SRC_TIME_TIMESTAMPEDVALUE_H_
27 
28 #include "ITimestampedValue.h"
29 #include "../quantity/ICopy.h"
30 #include "../interpolation/IValueInterpolator.h"
31 
32 template<typename T>
34  public ICopy<TimestampedValue<T>>,
35  public IValueInterpolator<TimestampedValue<T>> {
36 protected:
37  T value;
38  long timestamp;
39  uint8_t flags;
40 
41  static const uint8_t valid_flag = 0x01;
42  static const uint8_t interpolated_flag = 0x02;
43 
44 public:
46  timestamp = 0;
47  flags = 0;
48  }
49 
53  TimestampedValue(T& value) {
54  this->value = value;
55  timestamp = 0;
56  flags = 0;
57  }
58 
65  TimestampedValue(T& src, long timestamp) {
66  this->value = src.instantiate_copy();
67  setTimestamp(timestamp);
68  this->flags = 0;
69  }
70 
76  this->value = src.getValue();
77  this->timestamp = src.timestamp;
78  this->flags = src.flags;
79  }
80 
81  virtual ~TimestampedValue(){}
82 
86  long getTimestamp() {
87  return timestamp;
88  }
89 
90  void setTimestamp(long timestamp) {
91  this->timestamp = timestamp;
92  }
93 
98  void set(TimestampedValue<T>& src) {
99  this->value.copy(src.value);
100  this->timestamp = src.timestamp;
101  this->flags = src.flags;
102  }
103 
110  void set(T& src, long timestamp) {
111  this->value.copy(src);
112  this->timestamp = timestamp;
113  this->flags = valid_flag; /* Interpolated = false */
114  }
115 
116  T& getValue() {
117  return value;
118  }
119 
124  /* IValueInterpolator */
126  return ((flags & interpolated_flag) != 0);
127  }
128 
134  void setInterpolated(bool interpolated) {
135  if (interpolated) {
136  flags |= interpolated_flag;
137  } else {
138  flags &= ~interpolated_flag;
139  }
140  }
141 
149  void interpolate(TimestampedValue<T>& to, double time_ratio,
150  TimestampedValue<T>& out) {
151  TimestampedValue<T>& from = *this;
152  this->value.interpolate(to.value, time_ratio, out.getValue());
153  float delta_t = to.getTimestamp() - from.getTimestamp();
154  delta_t *= time_ratio;
155  out.setTimestamp((long) delta_t);
156  }
157 
162  /* ICopy */
164  this->set(t);
165  }
166 
173  /* ITimestampedValue */
174  bool getValid() {
175  return ((flags & valid_flag) != 0);
176  }
177 
184  void setValid(bool valid) {
185  if (valid) {
186  flags |= valid_flag;
187  } else {
188  flags &= ~valid_flag;
189  }
190  }
191 
192  TimestampedValue<T>* instantiate_copy() {
193  TimestampedValue<T>* new_tsv = new TimestampedValue<T>();
194  new_tsv->value = this->value;
195  return new_tsv;
196  }
197 
198  IQuantity& getQuantity() {
199  return this->value;
200  }
201 };
202 
203 #endif /* SRC_TIME_TIMESTAMPEDVALUE_H_ */
void setValid(bool valid)
Sets whether this TimestampedValue is valid or not.
Definition: TimestampedValue.h:184
void interpolate(TimestampedValue< T > &to, double time_ratio, TimestampedValue< T > &out)
Modifies this TimestampedValue (representing the "from" value) to represent a new value and Timestamp...
Definition: TimestampedValue.h:149
Definition: ICopy.h:29
long getTimestamp()
Returns the timestamp for this TimestampedValue.
Definition: TimestampedValue.h:86
TimestampedValue(T &value)
Default constructor for a TimestampedValue<T>; initializes all values to reasonable defaults...
Definition: TimestampedValue.h:53
Definition: TimestampedValue.h:33
TimestampedValue(T &src, long timestamp)
Constructor allowing a TimestampedValue<T> to be created from a T object and a timestamp.
Definition: TimestampedValue.h:65
TimestampedValue(TimestampedValue< T > &src)
Copy constructor; initializes all values to that of the source TimestampedValue<T>.
Definition: TimestampedValue.h:75
bool getInterpolated()
If true, this TimestampedValue<T> was interpolated, otherwise it is an actual (measured) TimestampedV...
Definition: TimestampedValue.h:125
Definition: IQuantity.h:33
bool getValid()
Returns whether this TimestampedValue is valid or not.
Definition: TimestampedValue.h:174
Definition: ITimestampedValue.h:30
void setInterpolated(bool interpolated)
Modifies this TimestampedValue<T>&#39;s interpolated state; if true, this TimestampedValue<T> was interpo...
Definition: TimestampedValue.h:134
void copy(TimestampedValue< T > &t)
Initalizes this TimestampedValue to be equal to the source TimestampedValue.
Definition: TimestampedValue.h:163