humotion / src / timestamped_list.cpp @ 2c4bbc89
History | View | Annotate | Download (5.591 KB)
1 | 8c6c1163 | Simon Schulz | /*
|
---|---|---|---|
2 | * This file is part of humotion
|
||
3 | *
|
||
4 | * Copyright(c) sschulz <AT> techfak.uni-bielefeld.de
|
||
5 | * http://opensource.cit-ec.de/projects/humotion
|
||
6 | *
|
||
7 | * This file may be licensed under the terms of the
|
||
8 | * GNU Lesser General Public License Version 3 (the ``LGPL''),
|
||
9 | * or (at your option) any later version.
|
||
10 | *
|
||
11 | * Software distributed under the License is distributed
|
||
12 | * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
|
||
13 | * express or implied. See the LGPL for the specific language
|
||
14 | * governing rights and limitations.
|
||
15 | *
|
||
16 | * You should have received a copy of the LGPL along with this
|
||
17 | * program. If not, go to http://www.gnu.org/licenses/lgpl.html
|
||
18 | * or write to the Free Software Foundation, Inc.,
|
||
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * The development of this software was supported by the
|
||
22 | * Excellence Cluster EXC 277 Cognitive Interaction Technology.
|
||
23 | * The Excellence Cluster EXC 277 is a grant of the Deutsche
|
||
24 | * Forschungsgemeinschaft (DFG) in the context of the German
|
||
25 | * Excellence Initiative.
|
||
26 | */
|
||
27 | |||
28 | #include "timestamped_list.h" |
||
29 | #include "assert.h" |
||
30 | |||
31 | using namespace std; |
||
32 | using namespace boost; |
||
33 | using namespace humotion; |
||
34 | |||
35 | TimestampedList::TimestampedList(unsigned int s){ |
||
36 | //initialize the list to its desired size:
|
||
37 | TimestampedFloat now; |
||
38 | tsf_list.resize(s, now); |
||
39 | } |
||
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:
|
||
45 | mutex::scoped_lock scoped_lock(l.access_mutex); |
||
46 | |||
47 | //now do a deep copy with locking!
|
||
48 | tsf_list = l.tsf_list; |
||
49 | } |
||
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:
|
||
55 | mutex::scoped_lock scoped_lock(access_mutex); |
||
56 | |||
57 | *target = tsf_list; |
||
58 | } |
||
59 | |||
60 | double TimestampedList::get_last_timestamp(){
|
||
61 | //lock the tsf_list for this access. by doing this we assure
|
||
62 | //that no other thread accessing this element can diturb the
|
||
63 | //following atomic instructions:
|
||
64 | mutex::scoped_lock scoped_lock(access_mutex); |
||
65 | |||
66 | if (tsf_list.empty()){
|
||
67 | return 0.0; |
||
68 | } |
||
69 | |||
70 | timestamped_float_list_t::iterator it = tsf_list.end(); |
||
71 | it--; |
||
72 | return it->timestamp;
|
||
73 | } |
||
74 | |||
75 | void TimestampedList::insert(double timestamp, float val){ |
||
76 | //erase first element:
|
||
77 | tsf_list.pop_front(); |
||
78 | tsf_list.push_back(TimestampedFloat(timestamp, val)); |
||
79 | //printf("insert [%5.3f] = %5.1f\n",timestamp,val);
|
||
80 | } |
||
81 | |||
82 | float TimestampedList::get_newest_value(){
|
||
83 | //lock the tsf_list for this access. by doing this we assure
|
||
84 | //that no other thread accessing this element can diturb the
|
||
85 | //following atomic instructions:
|
||
86 | mutex::scoped_lock scoped_lock(access_mutex); |
||
87 | |||
88 | if (tsf_list.empty()){
|
||
89 | return 0.0; |
||
90 | } |
||
91 | |||
92 | timestamped_float_list_t::iterator it = tsf_list.end(); |
||
93 | it--; |
||
94 | return it->value;
|
||
95 | } |
||
96 | |||
97 | float TimestampedList::get_interpolated_value(double target_ts){ |
||
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 | mutex::scoped_lock scoped_lock(access_mutex); |
||
102 | |||
103 | ///target_ts -= 0.001 * 4;
|
||
104 | |||
105 | TimestampedFloat previous; |
||
106 | //printf("> latency %3.2fms\n", (TimestampedFloat::get_timestamp() - target_ts)*1000.0);
|
||
107 | |||
108 | for ( timestamped_float_list_t::iterator it = tsf_list.begin(); it != tsf_list.end(); ++it ){
|
||
109 | if (it->timestamp >= target_ts){
|
||
110 | //ok found close target
|
||
111 | if (it == tsf_list.begin()){
|
||
112 | //no preceding element
|
||
113 | printf("> warning, timestamp %6.3f smaller than first element %6.3f in timestamped list. this should not happen (increase ts buffer?)\n",target_ts,tsf_list.begin()->timestamp);
|
||
114 | return it->value;
|
||
115 | }else{
|
||
116 | //do interpolation
|
||
117 | return interpolate(*it, previous, target_ts);
|
||
118 | } |
||
119 | } |
||
120 | previous = *it; |
||
121 | } |
||
122 | //we reached the end, return the last value
|
||
123 | printf("> warning: found no timestamp bigger than %f in timestamped list...\n", target_ts);
|
||
124 | printf(" this should not happen as images will always be behind\n");
|
||
125 | printf(" the motor data. returning most recent value (ts=%f)\n", previous.timestamp);
|
||
126 | |||
127 | return previous.value;
|
||
128 | } |
||
129 | |||
130 | float TimestampedList::interpolate(TimestampedFloat a, TimestampedFloat b, double timestamp){ |
||
131 | //a->timestamp < timestamp <= b->timestamp
|
||
132 | double dist_a = timestamp - a.timestamp;
|
||
133 | double dist_b = b.timestamp - timestamp;
|
||
134 | double dist = dist_a + dist_b;
|
||
135 | |||
136 | float interpolation = a.value + (dist_a / dist) * (b.value - a.value);
|
||
137 | return interpolation;
|
||
138 | } |
||
139 | |||
140 | |||
141 | ///tests
|
||
142 | void TimestampedList::run_tests(){
|
||
143 | int size = 10; |
||
144 | TimestampedList list(size); |
||
145 | |||
146 | for(int i=0; i<size; i++){ |
||
147 | list.insert(i * 100.0, i*10.0); |
||
148 | } |
||
149 | |||
150 | //test algorithm:
|
||
151 | |||
152 | //test exact match:
|
||
153 | for(int i=0; i<size; i++){ |
||
154 | printf("> testing get_interpolated_value(%f) == %f (value read back = %f)\n",i*100.0, i*10.0, list.get_interpolated_value(i*100.0)); |
||
155 | assert( list.get_interpolated_value(i*100.0) == i*10.0); |
||
156 | } |
||
157 | printf("passed test 1\n");
|
||
158 | |||
159 | assert(list.get_interpolated_value(0.0) == 0.0); |
||
160 | assert(list.get_interpolated_value(110.0) == 11.0); |
||
161 | assert(list.get_interpolated_value(150.0) == 15.0); |
||
162 | assert(list.get_interpolated_value(999990.0) == 90.0); |
||
163 | |||
164 | printf("passed test 2\n");
|
||
165 | |||
166 | list.insert(1000.0, 200.0); |
||
167 | list.insert(1300.0, -100.0); |
||
168 | assert(list.get_interpolated_value(1100) == 100.0); |
||
169 | assert(list.get_interpolated_value(1200) == 0.0); |
||
170 | assert(list.get_interpolated_value(1300) == -100.0); |
||
171 | assert(list.get_interpolated_value(1250) == -50.0); |
||
172 | |||
173 | printf("passed test 3\n");
|
||
174 | exit(0);
|
||
175 | } |