-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Juancams <[email protected]>
- Loading branch information
Showing
5 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2024 Juan Carlos Manzanares Serrano | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef KOBUKI__FAKE_BUMPER_HPP_ | ||
#define KOBUKI__FAKE_BUMPER_HPP_ | ||
|
||
#include <utility> | ||
|
||
#include "rclcpp/rclcpp.hpp" | ||
#include "kobuki_ros_interfaces/msg/bumper_event.hpp" | ||
#include "sensor_msgs/msg/laser_scan.hpp" | ||
|
||
namespace kobuki | ||
{ | ||
|
||
using std::placeholders::_1; | ||
|
||
class FakeBumper : public rclcpp::Node | ||
{ | ||
public: | ||
FakeBumper(); | ||
|
||
private: | ||
using LaserScan = sensor_msgs::msg::LaserScan; | ||
using BumperEvent = kobuki_ros_interfaces::msg::BumperEvent; | ||
|
||
void laserCallback(const LaserScan::UniquePtr msg); | ||
|
||
rclcpp::Publisher<BumperEvent>::SharedPtr bumper_pub_; | ||
rclcpp::Subscription<LaserScan>::SharedPtr scan_sub_; | ||
|
||
BumperEvent pressed_; | ||
|
||
static constexpr float OBSTACLE_DISTANCE = 0.2f; | ||
}; | ||
|
||
} // namespace kobuki | ||
|
||
#endif // KOBUKI__FAKE_BUMPER_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2024 Juan Carlos Manzanares Serrano | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "kobuki/FakeBumper.hpp" | ||
#include "rclcpp/rclcpp.hpp" | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
rclcpp::init(argc, argv); | ||
|
||
auto fake_bumper_node = std::make_shared<kobuki::FakeBumper>(); | ||
rclcpp::spin(fake_bumper_node); | ||
|
||
rclcpp::shutdown(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2024 Juan Carlos Manzanares Serrano | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "kobuki/FakeBumper.hpp" | ||
|
||
namespace kobuki | ||
{ | ||
FakeBumper::FakeBumper() | ||
: Node("fake_bumper") | ||
{ | ||
pressed_.state = BumperEvent::RELEASED; | ||
|
||
bumper_pub_ = | ||
this->create_publisher<BumperEvent>("/events/bumper", 10); | ||
scan_sub_ = create_subscription<LaserScan>( | ||
"/scan", rclcpp::SensorDataQoS(), | ||
std::bind(&FakeBumper::laserCallback, this, _1)); | ||
} | ||
|
||
void | ||
FakeBumper::laserCallback(const LaserScan::UniquePtr msg) | ||
{ | ||
if (pressed_.state) { | ||
|
||
for (size_t i = 0; i < msg->ranges.size(); i++) { | ||
if (msg->ranges[i] < OBSTACLE_DISTANCE) { | ||
return; | ||
} | ||
} | ||
|
||
pressed_.state = BumperEvent::RELEASED; | ||
bumper_pub_->publish(pressed_); | ||
} else { | ||
size_t pos = 0, threshold_left = msg->ranges.size() / 4, | ||
threshold_right = msg->ranges.size() - threshold_left; | ||
|
||
for (size_t i = 0; i < msg->ranges.size(); i++) { | ||
if (msg->ranges[i] < OBSTACLE_DISTANCE && (i<threshold_left || i> threshold_right)) { | ||
pressed_.state = BumperEvent::PRESSED; | ||
pos = i; | ||
break; | ||
} | ||
} | ||
|
||
if (!pressed_.state) {return;} | ||
|
||
size_t threshold_center_left = threshold_left / 3, | ||
threshold_center_right = msg->ranges.size() - threshold_center_left; | ||
|
||
if (pos > threshold_center_left && pos < threshold_left) { | ||
pressed_.bumper = BumperEvent::LEFT; | ||
} else if (pos > threshold_right && pos < threshold_center_right) { | ||
pressed_.bumper = BumperEvent::RIGHT; | ||
} else { | ||
pressed_.bumper = BumperEvent::CENTER; | ||
} | ||
|
||
bumper_pub_->publish(pressed_); | ||
} | ||
} | ||
} // namespace kobuki |