Skip to content

Commit 4e864b7

Browse files
committed
pacific spirit super capacitor finished
1 parent a63e95b commit 4e864b7

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

execution/super_capacitor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ find_package(rclcpp_components)
2020
add_library(super_capacitor SHARED
2121
src/super_capacitor_controller.cpp
2222
src/xidi_capacitor_driver.cpp
23+
src/pacific_spirit_capacitor_driver.cpp
2324
)
2425

2526
target_include_directories(super_capacitor PRIVATE
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef PACIFIC_SPIRIT_CAPACITOR_DRIVER_H
2+
#define PACIFIC_SPIRIT_CAPACITOR_DRIVER_H
3+
4+
#include <thread>
5+
#include <vector>
6+
#include <unordered_map>
7+
#include <stop_token>
8+
#include <memory>
9+
#include "super_capacitor/super_capacitor_base.h"
10+
#include "meta_hardware/can_driver/can_driver.hpp"
11+
12+
namespace super_capacitor {
13+
14+
class PacificSpiritCapacitorDriver : public super_capacitor::SuperCapacitorBase
15+
{
16+
public:
17+
explicit PacificSpiritCapacitorDriver();
18+
19+
void init(std::string can_interface) override;
20+
21+
void set_target_power(double target_power) override;
22+
23+
void set_referee_power(double referee_power) override;
24+
25+
std::string get_device_name() override;
26+
27+
std::unordered_map<std::string, double> get_state() override;
28+
29+
~PacificSpiritCapacitorDriver() override;
30+
31+
protected:
32+
double target_power_, referee_power_;
33+
std::vector<can_filter> can_filters_;
34+
std::unique_ptr<meta_hardware::CanDriver> can_driver_;
35+
void rx_loop(std::stop_token stop_token) override;
36+
std::unique_ptr<std::jthread> rx_thread_;
37+
double max_discharge_power_, base_power_, cap_energy_percentage_, cap_state_;
38+
void tx() override;
39+
40+
};
41+
}
42+
43+
44+
#endif // PACIFIC_SPIRIT_CAPACITOR_DRIVER_H
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <thread>
2+
#include <iostream>
3+
#include <exception>
4+
#include <linux/can.h>
5+
#include <stdexcept>
6+
#include <string>
7+
8+
#include "meta_hardware/can_driver/can_driver.hpp"
9+
#include "meta_hardware/can_driver/can_exceptions.hpp"
10+
#include "super_capacitor/super_capacitor_base.h"
11+
#include "super_capacitor/pacific_spirit_capacitor_driver.h"
12+
13+
namespace super_capacitor {
14+
PacificSpiritCapacitorDriver::PacificSpiritCapacitorDriver()
15+
: super_capacitor::SuperCapacitorBase(){}
16+
17+
void PacificSpiritCapacitorDriver::init(std::string can_interface){
18+
can_filters_.push_back({.can_id = 0x2c8, .can_mask = CAN_EFF_MASK});
19+
// Initialize CAN driver
20+
can_driver_ = std::make_unique<meta_hardware::CanDriver>(can_interface, false, can_filters_);
21+
22+
// Initialize RX thread
23+
rx_thread_ =
24+
std::make_unique<std::jthread>([this](std::stop_token s) { rx_loop(s); });
25+
}
26+
27+
void PacificSpiritCapacitorDriver::set_target_power(double power) {
28+
target_power_ = power;
29+
}
30+
31+
void PacificSpiritCapacitorDriver::set_referee_power(double power) {
32+
referee_power_ = power;
33+
}
34+
35+
std::unordered_map<std::string, double> PacificSpiritCapacitorDriver::get_state() {
36+
return {
37+
{"max_discharge_power", max_discharge_power_},
38+
{"base_power", base_power_},
39+
{"cap_energy_percentage", cap_energy_percentage_},
40+
{"cap_state", cap_state_}
41+
};
42+
}
43+
44+
std::string PacificSpiritCapacitorDriver::get_device_name() {
45+
return "Xidi Capacitor";
46+
}
47+
48+
void PacificSpiritCapacitorDriver::rx_loop(std::stop_token stop_token) {
49+
while (!stop_token.stop_requested()) {
50+
try {
51+
can_frame can_msg = can_driver_->read(2000);
52+
53+
max_discharge_power_ = static_cast<double>(static_cast<uint16_t>(static_cast<uint16_t>(can_msg.data[0]) |
54+
(static_cast<uint16_t>(can_msg.data[1]) << 8)) / 100.0);
55+
base_power_ = static_cast<double>(static_cast<uint16_t>(static_cast<uint16_t>(can_msg.data[2]) |
56+
(static_cast<uint16_t>(can_msg.data[3]) << 8)) / 100.0);
57+
cap_energy_percentage_ = static_cast<double>(static_cast<uint16_t>(static_cast<uint16_t>(can_msg.data[4]) |
58+
(static_cast<uint16_t>(can_msg.data[5]) << 8)) / 100.0);
59+
cap_state_ = static_cast<double>(static_cast<uint16_t>(static_cast<uint16_t>(can_msg.data[6]) |
60+
(static_cast<uint16_t>(can_msg.data[7]) << 8)) / 100.0);
61+
} catch (const meta_hardware::CanIOException &e) {
62+
std::cerr << "Error reading super capacitor CAN message: " << e.what() << std::endl;
63+
}
64+
}
65+
66+
}
67+
68+
void PacificSpiritCapacitorDriver::tx(){
69+
can_frame tx_frame{.can_id = 0x2c7, .len = 8, .data = {0}};
70+
tx_frame.data[0] = static_cast<uint8_t>(static_cast<uint32_t>(target_power_ * 100.0) & 0xFF);
71+
tx_frame.data[1] = static_cast<uint8_t>(static_cast<uint32_t>(target_power_ * 100.0) >> 8);
72+
tx_frame.data[2] = static_cast<uint8_t>(static_cast<uint32_t>(referee_power_ * 100.0) & 0xFF);
73+
tx_frame.data[3] = static_cast<uint8_t>(static_cast<uint32_t>(referee_power_ * 100.0) >> 8);
74+
tx_frame.data[4] = 0x12;
75+
tx_frame.data[5] = 0x20;
76+
tx_frame.data[6] = 0x12;
77+
tx_frame.data[7] = 0x07;
78+
try {
79+
can_driver_->write(tx_frame);
80+
} catch (meta_hardware::CanIOException &e) {
81+
std::cerr << "Error writing CAN message: " << e.what() << std::endl;
82+
}
83+
}
84+
85+
PacificSpiritCapacitorDriver::~PacificSpiritCapacitorDriver() {
86+
if (rx_thread_ && rx_thread_->joinable()) {
87+
rx_thread_->request_stop(); // Gracefully stop the thread
88+
rx_thread_->join();
89+
}
90+
}
91+
92+
}
93+
94+
// #include <pluginlib/class_list_macros.hpp>
95+
// PLUGINLIB_EXPORT_CLASS(super_capacitor::PacificSpiritCapacitorDriver, super_capacitor::SuperCapacitorBase)

execution/super_capacitor/src/super_capacitor_controller.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "super_capacitor/super_capacitor_controller.h"
1111
#include "meta_hardware/can_driver/can_driver.hpp"
1212
#include "super_capacitor/xidi_capacitor_driver.h"
13+
#include "super_capacitor/pacific_spirit_capacitor_driver.h"
1314

1415

1516
SuperCapacitorController::SuperCapacitorController(const rclcpp::NodeOptions & options)
@@ -45,6 +46,9 @@ SuperCapacitorController::SuperCapacitorController(const rclcpp::NodeOptions & o
4546
if(capacitor_device == "xidi"){
4647
capacitor_ = std::make_shared<super_capacitor::XidiCapacitorDriver>();
4748
capacitor_->init(can_interface);
49+
}else if(capacitor_device == "pacific_spirit"){
50+
capacitor_ = std::make_shared<super_capacitor::PacificSpiritCapacitorDriver>();
51+
capacitor_->init(can_interface);
4852
}else{
4953
RCLCPP_ERROR(node_->get_logger(), "Unknown capacitor device: %s", capacitor_device.c_str());
5054
}

0 commit comments

Comments
 (0)