LaserScannerTimestampTest.cpp
Go to the documentation of this file.
1/**
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package RobotComponents::ArmarXObjects::laser_scanner_timestamp_test
17 * @author Corvin-N ( corvin at navarro dot de )
18 * @date 2023
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23
25
27
28#include <cstddef>
29
32
33namespace RobotComponents
34{
35
36 const std::string Component::defaultName = "LaserScannerTimestampTest";
37
40 {
43
44 def->optional(
45 properties.topicName, "TopicName", "Name of the laserscanner topic to report to.");
46
47 def->component(laserScanUnitListenerPrx);
48
49
50 def->optional(properties.useTopic, "useTopic", "If enabled, will send data via topic.");
51
52 def->required(properties.frame, "frame", "Coordinate frame of the laserscanner.");
53
54 def->required(properties.freq,
55 "frequency",
56 "Default frequency of newly generated and published laser scans [Hz]");
57
58 def->required(
59 properties.min,
60 "minOffset",
61 "Smallest allowed offset of wrong timestamps [ms]. May be negative. Offset for wrong "
62 "generated timestamp randomly chosen from interval [minOffset,maxOffset]");
63 def->required(
64 properties.max,
65 "maxOffset",
66 "Greatest allowed offset of wrong timestamps [ms]. May be negative. Offset for wrong "
67 "generated timestamp randomly chosen from interval [minOffset,maxOffset]");
68
69 def->required(
70 properties.correctBatchSize,
71 "MaxCorrectBatchSize",
72 "Maximum size of consecutive generated correct timestamps. Batch size of correct "
73 "timestamps is randomly chosen from interval [1,MaxCorrectBatchSize]");
74 def->required(properties.wrongBatchSize,
75 "MaxWrongBatchSize",
76 "Maximum size of consecutive generated wrong timestamps. Batch size of wrong "
77 "timestamps is randomly chosen from interval [1,MaxWrongBatchSize");
78
79 return def;
80 }
81
82 void
84 {
85 // Topics and properties defined above are automagically registered.
86
87 if (properties.min >= properties.max)
88 {
89 ARMARX_ERROR << "following statement should be false but is not: min >= max";
90 }
91
92 if (properties.correctBatchSize < 1)
93 {
94 ARMARX_ERROR << "following statement should be false but is not: correctBatchSize < 1";
95 }
96
97 if (properties.wrongBatchSize < 1)
98 {
99 ARMARX_ERROR << "following statement should be false but is not: wrongBatchSize < 1";
100 }
101
102 // optional topic
103 if (properties.useTopic)
104 {
105 offeringTopic(properties.topicName);
106 }
107 }
108
109 void
111 {
112 // Do things after connecting to topics and components.
113
114 if (properties.useTopic)
115 {
116 getTopic(laserScanUnitListenerTopic, properties.topicName);
117 }
118
119 runTask = new armarx::RunningTask<Component>(this, &Component::run);
120 runTask->start();
121 }
122
123 void
124 Component::run()
125 {
126 ARMARX_IMPORTANT << "Started running task!!!";
127
128 int scansUntilToggle = randInRange(1, properties.correctBatchSize);
129 bool correctScans = true;
130
131 armarx::Metronome metronome(armarx::Frequency::Hertz(properties.freq));
132 while (true)
133 {
134 constexpr std::size_t nSamples = 1000;
135
136 armarx::LaserScan scanData;
137 for (std::size_t i = 0; i < nSamples; i++)
138 {
139 scanData.emplace_back(armarx::LaserScanStep{2 * M_PI / nSamples, 2500.0F, 1.0f});
140 }
141 armarx::DateTime scanTime = armarx::DateTime::Now();
142
143 if (!correctScans)
144 {
145 armarx::Duration scanOffset =
146 armarx::Duration::MilliSeconds(randInRange(properties.min, properties.max));
147 scanTime += scanOffset;
148 ARMARX_INFO << "Using offset of " << scanOffset.toMilliSeconds() << "ms.";
149 }
150
151 armarx::TimestampVariantPtr scanT(new armarx::TimestampVariant(scanTime));
152
153
154 if (laserScanUnitListenerTopic)
155 {
156 laserScanUnitListenerTopic->reportSensorValues(
157 properties.frame, properties.frame, scanData, scanT);
158 }
159
160 // if(laserScanUnitListenerPrx)
161 // {
162 // laserScanUnitListenerPrx->reportSensorValues(properties.frame, properties.frame, scanData, scanT);
163 // }
164
165 scansUntilToggle--;
166 if (scansUntilToggle <= 0)
167 {
168 if (correctScans)
169 {
170 scansUntilToggle = randInRange(1, properties.wrongBatchSize);
171 ARMARX_INFO << "Now sending wrong timestamps";
172 }
173 else
174 {
175 scansUntilToggle = randInRange(1, properties.correctBatchSize);
176 ARMARX_INFO << "Now sending correct timestamps";
177 }
178 correctScans = !correctScans;
179 }
180
181 metronome.waitForNextTick();
182 }
183 }
184
185 int
186 Component::randInRange(int min, int max)
187 {
188 int size = (max + 1) - min;
189 int rand = std::rand();
190
191 return (rand % size) + min;
192 }
193
194 void
196 {
197 runTask->stop();
198 }
199
200 void
204
205 std::string
207 {
208 return Component::defaultName;
209 }
210
211 std::string
213 {
214 return Component::defaultName;
215 }
216
218} // namespace RobotComponents
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
#define M_PI
Definition MathTools.h:17
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
static std::string GetDefaultName()
Get the component's default name.
std::string getDefaultName() const override
Default component property definition container.
Definition Component.h:70
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
static DateTime Now()
Definition DateTime.cpp:51
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition Duration.cpp:48
static Frequency Hertz(std::int64_t hertz)
Definition Frequency.cpp:20
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
std::int64_t toMilliSeconds() const
Returns the amount of milliseconds.
Definition Duration.cpp:60
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition Metronome.h:57
T min(T t1, T t2)
Definition gdiam.h:44
T max(T t1, T t2)
Definition gdiam.h:51
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
This file is part of ArmarX.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceInternal::Handle< TimestampVariant > TimestampVariantPtr