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.
MockLightBarDriver.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-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
17
18
19#include <ros/ros.h>
20#include <ros/node_handle.h>
21#include <iostream>
22#include <ros/service_client.h>
23#include <ros/service_server.h>
25#include <cav_srvs/GetLights.h>
26#include <cav_srvs/GetLightsRequest.h>
27#include <cav_srvs/GetLightsResponse.h>
28#include <cav_srvs/SetLights.h>
29#include <cav_srvs/SetLightsRequest.h>
30#include <cav_srvs/SetLightsResponse.h>
31#include <cav_msgs/LightBarStatus.h>
32using namespace std;
33
34namespace mock_drivers{
35 MockLightBarDriver::MockLightBarDriver(ros::CARMANodeHandle node) {
36 //lightbar topics and services
37 lbStatusTopic = "lightbar/light_bar_status";
38 lbGetLightService = "lightbar/get_lights";
39 lbSetLightService = "lightbar/set_lights";
40 //lightbar states
41 greenFlash = false;
42 yellowFlash = false;
43 leftArrow = false;
44 rightArrow = false;
45 sidesSolid = false;
46 greenSolid = false;
47 yellowDim = false;
48 takedown = false;
49 //topics published
50 lbPub = node.advertise<cav_msgs::LightBarStatus>(lbStatusTopic, 100);
51 discoveryPub = node.advertise<cav_msgs::DriverStatus>("driver_discovery",100);
52 //service server
53 getApiService = node.advertiseService("mock_lightbar/get_driver_api",&MockLightBarDriver::getApiService_cb, this);
54 bindService = node.advertiseService("mock_lightbar/bind",&MockLightBarDriver::bind_cb, this);
55 getDriverStatusService = node.advertiseService("mock_lightbar/get_status",&MockLightBarDriver::getDriverStatus_cb,this);
58 //publish driver status
59 driverStatus.status = cav_msgs::DriverStatus::OPERATIONAL;
61 }
62
63 //get driver status call back
64 bool MockLightBarDriver::getDriverStatus_cb(cav_srvs::GetDriverStatusRequest& req, cav_srvs::GetDriverStatusResponse& resp){
65 resp.status = getDriverStatus();
66 return true;
67 }
68
69 //bind service call back
70 bool MockLightBarDriver::bind_cb(cav_srvs::BindRequest& req, cav_srvs::BindResponse& resp){
71 ROS_INFO_STREAM("bind received");
72 return true;
73 }
74
75 //get api service call back
76 bool MockLightBarDriver::getApiService_cb(cav_srvs::GetDriverApiRequest& req, cav_srvs::GetDriverApiResponse& resp){
77 resp.api_list = getDriverApi();
78 return true;
79 }
80
81 //set light service call back
82 bool MockLightBarDriver::setLightService_cb(cav_srvs::SetLightsRequest& req, cav_srvs::SetLightsResponse& resp){
83 cav_msgs::LightBarStatus lightstatus = req.set_state;
84 greenFlash = lightstatus.green_flash == 1;
85 yellowFlash = lightstatus.flash == 1;
86 leftArrow = lightstatus.left_arrow == 1;
87 rightArrow = lightstatus.right_arrow== 1;
88 sidesSolid = lightstatus.sides_solid == 1;
89 greenSolid = lightstatus.green_solid == 1;
90 yellowDim = lightstatus.yellow_solid == 1;
91 takedown = lightstatus.takedown == 1;
92 return true;
93 }
94
95 //get light service call back
96 bool MockLightBarDriver::getLightService_cb(cav_srvs::GetLightsRequest& req, cav_srvs::GetLightsResponse& resp){
97 resp.status = getLightBarStatus();
98 return true;
99 }
100
105 cav_msgs::LightBarStatus MockLightBarDriver::getLightBarStatus(){
106 cav_msgs::LightBarStatus status;
107 status.green_solid = greenSolid? 1:0;
108 status.yellow_solid = yellowDim? 1:0;
109 status.right_arrow = rightArrow? 1:0;
110 status.left_arrow = leftArrow? 1:0;
111 status.sides_solid = sidesSolid? 1:0;
112 status.flash = yellowFlash? 1:0;
113 status.green_flash = greenFlash? 1:0;
114 status.takedown = takedown? 1:0;
115 return status;
116
117 }
118
119 cav_msgs::DriverStatus MockLightBarDriver::getDriverStatus(){
120 cav_msgs::DriverStatus status;
121 status.name = getNodeName();
122 status.status = driverStatus.status;
123 status.can = false;
124 status.radar = false;
125 status.gnss = false;
126 status.imu = false;
127 status.lidar = false;
128 status.roadway_sensor = false;
129 status.comms = false;
130 status.controller = false;
131 status.camera = false;
132 status.lightbar = true;
133 return status;
134
135 }
136
138 vector<string> api;
139 api.push_back(lbStatusTopic);
140 api.push_back(lbGetLightService);
141 api.push_back(lbSetLightService);
142 return api;
143
144 }
145
147 cav_msgs::LightBarStatus status = getLightBarStatus();
148 lbPub.publish(status);
149 }
150
152 discoveryPub.publish(getDriverStatus());
153 }
154
156 return "MockLightBarDriver";
157 }
158
160 vector<string> list;
161 list.push_back("lightbar");
162 return list;
163 }
164
165
166}
bool getApiService_cb(cav_srvs::GetDriverApiRequest &req, cav_srvs::GetDriverApiResponse &resp)
cav_msgs::LightBarStatus getLightBarStatus()
MockLightBarDriver(ros::CARMANodeHandle node)
bool setLightService_cb(cav_srvs::SetLightsRequest &req, cav_srvs::SetLightsResponse &resp)
cav_msgs::DriverStatus getDriverStatus()
bool getLightService_cb(cav_srvs::GetLightsRequest &req, cav_srvs::GetLightsResponse &resp)
bool getDriverStatus_cb(cav_srvs::GetDriverStatusRequest &req, cav_srvs::GetDriverStatusResponse &resp)
bool bind_cb(cav_srvs::BindRequest &req, cav_srvs::BindResponse &resp)