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.
MockDriverNode.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020-2021 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#pragma once
17
18#include <ros/ros.h>
19#include <carma_utils/CARMAUtils.h>
20#include "comm_types.h"
21#include "ROSComms.h"
22#include <vector>
23
24namespace mock_drivers
25{
28{
29 ros::Publisher pub;
30 std::string base_topic_name;
31};
32
35{
36private:
37 boost::shared_ptr<ros::CARMANodeHandle> cnh_;
38 std::vector<PublisherWapper> publishers_;
39 std::vector<ros::Subscriber> subscribers_;
40 std::vector<ros::ServiceServer> services_;
41
42 // used for testing
43 bool dummy_ = false; // if the mock driver node is a dummy then it wont initialize a CARMANodeHandle and its functions wont
44 // do anything
45 std::vector<std::string> topics_;
46 std::vector<ros::Time> time_stamps_;
47
48public:
49
55 std::string getGraphName() const;
56
58 template <typename T>
59 void addPub(T comm)
60 {
61 if (!dummy_)
62 {
64 pub.pub = cnh_->advertise<decltype(comm->getTemplateType())>(comm->getTopic(), comm->getQueueSize());
65 pub.base_topic_name = comm->getTopic();
66 publishers_.push_back(pub);
67 }
68 }
69
71 template <typename T>
72 void addSub(T comm)
73 {
74 if (!dummy_)
75 {
76 subscribers_.push_back(cnh_->subscribe<decltype(comm->getTemplateType())>(
77 comm->getTopic(), comm->getQueueSize(), &ROSComms<decltype(comm->getTemplateType())>::callback, comm));
78 }
79 }
80
82 template <typename T>
83 void addSrv(T comm)
84 {
85 if (!dummy_)
86 {
87 services_.push_back(cnh_->advertiseService(
88 comm->getTopic(), &ROSComms<decltype(comm->getReqType()), decltype(comm->getResType())>::callback, comm));
89 }
90 }
91
93 void spin(double rate) const;
94
96 void setSpinCallback(std::function<bool()> cb) const;
97
99 void init();
100
107 template <typename T>
108 void publishData(std::string topic, T msg)
109 {
110 if (!dummy_)
111 {
112 auto pub = std::find_if(publishers_.begin(), publishers_.end(),
113 [&](PublisherWapper p) { return p.base_topic_name == topic; });
114 if (pub == publishers_.end())
115 throw std::invalid_argument("Attempted to publish to topic " + topic + " but no publisher was found");
116 pub->pub.publish(msg);
117 }
118 else
119 {
120 topics_.push_back(topic);
121 time_stamps_.push_back(msg.header.stamp);
122 }
123 };
124
131 template <typename T> // TODO remove
132 void publishDataNoHeader(std::string topic, T msg)
133 {
134 if (!dummy_)
135 {
136 auto pub = std::find_if(publishers_.begin(), publishers_.end(),
137 [&](PublisherWapper p) { return p.base_topic_name == topic; });
138 if (pub == publishers_.end())
139 throw std::invalid_argument("Attempted to publish to topic " + topic + " but no publisher was found");
140 pub->pub.publish(msg);
141 }
142 else
143 {
144 topics_.push_back(topic);
145 }
146 };
147
149 MockDriverNode(bool dummy);
150
153 std::vector<std::string> getTopics()
154 {
155 return topics_;
156 }
159 std::vector<ros::Time> getTimeStamps()
160 {
161 return time_stamps_;
162 }
164 bool isDummy()
165 {
166 return dummy_;
167 }
168};
169
170} // namespace mock_drivers
Node class which connects to the ROS network.
std::vector< ros::Time > getTimeStamps()
Returns a vector of all the time stamps of the data that would be published (only when it is a dummy ...
void spin(double rate) const
Begin the ros node.
std::string getGraphName() const
Returns the fully qualified name of this node in the ROS network graph.
std::vector< ros::Time > time_stamps_
void addSub(T comm)
Function to add a subscriber from a ROSComms object.
std::vector< std::string > getTopics()
Returns a vector of all the topics that the node would publish to (only when it is a dummy node)....
std::vector< ros::Subscriber > subscribers_
void publishDataNoHeader(std::string topic, T msg)
Publish data with no header on a desired topic.
void setSpinCallback(std::function< bool()> cb) const
Set the spin callback for the ros node.
void init()
Initialize the CARMA Node Handle pointer for the MockDriverNode (must be called before spin)
void addSrv(T comm)
Function to add a service from a ROSComms object.
std::vector< ros::ServiceServer > services_
std::vector< PublisherWapper > publishers_
std::vector< std::string > topics_
void publishData(std::string topic, T msg)
Publish data on a desired topic.
void addPub(T comm)
Function to add a publisher from a ROSComms object.
boost::shared_ptr< ros::CARMANodeHandle > cnh_
bool isDummy()
Returns if the node is a dummy node. Used for testing.
This class is used to transfer all the information required to initialize ros topics/services.
Definition: ROSComms.h:34
bool callback(cav_srvs::PlanTrajectory::Request &req, cav_srvs::PlanTrajectory::Response &res)
Definition: helper.cpp:21
Wrapper for publishers which allow their base topic name to be extracted.