Skip to content

Commit c4e382c

Browse files
committed
super capacitor working now
1 parent db6acae commit c4e382c

File tree

6 files changed

+45
-40
lines changed

6 files changed

+45
-40
lines changed

execution/super_capacitor/CMakeLists.txt

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ target_include_directories(super_capacitor PRIVATE
2727
$<INSTALL_INTERFACE:include>)
2828

2929
rclcpp_components_register_node(super_capacitor
30-
PLUGIN "XidiCapacitorDriver"
30+
PLUGIN "SuperCapacitorController"
3131
EXECUTABLE super_capacitor_node)
3232

3333
ament_target_dependencies(super_capacitor
@@ -39,24 +39,12 @@ ament_target_dependencies(super_capacitor
3939
meta_hardware
4040
)
4141

42-
# ament_export_libraries(
43-
# super_capacitor
44-
# )
45-
# ament_export_targets(
46-
# export_${PROJECT_NAME}
47-
# )
48-
49-
50-
5142
pluginlib_export_plugin_description_file(super_capacitor plugins.xml)
5243

5344
install(DIRECTORY launch
5445
DESTINATION share/${PROJECT_NAME}/
5546
)
5647

57-
# install(TARGETS super_capacitor
58-
# LIBRARY DESTINATION lib
59-
# )
6048
install(
6149
TARGETS super_capacitor
6250
EXPORT export_${PROJECT_NAME}

execution/super_capacitor/include/super_capacitor/super_capacitor_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace super_capacitor
1010
class SuperCapacitorBase
1111
{
1212
public:
13-
SuperCapacitorBase();
13+
SuperCapacitorBase() = default;
1414

1515
virtual void init(std::string can_interface) = 0;
1616

execution/super_capacitor/include/super_capacitor/super_capacitor_controller.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
#include "device_interface/msg/capacitor_state.hpp"
1616
#include "meta_hardware/can_driver/can_driver.hpp"
1717

18-
class SuperCapacitorController : public rclcpp::Node
18+
class SuperCapacitorController
1919
{
2020
public:
21-
SuperCapacitorController();
21+
SuperCapacitorController(const rclcpp::NodeOptions & options);
2222
~SuperCapacitorController();
23+
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr get_node_base_interface() const;
2324

2425
private:
26+
rclcpp::Node::SharedPtr node_;
2527
std::shared_ptr<super_capacitor::SuperCapacitorBase> capacitor_;
2628
// double target_power_, referee_power_;
2729
std::atomic<double> target_power_;

execution/super_capacitor/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<test_depend>ament_lint_common</test_depend>
1414

1515
<depend>rclcpp</depend>
16+
<depend>rclcpp_components</depend>
1617
<depend>serial_driver</depend>
1718
<depend>pluginlib</depend>
1819
<depend>device_interface</depend>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<library path="super_capacitor">
2-
<class type="super_capacitor::XidiSuperCapacitorDriver" base_class_type="super_capacitor::SuperCapacitorBase">
2+
<class type="super_capacitor::XidiCapacitorDriver" base_class_type="super_capacitor::SuperCapacitorBase">
33
<description>This is a super capacitor plugin.</description>
44
</class>
55
</library>

execution/super_capacitor/src/super_capacitor_controller.cpp

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,60 @@
99
#include "super_capacitor/super_capacitor_base.h"
1010
#include "super_capacitor/super_capacitor_controller.h"
1111
#include "meta_hardware/can_driver/can_driver.hpp"
12+
#include "super_capacitor/xidi_capacitor_driver.h"
1213

1314

14-
SuperCapacitorController::SuperCapacitorController() : Node("SuperCapacitorController")
15+
SuperCapacitorController::SuperCapacitorController(const rclcpp::NodeOptions & options)
1516
{
17+
node_ = rclcpp::Node::make_shared("super_capacitor_controller", options);
1618

1719
// get parameters
18-
std::string can_interface = this->declare_parameter("can_interface", std::string("can0"));
19-
20+
std::string can_interface = node_->declare_parameter("can_interface", std::string("can0"));
21+
std::string capacitor_device = node_->declare_parameter("capacitor_device", std::string("xidi"));
2022
// create subscriptions
21-
goal_sub_ = this->create_subscription<device_interface::msg::CapacitorCmd>(
22-
"super_capacitor_goal", 10, [this](const device_interface::msg::CapacitorCmd::SharedPtr msg){
23-
goal_sub_callback(msg);
24-
});
25-
23+
// goal_sub_ = node_->create_subscription<device_interface::msg::CapacitorCmd>(
24+
// "super_capacitor_goal", 10, [node_](const device_interface::msg::CapacitorCmd::SharedPtr msg){
25+
// goal_sub_callback(msg);
26+
// });
27+
goal_sub_ = node_->create_subscription<device_interface::msg::CapacitorCmd>(
28+
"super_capacitor_goal", 10, std::bind(&SuperCapacitorController::goal_sub_callback, this, std::placeholders::_1));
2629
// create a timer for publishing super capacitor state
27-
state_pub_ = this->create_publisher<device_interface::msg::CapacitorState>("super_capacitor_state", 10);
28-
pub_timer_ = this->create_wall_timer(
29-
std::chrono::milliseconds(100), [this](){
30-
pub_state();
31-
});
30+
state_pub_ = node_->create_publisher<device_interface::msg::CapacitorState>("super_capacitor_state", 10);
31+
pub_timer_ = node_->create_wall_timer(
32+
std::chrono::milliseconds(100), std::bind(&SuperCapacitorController::pub_state, this));
3233

3334
// complete initialization
34-
tx_timer_ = this->create_wall_timer(
35-
std::chrono::milliseconds(100), [this](){
36-
send_command();
37-
});
35+
tx_timer_ = node_->create_wall_timer(
36+
std::chrono::milliseconds(100), std::bind(&SuperCapacitorController::send_command, this));
3837

3938

40-
pluginlib::ClassLoader<super_capacitor::SuperCapacitorBase> capacitor_loader("super_capacitor","super_capacitor::SuperCapacitorBase");
39+
// pluginlib::ClassLoader<super_capacitor::SuperCapacitorBase> capacitor_loader("super_capacitor","super_capacitor::SuperCapacitorBase");
4140

4241
try
4342
{
44-
capacitor_ = capacitor_loader.createSharedInstance("SuperCapacitorBase");
45-
capacitor_->init(can_interface);
43+
// capacitor_ = capacitor_loader.createSharedInstance("super_capacitor::XidiCapacitorDriver");
44+
// capacitor_->init(can_interface);
45+
if(capacitor_device == "xidi"){
46+
capacitor_ = std::make_shared<super_capacitor::XidiCapacitorDriver>();
47+
capacitor_->init(can_interface);
48+
}else{
49+
RCLCPP_ERROR(node_->get_logger(), "Unknown capacitor device: %s", capacitor_device.c_str());
50+
}
51+
4652
}
4753
catch(pluginlib::PluginlibException& ex)
4854
{
4955
printf("The plugin failed to load for some reason. Error: %s\n", ex.what());
5056
}
51-
RCLCPP_INFO(this->get_logger(), "SuperCapacitorController initialized");
57+
RCLCPP_INFO(node_->get_logger(), "SuperCapacitorController initialized");
58+
}
59+
60+
rclcpp::node_interfaces::NodeBaseInterface::SharedPtr SuperCapacitorController::get_node_base_interface() const
61+
{
62+
return node_->get_node_base_interface();
5263
}
5364

5465
void SuperCapacitorController::goal_sub_callback(const device_interface::msg::CapacitorCmd::SharedPtr msg){
55-
// RCLCPP_INFO(this->get_logger(), "Received super capacitor goal");
5666
target_power_.store(msg->target_power,std::memory_order_relaxed);
5767
referee_power_.store(msg->referee_power,std::memory_order_relaxed);
5868
capacitor_->set_target_power(target_power_.load(std::memory_order_relaxed));
@@ -79,4 +89,8 @@ void SuperCapacitorController::pub_state() {
7989

8090
SuperCapacitorController::~SuperCapacitorController() {
8191
// Destructor implementation
82-
}
92+
}
93+
94+
#include "rclcpp_components/register_node_macro.hpp"
95+
96+
RCLCPP_COMPONENTS_REGISTER_NODE(SuperCapacitorController)

0 commit comments

Comments
 (0)