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

Add ROSControlItem #5

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
3a1a618
Add ROSControl at instance
RyodoTanaka Sep 1, 2020
def1be9
Update plugin setting xml file
RyodoTanaka Sep 2, 2020
b192f46
Update to inherit RobortHW
RyodoTanaka Sep 2, 2020
e4fe49e
Update to add dependencies
RyodoTanaka Sep 2, 2020
2506b5e
Update to install plugins.xml file
RyodoTanaka Sep 2, 2020
58b614f
Update to set default value for argments
RyodoTanaka Sep 2, 2020
38c498a
Update dependencies & plugins.xml file name
RyodoTanaka Sep 2, 2020
fc9ac6b
Update indent
RyodoTanaka Sep 2, 2020
225f3ca
Update to add CnoidHW class
RyodoTanaka Sep 2, 2020
7ae6737
Update to utilize CnoidHW
RyodoTanaka Sep 2, 2020
179c350
Delete to change file name
RyodoTanaka Sep 4, 2020
d928baf
Update to add robot_hw_sim class
RyodoTanaka Sep 4, 2020
832f9ff
Update to utilize template for base class
RyodoTanaka Sep 4, 2020
a751205
Update to utilize RobotHwCnoid
RyodoTanaka Sep 4, 2020
46af663
Update to compile robot_hw_sim
RyodoTanaka Sep 4, 2020
5331391
Update to utilize templated robot_hw_sim class
RyodoTanaka Sep 4, 2020
078fc07
Update for input(), control(), output()
RyodoTanaka Sep 4, 2020
d768ce9
Add RoboHWCnoid class
RyodoTanaka Sep 4, 2020
78f1d8d
Update add dependencies
RyodoTanaka Sep 4, 2020
57910d2
Update controller manager to be update for every control() loop
RyodoTanaka Sep 4, 2020
fcc3bf7
Update to utilize ctrl_types instead of ctrl_type
RyodoTanaka Sep 4, 2020
14ad5b5
Update to implement read() write() function
RyodoTanaka Sep 4, 2020
2e3c110
Fix to remove not existing program to install
RyodoTanaka Sep 29, 2020
d7ad789
Merge branch 'master' into dev/ros-control
RyodoTanaka May 17, 2021
018f114
Update for controller item
RyodoTanaka May 23, 2021
97ec7a0
Merge remote-tracking branch 'choreonoid/master' into dev/ros-control
RyodoTanaka May 24, 2021
e198311
Update to utilize library directly
RyodoTanaka May 24, 2021
142133f
Update to add catkin depends
RyodoTanaka May 24, 2021
3c96136
Fix typo
RyodoTanaka May 24, 2021
f9eccad
Update to delete unnecessary part
RyodoTanaka May 24, 2021
01a1334
Update initialization materials
RyodoTanaka May 24, 2021
d48c5d1
Fix to return true
RyodoTanaka May 24, 2021
141140a
Update to move header from h to cpp file
RyodoTanaka May 24, 2021
9c35dc4
Update for overroad function
RyodoTanaka May 24, 2021
44189cd
Update to avoid nullptr
RyodoTanaka May 25, 2021
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
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ find_package(catkin REQUIRED COMPONENTS
sensor_msgs
image_transport
choreonoid
controller_manager
control_toolbox
pluginlib
hardware_interface
transmission_interface
joint_limits_interface
urdf
angles
Comment on lines +16 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

長期手なメンテナンス性の視点から,既存パッケージ含めアルファベット順に整理していただいた方が良いかと思いました.

)

catkin_package(SKIP_CMAKE_CONFIG_GENERATION SKIP_PKG_CONFIG_GENERATION)
Expand Down
10 changes: 10 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@
<depend>std_msgs</depend>
<depend>sensor_msgs</depend>
<depend>image_transport</depend>

<depend>controller_manager</depend>
<depend>control_toolbox</depend>
<depend>pluginlib</depend>
<depend>hardware_interface</depend>
<depend>transmission_interface</depend>
<depend>joint_limits_interface</depend>
<depend>urdf</depend>
<depend>angles</depend>
Comment on lines +18 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMakeと同じく既存パッケージ含めアルファベット順に整理していただいた方が良いかと思いました.

<export>
<build_type>cmake</build_type>
<choreonoid_ros plugin="${prefix}/plugins.xml"/>
</export>
</package>
10 changes: 10 additions & 0 deletions plugins.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library path="lib/libRobotHWCnoid">
<class
name="hardware_interface/RobotHWCnoid"
type="hardware_interface::RobotHWCnoid"
base_class_type="hardware_interface::RobotHWSim<cnoid::ControllerIO*>">
<description>
A default robot simulation interface which constructs joint handles from an SDF/URDF.
</description>
</class>
</library>
14 changes: 14 additions & 0 deletions src/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ set(target CnoidROSPlugin)

choreonoid_make_gettext_mo_files(${target} mofiles)

# RobotHW inherited ros_control Choreonoid interface class #
add_library(RobotHWCnoid robot_hw_sim.h robot_hw_cnoid.cpp)
target_link_libraries(RobotHWCnoid ${catkin_LIBRARIES} Choreonoid::CnoidBody Choreonoid::CnoidBodyPlugin)

install(TARGETS RobotHWCnoid
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
RUNTIME DESTINATION ${CATKIN_GLOBAL_BIN_DESTINATION}
)

# Choreonoid Plugins #
choreonoid_add_plugin(${target}
ROSPlugin.cpp
WorldROSItem.cpp
BodyROSItem.cpp
robot_hw_sim.h
ROSControlItem.cpp
deprecated/BodyPublisherItem.cpp
${mofiles}
)
Expand All @@ -16,6 +29,7 @@ target_link_libraries(${target}
${sensor_msgs_LIBRARIES}
${image_transport_LIBRARIES}
Choreonoid::CnoidBodyPlugin
${catkin_LIBRARIES}
)

if(CHOREONOID_ENABLE_PYTHON)
Expand Down
175 changes: 175 additions & 0 deletions src/plugin/ROSControlItem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/////////////////////////////////
/// @author Ryodo Tanaka
/////////////////////////////////

#include "ROSControlItem.h"
#include <cnoid/MessageView>
#include <cnoid/PutPropertyFunction>
#include <cnoid/BodyItem>
#include <cnoid/ItemManager>
#include <cnoid/Archive>
#include <sstream>
#include <fmt/format.h>
#include "gettext.h"

namespace cnoid
{
void ROSControlItem::initializeClass(ExtensionManager* ext)
{
using namespace std;
using fmt::format;
ext->itemManager().registerClass<ROSControlItem>(N_("ROSControlItem"));
ext->itemManager().addCreationPanel<ROSControlItem>();
}

ROSControlItem::ROSControlItem(void)
{
io_ = nullptr;
}

ROSControlItem::ROSControlItem(const ROSControlItem& org) : ControllerItem(org)
{
io_ = nullptr;
}

ROSControlItem::~ROSControlItem()
{
//stop();
}

Item* ROSControlItem::doDuplicate(void) const
{
return new ROSControlItem(*this);
}

bool ROSControlItem::store(Archive& archive)
{
archive.write("name space", namespace_);

return true;
}

bool ROSControlItem::restore(const Archive& archive)
{
if(!archive.read("name space", namespace_))
archive.read("name space", namespace_);

return true;
}

void ROSControlItem::doPutProperties(PutPropertyFunction& putProperty)
{
putProperty("Name space",
namespace_, changeProperty(namespace_));
}

bool ROSControlItem::initialize(ControllerIO* io)
{
using namespace std;
using namespace cnoid;
using namespace hardware_interface;

stringstream ss;

// Check body //
if (!io->body()) {
ss.str("");
ss << "ROSControlItem \"" << displayName() << "\" is invalid." << endl;
ss << "Because it is not assigned to a body." << endl;
MessageView::instance()->put(ss.str(), MessageView::Error);

return false;
}

// Check that ROS has been initialized
if(!ros::isInitialized()) {
ss.str("");
ss << "ROS master is not initialized." << endl;
MessageView::instance()->put(ss.str(), MessageView::Warning);

return false;
}

// Copy elements to the local arguments //
io_ = io;
body_ = io->body();
tstep_ = io->worldTimeStep();
time_ = io->currentTime();

// ROS Initialize
nh_ = ros::NodeHandle(namespace_);

try {
// ros plugin loader //
if(!rbt_hw_sim_loader_)
rbt_hw_sim_loader_ = make_shared<pluginlib::ClassLoader<RobotHWSim<cnoid::ControllerIO*>>>("choreonoid_ros", "hardware_interface::RobotHWSim<cnoid::ControllerIO*>");

// load RobotHWCnoid plugin
if(!rbt_hw_sim_) {
rbt_hw_sim_ = rbt_hw_sim_loader_->createInstance("hardware_interface/RobotHWCnoid");

// load hardware_interface //
if(!rbt_hw_sim_->initSim(nh_, io_)) {
ss.str("");
ss << "Could not initialize robot simulation interface" << endl;
MessageView::instance()->put(ss.str(), MessageView::Error);
}
}

// register ros control manager //
if(!manager_)
manager_ = make_shared<controller_manager::ControllerManager>(rbt_hw_sim_.get(), nh_);

} catch(pluginlib::LibraryLoadException &ex) {
ss.str("");
ss << "Failed to create robot simulation interface loader : " << ex.what() << endl;
MessageView::instance()->put(ss.str(), MessageView::Error);
return false;
}

ss.str("");
ss << "Loaded cnoid::ROSControlItem" << endl;
MessageView::instance()->put(ss.str(), MessageView::Normal);

return true;
}

bool ROSControlItem::start(void)
{
return true;
}

void ROSControlItem::input(void)
{
}

bool ROSControlItem::control(void)
{
int last_sec = static_cast<int>(time_);
double last_nsec = (time_ - last_sec) * 1e9;
int now_sec = static_cast<int>(io_->currentTime());
double now_nsec = (io_->currentTime() - now_sec) * 1e9;

ros::Time last(last_sec, static_cast<int>(last_nsec));
ros::Time now(now_sec, static_cast<int>(now_nsec));
ros::Duration period = now - last;

rbt_hw_sim_->read(now, period);
rbt_hw_sim_->write(now, period);

manager_->update(now, period, false);

time_ = io_->currentTime();

return true;
}

void ROSControlItem::output(void)
{
}

void ROSControlItem::stop(void)
{
}

} // namespace cnoid
73 changes: 73 additions & 0 deletions src/plugin/ROSControlItem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
///////////////////////////////////////////////////////////
/// @file ROSControlItem.h
/// @brief Choreonoid ControlItem Plugin for ros_control
/// @author Ryodo Tanaka
///////////////////////////////////////////////////////////

#ifndef CNOID_ROS_PLUGIN_ROS_CONTROL_ITEM_H
#define CNOID_ROS_PLUGIN_ROS_CONTROL_ITEM_H

// Choreonoid //
#include <cnoid/ControllerItem>
#include <cnoid/Body>

// ROS //
#include <ros/ros.h>
#include <pluginlib/class_loader.h>
#include <controller_manager/controller_manager.h>
#include "robot_hw_sim.h"

// STL //
#include <vector>
#include <fstream>

// Boost //
#include <boost/shared_ptr.hpp>

namespace cnoid
{
class ROSControlItem : public ControllerItem
{
public:
static void initializeClass(ExtensionManager* ext);

ROSControlItem(void);
ROSControlItem(const ROSControlItem& org);

virtual ~ROSControlItem();

virtual bool initialize(ControllerIO* io) override;
virtual bool start(void) override;
virtual void input(void) override;
virtual bool control(void) override;
virtual void output(void) override;
virtual void stop(void) override;
Comment on lines +40 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++なのでvoid引数は不要かと思います.
他の関数含めまとめて削除をお願いします.


virtual double timeStep() const override { return tstep_; };

protected:
virtual Item* doDuplicate(void) const;
virtual bool store(Archive& archive);
virtual bool restore(const Archive& archive);
void doPutProperties(PutPropertyFunction& putProperty);

private:
ControllerIO* io_;
Body* body_;
double tstep_;
double time_;

ros::NodeHandle nh_;
std::shared_ptr<pluginlib::ClassLoader<hardware_interface::RobotHWSim<cnoid::ControllerIO*>>> rbt_hw_sim_loader_;
boost::shared_ptr<hardware_interface::RobotHWSim<cnoid::ControllerIO*>> rbt_hw_sim_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boost::shared_ptrを使っている理由がplugin_libの問題である旨一言コメント入れておいていただければと思います.

std::shared_ptr<controller_manager::ControllerManager> manager_;

std::string namespace_{""};

};

typedef ref_ptr<ROSControlItem> ROSControlItemPtr;

} // namespace cnoid

#endif // CNOID_ROS_PLUGIN_ROS_CONTROL_ITEM_H
2 changes: 2 additions & 0 deletions src/plugin/ROSPlugin.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "WorldROSItem.h"
#include "BodyROSItem.h"
#include "ROSControlItem.h"
#include "deprecated/BodyPublisherItem.h"
#include <cnoid/Plugin>
#include <cnoid/MessageView>
Expand Down Expand Up @@ -28,6 +29,7 @@ class ROSPlugin : public Plugin

WorldROSItem::initializeClass(this);
BodyROSItem::initializeClass(this);
ROSControlItem::initializeClass(this);
BodyPublisherItem::initializeClass(this);

return true;
Expand Down
Loading