Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wb_robot_cleanup fix for python plugins #781

Merged
merged 6 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace webots_ros2_driver {
public:
void init(webots_ros2_driver::WebotsNode *node, std::unordered_map<std::string, std::string> &parameters) override;
void step() override;
void stop();

static std::shared_ptr<PythonPlugin> createFromType(const std::string &type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace webots_ros2_driver {
int step();
std::string urdf() const { return mRobotDescription; };
static void handleSignals();
std::vector<std::shared_ptr<PluginInterface>> mPlugins;

private:
std::unordered_map<std::string, std::string> getDeviceRosProperties(const std::string &name) const;
Expand All @@ -57,7 +58,6 @@ namespace webots_ros2_driver {

rclcpp::TimerBase::SharedPtr mTimer;
int mStep;
std::vector<std::shared_ptr<PluginInterface>> mPlugins;
pluginlib::ClassLoader<PluginInterface> mPluginLoader;
tinyxml2::XMLElement *mWebotsXMLElement;
std::shared_ptr<tinyxml2::XMLDocument> mRobotDescriptionDocument;
Expand Down
18 changes: 17 additions & 1 deletion webots_ros2_driver/src/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <webots/vehicle/driver.h>
#include <memory>
#include <rclcpp/rclcpp.hpp>
#include <webots_ros2_driver/PluginInterface.hpp>
#include <webots_ros2_driver/PythonPlugin.hpp>
#include <webots_ros2_driver/WebotsNode.hpp>

int main(int argc, char **argv) {
Expand Down Expand Up @@ -44,7 +46,21 @@ int main(int argc, char **argv) {
break;
rclcpp::spin_some(node);
}
wb_robot_cleanup();
// Check if the plugin is actually an instance of PythonPlugin
bool isPython = false;
for (std::shared_ptr<webots_ros2_driver::PluginInterface> plugin : node->mPlugins) {
std::shared_ptr<webots_ros2_driver::PythonPlugin> pythonPlugin =
std::dynamic_pointer_cast<webots_ros2_driver::PythonPlugin>(plugin);
if (pythonPlugin) {
pythonPlugin->stop(); // stop only for python plugins
isPython = true;
break;
}
}
if (!isPython) {
wb_robot_cleanup();
}
RCLCPP_INFO(node->get_logger(), "Controller successfully disconnected from robot in Webots simulation.");
rclcpp::shutdown();
return 0;
}
6 changes: 6 additions & 0 deletions webots_ros2_driver/src/PythonPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ namespace webots_ros2_driver {
PyErr_Print();
}

void PythonPlugin::stop() {
PyObject_CallMethod(mPyPlugin, "stop", "");
Py_Finalize();
}

PyObject *PythonPlugin::getPyWebotsNodeInstance() {
if (gPyWebotsNode)
return gPyWebotsNode;
Expand All @@ -37,6 +42,7 @@ from controller import Supervisor
sys.path.insert(1, os.path.dirname(controller.__file__))
from vehicle import Driver


angel-ayala marked this conversation as resolved.
Show resolved Hide resolved
class WebotsNode:
def __init__(self):
if Driver.isInitialisationPossible():
Expand Down
3 changes: 1 addition & 2 deletions webots_ros2_driver/src/WebotsNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
#include <webots_ros2_driver/plugins/static/Ros2RangeFinder.hpp>
#include <webots_ros2_driver/plugins/static/Ros2Receiver.hpp>
#include <webots_ros2_driver/plugins/static/Ros2VacuumGripper.hpp>
#include "webots_ros2_driver/PluginInterface.hpp"

#include "webots_ros2_driver/PluginInterface.hpp"
#include "webots_ros2_driver/PythonPlugin.hpp"

Expand Down Expand Up @@ -258,6 +256,7 @@ namespace webots_ros2_driver {
if (gShutdownSignalReceived && !mWaitingForUrdfRobotToBeRemoved) {
mRemoveUrdfRobotPublisher->publish(mRemoveUrdfRobotMessage);
mWaitingForUrdfRobotToBeRemoved = true;
return -1;
}

const int result = wb_robot_step(mStep);
Expand Down