Skip to content

Commit a7a824c

Browse files
committed
obstacle avoidance cpp support
1 parent 65a31d0 commit a7a824c

16 files changed

Lines changed: 211 additions & 58 deletions

File tree

database/exercises/db.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ COPY public.exercises (id, exercise_id, name, description, tags, status, url) FR
118118
5 vacuum_cleaner_loc Localized Vacuum Cleaner Localiized vauum clenaer ["ROS2", "MOBILE ROBOTS", "SERVICE ROBOTS"] ACTIVE https://jderobot.github.io/RoboticsAcademy/exercises/MobileRobots/vacuum_cleaner_loc
119119
6 global_navigation Global Navigation Global navigation exercise using REACT and RAM ["ROS2"] ACTIVE https://jderobot.github.io/RoboticsAcademy/exercises/AutonomousCars/global_navigation
120120
7 rescue_people Drone Rescue People Drone Rescue People exercise ["ROS2"] ACTIVE https://jderobot.github.io/RoboticsAcademy/exercises/Drones/rescue_people
121-
8 obstacle_avoidance Obstacle Avoidance Obstacle Avoidance exercise using React and RAM ["ROS2","AUTONOMOUS DRIVING"] ACTIVE https://jderobot.github.io/RoboticsAcademy/exercises/AutonomousCars/obstacle_avoidance
121+
8 obstacle_avoidance Obstacle Avoidance Obstacle Avoidance exercise using React and RAM ["ROS2","AUTONOMOUS DRIVING", "MULTILANGUAGE"] ACTIVE https://jderobot.github.io/RoboticsAcademy/exercises/AutonomousCars/obstacle_avoidance
122122
9 3d_reconstruction 3D Reconstruction 3D Reconstruction exercise using React and RAM ["ROS2","COMPUTER VISION"] ACTIVE https://jderobot.github.io/RoboticsAcademy/exercises/ComputerVision/3d_reconstruction
123123
10 amazon_warehouse Amazon Warehouse Control an amazon-like robot to organize a warehouse ["ROS2"] ACTIVE https://jderobot.github.io/RoboticsAcademy/exercises/MobileRobots/amazon_warehouse
124124
11 montecarlo_laser_loc Montecarlo Laser Loc Calculate the position of the robot based on the ["ROS2"] ACTIVE https://jderobot.github.io/RoboticsAcademy/exercises/MobileRobots/montecarlo_laser_loc

exercises/obstacle_avoidance/cpp_lib/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
77

88
find_package(ament_cmake REQUIRED)
99

10-
# Ajustado para los tópicos actuales (Laser, Odom, Cmd_vel, Points, Bools)
1110
set(ROS2_PACKAGES
1211
rclcpp
1312
nav_msgs

exercises/obstacle_avoidance/cpp_lib/config/targets_f1.json

Lines changed: 0 additions & 21 deletions
This file was deleted.

exercises/obstacle_avoidance/cpp_lib/include/WebGUI.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class WebGUINode : public rclcpp::Node {
3737
static std::shared_ptr<Map> map_;
3838
static std::shared_ptr<Lap> lap_;
3939

40-
// Nodos propios del WebGUI a través de interfaces comunes
4140
static std::shared_ptr<OdometryNode> pose3d_node_;
4241
static std::shared_ptr<LaserNode> laser_node_;
4342

exercises/obstacle_avoidance/cpp_lib/src/Map.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Map::Map(std::function<LaserData()> laser_cb, std::function<Pose3d()> pose_cb)
1414
: carx(0), cary(0), obsx(0), obsy(0), avgx(0), avgy(0),
1515
targetx(0), targety(0), laser_callback_(laser_cb), pose_callback_(pose_cb) {
1616

17-
std::ifstream file("config/targets_f1.json");
17+
std::ifstream file("/resources/exercises/obstacle_avoidance/simple_circuit_targets.json");
1818
nlohmann::json j;
1919
if (file.is_open() && (file >> j)) {
2020
if (j.contains("targets")) {

exercises/obstacle_avoidance/cpp_lib/src/WebGUI.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "WebGUI.hpp"
22
#include <thread>
33
#include <chrono>
4+
#include <ament_index_cpp/get_package_share_directory.hpp>
45

56
using namespace std::chrono_literals;
67
using std::placeholders::_1;
@@ -11,21 +12,33 @@ std::shared_ptr<OdometryNode> WebGUINode::pose3d_node_ = nullptr;
1112
std::shared_ptr<LaserNode> WebGUINode::laser_node_ = nullptr;
1213

1314
WebGUINode::WebGUINode() : Node("webgui_bridge_node") {
14-
if (!pose3d_node_) pose3d_node_ = std::make_shared<OdometryNode>("/odom");
15+
if (!pose3d_node_) pose3d_node_ = std::make_shared<OdometryNode>("/odom", "webgui_odom_node");
1516
if (!laser_node_) laser_node_ = std::make_shared<LaserNode>("/f1/laser/scan");
1617

1718
if (!map_) {
1819
map_ = std::make_shared<Map>(
1920
[]() {
2021
LaserData d = WebGUINode::laser_node_->getLaserData();
2122
while (d.values.empty() && rclcpp::ok()) {
23+
std::this_thread::sleep_for(std::chrono::milliseconds(50)); // Evita que la CPU se congele esperando
2224
d = WebGUINode::laser_node_->getLaserData();
2325
}
2426
return d;
2527
},
2628
[]() { return WebGUINode::pose3d_node_->getPose3d(); }
2729
);
2830
lap_ = std::make_shared<Lap>(map_);
31+
32+
std::thread spin_thread([]() {
33+
rclcpp::executors::SingleThreadedExecutor executor;
34+
executor.add_node(WebGUINode::pose3d_node_);
35+
executor.add_node(WebGUINode::laser_node_);
36+
while (rclcpp::ok()) {
37+
executor.spin_some();
38+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
39+
}
40+
});
41+
spin_thread.detach();
2942
}
3043

3144
auto cb_car = [](geometry_msgs::msg::Point::UniquePtr m) { map_->setCar(m->x, m->y); };
@@ -66,6 +79,7 @@ void WebGUINode::publish_current_target() {
6679
}
6780
}
6881

82+
// WebSocket Session Helper
6983
class session : public std::enable_shared_from_this<session> {
7084
tcp::resolver resolver_;
7185
websocket::stream<beast::tcp_stream> ws_;
@@ -130,22 +144,6 @@ class session : public std::enable_shared_from_this<session> {
130144
};
131145

132146
WebGUI::WebGUI() {
133-
auto webgui_node = std::make_shared<WebGUINode>();
134-
135-
// Creamos un hilo para ejecutar el propio executor del WebGUI
136-
std::thread spin_thread([webgui_node]() {
137-
rclcpp::executors::MultiThreadedExecutor executor;
138-
executor.add_node(webgui_node);
139-
executor.add_node(WebGUINode::pose3d_node_);
140-
executor.add_node(WebGUINode::laser_node_);
141-
142-
while (rclcpp::ok()) {
143-
executor.spin_some();
144-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
145-
}
146-
});
147-
spin_thread.detach();
148-
149147
net::io_context ioc;
150148
std::make_shared<session>(ioc)->run("127.0.0.1", "2303", "{\"lap\":\"\",\"map\":\"\"}");
151149
ioc.run();

exercises/obstacle_avoidance/cpp_template/CMakeLists.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ find_package(rclcpp REQUIRED)
1010
find_package(geometry_msgs REQUIRED)
1111
find_package(std_msgs REQUIRED)
1212
find_package(nav_msgs REQUIRED)
13-
find_package(message_filters REQUIRED)
1413
find_package(sensor_msgs REQUIRED)
15-
find_package(gazebo_msgs REQUIRED)
16-
find_package(tf2_ros REQUIRED)
17-
find_package(tf2_geometry_msgs REQUIRED)
1814
find_package(common_interfaces_cpp REQUIRED)
19-
2015
find_package(nlohmann_json REQUIRED)
2116
find_package(Boost REQUIRED COMPONENTS system)
2217

@@ -26,17 +21,12 @@ set(dependencies
2621
nav_msgs
2722
geometry_msgs
2823
std_msgs
29-
gazebo_msgs
30-
message_filters
31-
tf2_ros
32-
tf2_geometry_msgs
3324
common_interfaces_cpp
3425
)
3526

3627
include_directories(
3728
${CMAKE_CURRENT_SOURCE_DIR}
3829
${CMAKE_CURRENT_SOURCE_DIR}/libs/include
39-
${CMAKE_CURRENT_SOURCE_DIR}/libs/include/vacuum_cleaner_cpp
4030
)
4131

4232
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/libs)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef INCLUDE_FREQUENCY_HPP_
2+
#define INCLUDE_FREQUENCY_HPP_
3+
4+
#include <chrono>
5+
#include <cmath>
6+
#include <thread>
7+
8+
class Frequency
9+
{
10+
private:
11+
std::chrono::high_resolution_clock::time_point last_time;
12+
int is_first_iteration;
13+
14+
public:
15+
Frequency();
16+
~Frequency();
17+
18+
static int rate;
19+
void tick(int ideal_cycle = 50);
20+
};
21+
22+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef INCLUDE_HAL_HPP_
2+
#define INCLUDE_HAL_HPP_
3+
4+
#include <vector>
5+
#include <memory>
6+
#include <thread>
7+
#include <chrono>
8+
#include "rclcpp/rclcpp.hpp"
9+
#include "common_interfaces_cpp/hal/motors.hpp"
10+
#include "common_interfaces_cpp/hal/laser.hpp"
11+
#include "common_interfaces_cpp/hal/odometry.hpp"
12+
13+
class HAL : public rclcpp::Node
14+
{
15+
public:
16+
HAL();
17+
static void set_v(const double velocity);
18+
static void set_w(const double velocity);
19+
static Pose3d get_pose3d();
20+
static const LaserData *get_laser_data();
21+
22+
private:
23+
static std::shared_ptr<OdometryNode> pose3d_node_;
24+
static std::shared_ptr<MotorsNode> motors_node_;
25+
static std::shared_ptr<LaserNode> laser_node_;
26+
std::thread spin_thread_;
27+
};
28+
29+
#endif
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef INCLUDE_LAP_HPP_
2+
#define INCLUDE_LAP_HPP_
3+
4+
#include <string>
5+
#include <chrono>
6+
#include <memory>
7+
#include "Map.hpp"
8+
9+
class Lap {
10+
public:
11+
Lap(std::shared_ptr<Map> map_object);
12+
13+
double check_threshold();
14+
double return_lap_time() const;
15+
void reset();
16+
void pause();
17+
void unpause();
18+
19+
private:
20+
std::shared_ptr<Map> map_;
21+
std::string target_start_;
22+
std::string target_end_;
23+
24+
std::chrono::time_point<std::chrono::system_clock> start_time_;
25+
std::chrono::duration<double> lap_time_;
26+
bool buffer_;
27+
bool pause_condition_;
28+
};
29+
30+
#endif

0 commit comments

Comments
 (0)