17 #define _USE_MATH_DEFINES
19 #include <tf2/LinearMath/Quaternion.h>
20 #include <tf2/LinearMath/Matrix3x3.h>
21 #include <geometry_msgs/Pose.h>
24 #include <libsumo/libsumo.h>
26 #include <std_msgs/Float64.h>
27 #include <adore_if_ros_msg/TrafficParticipantSetSimulation.h>
28 #include <adore_if_ros_msg/TrafficParticipantSimulation.h>
29 #include <ros/console.h>
32 #include <unordered_set>
33 #include <unordered_map>
46 void receive(
const std_msgs::Float64ConstPtr& msg)
55 std::unordered_map<int, adore_if_ros_msg::TrafficParticipantSimulation>
data_;
58 void receive(adore_if_ros_msg::TrafficParticipantSimulationConstPtr msg)
61 if (
data_.find(msg->simulationID) ==
data_.end())
63 data_.emplace(msg->simulationID, *msg);
67 data_[msg->simulationID] = *msg;
144 libsumo::Vehicle::remove(
id);
148 std::cout <<
"Error: removal of sumo vehicle failed" << std::endl;
159 std::cout <<
"Error: adding a sumo vehicle failed" << std::endl;
166 libsumo::Vehicle::setMaxSpeed(
id, val);
170 std::cout <<
"Error: setMaxSpeed for sumo vehicle failed" << std::endl;
177 libsumo::Vehicle::setSpeed(
id, val);
181 std::cout <<
"Error: setSpeed for sumo vehicle failed" << std::endl;
188 libsumo::Vehicle::moveToXY(
id,
z, a,
x,
y,
heading, b);
192 std::cout <<
"Error: moveToXY for sumo vehicle failed" << std::endl;
213 double tSUMO_new = libsumo::Simulation::getTime();
215 if (tSUMO_new <=
tSUMO)
236 for (
auto&& mapem_item : v2x_mapem)
240 for (
auto&& spat : spatems)
249 adore_if_ros_msg::TrafficParticipantSetSimulation tpset;
250 tpset.simulator =
"SUMO";
266 intid = idtranslation->second;
275 libsumo::TraCIPosition tracipos = libsumo::Vehicle::getPosition(
id);
276 double heading =
M_PI * 0.5 - libsumo::Vehicle::getAngle(
id) / 180.0 *
M_PI;
277 double v = libsumo::Vehicle::getSpeed(
id);
278 std::string type = libsumo::Vehicle::getTypeID(
id);
279 double L = libsumo::Vehicle::getLength(
id);
280 double w = libsumo::Vehicle::getWidth(
id);
281 double H = libsumo::Vehicle::getHeight(
id);
282 int signals = libsumo::Vehicle::getSignals(
id);
286 adore_if_ros_msg::TrafficParticipantSimulation tp;
287 tp.simulationID = intid;
288 tp.data.v2xStationID = intid;
290 tp.data.classification.type_id =
291 adore_if_ros_msg::TrafficClassification::CAR;
292 tp.data.shape.type = 1;
293 tp.data.shape.dimensions.push_back(L);
294 tp.data.shape.dimensions.push_back(
w);
295 tp.data.shape.dimensions.push_back(H);
296 auto geopos = libsumo::Simulation::convertGeo(tracipos.x, tracipos.y,
false);
297 tp.data.motion_state.pose.pose.position.x = geopos.x;
298 tp.data.motion_state.pose.pose.position.y = geopos.y;
299 tp.data.motion_state.pose.pose.position.z = 0;
302 tp.data.motion_state.pose.pose.orientation.x = q.getX();
303 tp.data.motion_state.pose.pose.orientation.y = q.getY();
304 tp.data.motion_state.pose.pose.orientation.z = q.getZ();
305 tp.data.motion_state.pose.pose.orientation.w = q.getW();
306 tp.data.motion_state.twist.twist.linear.x = v;
308 tpset.data.push_back(tp);
312 std::cout <<
"Error: failed to get information with libsumo::Vehicle" << std::endl;
332 intid = idtranslation->second;
336 libsumo::TraCIPosition tracipos = libsumo::Person::getPosition(
id);
337 double heading =
M_PI * 0.5 - libsumo::Person::getAngle(
id) / 180.0 *
M_PI;
338 double v = libsumo::Person::getSpeed(
id);
339 std::string type = libsumo::Person::getTypeID(
id);
340 double L = libsumo::Person::getLength(
id);
341 double w = libsumo::Person::getLength(
id);
348 adore_if_ros_msg::TrafficParticipantSimulation tp;
349 tp.simulationID = intid;
351 tp.data.classification.type_id = adore_if_ros_msg::TrafficClassification::PEDESTRIAN;
352 tp.data.shape.type = 1;
353 tp.data.shape.dimensions.push_back(L);
354 tp.data.shape.dimensions.push_back(
w);
355 tp.data.shape.dimensions.push_back(H);
356 auto geopos = libsumo::Simulation::convertGeo(tracipos.x, tracipos.y,
false);
357 tp.data.motion_state.pose.pose.position.x = geopos.x;
358 tp.data.motion_state.pose.pose.position.y = geopos.y;
359 tp.data.motion_state.pose.pose.position.z = 0;
362 tp.data.motion_state.pose.pose.orientation.x = q.getX();
363 tp.data.motion_state.pose.pose.orientation.y = q.getY();
364 tp.data.motion_state.pose.pose.orientation.z = q.getZ();
365 tp.data.motion_state.pose.pose.orientation.w = q.getW();
366 tp.data.motion_state.twist.twist.linear.x = v;
368 tpset.data.push_back(tp);
382 auto msg = pair.second;
392 std::stringstream ss;
398 sumoid = replacement_id->second;
406 auto p = msg.data.motion_state.pose.pose;
408 q.setValue(p.orientation.x, p.orientation.y, p.orientation.z, p.orientation.w);
410 double roll, pitch,
yaw;
411 m.getRPY(roll, pitch,
yaw);
414 auto sumopos = libsumo::Simulation::convertGeo(p.position.x, p.position.y,
true);
418 setSpeed(sumoid, msg.data.motion_state.twist.twist.linear.x);
424 ros::init(argc, argv, nodename);
425 nh_ =
new ros::NodeHandle();
432 double step_length = 0.05;
433 std::string cfg_file;
434 nh_->getParam(
"/sumo/port", port);
435 nh_->getParam(
"/sumo/cfg_file", cfg_file);
436 nh_->getParam(
"/sumo/step_length", step_length);
437 if (cfg_file.empty())
439 throw std::runtime_error(
"Error: No config file for sumo provided.");
442 std::vector<std::string> sumoargs;
443 sumoargs.push_back(
"-c");
444 sumoargs.push_back(cfg_file);
445 sumoargs.push_back(
"--step-length");
446 sumoargs.push_back(std::to_string(step_length));
449 sumoargs.push_back(
"--remote-port");
450 sumoargs.push_back(std::to_string(port));
457 std::cout <<
"load sumo ..." << std::flush;
458 libsumo::Simulation::load(sumoargs);
459 std::cout <<
" done." << std::endl;
462 catch (
const std::exception& exc)
464 std::cout << exc.what() << std::endl;
465 std::cout <<
"Arguments for sumo:" << std::endl;
466 for (
auto a : sumoargs)
468 std::cout << a << std::endl;
470 std::cout <<
"try again ..." << std::endl;
472 std::this_thread::sleep_for(std::chrono::milliseconds(500));
474 std::cout <<
"started sumo with config file " << cfg_file <<
" and step length " << step_length
476 tSUMO0 = libsumo::Simulation::getTime();
478 publisher_ =
nh_->advertise<adore_if_ros_msg::TrafficParticipantSetSimulation>(
"/SIM/"
486 ROS_INFO_STREAM_ONCE(
"SPAT/MAP Generation Parameters");
491 ROS_INFO_STREAM_ONCE(
"-------------------------------");
493 nh_->advertise<adore_v2x_sim::SimMAPEM>(
"/SIM/v2x/MAPEM",100);
495 nh_->advertise<adore_v2x_sim::SimSPATEM>(
"/SIM/v2x/SPATEM",100);
497 subscriber2_ =
nh_->subscribe<adore_if_ros_msg::TrafficParticipantSimulationConstPtr>(
510 libsumo::Simulation::close();
Definition: sumotraffic2ros.h:73
SUMOTrafficToROS()
Definition: sumotraffic2ros.h:75
~SUMOTrafficToROS()
Definition: sumotraffic2ros.h:82
void init_sumo()
Definition: sumotraffic2ros.h:429
void moveToXY(std::string &id, std::string z, int a, double x, double y, double heading, int b)
Definition: sumotraffic2ros.h:184
std::vector< ros::Timer > rostimers_
Definition: sumotraffic2ros.h:99
std::vector< std::string > vehidlist_
Definition: sumotraffic2ros.h:106
ROSVehicleSet rosVehicleSet_
Definition: sumotraffic2ros.h:88
void closeSumo()
Definition: sumotraffic2ros.h:508
std::vector< int > ros_to_sumo_ignore_list_
Definition: sumotraffic2ros.h:109
double min_update_period_
Definition: sumotraffic2ros.h:115
std::vector< std::string > sumo_to_ros_ignore_list_
Definition: sumotraffic2ros.h:108
double tSUMO
Definition: sumotraffic2ros.h:113
void setRosNodeHandle(ros::NodeHandle *nh)
Definition: sumotraffic2ros.h:503
std::string sumo_rosped_prefix_
Definition: sumotraffic2ros.h:98
ros::Subscriber subscriber1_
Definition: sumotraffic2ros.h:95
void setSpeed(std::string &id, double val)
Definition: sumotraffic2ros.h:173
std::vector< std::string > pedidlist_
Definition: sumotraffic2ros.h:107
std::map< int, std::string > replacement_ids_
Definition: sumotraffic2ros.h:104
std::unordered_map< std::string, int > sumopedid2int_
Definition: sumotraffic2ros.h:102
ros::Publisher mapem_publisher_
Definition: sumotraffic2ros.h:91
double tSUMO0
Definition: sumotraffic2ros.h:112
void transferDataSumoToRos()
Definition: sumotraffic2ros.h:227
bool newStep()
Definition: sumotraffic2ros.h:206
int last_assigned_int_id_
Definition: sumotraffic2ros.h:103
double last_tl_update_time_
Definition: sumotraffic2ros.h:94
ros::Publisher publisher_
Definition: sumotraffic2ros.h:90
void run()
Definition: sumotraffic2ros.h:123
double delta_t_
Definition: sumotraffic2ros.h:114
int getNewIntID()
Definition: sumotraffic2ros.h:132
void runCallback(const ros::TimerEvent &te)
Definition: sumotraffic2ros.h:197
ros::NodeHandle * nh_
Definition: sumotraffic2ros.h:89
double min_tl_update_period_
Definition: sumotraffic2ros.h:116
void removeVehicle(std::string &id)
Definition: sumotraffic2ros.h:140
SumoTLs2Ros sumotls2ros
Definition: sumotraffic2ros.h:93
ros::Publisher spatem_publisher_
Definition: sumotraffic2ros.h:92
std::unordered_map< std::string, int > sumovehid2int_
Definition: sumotraffic2ros.h:101
ros::Subscriber subscriber2_
Definition: sumotraffic2ros.h:96
void setMaxSpeed(std::string &id, double val)
Definition: sumotraffic2ros.h:162
Timer timer_
Definition: sumotraffic2ros.h:111
void transferDataRosToSumo()
Definition: sumotraffic2ros.h:376
ros::NodeHandle * getRosNodeHandle()
Definition: sumotraffic2ros.h:119
void init_rosconnection(int argc, char **argv, double rate, std::string nodename)
Definition: sumotraffic2ros.h:422
void addVehicle(std::string &id)
Definition: sumotraffic2ros.h:151
std::string sumo_rosveh_prefix_
Definition: sumotraffic2ros.h:97
Definition: sumotls2ros.h:36
bool _generate_spat_timing
Definition: sumotls2ros.h:113
std::vector< adore_v2x_sim::SimSPATEM > getSPATEMFromSUMO(double time, double power=23)
Definition: sumotls2ros.cpp:546
bool is_south_hemi_
Definition: sumotls2ros.h:109
std::unordered_map< int, adore_v2x_sim::SimMAPEM > getMAPEMFromSUMO(double time, double power=23)
Definition: sumotls2ros.cpp:95
int utm_zone_
Definition: sumotls2ros.h:105
bool _use_system_time
Definition: sumotls2ros.h:111
ALFunction< T, T > * heading(AScalarToN< T, 2 > *df)
Definition: heading.h:114
ALFunction< DT, CT > * add(ALFunction< DT, CT > *a, ALFunction< DT, CT > *b)
Definition: alfunction.h:742
adoreMatrix< T, N, M > max(adoreMatrix< T, N, M > a, const adoreMatrix< T, N, M > &b)
Definition: adoremath.h:686
x
Definition: adore_set_goal.py:30
y
Definition: adore_set_goal.py:31
z
Definition: adore_set_goal.py:32
yaw
Definition: adore_set_pose.py:36
w
Definition: adore_set_pose.py:40
Definition: areaofeffectconverter.h:20
Definition: sumotraffic2ros.h:53
std::unordered_map< int, adore_if_ros_msg::TrafficParticipantSimulation > data_
Definition: sumotraffic2ros.h:55
void receive(adore_if_ros_msg::TrafficParticipantSimulationConstPtr msg)
Definition: sumotraffic2ros.h:58
Definition: sumotraffic2ros.h:40
double tUTC_
Definition: sumotraffic2ros.h:42
void receive(const std_msgs::Float64ConstPtr &msg)
Definition: sumotraffic2ros.h:46
Timer()
Definition: sumotraffic2ros.h:43