KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
ros.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 "entry_publisher.h"
35 #include "entry_subscriber.h"
36 
37 #include <thread>
38 #include <chrono>
39 #include <memory>
40 
41 int main(int argc, char* argv[]) {
42 
43  // Set the name of your CAN bus. "slcan0" is a common bus name
44  // for the first SocketCAN device on a Linux system.
45  const std::string busname = "slcan0";
46 
47  // Set the baudrate of your CAN bus. Most drivers support the values
48  // "1M", "500K", "125K", "100K", "50K", "20K", "10K" and "5K".
49  const std::string baudrate = "500K";
50 
51  kaco::Master master;
52  if (!master.start(busname, baudrate)) {
53  ERROR("Starting master failed.");
54  return EXIT_FAILURE;
55  }
56 
57  std::this_thread::sleep_for(std::chrono::seconds(1));
58 
59  if (master.num_devices()<1) {
60  ERROR("No devices found.");
61  return EXIT_FAILURE;
62  }
63 
64  // should be a 401 device
65  kaco::Device& device = master.get_device(0);
66  device.start();
68  uint16_t profile = device.get_device_profile_number();
69 
70  if (profile != 401) {
71  ERROR("This example is intended for use with a CiA 401 device. You plugged a device with profile number "<<std::dec<<profile);
72  return EXIT_FAILURE;
73  }
74 
75  device.print_dictionary();
76 
77  DUMP(device.get_entry("Manufacturer device name"));
78 
79  // map PDOs (optional)
80  device.add_receive_pdo_mapping(0x188, "Read input 8-bit/Digital Inputs 1-8", 0); // offset 0
81  device.add_receive_pdo_mapping(0x188, "Read input 8-bit/Digital Inputs 9-16", 1); // offset 1
82 
83  // set some output (optional)
84  device.set_entry("Write output 8-bit/Digital Outputs 1-8", (uint8_t) 0xFF);
85 
86  ros::init(argc, argv, "canopen_bridge");
87 
88  // Create bridge
89  kaco::Bridge bridge;
90 
91  // create a publisher for reading second 8-bit input and add it to the bridge
92  // communication via POD
93  // publishing rate = 10 Hz
94  auto iopub = std::make_shared<kaco::EntryPublisher>(device, "Read input 8-bit/Digital Inputs 9-16");
95  bridge.add_publisher(iopub,1);
96 
97  // create a subscriber for editing IO output and add it to the bridge
98  // communication via SOD
99  auto iosub = std::make_shared<kaco::EntrySubscriber>(device, "Write output 8-bit/Digital Outputs 1-8");
100  bridge.add_subscriber(iosub);
101 
102  // run ROS loop
103  bridge.run();
104 
105  master.stop();
106 
107 }
void print_dictionary() const
Prints the dictionary together with currently cached values to command line.
Definition: device.cpp:461
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
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 stop()
Stops master and core.
Definition: master.cpp:75
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
This class represents a master node. It listens for new slaves and provides access to them via get_sl...
Definition: master.h:47
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