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 
26 #include <cstddef>
27 
30 
31 namespace RobotComponents
32 {
33 
34  const std::string Component::defaultName = "LaserScannerTimestampTest";
35 
38  {
41 
42  def->optional(
43  properties.topicName, "TopicName", "Name of the laserscanner topic to report to.");
44 
45  def->component(laserScanUnitListenerPrx);
46 
47 
48  def->optional(properties.useTopic, "useTopic", "If enabled, will send data via topic.");
49 
50  def->required(properties.frame, "frame", "Coordinate frame of the laserscanner.");
51 
52  def->required(properties.freq,
53  "frequency",
54  "Default frequency of newly generated and published laser scans [Hz]");
55 
56  def->required(
57  properties.min,
58  "minOffset",
59  "Smallest allowed offset of wrong timestamps [ms]. May be negative. Offset for wrong "
60  "generated timestamp randomly chosen from interval [minOffset,maxOffset]");
61  def->required(
62  properties.max,
63  "maxOffset",
64  "Greatest allowed offset of wrong timestamps [ms]. May be negative. Offset for wrong "
65  "generated timestamp randomly chosen from interval [minOffset,maxOffset]");
66 
67  def->required(
68  properties.correctBatchSize,
69  "MaxCorrectBatchSize",
70  "Maximum size of consecutive generated correct timestamps. Batch size of correct "
71  "timestamps is randomly chosen from interval [1,MaxCorrectBatchSize]");
72  def->required(properties.wrongBatchSize,
73  "MaxWrongBatchSize",
74  "Maximum size of consecutive generated wrong timestamps. Batch size of wrong "
75  "timestamps is randomly chosen from interval [1,MaxWrongBatchSize");
76 
77  return def;
78  }
79 
80  void
82  {
83  // Topics and properties defined above are automagically registered.
84 
85  if (properties.min >= properties.max)
86  {
87  ARMARX_ERROR << "following statement should be false but is not: min >= max";
88  }
89 
90  if (properties.correctBatchSize < 1)
91  {
92  ARMARX_ERROR << "following statement should be false but is not: correctBatchSize < 1";
93  }
94 
95  if (properties.wrongBatchSize < 1)
96  {
97  ARMARX_ERROR << "following statement should be false but is not: wrongBatchSize < 1";
98  }
99 
100  // optional topic
101  if (properties.useTopic)
102  {
103  offeringTopic(properties.topicName);
104  }
105  }
106 
107  void
109  {
110  // Do things after connecting to topics and components.
111 
112  if (properties.useTopic)
113  {
114  getTopic(laserScanUnitListenerTopic, properties.topicName);
115  }
116 
117  runTask = new armarx::RunningTask<Component>(this, &Component::run);
118  runTask->start();
119  }
120 
121  void
122  Component::run()
123  {
124  ARMARX_IMPORTANT << "Started running task!!!";
125 
126  int scansUntilToggle = randInRange(1, properties.correctBatchSize);
127  bool correctScans = true;
128 
129  armarx::Metronome metronome(armarx::Frequency::Hertz(properties.freq));
130  while (true)
131  {
132  constexpr std::size_t nSamples = 1000;
133 
134  armarx::LaserScan scanData;
135  for (std::size_t i = 0; i < nSamples; i++)
136  {
137  scanData.emplace_back(armarx::LaserScanStep{2 * M_PI / nSamples, 2500.0F, 1.0f});
138  }
140 
141  if (!correctScans)
142  {
143  armarx::Duration scanOffset =
144  armarx::Duration::MilliSeconds(randInRange(properties.min, properties.max));
145  scanTime += scanOffset;
146  ARMARX_INFO << "Using offset of " << scanOffset.toMilliSeconds() << "ms.";
147  }
148 
150 
151 
152  if (laserScanUnitListenerTopic)
153  {
154  laserScanUnitListenerTopic->reportSensorValues(
155  properties.frame, properties.frame, scanData, scanT);
156  }
157 
158  // if(laserScanUnitListenerPrx)
159  // {
160  // laserScanUnitListenerPrx->reportSensorValues(properties.frame, properties.frame, scanData, scanT);
161  // }
162 
163  scansUntilToggle--;
164  if (scansUntilToggle <= 0)
165  {
166  if (correctScans)
167  {
168  scansUntilToggle = randInRange(1, properties.wrongBatchSize);
169  ARMARX_INFO << "Now sending wrong timestamps";
170  }
171  else
172  {
173  scansUntilToggle = randInRange(1, properties.correctBatchSize);
174  ARMARX_INFO << "Now sending correct timestamps";
175  }
176  correctScans = !correctScans;
177  }
178 
179  metronome.waitForNextTick();
180  }
181  }
182 
183  int
184  Component::randInRange(int min, int max)
185  {
186  int size = (max + 1) - min;
187  int rand = std::rand();
188 
189  return (rand % size) + min;
190  }
191 
192  void
194  {
195  runTask->stop();
196  }
197 
198  void
200  {
201  }
202 
203  std::string
205  {
206  return Component::defaultName;
207  }
208 
209  std::string
211  {
212  return Component::defaultName;
213  }
214 
215 } // namespace RobotComponents
RobotComponents::Component::getDefaultName
std::string getDefaultName() const override
Definition: LaserScannerTimestampTest.cpp:204
LaserScannerTimestampTest.h
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:190
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:51
armarx::TimestampVariant
Definition: TimestampVariant.h:54
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:36
RobotComponents::Component::onInitComponent
void onInitComponent() override
Definition: LaserScannerTimestampTest.cpp:81
RobotComponents::Component::onConnectComponent
void onConnectComponent() override
Definition: LaserScannerTimestampTest.cpp:108
IceInternal::Handle< TimestampVariant >
RobotComponents::Component::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: LaserScannerTimestampTest.cpp:37
RobotComponents::Component::onDisconnectComponent
void onDisconnectComponent() override
Definition: LaserScannerTimestampTest.cpp:193
M_PI
#define M_PI
Definition: MathTools.h:17
TimestampVariant.h
Metronome.h
max
T max(T t1, T t2)
Definition: gdiam.h:51
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
RobotComponents::Component::onExitComponent
void onExitComponent() override
Definition: LaserScannerTimestampTest.cpp:199
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ManagedIceObject::getTopic
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
Definition: ManagedIceObject.h:480
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:300
IceUtil::Handle
Definition: forward_declarations.h:30
armarx::core::time::Metronome
Simple rate limiter for use in loops to maintain a certain frequency given a clock.
Definition: Metronome.h:34
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:16
armarx::core::time::Duration::toMilliSeconds
std::int64_t toMilliSeconds() const
Returns the amount of milliseconds.
Definition: Duration.cpp:60
armarx::core::time::Frequency::Hertz
static Frequency Hertz(std::int64_t hertz)
Definition: Frequency.cpp:20
min
T min(T t1, T t2)
Definition: gdiam.h:44
RobotComponents::Component::GetDefaultName
static std::string GetDefaultName()
Get the component's default name.
Definition: LaserScannerTimestampTest.cpp:210
armarx::core::time::Duration::MilliSeconds
static Duration MilliSeconds(std::int64_t milliSeconds)
Constructs a duration in milliseconds.
Definition: Duration.cpp:48
RobotComponents
This file is part of ArmarX.