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