KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
motor_and_io_bridge.cpp
1 /*
2  * Copyright (c) 2015-2016, Thomas Keh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "bridge.h"
33 #include "logger.h"
34 #include "joint_state_publisher.h"
35 #include "joint_state_subscriber.h"
36 #include "entry_publisher.h"
37 #include "entry_subscriber.h"
38 
39 #include <thread>
40 #include <chrono>
41 #include <memory>
42 
43 // #define BUSNAME ... // set by CMake
44 // #define BAUDRATE ... // set by CMake
45 
46 int main(int argc, char* argv[]) {
47 
48  // Set the name of your CAN bus. "slcan0" is a common bus name
49  // for the first SocketCAN device on a Linux system.
50  const std::string busname = "slcan0";
51 
52  // Set the baudrate of your CAN bus. Most drivers support the values
53  // "1M", "500K", "125K", "100K", "50K", "20K", "10K" and "5K".
54  const std::string baudrate = "500K";
55 
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).");
58 
59  const double loop_rate = 10; // [Hz]
60 
61  kaco::Master master;
62  if (!master.start(busname, baudrate)) {
63  ERROR("Starting master failed.");
64  return EXIT_FAILURE;
65  }
66 
67  //master.core.nmt.reset_all_nodes();
68 
69  std::this_thread::sleep_for(std::chrono::seconds(1));
70  size_t num_devices_required = 1;
71  while (master.num_devices()<num_devices_required) {
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...");
74  master.core.nmt.discover_nodes();
75  std::this_thread::sleep_for(std::chrono::seconds(1));
76  }
77 
78  // Create bridge
79  ros::init(argc, argv, "canopen_bridge");
80  kaco::Bridge bridge;
81 
82  bool found = false;
83  for (size_t i=0; i<master.num_devices(); ++i) {
84 
85  kaco::Device& device = master.get_device(i);
86  device.start();
87 
89 
90  const auto profile = device.get_device_profile_number();
91  PRINT("Found CiA "<<std::dec<<(unsigned)profile<<" device with node ID "<<device.get_node_id()<<": "<<device.get_entry("manufacturer_device_name"));
92 
93  if (profile==401) {
94 
95  found = true;
96 
97  // TODO: we should determine the number of input / output bytes fiŕst.
98 
99  // map PDOs (optional)
100  device.add_receive_pdo_mapping(0x188, "Read input 8-bit/Digital Inputs 1-8", 0); // offest 0
101  device.add_receive_pdo_mapping(0x188, "Read input 8-bit/Digital Inputs 9-16", 1); // offset 1
102 
103  // set some output (optional)
104  device.set_entry("Write output 8-bit/Digital Outputs 1-8", (uint8_t) 0xFF);
105 
106  // create a publisher for reading second 8-bit input and add it to the bridge
107  // communication via POD
108  auto iopub = std::make_shared<kaco::EntryPublisher>(device, "Read input 8-bit/Digital Inputs 9-16");
109  bridge.add_publisher(iopub);
110 
111  // create a subscriber for editing IO output and add it to the bridge
112  // communication via SOD
113  auto iosub = std::make_shared<kaco::EntrySubscriber>(device, "Write output 8-bit/Digital Outputs 1-8");
114  bridge.add_subscriber(iosub);
115 
116  } else if (profile==402) {
117 
118  found = true;
119 
120  PRINT("Set position mode");
121  device.set_entry("modes_of_operation", device.get_constant("profile_position_mode"));
122 
123  PRINT("Enable operation");
124  device.execute("enable_operation");
125 
126  // TODO: target_position should be mapped to a PDO
127 
128  auto jspub = std::make_shared<kaco::JointStatePublisher>(device, 0, 350000);
129  bridge.add_publisher(jspub, loop_rate);
130 
131  auto jssub = std::make_shared<kaco::JointStateSubscriber>(device, 0, 350000);
132  bridge.add_subscriber(jssub);
133 
134  }
135 
136  }
137 
138  if (!found) {
139  ERROR("This example is intended for use with a CiA 402 device but I can't find one.");
140  return EXIT_FAILURE;
141  }
142 
143  bridge.run();
144 
145 }
Device & get_device(size_t index) const
Returns a reference to a slave device object.
Definition: master.cpp:84
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 ...
Definition: device.cpp:102
NMT nmt
The NMT sub-protocol.
Definition: core.h:109
This class is a bridge between a ROS network and a CanOpen network.
Definition: bridge.h:48
void start()
Starts the node via NMT protocol and loads mandatory entries, operations, and constants.
Definition: device.cpp:54
size_t num_devices() const
Returns the number of slave devices in the network.
Definition: master.cpp:80
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...
Definition: device.cpp:163
void run()
Runs the ROS spinner and blocks until shutdown (e.g. via Ctrl+c).
Definition: bridge.cpp:60
bool start(const std::string busname, const std::string &baudrate)
Starts master and creates Core.
Definition: master.cpp:51
uint16_t get_device_profile_number()
Returns the CiA profile number.
Definition: device.cpp:274
void discover_nodes()
Discovers nodes in the network via node guard protocol.
Definition: nmt.cpp:67
This class represents a master node. It listens for new slaves and provides access to them via get_sl...
Definition: master.h:47
uint8_t get_node_id() const
Returns the node ID of the device.
Definition: device.cpp:74
Core core
Core instance.
Definition: master.h:96
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.
Definition: device.cpp:327
This class represents a CanOpen slave device in the network.
Definition: device.h:83
void add_publisher(std::shared_ptr< Publisher > publisher, double loop_rate=10)
Adds a Publisher, advertises it and publishes messages repeatedly.
Definition: bridge.cpp:41
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...
Definition: device.cpp:123
void add_subscriber(std::shared_ptr< Subscriber > subscriber)
Adds a Subscriber, which can advertise itself and receive messages on its own.
Definition: bridge.cpp:55