Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
bsm_generator_worker.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2022 LEIDOS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
18#include <random>
19
20namespace bsm_generator
21{
22
24
26 {
27 uint8_t old_msg_count = msg_count_;
28 msg_count_++;
29 if(msg_count_ == 128)
30 {
31 msg_count_ = 0;
32 }
33 return old_msg_count;
34 }
35
36 std::vector<uint8_t> BSMGeneratorWorker::getMsgId(const rclcpp::Time now, double secs)
37 {
38 // need to change ID every designated period
39 rclcpp::Duration id_timeout(secs * 1e9);
40
41 generator_.seed(std::random_device{}()); // guarantee randomness
42 std::uniform_int_distribution<int> dis(0,INT_MAX);
43
44 std::vector<uint8_t> id(4);
45
46 if (first_msg_id_) {
48 first_msg_id_ = false;
50 }
51
52 if(now - last_id_generation_time_ >= id_timeout)
53 {
55 RCLCPP_DEBUG_STREAM(rclcpp::get_logger("bsm_generator"), "Newly generated random id: " << random_id_);
57 }
58
59 for(int i = 0; i < id.size(); ++i)
60 {
61 id[i] = random_id_ >> (8 * i);
62 }
63 return id;
64 }
65
66 uint16_t BSMGeneratorWorker::getSecMark(const rclcpp::Time now)
67 {
68 return static_cast<uint16_t>((now.nanoseconds() / 1000000) % 60000);
69 }
70
71 float BSMGeneratorWorker::getSpeedInRange(const double speed)
72 {
73 return static_cast<float>(std::max(std::min(speed, 163.8), 0.0));
74 }
75
77 {
78 return static_cast<float>(std::max(std::min(angle * 57.2958, 189.0), -189.0));
79 }
80
82 {
83 return std::max(std::min(accel, 20.0f), -20.0f);
84 }
85
86 float BSMGeneratorWorker::getYawRateInRange(const double yaw_rate)
87 {
88 return static_cast<float>(std::max(std::min(yaw_rate, 327.67), -327.67));
89 }
90
92 {
93 return brake >= 0.05 ? 0b1111 : 0;
94 }
95
96 float BSMGeneratorWorker::getHeadingInRange(const float heading)
97 {
98 return std::max(std::min(heading, 359.9875f), 0.0f);
99 }
100} // namespace bsm_generator
std::default_random_engine generator_
float getSteerWheelAngleInRange(const double angle)
Function to apply minimum and maximum limits to a steering wheel angle value. Minimum limit is -189....
float getYawRateInRange(const double yaw_rate)
Function to apply minimum and maximum limits to a yaw rate value. Minimum limit is -327....
float getSpeedInRange(const double speed)
Function to apply minimum and maximum limits to a speed value. Minimum limit is 0....
BSMGeneratorWorker()
Default Constructor for BSMGeneratorWorker.
float getLongAccelInRange(const float accel)
Function to apply minimum and maximum limits to a longitudinal acceleration value....
std::vector< uint8_t > getMsgId(const rclcpp::Time now, double secs)
Function to obtain the current BSM message ID. The ID is updated to a new random BSM message ID every...
uint8_t getNextMsgCount()
Function to increment the BSM message counter and obtain the new counter value. Counter restarts at 0...
uint8_t getBrakeAppliedStatus(const double brake)
Function to convert the current applied brake status to a value used within the BSM message.
uint16_t getSecMark(const rclcpp::Time now)
Function to obtain the 'milliseconds' mark of the provided time within the last minute.
float getHeadingInRange(const float heading)
Function to apply minimum and maximum limits to a vehicle heading value. Minimum limit is 0....