Skip to content

Commit e75f87f

Browse files
committed
add targets in the environment
1 parent 02aaafe commit e75f87f

File tree

3 files changed

+191
-1
lines changed

3 files changed

+191
-1
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ add_library(${PROJECT_NAME}_lib
4141
"src/gmm_visualizer.cpp"
4242
"src/gmm_test_node.cpp"
4343
"src/hs_interface.cpp"
44+
"src/target_publisher_node.cpp"
4445
)
4546

4647

@@ -91,6 +92,9 @@ add_executable(gmm_test src/gmm_test_node.cpp )
9192
# target_link_libraries(gmm_test ${SFML_libraries})
9293
ament_target_dependencies(gmm_test ${dependencies})
9394

95+
add_executable(target_publisher_node src/target_publisher_node.cpp )
96+
ament_target_dependencies(target_publisher_node ${dependencies})
97+
9498

9599
# Install python modules
96100
ament_python_install_package(${PROJECT_NAME})
@@ -102,7 +106,7 @@ install(PROGRAMS
102106
)
103107

104108

105-
install(TARGETS centralized_gmm distributed_gmm gmm_visualizer supervisor_gmm gmm_test hs_interface
109+
install(TARGETS centralized_gmm distributed_gmm gmm_visualizer supervisor_gmm gmm_test hs_interface target_publisher_node
106110
DESTINATION lib/${PROJECT_NAME}
107111
)
108112

launch/gmm_online_world.launch.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import random
5+
6+
from ament_index_python.packages import get_package_share_directory
7+
from launch import LaunchDescription
8+
from launch.actions import IncludeLaunchDescription
9+
from launch.launch_description_sources import PythonLaunchDescriptionSource
10+
from launch.substitutions import ThisLaunchFileDir
11+
from launch.actions import ExecuteProcess
12+
from launch.substitutions import LaunchConfiguration
13+
from launch_ros.actions import Node
14+
15+
TURTLEBOT3_MODEL = os.environ['TURTLEBOT3_MODEL']
16+
17+
AREA_W = 10.0
18+
ROBOTS_NUM = 8
19+
TARGETS_NUM = 2
20+
21+
22+
def generate_launch_description():
23+
use_sim_time = LaunchConfiguration('use_sim_time', default='true')
24+
launch_file_dir = os.path.join(get_package_share_directory('gazebo_ros'), 'launch')
25+
launch_list = []
26+
27+
gazebo = IncludeLaunchDescription(
28+
PythonLaunchDescriptionSource([launch_file_dir, '/gazebo.launch.py']),
29+
launch_arguments={'gui': 'true'}.items(),
30+
)
31+
32+
launch_list.append(gazebo)
33+
34+
35+
x_pos = []
36+
y_pos = []
37+
theta = []
38+
39+
for i in range(ROBOTS_NUM):
40+
ranx = random.uniform(-0.5*AREA_W, 0.5*AREA_W)
41+
rany = random.uniform(-0.5*AREA_W, 0.5*AREA_W)
42+
th = random.uniform(0,6)
43+
44+
x_pos.append(ranx)
45+
y_pos.append(rany)
46+
theta.append(th)
47+
# print(th)
48+
49+
50+
51+
for i in range(ROBOTS_NUM):
52+
spawn_entity = Node(package='gazebo_ros', executable='spawn_entity.py',
53+
arguments=['-entity', 'turtlebot'+str(i), '-database', 'turtlebot3_' + TURTLEBOT3_MODEL, '-robot_namespace', 'turtlebot'+str(i),
54+
'-x', str(float(x_pos[i])), '-y', str(float(y_pos[i])), '-z', str(0.1), '-Y', str(theta[i]),
55+
],
56+
output='screen')
57+
58+
launch_list.append(spawn_entity)
59+
60+
61+
xtargets = []
62+
ytargets = []
63+
for i in range(TARGETS_NUM):
64+
xt = random.uniform(-0.5*AREA_W, 0.5*AREA_W)
65+
yt = random.uniform(-0.5*AREA_W, 0.5*AREA_W)
66+
xtargets.append(xt)
67+
ytargets.append(yt)
68+
target = Node(package='gazebo_ros', executable='spawn_entity.py',
69+
arguments=['-entity', 'target'+str(i), '-database', 'target', '-robot_namespace', 'target'+str(i),
70+
'-x', str(float(xt)), '-y', str(float(yt)), '-z', str(0.2),
71+
],
72+
output='screen')
73+
74+
launch_list.append(target)
75+
76+
for i in range(TARGETS_NUM):
77+
target_pub = Node(
78+
package='gmm_coverage',
79+
node_executable='target_publisher_node',
80+
name='target_pub',
81+
parameters=[{"XT": xtargets[i]},
82+
{"YT": ytargets[i]},
83+
{"ID": i}],
84+
output='screen')
85+
86+
launch_list.append(target_pub)
87+
88+
89+
return LaunchDescription(launch_list)

src/target_publisher_node.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// STL
2+
#include <iostream>
3+
#include <vector>
4+
#include <chrono>
5+
#include <random>
6+
#include <tf2/LinearMath/Quaternion.h>
7+
#include <tf2/impl/utils.h>
8+
#include <chrono>
9+
#include <functional>
10+
#include <memory>
11+
#include <string>
12+
#include <eigen3/Eigen/Dense>
13+
#include <eigen3/Eigen/Core>
14+
#include <eigen3/Eigen/SVD>
15+
#include <cstdlib>
16+
#include <cmath>
17+
#include <algorithm>
18+
#include <netinet/in.h>
19+
#include <sys/types.h>
20+
#include <stdlib.h>
21+
#include <time.h>
22+
#include <fstream>
23+
#include <stdio.h>
24+
#include <unistd.h>
25+
#include <sys/stat.h>
26+
#include <math.h>
27+
28+
// ROS includes
29+
#include "rclcpp/rclcpp.hpp"
30+
#include <geometry_msgs/msg/pose.hpp>
31+
32+
33+
using namespace std::chrono_literals;
34+
using std::placeholders::_1;
35+
36+
37+
38+
class Target : public rclcpp::Node
39+
{
40+
41+
public:
42+
Target() : Node("target_publisher_node")
43+
{
44+
//------------------------------------------------- ROS parameters ---------------------------------------------------------
45+
this->declare_parameter<float>("XT", 0.0);
46+
this->get_parameter("XT", XT);
47+
this->declare_parameter<float>("YT", 0.0);
48+
this->get_parameter("YT", YT);
49+
this->declare_parameter<int>("ID", 0);
50+
this->get_parameter("ID", ID);
51+
52+
//-----------------------------------------------------------------------------------------------------------------------------------
53+
54+
//--------------------------------------------------- Subscribers and Publishers ----------------------------------------------------
55+
pub_ = this->create_publisher<geometry_msgs::msg::Pose>("/target" + std::to_string(ID) + "/pose", 1);
56+
timer_ = this->create_wall_timer(100ms, std::bind(&Target::loop, this));
57+
58+
msg.position.x = XT;
59+
msg.position.y = YT;
60+
msg.position.z = 0.0;
61+
62+
std::cout << "Target " << ID << " created in position " << XT << " " << YT << std::endl;
63+
64+
65+
}
66+
~Target()
67+
{
68+
std::cout<<"DESTROYER HAS BEEN CALLED"<<std::endl;
69+
}
70+
71+
//void stop(int signum);
72+
void loop();
73+
74+
private:
75+
double XT;
76+
double YT;
77+
int ID;
78+
79+
rclcpp::Publisher<geometry_msgs::msg::Pose>::SharedPtr pub_;
80+
rclcpp::TimerBase::SharedPtr timer_;
81+
geometry_msgs::msg::Pose msg;
82+
};
83+
84+
void Target::loop()
85+
{
86+
pub_->publish(msg);
87+
}
88+
89+
90+
91+
int main(int argc, char **argv)
92+
{
93+
rclcpp::init(argc, argv);
94+
rclcpp::spin(std::make_shared<Target>());
95+
rclcpp::shutdown();
96+
return 0;
97+
}

0 commit comments

Comments
 (0)