CanOpenController.hpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of the SCHUNK Canopen Driver suite.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 SCHUNK GmbH, Lauffen/Neckar Germany
12 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
22 //----------------------------------------------------------------------
23 #ifndef CANOPENCONTROLLER_HPP
24 #define CANOPENCONTROLLER_HPP
25 
26 #include "Logging.h"
27 #include "exceptions.h"
28 #include "DS402Group.h"
29 
30 namespace icl_hardware {
31 namespace canopen_schunk {
32 
33 template <typename GroupT>
34 void CanOpenController::addGroup(const std::string& identifier)
35 {
36  std::string sanitized_identifier = sanitizeString(identifier);
37  if (m_groups.find(sanitized_identifier) == m_groups.end())
38  {
39  DS301Group::Ptr group(new GroupT(sanitized_identifier));
40 
41  #ifdef _IC_BUILDER_ICL_COMM_WEBSOCKET_
42  group->registerWSBroadcaster(m_ws_broadcaster);
43  #endif // _IC_BUILDER_ICL_COMM_WEBSOCKET_
44 
45  m_groups[sanitized_identifier] = group;
46 
47  }
48  else
49  {
50  LOGGING_ERROR_C(CanOpen, CanOpenController, "Group with the given identifier " << sanitized_identifier
51  << " already exists. Not adding new group." << endl);
52  }
53 }
54 
55 template <typename GroupT>
57 {
58  std::string sanitized_index = sanitizeString(index);
60  if (m_groups.find(sanitized_index) != m_groups.end())
61  {
62  group = boost::dynamic_pointer_cast<GroupT>(m_groups[sanitized_index]);
63  if (!group)
64  {
65  LOGGING_ERROR_C(CanOpen, CanOpenController, "Cannot cast group to requested type. Returning null pointer." << endl);
66  }
67  }
68  else
69  {
70  std::stringstream ss;
71  ss << "No group with the given index " << sanitized_index
72  << " exists. Returning null pointer.";
73  throw NotFoundException(ss.str());
74  }
75  return group;
76  }
77 
78 
79 template <typename NodeT>
80 void CanOpenController::addNode(const uint8_t node_id, const std::string& group_name)
81 {
82  std::string sanitized_identifier = sanitizeString(group_name);
83  std::map<std::string, DS301Group::Ptr>::iterator group_it;
84  group_it = m_groups.find(sanitized_identifier);
85 
86 
87  if (m_nodes.find(node_id) == m_nodes.end())
88  {
89  if (group_it != m_groups.end())
90  {
91  DS301Node::Ptr new_node;
92 
93  // TODO: Maybe this can be done prettier with templates, however for now this works
94  DS402Group::Ptr ds402_ptr;
95  if (ds402_ptr = boost::dynamic_pointer_cast<DS402Group>(group_it->second))
96  {
97  new_node = ds402_ptr->addNode<NodeT>(node_id, m_can_device, m_heartbeat_monitor);
98  }
99  else
100  {
101  new_node = group_it->second->addNode<NodeT>(node_id, m_can_device, m_heartbeat_monitor);
102  }
103 
104  #ifdef _IC_BUILDER_ICL_COMM_WEBSOCKET_
105  new_node->registerWSBroadcaster(m_ws_broadcaster);
106  #endif // _IC_BUILDER_ICL_COMM_WEBSOCKET_
107 
108  m_nodes.insert (std::pair<uint8_t, DS301Node::Ptr>(node_id, new_node));
109  }
110  else
111  {
112  LOGGING_ERROR_C(CanOpen, CanOpenController, "No group with the given index " << sanitized_identifier
113  << " exists. New node not added!" << endl);
114  }
115  }
116  else
117  {
118  LOGGING_ERROR_C(CanOpen, CanOpenController, "Node with CANOPEN ID " << node_id
119  << " already exists. Not adding new node." << endl);
120  }
121 }
122 
123 template <typename NodeT>
125 {
126  if (m_nodes.find(node_id) == m_nodes.end())
127  {
128  std::stringstream ss;
129  ss << "No node with the given id '" << static_cast<int>(node_id) << "' found.";
130  throw NotFoundException(ss.str());
131  }
132 
133  if (boost::dynamic_pointer_cast<NodeT>(m_nodes[node_id]))
134  {
135  return boost::dynamic_pointer_cast<NodeT>(m_nodes[node_id]);
136  }
137  else
138  {
139  throw std::bad_cast();
140  }
141 }
142 
143 }} //End of NS
144 
145 #endif
std::map< uint8_t, DS301Node::Ptr > m_nodes
Map of all nodes with id identifier.
The CanOpenController class is the main entry point for any calls to the canOpen System.
std::map< std::string, DS301Group::Ptr > m_groups
Map of all node groups, with string identifier.
unsigned char uint8_t
boost::shared_ptr< NodeT > getNode(const uint8_t node_id)
Returns a shared pointer handle to a node.
This exception is thrown if a requested node or node group does not exist.
Definition: exceptions.h:159
ThreadStream & endl(ThreadStream &stream)
boost::shared_ptr< GroupT > getGroup(const std::string &index="default")
Returns a shared pointer to the group with a given index if possible.
boost::shared_ptr< icl_comm::websocket::WsBroadcaster > m_ws_broadcaster
Interface to send out diagnostics data. Only available if compiled with IC_BUILDER_ICL_COMM_WEBSOCKET...
CanDevPtr m_can_device
Handle for the can device.
std::string sanitizeString(const std::string &text)
This function removes all non-graphical characters from the given string.
Definition: helper.cpp:62
void addNode(const uint8_t node_id, const std::string &group_name="default")
Adds a new node to a group. If the group is not found (e.g. it was not created before), nothing will be done.
void addGroup(const std::string &identifier)
Adds a new node group with a given identifier. The group&#39;s type is given as template parameter...
#define LOGGING_ERROR_C(streamname, classname, arg)


schunk_canopen_driver
Author(s): Felix Mauch , Georg Heppner
autogenerated on Mon Jun 10 2019 15:07:49