1+ #include " vacuum_cleaner_cpp/HAL.hpp"
2+
3+ using namespace std ::chrono_literals;
4+
5+ std::shared_ptr<MotorsNode> HAL::motors_node_ = nullptr ;
6+ std::shared_ptr<LaserNode> HAL::laser_node_ = nullptr ;
7+ std::shared_ptr<BumperNode> HAL::bumper_node_ = nullptr ;
8+
9+ HAL::HAL () : Node(" hal_node" )
10+ {
11+ std::vector<std::string> bumper_topics = {
12+ " /roombaROS/events/right_bumper" ,
13+ " /roombaROS/events/center_bumper" ,
14+ " /roombaROS/events/left_bumper"
15+ };
16+
17+ motors_node_ = std::make_shared<MotorsNode>(" /cmd_vel" , 1.0 , 1.0 );
18+ laser_node_ = std::make_shared<LaserNode>(" /roombaROS/laser/scan" );
19+ bumper_node_ = std::make_shared<BumperNode>(bumper_topics);
20+
21+ spin_thread_ = std::thread ([]() {
22+ rclcpp::executors::SingleThreadedExecutor executor;
23+ executor.add_node (HAL::motors_node_);
24+ executor.add_node (HAL::laser_node_);
25+ executor.add_node (HAL::bumper_node_);
26+
27+ while (rclcpp::ok ()) {
28+ executor.spin_some ();
29+ std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
30+ }
31+ });
32+ spin_thread_.detach ();
33+ }
34+
35+ void HAL::set_v (const float speed)
36+ {
37+ if (motors_node_) motors_node_->sendV (static_cast <double >(speed));
38+ }
39+
40+ void HAL::set_w (const float speed)
41+ {
42+ if (motors_node_) motors_node_->sendW (static_cast <double >(speed));
43+ }
44+
45+ const LaserData *HAL::get_laser_data ()
46+ {
47+ if (!laser_node_) return nullptr ;
48+ return new LaserData (laser_node_->getLaserData ());
49+ }
50+
51+ std::vector<bool > HAL::get_bumper_data ()
52+ {
53+ std::vector<bool > v = {false , false , false };
54+ if (bumper_node_) {
55+ BumperData b_data = bumper_node_->getBumperData ();
56+ if (b_data.state == 1 && b_data.bumper >= 0 && b_data.bumper < 3 ) {
57+ v[b_data.bumper ] = true ;
58+ }
59+ }
60+ return v;
61+ }
0 commit comments