etsi_its_messages 1.0.0
Loading...
Searching...
No Matches
cpm_ts_setters.h
Go to the documentation of this file.
1/*
2=============================================================================
3MIT License
4
5Copyright (c) 2023-2024 Institute for Automotive Engineering (ika), RWTH Aachen University
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24=============================================================================
25*/
26
32#pragma once
33
35
36namespace etsi_its_cpm_ts_msgs::access {
37
39
49inline void setItsPduHeader(CollectivePerceptionMessage& cpm, const uint32_t station_id,
50 const uint8_t protocol_version = 0) {
51 setItsPduHeader(cpm.header, MessageId::CPM, station_id, protocol_version);
52}
53
65inline void setReferenceTime(
66 CollectivePerceptionMessage& cpm, const uint64_t unix_nanosecs,
67 const uint16_t n_leap_seconds = etsi_its_msgs::LEAP_SECOND_INSERTIONS_SINCE_2004.end()->second) {
68 TimestampIts t_its;
69 setTimestampITS(t_its, unix_nanosecs, n_leap_seconds);
70 throwIfOutOfRange(t_its.value, TimestampIts::MIN, TimestampIts::MAX, "TimestampIts");
71 cpm.payload.management_container.reference_time = t_its;
72}
73
85inline void setReferencePosition(CollectivePerceptionMessage& cpm, const double latitude, const double longitude,
86 const double altitude = AltitudeValue::UNAVAILABLE) {
87 setReferencePosition(cpm.payload.management_container.reference_position, latitude, longitude, altitude);
88}
89
102inline void setFromUTMPosition(CollectivePerceptionMessage& cpm, const gm::PointStamped& utm_position, const int& zone,
103 const bool& northp) {
104 setFromUTMPosition(cpm.payload.management_container.reference_position, utm_position, zone, northp);
105}
106
115inline void setIdOfPerceivedObject(PerceivedObject& object, const uint16_t id) {
116 object.object_id.value = id;
117 object.object_id_is_present = true;
118}
119
131inline void setMeasurementDeltaTimeOfPerceivedObject(PerceivedObject& object, const int16_t delta_time = 0) {
132 if (delta_time < DeltaTimeMilliSecondSigned::MIN || delta_time > DeltaTimeMilliSecondSigned::MAX) {
133 throw std::invalid_argument("MeasurementDeltaTime out of range");
134 } else {
135 object.measurement_delta_time.value = delta_time;
136 }
137}
138
152inline void setCartesianCoordinateWithConfidence(CartesianCoordinateWithConfidence& coordinate, const int32_t value,
153 const uint16_t confidence = CoordinateConfidence::UNAVAILABLE) {
154 // limit value range
155 if (value < CartesianCoordinateLarge::NEGATIVE_OUT_OF_RANGE) {
156 coordinate.value.value = CartesianCoordinateLarge::NEGATIVE_OUT_OF_RANGE;
157 } else if (value > CartesianCoordinateLarge::POSITIVE_OUT_OF_RANGE) {
158 coordinate.value.value = CartesianCoordinateLarge::POSITIVE_OUT_OF_RANGE;
159 } else {
160 coordinate.value.value = value;
161 }
162
163 // limit confidence range
164 if (confidence > CoordinateConfidence::MAX || confidence < CoordinateConfidence::MIN) {
165 coordinate.confidence.value = CoordinateConfidence::OUT_OF_RANGE;
166 } else {
167 coordinate.confidence.value = confidence;
168 }
170
182inline void setPositionOfPerceivedObject(PerceivedObject& object, const gm::Point& point,
183 const uint16_t x_confidence = CoordinateConfidence::UNAVAILABLE,
184 const uint16_t y_confidence = CoordinateConfidence::UNAVAILABLE,
185 const uint16_t z_confidence = CoordinateConfidence::UNAVAILABLE) {
186 setCartesianCoordinateWithConfidence(object.position.x_coordinate, point.x * 100, x_confidence * 100);
187 setCartesianCoordinateWithConfidence(object.position.y_coordinate, point.y * 100, y_confidence * 100);
188 if (point.z != 0.0) {
189 setCartesianCoordinateWithConfidence(object.position.z_coordinate, point.z * 100, z_confidence * 100);
190 object.position.z_coordinate_is_present = true;
191 }
192}
193
209inline void setUTMPositionOfPerceivedObject(CollectivePerceptionMessage& cpm, PerceivedObject& object,
210 const gm::PointStamped& utm_position,
211 const uint16_t x_confidence = CoordinateConfidence::UNAVAILABLE,
212 const uint16_t y_confidence = CoordinateConfidence::UNAVAILABLE,
213 const uint16_t z_confidence = CoordinateConfidence::UNAVAILABLE) {
214 gm::PointStamped reference_position = getUTMPosition(cpm);
215 if (utm_position.header.frame_id != reference_position.header.frame_id) {
216 throw std::invalid_argument("UTM-Position frame_id (" + utm_position.header.frame_id +
217 ") does not match the reference position frame_id (" + reference_position.header.frame_id +
218 ")");
219 }
220 setCartesianCoordinateWithConfidence(object.position.x_coordinate,
221 (utm_position.point.x - reference_position.point.x) * 100, x_confidence);
222 setCartesianCoordinateWithConfidence(object.position.y_coordinate,
223 (utm_position.point.y - reference_position.point.y) * 100, y_confidence);
224 if (utm_position.point.z != 0.0) {
225 setCartesianCoordinateWithConfidence(object.position.z_coordinate,
226 (utm_position.point.z - reference_position.point.z) * 100, z_confidence);
227 object.position.z_coordinate_is_present = true;
228 }
229}
230
242inline void setVelocityComponent(VelocityComponent& velocity, const int16_t value,
243 const uint8_t confidence = SpeedConfidence::UNAVAILABLE) {
244 // limit value range
245 if (value < VelocityComponentValue::NEGATIVE_OUT_OF_RANGE) {
246 velocity.value.value = VelocityComponentValue::NEGATIVE_OUT_OF_RANGE;
247 } else if (value > VelocityComponentValue::POSITIVE_OUT_OF_RANGE) {
248 velocity.value.value = VelocityComponentValue::POSITIVE_OUT_OF_RANGE;
249 } else {
250 velocity.value.value = value;
252
253 // limit confidence range
254 if (confidence > SpeedConfidence::MAX || confidence < SpeedConfidence::MIN) {
255 velocity.confidence.value = SpeedConfidence::OUT_OF_RANGE;
256 } else {
257 velocity.confidence.value = confidence;
258 }
259}
260
273inline void setVelocityOfPerceivedObject(PerceivedObject& object, const gm::Vector3& cartesian_velocity,
274 const uint8_t x_confidence = SpeedConfidence::UNAVAILABLE,
275 const uint8_t y_confidence = SpeedConfidence::UNAVAILABLE,
276 const uint8_t z_confidence = SpeedConfidence::UNAVAILABLE) {
277 object.velocity.choice = Velocity3dWithConfidence::CHOICE_CARTESIAN_VELOCITY;
278 setVelocityComponent(object.velocity.cartesian_velocity.x_velocity, cartesian_velocity.x * 100, x_confidence * 100);
279 setVelocityComponent(object.velocity.cartesian_velocity.y_velocity, cartesian_velocity.y * 100, y_confidence * 100);
280 if (cartesian_velocity.z != 0.0) {
281 setVelocityComponent(object.velocity.cartesian_velocity.z_velocity, cartesian_velocity.z * 100, z_confidence * 100);
282 object.velocity.cartesian_velocity.z_velocity_is_present = true;
283 }
284 object.velocity_is_present = true;
285}
286
298inline void setAccelerationComponent(AccelerationComponent& acceleration, const int16_t value,
299 const uint8_t confidence = AccelerationConfidence::UNAVAILABLE) {
300 // limit value range
301 if (value < AccelerationValue::NEGATIVE_OUT_OF_RANGE) {
302 acceleration.value.value = AccelerationValue::NEGATIVE_OUT_OF_RANGE;
303 } else if (value > AccelerationValue::POSITIVE_OUT_OF_RANGE) {
304 acceleration.value.value = AccelerationValue::POSITIVE_OUT_OF_RANGE;
305 } else {
306 acceleration.value.value = value;
307 }
308
309 // limit confidence range
310 if (confidence > AccelerationConfidence::MAX || confidence < AccelerationConfidence::MIN) {
311 acceleration.confidence.value = AccelerationConfidence::OUT_OF_RANGE;
312 } else {
313 acceleration.confidence.value = confidence;
314 }
315}
316
329inline void setAccelerationOfPerceivedObject(PerceivedObject& object, const gm::Vector3& cartesian_acceleration,
330 const uint8_t x_confidence = AccelerationConfidence::UNAVAILABLE,
331 const uint8_t y_confidence = AccelerationConfidence::UNAVAILABLE,
332 const uint8_t z_confidence = AccelerationConfidence::UNAVAILABLE) {
333 object.acceleration.choice = Acceleration3dWithConfidence::CHOICE_CARTESIAN_ACCELERATION;
334 setAccelerationComponent(object.acceleration.cartesian_acceleration.x_acceleration, cartesian_acceleration.x * 10,
335 x_confidence * 10);
336 setAccelerationComponent(object.acceleration.cartesian_acceleration.y_acceleration, cartesian_acceleration.y * 10,
337 y_confidence * 10);
338 if (cartesian_acceleration.z != 0.0) {
339 setAccelerationComponent(object.acceleration.cartesian_acceleration.z_acceleration, cartesian_acceleration.z * 10,
340 z_confidence * 10);
341 object.acceleration.cartesian_acceleration.z_acceleration_is_present = true;
342 }
343 object.acceleration_is_present = true;
344}
356inline void setYawOfPerceivedObject(PerceivedObject& object, const double yaw,
357 const uint8_t confidence = AngleConfidence::UNAVAILABLE) {
358 // wrap angle to range [0, 360]
359 double yaw_in_degrees = yaw * 180 / M_PI + 180; // TODO: check if this is correct
360 while (yaw_in_degrees > 360.0) yaw_in_degrees -= 360.0;
361 while (yaw_in_degrees < 0) yaw_in_degrees += 360.0;
362 object.angles.z_angle.value.value = yaw_in_degrees * 10;
363
364 if (confidence > AngleConfidence::MAX || confidence < AngleConfidence::MIN) {
365 object.angles.z_angle.confidence.value = AngleConfidence::OUT_OF_RANGE;
366 } else {
367 object.angles.z_angle.confidence.value = confidence;
368 }
369 object.angles_is_present = true;
370}
371
383inline void setYawRateOfPerceivedObject(PerceivedObject& object, const double yaw_rate,
384 const uint8_t confidence = AngularSpeedConfidence::UNAVAILABLE) {
385 int16_t yaw_rate_in_degrees = yaw_rate * 180 / M_PI;
386 if (yaw_rate_in_degrees != CartesianAngularVelocityComponentValue::UNAVAILABLE) {
387 // limit value range
388 if (yaw_rate_in_degrees < CartesianAngularVelocityComponentValue::NEGATIVE_OUTOF_RANGE) {
389 yaw_rate_in_degrees = CartesianAngularVelocityComponentValue::NEGATIVE_OUTOF_RANGE;
390 } else if (yaw_rate_in_degrees > CartesianAngularVelocityComponentValue::POSITIVE_OUT_OF_RANGE) {
391 yaw_rate_in_degrees = CartesianAngularVelocityComponentValue::POSITIVE_OUT_OF_RANGE;
392 }
393 }
394 object.z_angular_velocity.value.value = yaw_rate_in_degrees;
395 object.z_angular_velocity.confidence.value = confidence;
396 object.z_angular_velocity_is_present = true;
397}
398
413inline void setObjectDimension(ObjectDimension& dimension, const uint16_t value,
414 const uint8_t confidence = ObjectDimensionConfidence::UNAVAILABLE) {
415 // limit value range
416 if (value < ObjectDimensionValue::MIN || value > ObjectDimensionValue::MAX) {
417 dimension.value.value = ObjectDimensionValue::OUT_OF_RANGE;
418 } else {
419 dimension.value.value = value;
420 }
421
422 // limit confidence range
423 if (confidence > ObjectDimensionConfidence::MAX || confidence < ObjectDimensionConfidence::MIN) {
424 dimension.confidence.value = ObjectDimensionConfidence::OUT_OF_RANGE;
425 } else {
426 dimension.confidence.value = confidence;
427 }
428}
429
440inline void setXDimensionOfPerceivedObject(PerceivedObject& object, const double value,
441 const uint8_t confidence = ObjectDimensionConfidence::UNAVAILABLE) {
442 setObjectDimension(object.object_dimension_x, (uint16_t)(value * 10), confidence * 10);
443 object.object_dimension_x_is_present = true;
444}
445
456inline void setYDimensionOfPerceivedObject(PerceivedObject& object, const double value,
457 const uint8_t confidence = ObjectDimensionConfidence::UNAVAILABLE) {
458 setObjectDimension(object.object_dimension_y, (uint16_t)(value * 10), confidence * 10);
459 object.object_dimension_y_is_present = true;
460}
461
472inline void setZDimensionOfPerceivedObject(PerceivedObject& object, const double value,
473 const uint8_t confidence = ObjectDimensionConfidence::UNAVAILABLE) {
474 setObjectDimension(object.object_dimension_z, (uint16_t)(value * 10), confidence * 10);
475 object.object_dimension_z_is_present = true;
476}
477
489inline void setDimensionsOfPerceivedObject(PerceivedObject& object, const gm::Vector3& dimensions,
490 const uint8_t x_confidence = ObjectDimensionConfidence::UNAVAILABLE,
491 const uint8_t y_confidence = ObjectDimensionConfidence::UNAVAILABLE,
492 const uint8_t z_confidence = ObjectDimensionConfidence::UNAVAILABLE) {
493 setXDimensionOfPerceivedObject(object, dimensions.x, x_confidence);
494 setYDimensionOfPerceivedObject(object, dimensions.y, y_confidence);
495 setZDimensionOfPerceivedObject(object, dimensions.z, z_confidence);
496}
497
507inline void initPerceivedObject(PerceivedObject& object, const gm::Point& point, const int16_t delta_time = 0) {
508 setPositionOfPerceivedObject(object, point);
509 setMeasurementDeltaTimeOfPerceivedObject(object, delta_time);
510}
511
524inline void initPerceivedObjectWithUTMPosition(CollectivePerceptionMessage& cpm, PerceivedObject& object,
525 const gm::PointStamped& point, const int16_t delta_time = 0) {
526 setUTMPositionOfPerceivedObject(cpm, object, point);
527 setMeasurementDeltaTimeOfPerceivedObject(object, delta_time);
528}
529
539inline void initPerceivedObjectContainer(WrappedCpmContainer& container, const uint8_t n_objects = 0) {
540 container.container_id.value = CpmContainerId::PERCEIVED_OBJECT_CONTAINER;
541 container.container_data.choice = container.container_id;
542 container.container_data.perceived_object_container.number_of_perceived_objects.value = n_objects;
543}
544
558inline void addPerceivedObjectToContainer(WrappedCpmContainer& container, const PerceivedObject& perceived_object) {
559 if (container.container_id.value == CpmContainerId::PERCEIVED_OBJECT_CONTAINER &&
560 container.container_data.choice.value == CpmContainerId::PERCEIVED_OBJECT_CONTAINER) {
561 container.container_data.perceived_object_container.perceived_objects.array.push_back(perceived_object);
562 container.container_data.perceived_object_container.number_of_perceived_objects.value =
563 container.container_data.perceived_object_container.perceived_objects.array.size();
564 } else {
565 throw std::invalid_argument("Container is not a PerceivedObjectContainer");
566 }
567}
568
581inline void addContainerToCPM(CollectivePerceptionMessage& cpm, const WrappedCpmContainer& container) {
582 // check for maximum number of containers
583 if (cpm.payload.cpm_containers.value.array.size() < WrappedCpmContainers::MAX_SIZE) {
584 cpm.payload.cpm_containers.value.array.push_back(container);
585 } else {
586 throw std::invalid_argument("Maximum number of CPM-Containers reached");
587 }
588}
589
590} // namespace etsi_its_cpm_ts_msgs::access
void setTimestampITS(TimestampIts &timestamp_its, const uint64_t unix_nanosecs, const uint16_t n_leap_seconds=etsi_its_msgs::LEAP_SECOND_INSERTIONS_SINCE_2004.end() ->second)
Set the TimestampITS object.
Definition cam_setters.h:83
void setItsPduHeader(ItsPduHeader &header, const uint8_t message_id, const uint32_t station_id, const uint8_t protocol_version=0)
Set the Its Pdu Header object.
Definition cam_setters.h:66
void setReferencePosition(T &ref_position, const double latitude, const double longitude, const double altitude=AltitudeValue::UNAVAILABLE)
Sets the reference position in the given ReferencePostion object.
void setFromUTMPosition(T &reference_position, const gm::PointStamped &utm_position, const int zone, const bool northp)
Set the ReferencePosition from a given UTM-Position.
Setter functions for the ETSI ITS Common Data Dictionary (CDD) v2.1.1.
File containing constants that are used in the context of ETIS ITS Messages.
void setCartesianCoordinateWithConfidence(CartesianCoordinateWithConfidence &coordinate, const int32_t value, const uint16_t confidence=CoordinateConfidence::UNAVAILABLE)
Sets the value and confidence of a CartesianCoordinateWithConfidence object.
void setDimensionsOfPerceivedObject(PerceivedObject &object, const gm::Vector3 &dimensions, const uint8_t x_confidence=ObjectDimensionConfidence::UNAVAILABLE, const uint8_t y_confidence=ObjectDimensionConfidence::UNAVAILABLE, const uint8_t z_confidence=ObjectDimensionConfidence::UNAVAILABLE)
Sets all dimensions of a perceived object.
void setVelocityComponent(VelocityComponent &velocity, const int16_t value, const uint8_t confidence=SpeedConfidence::UNAVAILABLE)
Sets the value and confidence of a VelocityComponent.
void initPerceivedObjectContainer(WrappedCpmContainer &container, const uint8_t n_objects=0)
Initializes a WrappedCpmContainer as a PerceivedObjectContainer with the given number of objects.
void setYDimensionOfPerceivedObject(PerceivedObject &object, const double value, const uint8_t confidence=ObjectDimensionConfidence::UNAVAILABLE)
Sets the y-dimension of a perceived object.
void addPerceivedObjectToContainer(WrappedCpmContainer &container, const PerceivedObject &perceived_object)
Adds a PerceivedObject to the PerceivedObjectContainer / WrappedCpmContainer.
void setObjectDimension(ObjectDimension &dimension, const uint16_t value, const uint8_t confidence=ObjectDimensionConfidence::UNAVAILABLE)
Sets the object dimension with the given value and confidence.
void addContainerToCPM(CollectivePerceptionMessage &cpm, const WrappedCpmContainer &container)
Adds a container to the Collective Perception Message (CPM).
void setIdOfPerceivedObject(PerceivedObject &object, const uint16_t id)
Set the ID of a PerceivedObject.
void setReferenceTime(CollectivePerceptionMessage &cpm, const uint64_t unix_nanosecs, const uint16_t n_leap_seconds=etsi_its_msgs::LEAP_SECOND_INSERTIONS_SINCE_2004.end() ->second)
Sets the reference time in a CPM.
void setYawOfPerceivedObject(PerceivedObject &object, const double yaw, const uint8_t confidence=AngleConfidence::UNAVAILABLE)
Sets the yaw angle of a perceived object.
void setXDimensionOfPerceivedObject(PerceivedObject &object, const double value, const uint8_t confidence=ObjectDimensionConfidence::UNAVAILABLE)
Sets the x-dimension of a perceived object.
void setMeasurementDeltaTimeOfPerceivedObject(PerceivedObject &object, const int16_t delta_time=0)
Sets the measurement delta time of a PerceivedObject.
void setVelocityOfPerceivedObject(PerceivedObject &object, const gm::Vector3 &cartesian_velocity, const uint8_t x_confidence=SpeedConfidence::UNAVAILABLE, const uint8_t y_confidence=SpeedConfidence::UNAVAILABLE, const uint8_t z_confidence=SpeedConfidence::UNAVAILABLE)
void setAccelerationOfPerceivedObject(PerceivedObject &object, const gm::Vector3 &cartesian_acceleration, const uint8_t x_confidence=AccelerationConfidence::UNAVAILABLE, const uint8_t y_confidence=AccelerationConfidence::UNAVAILABLE, const uint8_t z_confidence=AccelerationConfidence::UNAVAILABLE)
Sets the acceleration of a perceived object.
void setPositionOfPerceivedObject(PerceivedObject &object, const gm::Point &point, const uint16_t x_confidence=CoordinateConfidence::UNAVAILABLE, const uint16_t y_confidence=CoordinateConfidence::UNAVAILABLE, const uint16_t z_confidence=CoordinateConfidence::UNAVAILABLE)
Sets the position of a perceived object (relative to the CPM's reference position).
void setZDimensionOfPerceivedObject(PerceivedObject &object, const double value, const uint8_t confidence=ObjectDimensionConfidence::UNAVAILABLE)
Sets the z-dimension of a perceived object.
void setAccelerationComponent(AccelerationComponent &acceleration, const int16_t value, const uint8_t confidence=AccelerationConfidence::UNAVAILABLE)
Sets the value and confidence of a AccelerationComponent.