etsi_its_messages v3.4.0
Loading...
Searching...
No Matches
spatem_ts_utils.h File Reference

Utility functions for the ETSI ITS SPATEM. More...

#include <ctime>

Go to the source code of this file.

Enumerations

enum  time_mark_value_interpretation { normal , undefined , over_an_hour , leap_second }

Functions

uint64_t etsi_its_spatem_ts_msgs::access::getUnixSecondsOfYear (const uint64_t unixSecond)
 Get the unix seconds of the beginning of a year that corresponds to a given unix timestamp.
uint64_t etsi_its_spatem_ts_msgs::access::getUnixNanosecondsFromMinuteOfTheYear (const MinuteOfTheYear &moy, const uint64_t unix_nanoseconds_estimate)
 Get the unix nanoseconds from MinuteOfTheYear object.
float etsi_its_spatem_ts_msgs::access::interpretTimeIntervalConfidenceAsFloat (const uint16_t encoded_probability)
 Interprets the TimeIntervalConfidence type as a float value (see etsi definition).
std::array< float, 4 > etsi_its_spatem_ts_msgs::access::interpretMovementPhaseStateAsColor (const uint8_t value)
 Interprets the MovementPhaseState type as a color (see etsi definition).
time_mark_value_interpretation etsi_its_spatem_ts_msgs::access::interpretTimeMarkValueType (const uint16_t time)
 Interprets the type of a TimeMark message See etsi ASNI1 - IS TS 103 301 documentation for for the encoding of "TimeMark".
int64_t etsi_its_spatem_ts_msgs::access::interpretTimeMarkDeltaTimeAsNanoSeconds (const uint16_t time, const uint64_t nanosec)
 Calculates the delta between a TimeMark and a given unix timestamp in nanoseconds.
float etsi_its_spatem_ts_msgs::access::interpretTimeMarkDeltaTimeValueAsSeconds (const uint16_t time, const int32_t seconds, const uint32_t nanosec)
 Calculates the delta between a TimeMark and a given timestamp.
std::string etsi_its_spatem_ts_msgs::access::parseTimeMarkValueToString (const uint16_t time, const int32_t seconds, const uint32_t nanosec)
 Converts a value from message type TimeMarkValue into a string representation.

Detailed Description

Utility functions for the ETSI ITS SPATEM.

Definition in file spatem_ts_utils.h.

Enumeration Type Documentation

◆ time_mark_value_interpretation

enum etsi_its_spatem_ts_msgs::access::time_mark_value_interpretation

Definition at line 29 of file spatem_ts_utils.h.

29{ normal, undefined, over_an_hour, leap_second };

Function Documentation

◆ getUnixNanosecondsFromMinuteOfTheYear()

uint64_t etsi_its_spatem_ts_msgs::access::getUnixNanosecondsFromMinuteOfTheYear ( const MinuteOfTheYear & moy,
const uint64_t unix_nanoseconds_estimate )
inline

Get the unix nanoseconds from MinuteOfTheYear object.

Parameters
moygiven MinuteOfTheYear object
unix_nanoseconds_estimateunix timestamp to derive the current year from in nanoseconds
Returns
uint64_t unix timestamp according to the given MinuteOfTheYear in nanoseconds

Definition at line 61 of file spatem_ts_utils.h.

62 {
63 return ((uint64_t)(moy.value * 60) + getUnixSecondsOfYear(unix_nanoseconds_estimate * 1e-9)) * 1e9;
64}
uint64_t getUnixSecondsOfYear(const uint64_t unixSecond)
Get the unix seconds of the beginning of a year that corresponds to a given unix timestamp.

◆ getUnixSecondsOfYear()

uint64_t etsi_its_spatem_ts_msgs::access::getUnixSecondsOfYear ( const uint64_t unixSecond)
inline

Get the unix seconds of the beginning of a year that corresponds to a given unix timestamp.

Parameters
unixSecondtimestamp that defines the year for that the unix seconds for the beginning of the year should be derived
Returns
uint64_t unix seconds of the beginning of the year

Definition at line 37 of file spatem_ts_utils.h.

37 {
38 // Get current time as a time_point
39 time_t ts = static_cast<time_t>(unixSecond); // Convert uint64_t to time_t
40
41 struct tm* timeinfo;
42 timeinfo = gmtime(&ts);
43
44 // Set the timeinfo to the beginning of the year
45 timeinfo->tm_sec = 0;
46 timeinfo->tm_min = 0;
47 timeinfo->tm_hour = 0;
48 timeinfo->tm_mday = 1;
49 timeinfo->tm_mon = 0;
50
51 return timegm(timeinfo); // Convert struct tm back to Unix timestamp
52}

◆ interpretMovementPhaseStateAsColor()

std::array< float, 4 > etsi_its_spatem_ts_msgs::access::interpretMovementPhaseStateAsColor ( const uint8_t value)
inline

Interprets the MovementPhaseState type as a color (see etsi definition).

Parameters
valueEncoded color value from msg type MovementPhaseState
Returns
4-dimensional array with color values as follows: r, g, b, a, each of these values within a range between [0, 1]

Definition at line 135 of file spatem_ts_utils.h.

135 {
136 std::array<float, 4> color;
137
138 switch (value) {
139 case MovementPhaseState::UNAVAILABLE:
140 color = COLOR_GREY;
141 break;
142
143 case MovementPhaseState::DARK:
144 color = COLOR_GREY;
145 break;
146 case MovementPhaseState::STOP_THEN_PROCEED:
147 color = COLOR_RED;
148 break;
149 case MovementPhaseState::STOP_AND_REMAIN:
150 color = COLOR_RED;
151 break;
152 case MovementPhaseState::PRE_MOVEMENT:
153 color = COLOR_ORANGE;
154 break;
155 case MovementPhaseState::PERMISSIVE_MOVEMENT_ALLOWED:
156 color = COLOR_GREEN;
157 break;
158 case MovementPhaseState::PROTECTED_MOVEMENT_ALLOWED:
159 color = COLOR_GREEN;
160 break;
161 case MovementPhaseState::PERMISSIVE_CLEARANCE:
162 color = COLOR_ORANGE;
163 break;
164 case MovementPhaseState::PROTECTED_CLEARANCE:
165 color = COLOR_ORANGE;
166 break;
167 case MovementPhaseState::CAUTION_CONFLICTING_TRAFFIC:
168 color = COLOR_ORANGE;
169 break;
170 default:
171 color = COLOR_GREY;
172 break;
173 }
174
175 return color;
176}

◆ interpretTimeIntervalConfidenceAsFloat()

float etsi_its_spatem_ts_msgs::access::interpretTimeIntervalConfidenceAsFloat ( const uint16_t encoded_probability)
inline

Interprets the TimeIntervalConfidence type as a float value (see etsi definition).

Parameters
encoded_probabilityValue from msg type TimeIntervalConfidence
Returns
confidence as float value [0, 1]

Definition at line 72 of file spatem_ts_utils.h.

72 {
73 float probability = 0;
74
75 switch (encoded_probability) {
76 case 0:
77 probability = 0.21;
78 break;
79 case 1:
80 probability = 0.36;
81 break;
82 case 2:
83 probability = 0.47;
84 break;
85 case 3:
86 probability = 0.56;
87 break;
88 case 4:
89 probability = 0.62;
90 break;
91 case 5:
92 probability = 0.68;
93 break;
94 case 6:
95 probability = 0.73;
96 break;
97 case 7:
98 probability = 0.77;
99 break;
100 case 8:
101 probability = 0.81;
102 break;
103 case 9:
104 probability = 0.85;
105 break;
106 case 10:
107 probability = 0.88;
108 break;
109 case 11:
110 probability = 0.91;
111 break;
112 case 12:
113 probability = 0.94;
114 break;
115 case 13:
116 probability = 0.96;
117 break;
118 case 14:
119 probability = 0.98;
120 break;
121 case 15:
122 probability = 1.0;
123 break;
124 }
125
126 return probability;
127}

◆ interpretTimeMarkDeltaTimeAsNanoSeconds()

int64_t etsi_its_spatem_ts_msgs::access::interpretTimeMarkDeltaTimeAsNanoSeconds ( const uint16_t time,
const uint64_t nanosec )
inline

Calculates the delta between a TimeMark and a given unix timestamp in nanoseconds.

Parameters
timeTimeMark value since the start of the last full hour in 0.1 seconds
nanosecUnix timestamp in nanoseconds
Returns
Delta time between the timestamp and the given TimeMark in nanoseconds

Definition at line 210 of file spatem_ts_utils.h.

210 {
211 // calculate elapsed nanoseconds since the start of the last full hour
212 int64_t timestamp_hour_nanosec = ((nanosec) % (HOUR_IN_SECONDS * FACTOR_SECONDS_TO_NANOSECONDS));
213 // convert TimeMark value to nanoseconds
214 int64_t timemark_in_nanosec =
215 static_cast<int64_t>(time) * FACTOR_ETSI_TIMEMARK_TO_SECONDS * FACTOR_SECONDS_TO_NANOSECONDS;
216 // calculate relative time until the next change occurs
217 int64_t rel_time_until_change = timemark_in_nanosec - timestamp_hour_nanosec;
218
219 // adjust relative time if a jump to the next hour occurs (relative time should be inside the interval [0, 3600e9) )
220 if (rel_time_until_change < 0) {
221 rel_time_until_change += HOUR_IN_SECONDS * FACTOR_SECONDS_TO_NANOSECONDS;
222 }
223
224 return rel_time_until_change;
225}

◆ interpretTimeMarkDeltaTimeValueAsSeconds()

float etsi_its_spatem_ts_msgs::access::interpretTimeMarkDeltaTimeValueAsSeconds ( const uint16_t time,
const int32_t seconds,
const uint32_t nanosec )
inline

Calculates the delta between a TimeMark and a given timestamp.

Parameters
timeTimeMark value since the start of the last full hour in 0.1 seconds
secondsseconds of current timestamp
nanosecnanoseconds of current timestamp [0, 999999999]
Returns
Delta time between the timestamp and the given TimeMark in seconds

Definition at line 235 of file spatem_ts_utils.h.

236 {
237 uint64_t timestamp_in_nanoseconds = seconds * FACTOR_SECONDS_TO_NANOSECONDS + nanosec;
238 return interpretTimeMarkDeltaTimeAsNanoSeconds(time, timestamp_in_nanoseconds) /
239 static_cast<float>(FACTOR_SECONDS_TO_NANOSECONDS);
240}
int64_t interpretTimeMarkDeltaTimeAsNanoSeconds(const uint16_t time, const uint64_t nanosec)
Calculates the delta between a TimeMark and a given unix timestamp in nanoseconds.

◆ interpretTimeMarkValueType()

time_mark_value_interpretation etsi_its_spatem_ts_msgs::access::interpretTimeMarkValueType ( const uint16_t time)
inline

Interprets the type of a TimeMark message See etsi ASNI1 - IS TS 103 301 documentation for for the encoding of "TimeMark".

Parameters
timeThe value inside the TimeMark message
Returns
Type as time_mark_value_interpretation

Definition at line 184 of file spatem_ts_utils.h.

184 {
185 time_mark_value_interpretation type;
186
187 if (time == 36001) {
188 // value is undefined or unknown
189 type = time_mark_value_interpretation::undefined;
190 } else if (time == 36000) {
191 // used to indicate time >3600 seconds
192 type = time_mark_value_interpretation::over_an_hour;
193 } else if (time >= 35991 && time <= 35999) {
194 // leap second
195 type = time_mark_value_interpretation::leap_second;
196 } else { // time >= 0 && time <= 36000
197 type = time_mark_value_interpretation::normal;
198 }
199
200 return type;
201}

◆ parseTimeMarkValueToString()

std::string etsi_its_spatem_ts_msgs::access::parseTimeMarkValueToString ( const uint16_t time,
const int32_t seconds,
const uint32_t nanosec )
inline

Converts a value from message type TimeMarkValue into a string representation.

Parameters
timeTime in 0.1 seconds until the next change occours in the future, counting from the last started hour
secondsseconds of current timestamp
nanosecnanoseconds of current timestamp [0, 999999999]
Returns
Decoded String representation of the encoded time

Definition at line 250 of file spatem_ts_utils.h.

250 {
251 time_mark_value_interpretation time_type = interpretTimeMarkValueType(time);
252
253 std::string text_content;
254
255 switch (time_type) {
256 case time_mark_value_interpretation::undefined:
257 text_content = "undefined";
258 break;
259 case time_mark_value_interpretation::over_an_hour:
260 text_content = ">36000s";
261 break;
262 case time_mark_value_interpretation::leap_second:
263 text_content = "leap second";
264 break;
265 case time_mark_value_interpretation::normal:
266 float rel_time_until_change = interpretTimeMarkDeltaTimeValueAsSeconds(time, seconds, nanosec);
267
268 // set displayed precision to 0.1
269 std::stringstream ss;
270 ss << std::fixed << std::setprecision(1) << rel_time_until_change << "s";
271 text_content = ss.str();
272 break;
273 }
274
275 return text_content;
276}
float interpretTimeMarkDeltaTimeValueAsSeconds(const uint16_t time, const int32_t seconds, const uint32_t nanosec)
Calculates the delta between a TimeMark and a given timestamp.
time_mark_value_interpretation interpretTimeMarkValueType(const uint16_t time)
Interprets the type of a TimeMark message See etsi ASNI1 - IS TS 103 301 documentation for for the en...