CanOpenController.hpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of the SCHUNK Canopen Driver suite.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 SCHUNK GmbH, Lauffen/Neckar Germany
00012 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00022 //----------------------------------------------------------------------
00023 #ifndef CANOPENCONTROLLER_HPP
00024 #define CANOPENCONTROLLER_HPP
00025 
00026 #include "Logging.h"
00027 #include "exceptions.h"
00028 #include "DS402Group.h"
00029 
00030 namespace icl_hardware {
00031 namespace canopen_schunk {
00032 
00033 template <typename GroupT>
00034 void CanOpenController::addGroup(const std::string& identifier)
00035 {
00036   std::string sanitized_identifier = sanitizeString(identifier);
00037   if (m_groups.find(sanitized_identifier) == m_groups.end())
00038   {
00039     DS301Group::Ptr group(new GroupT(sanitized_identifier));
00040 
00041     #ifdef _IC_BUILDER_ICL_COMM_WEBSOCKET_
00042     group->registerWSBroadcaster(m_ws_broadcaster);
00043     #endif // _IC_BUILDER_ICL_COMM_WEBSOCKET_
00044 
00045     m_groups[sanitized_identifier] = group;
00046 
00047   }
00048   else
00049   {
00050     LOGGING_ERROR_C(CanOpen, CanOpenController, "Group with the given identifier " << sanitized_identifier
00051       << " already exists. Not adding new group." << endl);
00052   }
00053 }
00054 
00055 template <typename GroupT>
00056 boost::shared_ptr<GroupT> CanOpenController::getGroup (const std::string& index)
00057 {
00058     std::string sanitized_index = sanitizeString(index);
00059     boost::shared_ptr<GroupT> group;
00060     if (m_groups.find(sanitized_index) != m_groups.end())
00061     {
00062       group = boost::dynamic_pointer_cast<GroupT>(m_groups[sanitized_index]);
00063       if (!group)
00064       {
00065         LOGGING_ERROR_C(CanOpen, CanOpenController, "Cannot cast group to requested type. Returning null pointer." << endl);
00066       }
00067     }
00068     else
00069     {
00070       std::stringstream ss;
00071       ss << "No group with the given index " << sanitized_index
00072         << " exists. Returning null pointer.";
00073       throw NotFoundException(ss.str());
00074     }
00075     return group;
00076   }
00077 
00078 
00079 template <typename NodeT>
00080 void CanOpenController::addNode(const uint8_t node_id, const std::string& group_name)
00081 {
00082   std::string sanitized_identifier = sanitizeString(group_name);
00083   std::map<std::string, DS301Group::Ptr>::iterator group_it;
00084   group_it = m_groups.find(sanitized_identifier);
00085 
00086 
00087   if (m_nodes.find(node_id) == m_nodes.end())
00088   {
00089     if (group_it != m_groups.end())
00090     {
00091       DS301Node::Ptr new_node;
00092 
00093       // TODO: Maybe this can be done prettier with templates, however for now this works
00094       DS402Group::Ptr ds402_ptr;
00095       if (ds402_ptr = boost::dynamic_pointer_cast<DS402Group>(group_it->second))
00096       {
00097         new_node = ds402_ptr->addNode<NodeT>(node_id, m_can_device, m_heartbeat_monitor);
00098       }
00099       else
00100       {
00101         new_node = group_it->second->addNode<NodeT>(node_id, m_can_device, m_heartbeat_monitor);
00102       }
00103 
00104       #ifdef _IC_BUILDER_ICL_COMM_WEBSOCKET_
00105         new_node->registerWSBroadcaster(m_ws_broadcaster);
00106       #endif // _IC_BUILDER_ICL_COMM_WEBSOCKET_
00107 
00108       m_nodes.insert (std::pair<uint8_t, DS301Node::Ptr>(node_id, new_node));
00109     }
00110     else
00111     {
00112       LOGGING_ERROR_C(CanOpen, CanOpenController, "No group with the given index " << sanitized_identifier
00113         << " exists. New node not added!" << endl);
00114     }
00115   }
00116   else
00117   {
00118     LOGGING_ERROR_C(CanOpen, CanOpenController, "Node with CANOPEN ID " << node_id
00119       << " already exists. Not adding new node."  << endl);
00120   }
00121 }
00122 
00123 template <typename NodeT>
00124 boost::shared_ptr<NodeT> CanOpenController::getNode (const uint8_t node_id)
00125 {
00126   if (m_nodes.find(node_id) == m_nodes.end())
00127   {
00128     std::stringstream ss;
00129     ss << "No node with the given id '" << static_cast<int>(node_id) << "' found.";
00130     throw NotFoundException(ss.str());
00131   }
00132 
00133   if (boost::dynamic_pointer_cast<NodeT>(m_nodes[node_id]))
00134   {
00135     return boost::dynamic_pointer_cast<NodeT>(m_nodes[node_id]);
00136   }
00137   else
00138   {
00139     throw std::bad_cast();
00140   }
00141 }
00142 
00143 }} //End of NS
00144 
00145 #endif


schunk_canopen_driver
Author(s): Felix Mauch , Georg Heppner
autogenerated on Thu Jun 6 2019 20:17:24