34 #include "joint_state_publisher.h"
35 #include "joint_state_subscriber.h"
36 #include "entry_publisher.h"
37 #include "entry_subscriber.h"
46 int main(
int argc,
char* argv[]) {
50 const std::string busname =
"slcan0";
54 const std::string baudrate =
"500K";
56 PRINT(
"This example publishes and subscribes JointState messages for each connected CiA 402 device as well as"
57 <<
"uint8 messages for each connected digital IO device (CiA 401).");
59 const double loop_rate = 10;
62 if (!master.
start(busname, baudrate)) {
63 ERROR(
"Starting master failed.");
69 std::this_thread::sleep_for(std::chrono::seconds(1));
70 size_t num_devices_required = 1;
72 ERROR(
"Number of devices found: " << master.
num_devices() <<
". Waiting for " << num_devices_required <<
".");
73 PRINT(
"Trying to discover more nodes via NMT Node Guarding...");
75 std::this_thread::sleep_for(std::chrono::seconds(1));
79 ros::init(argc, argv,
"canopen_bridge");
91 PRINT(
"Found CiA "<<std::dec<<(
unsigned)profile<<
" device with node ID "<<device.
get_node_id()<<
": "<<device.
get_entry(
"manufacturer_device_name"));
104 device.
set_entry(
"Write output 8-bit/Digital Outputs 1-8", (uint8_t) 0xFF);
108 auto iopub = std::make_shared<kaco::EntryPublisher>(device,
"Read input 8-bit/Digital Inputs 9-16");
113 auto iosub = std::make_shared<kaco::EntrySubscriber>(device,
"Write output 8-bit/Digital Outputs 1-8");
116 }
else if (profile==402) {
120 PRINT(
"Set position mode");
121 device.set_entry(
"modes_of_operation", device.get_constant(
"profile_position_mode"));
123 PRINT(
"Enable operation");
124 device.execute(
"enable_operation");
128 auto jspub = std::make_shared<kaco::JointStatePublisher>(device, 0, 350000);
131 auto jssub = std::make_shared<kaco::JointStateSubscriber>(device, 0, 350000);
139 ERROR(
"This example is intended for use with a CiA 402 device but I can't find one.");
Device & get_device(size_t index) const
Returns a reference to a slave device object.
const Value & get_entry(const std::string &entry_name, const ReadAccessMethod access_method=ReadAccessMethod::use_default)
Gets the value of a dictionary entry by name internally. If there is no cached value or the entry is ...
NMT nmt
The NMT sub-protocol.
This class is a bridge between a ROS network and a CanOpen network.
void start()
Starts the node via NMT protocol and loads mandatory entries, operations, and constants.
size_t num_devices() const
Returns the number of slave devices in the network.
void add_receive_pdo_mapping(uint16_t cob_id, const std::string &entry_name, uint8_t offset)
Adds a receive PDO mapping. This means values sent by the device via PDO are saved into the dictionar...
void run()
Runs the ROS spinner and blocks until shutdown (e.g. via Ctrl+c).
bool start(const std::string busname, const std::string &baudrate)
Starts master and creates Core.
uint16_t get_device_profile_number()
Returns the CiA profile number.
void discover_nodes()
Discovers nodes in the network via node guard protocol.
This class represents a master node. It listens for new slaves and provides access to them via get_sl...
uint8_t get_node_id() const
Returns the node ID of the device.
std::string load_dictionary_from_library()
Tries to load the most specific EDS file available in KaCanOpen's internal EDS library. This is either device specific, CiA profile specific, or mandatory CiA 301.
This class represents a CanOpen slave device in the network.
void add_publisher(std::shared_ptr< Publisher > publisher, double loop_rate=10)
Adds a Publisher, advertises it and publishes messages repeatedly.
void set_entry(const std::string &entry_name, const Value &value, const WriteAccessMethod access_method=WriteAccessMethod::use_default)
Sets the value of a dictionary entry by name internally. If the entry is configured to send an SDO on...
void add_subscriber(std::shared_ptr< Subscriber > subscriber)
Adds a Subscriber, which can advertise itself and receive messages on its own.