uavcan_node.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-2023 RaccoonLab.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 3.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Author: Dmitry Ponomarev <ponomarevda96@gmail.com>
17  */
18 
19 #include <ros/ros.h>
20 
21 #include <iostream>
22 #include <vector>
23 #include <string>
24 #include <map>
25 
26 #include "converters.hpp"
27 
28 
32  static UavcanNode uavcan_node(getCanDriver(), getSystemClock());
33  return uavcan_node;
34 }
35 
36 
37 int main(int argc, char** argv) {
39  ros::init(argc, argv, "dronecan_communicator");
42  }
43  ros::NodeHandle ros_node;
44 
45 
47  int node_id;
48  std::string node_id_param = "uavcan_node_id";
49  if (ros_node.getParam(node_id_param.c_str(), node_id)) {
50  std::cout << "Param " << node_id_param << ": " << node_id << std::endl;
51  } else {
52  std::cout << "Param " << node_id_param << ": is missing in your config file." << std::endl;
53  return -1;
54  }
55 
56  std::string uavcan_node_name;
57  std::string node_name_param = "uavcan_node_name";
58  if (ros_node.getParam(node_name_param.c_str(), uavcan_node_name)) {
59  std::cout << "Param " << node_name_param << ": " << uavcan_node_name << std::endl;
60  } else {
61  std::cout << "Param " << node_name_param << ": is missing in your config file." << std::endl;
62  return -1;
63  }
64 
65  auto& uavcan_node = getUavcanNode();
66  const int self_node_id = node_id;
67  uavcan_node.setNodeID(self_node_id);
68  uavcan_node.setName(uavcan_node_name.c_str());
69  uavcan::protocol::SoftwareVersion sw_version;
70  sw_version.major = 1;
71  uavcan_node.setSoftwareVersion(sw_version);
72  uavcan::protocol::HardwareVersion hw_version;
73  hw_version.major = 1;
74  uavcan_node.setHardwareVersion(hw_version);
75  const int node_start_res = uavcan_node.start();
76  if (node_start_res < 0) {
77  throw std::runtime_error("ERROR: Uavcan_node: " + std::to_string(node_start_res));
78  }
79 
80 
82  std::vector<std::string> bridges;
83  std::vector<std::unique_ptr<Converter>> converters;
84  if (ros_node.getParam("bridges", bridges)) {
85  if (bridges.size() == 0 || bridges.size() % 2 == 1) {
86  std::cout << "ERROR. Param problem: The size of `bridges` must be even." << std::endl;
87  return -1;
88  }
89  for (size_t idx = 0; idx < bridges.size(); idx += 2) {
90  auto bridge_name = bridges[idx];
91  auto topic_name = bridges[idx + 1].c_str();
92  auto converter = instantiate_converter(bridge_name, ros_node, uavcan_node, topic_name);
93  std::cout << idx / 2 << ". Creation of converter with name `" << bridge_name << "` "
94  << "and topic `" << topic_name << "` has been ";
95  if (converter.get() == nullptr) {
96  std::cout << "\033[1;31m" << "failed: wrong converter name. Exit." << "\033[0m" << std::endl;
97  return -1;
98  } else {
99  converters.push_back(std::move(converter));
100  std::cout << "successful." << std::endl;
101  }
102  }
103  } else {
104  std::cout << "Param problem: you should specify bridges in your config file." << std::endl;
105  }
106 
107  // 4. Spinner
108  uavcan_node.setModeOperational();
109  uavcan_node.setHealthOk();
110  while (ros::ok()) {
111  const int res = uavcan_node.spin(uavcan::MonotonicDuration::fromMSec(2));
112  ros::spinOnce();
113  if (res < 0) {
114  std::cerr << "Transient failure: " << res << std::endl;
115  }
116  }
117 }
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
converters.hpp
getCanDriver
uavcan::ICanDriver & getCanDriver()
Definition: platform_linux.cpp:13
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
ros.h
uavcan::DurationBase< MonotonicDuration >::fromMSec
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
ros::spinOnce
ROSCPP_DECL void spinOnce()
uavcan::ICanDriver
Definition: libuavcan/libuavcan/include/uavcan/driver/can.hpp:207
ros::console::set_logger_level
ROSCONSOLE_DECL bool set_logger_level(const std::string &name, levels::Level level)
ros::ok
ROSCPP_DECL bool ok()
ros::console::levels::Debug
Debug
getUavcanNode
static UavcanNode & getUavcanNode()
Definition: uavcan_node.cpp:31
ros::console::notifyLoggerLevelsChanged
ROSCONSOLE_DECL void notifyLoggerLevelsChanged()
main
int main(int argc, char **argv)
Definition: uavcan_node.cpp:37
ROSCONSOLE_DEFAULT_NAME
#define ROSCONSOLE_DEFAULT_NAME
uavcan::ISystemClock
Definition: system_clock.hpp:19
instantiate_converter
std::unique_ptr< Converter > instantiate_converter(std::string converter_name, ros::NodeHandle &ros_node, UavcanNode &uavcan_node, const char *ros_topic)
Definition: converters.cpp:223
getSystemClock
uavcan::ISystemClock & getSystemClock()
Definition: platform_linux.cpp:8
uavcan::Node
Definition: node.hpp:38
ros::NodeHandle


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03