Revision 0c8d22a5 src/timestamped_list.cpp
src/timestamped_list.cpp | ||
---|---|---|
25 | 25 |
* Excellence Initiative. |
26 | 26 |
*/ |
27 | 27 |
|
28 |
#include "timestamped_list.h" |
|
29 |
#include "assert.h" |
|
28 |
#include <assert.h> |
|
30 | 29 |
|
31 |
using namespace std; |
|
32 |
using namespace boost; |
|
33 |
using namespace humotion; |
|
30 |
#include "humotion/timestamped_list.h" |
|
34 | 31 |
|
35 |
TimestampedList::TimestampedList(unsigned int s){ |
|
36 |
//initialize the list to its desired size: |
|
32 |
using boost::mutex; |
|
33 |
using humotion::TimestampedList; |
|
34 |
|
|
35 |
TimestampedList::TimestampedList(unsigned int s) { |
|
36 |
// initialize the list to its desired size: |
|
37 | 37 |
TimestampedFloat now; |
38 | 38 |
tsf_list.resize(s, now); |
39 | 39 |
} |
40 | 40 |
|
41 |
TimestampedList::TimestampedList(TimestampedList const &l){ |
|
42 |
//lock the tsf_list for this access. by doing this we assure |
|
43 |
//that no other thread accessing this element can diturb the |
|
44 |
//following atomic instructions: |
|
41 |
TimestampedList::TimestampedList(TimestampedList const &l) {
|
|
42 |
// lock the tsf_list for this access. by doing this we assure
|
|
43 |
// that no other thread accessing this element can diturb the
|
|
44 |
// following atomic instructions:
|
|
45 | 45 |
mutex::scoped_lock scoped_lock(l.access_mutex); |
46 | 46 |
|
47 |
//now do a deep copy with locking! |
|
47 |
// now do a deep copy with locking!
|
|
48 | 48 |
tsf_list = l.tsf_list; |
49 | 49 |
} |
50 | 50 |
|
51 |
void TimestampedList::copy_tsf_list_to(timestamped_float_list_t *target){ |
|
52 |
//lock the tsf_list for this access. by doing this we assure |
|
53 |
//that no other thread accessing this element can diturb the |
|
54 |
//following atomic instructions: |
|
51 |
void TimestampedList::copy_tsf_list_to(timestamped_float_list_t *target) {
|
|
52 |
// lock the tsf_list for this access. by doing this we assure
|
|
53 |
// that no other thread accessing this element can diturb the
|
|
54 |
// following atomic instructions:
|
|
55 | 55 |
mutex::scoped_lock scoped_lock(access_mutex); |
56 | 56 |
|
57 | 57 |
*target = tsf_list; |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
|
61 |
Timestamp TimestampedList::get_first_timestamp(){
|
|
62 |
//lock the tsf_list for this access. by doing this we assure |
|
63 |
//that no other thread accessing this element can diturb the |
|
64 |
//following atomic instructions: |
|
61 |
humotion::Timestamp TimestampedList::get_first_timestamp() {
|
|
62 |
// lock the tsf_list for this access. by doing this we assure
|
|
63 |
// that no other thread accessing this element can diturb the
|
|
64 |
// following atomic instructions:
|
|
65 | 65 |
mutex::scoped_lock scoped_lock(access_mutex); |
66 | 66 |
|
67 |
if (tsf_list.empty()){ |
|
68 |
return Timestamp(0,0); |
|
67 |
if (tsf_list.empty()) {
|
|
68 |
return Timestamp(0, 0);
|
|
69 | 69 |
} |
70 | 70 |
|
71 | 71 |
timestamped_float_list_t::iterator it = tsf_list.begin(); |
72 | 72 |
return it->timestamp; |
73 | 73 |
} |
74 | 74 |
|
75 |
Timestamp TimestampedList::get_last_timestamp(){
|
|
76 |
//lock the tsf_list for this access. by doing this we assure |
|
77 |
//that no other thread accessing this element can diturb the |
|
78 |
//following atomic instructions: |
|
75 |
humotion::Timestamp TimestampedList::get_last_timestamp() {
|
|
76 |
// lock the tsf_list for this access. by doing this we assure
|
|
77 |
// that no other thread accessing this element can diturb the
|
|
78 |
// following atomic instructions:
|
|
79 | 79 |
mutex::scoped_lock scoped_lock(access_mutex); |
80 | 80 |
|
81 |
if (tsf_list.empty()){ |
|
82 |
return Timestamp(0,0); |
|
81 |
if (tsf_list.empty()) {
|
|
82 |
return Timestamp(0, 0);
|
|
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
timestamped_float_list_t::iterator it = tsf_list.end(); |
... | ... | |
87 | 87 |
return it->timestamp; |
88 | 88 |
} |
89 | 89 |
|
90 |
void TimestampedList::insert(Timestamp timestamp, float val){ |
|
91 |
//erase first element: |
|
90 |
void TimestampedList::insert(Timestamp timestamp, float val) {
|
|
91 |
// erase first element:
|
|
92 | 92 |
tsf_list.pop_front(); |
93 | 93 |
tsf_list.push_back(TimestampedFloat(timestamp, val)); |
94 |
//printf("insert [%5.3f] = %5.1f\n",timestamp,val); |
|
94 |
// printf("insert [%5.3f] = %5.1f\n",timestamp,val);
|
|
95 | 95 |
} |
96 | 96 |
|
97 |
float TimestampedList::get_newest_value(){ |
|
98 |
//lock the tsf_list for this access. by doing this we assure |
|
99 |
//that no other thread accessing this element can diturb the |
|
100 |
//following atomic instructions: |
|
97 |
float TimestampedList::get_newest_value() {
|
|
98 |
// lock the tsf_list for this access. by doing this we assure
|
|
99 |
// that no other thread accessing this element can diturb the
|
|
100 |
// following atomic instructions:
|
|
101 | 101 |
mutex::scoped_lock scoped_lock(access_mutex); |
102 | 102 |
|
103 |
if (tsf_list.empty()){ |
|
103 |
if (tsf_list.empty()) {
|
|
104 | 104 |
printf("> WARNING: requested newest value from empty list, returning 0.0\n"); |
105 | 105 |
return 0.0; |
106 | 106 |
} |
... | ... | |
110 | 110 |
return it->value; |
111 | 111 |
} |
112 | 112 |
|
113 |
float TimestampedList::get_interpolated_value(Timestamp target_ts){ |
|
114 |
//lock the tsf_list for this access. by doing this we assure |
|
115 |
//that no other thread accessing this element can diturb the |
|
116 |
//following atomic instructions: |
|
113 |
float TimestampedList::get_interpolated_value(Timestamp target_ts) {
|
|
114 |
// lock the tsf_list for this access. by doing this we assure
|
|
115 |
// that no other thread accessing this element can diturb the
|
|
116 |
// following atomic instructions:
|
|
117 | 117 |
mutex::scoped_lock scoped_lock(access_mutex); |
118 | 118 |
|
119 |
///target_ts -= 0.001 * 4; |
|
120 |
|
|
121 | 119 |
TimestampedFloat previous; |
122 |
//printf("> latency %3.2fms\n", (Timestamped().to_seconds() - target_ts.to_seconds())*1000.0); |
|
123 |
|
|
124 |
for ( timestamped_float_list_t::iterator it = tsf_list.begin(); it != tsf_list.end(); ++it ){ |
|
125 |
if (it->timestamp >= target_ts){ |
|
126 |
//ok found close target |
|
127 |
if (it == tsf_list.begin()){ |
|
128 |
//no preceding element |
|
129 |
printf("> warning, timestamp %6.3f smaller than first element %6.3f in timestamped list. this should not happen (increase ts buffer?)\n",target_ts.to_seconds(),tsf_list.begin()->timestamp.to_seconds()); |
|
120 |
// printf("> latency %3.2fms\n", (Timestamped().to_seconds() - target_ts.to_seconds())*1000.0); |
|
121 |
|
|
122 |
for ( timestamped_float_list_t::iterator it = tsf_list.begin(); it != tsf_list.end(); ++it ) { |
|
123 |
if (it->timestamp >= target_ts) { |
|
124 |
// ok found close target |
|
125 |
if (it == tsf_list.begin()) { |
|
126 |
// no preceding element |
|
127 |
printf("> warning, timestamp %6.3f smaller than first element %6.3f in timestamped" |
|
128 |
"list. this should not happen (increase ts buffer?)\n", |
|
129 |
target_ts.to_seconds(), tsf_list.begin()->timestamp.to_seconds()); |
|
130 | 130 |
return it->value; |
131 |
}else{
|
|
132 |
//do interpolation |
|
131 |
} else {
|
|
132 |
// do interpolation
|
|
133 | 133 |
return interpolate(*it, previous, target_ts); |
134 | 134 |
} |
135 | 135 |
} |
136 | 136 |
previous = *it; |
137 | 137 |
} |
138 | 138 |
|
139 |
//we reached the end, return the last value |
|
140 |
printf("> warning: found no timestamp >= than %f in timestamped list...\n", target_ts.to_seconds()); |
|
139 |
// we reached the end, return the last value |
|
140 |
printf("> warning: found no timestamp >= than %f in timestamped list...\n", |
|
141 |
target_ts.to_seconds()); |
|
141 | 142 |
printf(" this should not happen as images will always be behind\n"); |
142 |
printf(" the motor data. returning most recent value (ts=%f)\n", previous.timestamp.to_seconds()); |
|
143 |
printf(" the motor data. returning most recent value (ts=%f)\n", |
|
144 |
previous.timestamp.to_seconds()); |
|
143 | 145 |
|
144 | 146 |
return previous.value; |
145 | 147 |
} |
146 | 148 |
|
147 |
float TimestampedList::interpolate(TimestampedFloat a, TimestampedFloat b, Timestamp timestamp){ |
|
148 |
//a->timestamp < timestamp <= b->timestamp |
|
149 |
float TimestampedList::interpolate(TimestampedFloat a, TimestampedFloat b, Timestamp timestamp) {
|
|
150 |
// a->timestamp < timestamp <= b->timestamp
|
|
149 | 151 |
double dist_a = timestamp.to_seconds() - a.timestamp.to_seconds(); |
150 | 152 |
double dist_b = b.timestamp.to_seconds() - timestamp.to_seconds(); |
151 | 153 |
double dist = dist_a + dist_b; |
... | ... | |
155 | 157 |
} |
156 | 158 |
|
157 | 159 |
|
158 |
///tests
|
|
159 |
void TimestampedList::run_tests(){ |
|
160 |
// tests
|
|
161 |
void TimestampedList::run_tests() {
|
|
160 | 162 |
int size = 10; |
161 | 163 |
TimestampedList list(size); |
162 | 164 |
|
163 |
for(int i=0; i<size; i++){
|
|
164 |
list.insert(Timestamp(i * 100.0,0), i*10.0); |
|
165 |
for (int i = 0; i < size; i++) {
|
|
166 |
list.insert(Timestamp(i * 100.0, 0), i*10.0);
|
|
165 | 167 |
} |
166 | 168 |
|
167 |
//test algorithm: |
|
169 |
// test algorithm:
|
|
168 | 170 |
|
169 |
//test exact match: |
|
170 |
for(int i=0; i<size; i++){ |
|
171 |
Timestamp ts(i*100.0,0); |
|
172 |
printf("> testing get_interpolated_value(%f) == %f (value read back = %f)\n",ts.to_seconds(), i*10.0, list.get_interpolated_value(ts)); |
|
173 |
assert( list.get_interpolated_value(ts) == i*10.0); |
|
171 |
// test exact match: |
|
172 |
for (int i =0 ; i < size; i++) { |
|
173 |
Timestamp ts(i*100.0, 0); |
|
174 |
printf("> testing get_interpolated_value(%f) == %f (value read back = %f)\n", |
|
175 |
ts.to_seconds(), i*10.0, list.get_interpolated_value(ts)); |
|
176 |
assert(list.get_interpolated_value(ts) == i*10.0); |
|
174 | 177 |
} |
175 | 178 |
printf("passed test 1\n"); |
176 | 179 |
|
177 |
assert(list.get_interpolated_value(Timestamp(0.0,0)) == 0.0); |
|
178 |
assert(list.get_interpolated_value(Timestamp(110.0,0)) == 11.0); |
|
179 |
assert(list.get_interpolated_value(Timestamp(150.0,0)) == 15.0); |
|
180 |
assert(list.get_interpolated_value(Timestamp(999990.0,0)) == 90.0); |
|
180 |
assert(list.get_interpolated_value(Timestamp(0.0, 0)) == 0.0);
|
|
181 |
assert(list.get_interpolated_value(Timestamp(110.0, 0)) == 11.0);
|
|
182 |
assert(list.get_interpolated_value(Timestamp(150.0, 0)) == 15.0);
|
|
183 |
assert(list.get_interpolated_value(Timestamp(999990.0, 0)) == 90.0);
|
|
181 | 184 |
|
182 | 185 |
printf("passed test 2\n"); |
183 | 186 |
|
184 |
list.insert(Timestamp(1000.0 ,0), 200.0);
|
|
187 |
list.insert(Timestamp(1000.0, 0), 200.0);
|
|
185 | 188 |
list.insert(Timestamp(1300.0, 0), -100.0); |
186 | 189 |
assert(list.get_interpolated_value(Timestamp(1100, 0)) == 100.0); |
187 | 190 |
assert(list.get_interpolated_value(Timestamp(1200, 0)) == 0.0); |
... | ... | |
189 | 192 |
assert(list.get_interpolated_value(Timestamp(1250, 0)) == -50.0); |
190 | 193 |
|
191 | 194 |
printf("passed test 3\n"); |
192 |
exit(0);
|
|
195 |
exit(EXIT_SUCCESS);
|
|
193 | 196 |
} |
Also available in: Unified diff