-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8123d75
commit 3d2769d
Showing
10 changed files
with
333 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# auv_canbus_bridge | ||
|
||
|
||
- [auv\_canbus\_bridge](#auv_canbus_bridge) | ||
- [setup virtual can for testing](#setup-virtual-can-for-testing) | ||
|
||
|
||
## setup virtual can for testing | ||
sudo modprobe vcan | ||
sudo ip link add dev vcan0 type vcan | ||
sudo ip link set up vcan0 |
62 changes: 62 additions & 0 deletions
62
auv_hardware/auv_canbus_bridge/include/auv_canbus_bridge/auv_canbus_bridge_ros.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <algorithm> | ||
#include <array> | ||
#include <memory> | ||
#include <vector> | ||
|
||
#include "auv_canbus_bridge/canbus/helper.hpp" | ||
#include "auv_canbus_bridge/canbus_socket.hpp" | ||
#include "auv_canbus_bridge/modules.hpp" | ||
|
||
namespace auv_hardware { | ||
|
||
class CanbusBridgeROS { | ||
public: | ||
using ModuleBase = auv_hardware::canbus::modules::ModuleBase; | ||
using DrivePulseModule = auv_hardware::canbus::modules::DrivePulseModule; | ||
using LaunchTorpedoModule = | ||
auv_hardware::canbus::modules::LaunchTorpedoModule; | ||
using PowerReportModule = auv_hardware::canbus::modules::PowerReportModule; | ||
using KillswitchReportModule = | ||
auv_hardware::canbus::modules::KillswitchReportModule; | ||
|
||
explicit CanbusBridgeROS(const ros::NodeHandle& node_handle) | ||
: node_handle_{node_handle}, socket_{}, interface_name_{""} { | ||
auto node_handle_private = ros::NodeHandle{"~"}; | ||
node_handle_private.param<std::string>("interface", interface_name_, | ||
"vcan0"); | ||
|
||
socket_.initialize(interface_name_); | ||
|
||
modules_.reserve(4); | ||
|
||
modules_.emplace_back( | ||
std::make_unique<DrivePulseModule>(node_handle_, socket_)); | ||
modules_.emplace_back( | ||
std::make_unique<LaunchTorpedoModule>(node_handle_, socket_)); | ||
modules_.emplace_back( | ||
std::make_unique<PowerReportModule>(node_handle_, socket_)); | ||
modules_.emplace_back( | ||
std::make_unique<KillswitchReportModule>(node_handle_, socket_)); | ||
} | ||
|
||
void spin() { | ||
while (ros::ok()) { | ||
const auto message = socket_.handle(); | ||
if (message.has_value()) { | ||
std::for_each(modules_.begin(), modules_.end(), | ||
[&message](const std::unique_ptr<ModuleBase>& module) { | ||
module->on_received_message(message->id, message->data); | ||
}); | ||
} | ||
|
||
ros::spinOnce(); | ||
} | ||
} | ||
|
||
private: | ||
ros::NodeHandle node_handle_; | ||
auv_hardware::CanbusSocket socket_; | ||
std::string interface_name_; | ||
std::vector<std::unique_ptr<ModuleBase>> modules_; | ||
}; | ||
} // namespace auv_hardware |
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
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
52 changes: 52 additions & 0 deletions
52
...hardware/auv_canbus_bridge/include/auv_canbus_bridge/modules/killswitch_report_module.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
#include <array> | ||
|
||
#include "auv_canbus_bridge/modules/module_base.hpp" | ||
#include "ros/ros.h" | ||
#include "std_msgs/Bool.h" | ||
|
||
namespace auv_hardware { | ||
namespace canbus { | ||
namespace modules { | ||
|
||
class KillswitchReportModule : public ModuleBase { | ||
static constexpr auto kPropulsionBoardStatusReportIdentifier = | ||
auv_hardware::canbus::make_extended_id( | ||
0x00, auv_hardware::canbus::NodeID::ExpansionBoard, | ||
auv_hardware::canbus::Function::Write, | ||
auv_hardware::canbus::Endpoint::PropulsionBoardStatusReport); | ||
|
||
public: | ||
KillswitchReportModule(const ros::NodeHandle &node_handle, | ||
CanbusSocket &socket) | ||
: ModuleBase(node_handle, socket) { | ||
status_report_publisher_ = | ||
ModuleBase::node_handle().advertise<std_msgs::Bool>( | ||
"propulsion_board/status", 10); | ||
ROS_INFO_STREAM("Initialized KillswitchReportModule for CANBUS"); | ||
} | ||
|
||
virtual void on_received_message(auv_hardware::canbus::ExtendedIdType id, | ||
const std::vector<uint8_t> &data) override { | ||
if (id != kPropulsionBoardStatusReportIdentifier) { | ||
return; | ||
} | ||
|
||
if (data.size() != 1) { | ||
ROS_WARN_STREAM("Received invalid status report message"); | ||
return; | ||
} | ||
|
||
auto status_msg = std_msgs::Bool{}; | ||
status_msg.data = data[0]; | ||
|
||
status_report_publisher_.publish(status_msg); | ||
}; | ||
|
||
private: | ||
ros::Publisher status_report_publisher_; | ||
}; | ||
|
||
} // namespace modules | ||
} // namespace canbus | ||
} // namespace auv_hardware |
79 changes: 79 additions & 0 deletions
79
auv_hardware/auv_canbus_bridge/include/auv_canbus_bridge/modules/launch_torpedo_module.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#pragma once | ||
#include <array> | ||
|
||
#include "auv_canbus_bridge/modules/module_base.hpp" | ||
#include "ros/ros.h" | ||
#include "std_srvs/Trigger.h" | ||
|
||
namespace auv_hardware { | ||
namespace canbus { | ||
namespace modules { | ||
|
||
class LaunchTorpedoModule : public ModuleBase { | ||
constexpr static auto kLaunchTorpedo1ExtendedId = | ||
auv_hardware::canbus::make_extended_id( | ||
0x00, auv_hardware::canbus::NodeID::Mainboard, | ||
auv_hardware::canbus::Function::Write, | ||
auv_hardware::canbus::Endpoint::SetHSS1OutputCommand); | ||
|
||
constexpr static auto kLaunchTorpedo2ExtendedId = | ||
auv_hardware::canbus::make_extended_id( | ||
0x00, auv_hardware::canbus::NodeID::Mainboard, | ||
auv_hardware::canbus::Function::Write, | ||
auv_hardware::canbus::Endpoint::SetHSS2OutputCommand); | ||
|
||
public: | ||
LaunchTorpedoModule(const ros::NodeHandle &node_handle, CanbusSocket &socket) | ||
: ModuleBase(node_handle, socket) { | ||
launch_torpedo_1_service_ = ModuleBase::node_handle().advertiseService( | ||
"actuators/torpedo_1/launch", | ||
&LaunchTorpedoModule::launch_torpedo_1_handler, this); | ||
|
||
launch_torpedo_2_service_ = ModuleBase::node_handle().advertiseService( | ||
"actuators/torpedo_2/launch", | ||
&LaunchTorpedoModule::launch_torpedo_2_handler, this); | ||
|
||
ROS_INFO_STREAM("Initialized LaunchTorpedoModule for CANBUS"); | ||
} | ||
|
||
bool launch_torpedo_1_handler(std_srvs::Trigger::Request &req, | ||
std_srvs::Trigger::Response &res) { | ||
res.success = send_hss_pulse(kLaunchTorpedo1ExtendedId); | ||
res.message = "Torpedo 1 launched"; | ||
return true; | ||
} | ||
|
||
bool launch_torpedo_2_handler(std_srvs::Trigger::Request &req, | ||
std_srvs::Trigger::Response &res) { | ||
res.success = send_hss_pulse(kLaunchTorpedo2ExtendedId); | ||
res.message = "Torpedo 2 launched"; | ||
return true; | ||
} | ||
|
||
virtual void on_received_message(auv_hardware::canbus::ExtendedIdType id, | ||
const std::vector<uint8_t> &data) override { | ||
// | ||
}; | ||
|
||
protected: | ||
bool send_hss_pulse(auv_hardware::canbus::ExtendedIdType id) { | ||
bool success = true; | ||
std::array<uint8_t, 1> data = {1}; | ||
|
||
success = dispatch_message(id, data); | ||
ros::Duration(1.0).sleep(); | ||
|
||
data[0] = 0; | ||
success &= dispatch_message(id, data); | ||
|
||
return success; | ||
} | ||
|
||
private: | ||
ros::ServiceServer launch_torpedo_1_service_; | ||
ros::ServiceServer launch_torpedo_2_service_; | ||
}; | ||
|
||
} // namespace modules | ||
} // namespace canbus | ||
} // namespace auv_hardware |
Oops, something went wrong.