Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00022
00023
00024 #include "NMT.h"
00025 #include "Logging.h"
00026
00027 namespace icl_hardware {
00028 namespace canopen_schunk {
00029
00030 NMT::NMT(const uint8_t &node_id, const CanDevPtr &can_device):
00031 m_can_device(can_device),
00032 m_node_id(node_id),
00033 m_state(NMTS_INITIALISATION)
00034 {
00035 }
00036
00037 void NMT::update(const CanMsg &msg)
00038 {
00039 uint8_t node_id = (msg.id - ds301::ID_NMT_ERROR_MIN)+1;
00040
00041 if (node_id != m_node_id)
00042 {
00043 LOGGING_ERROR_C(CanOpen,NMT,"NMT Update called with wrong canopen ID. Received ID: " << node_id << " Node ID: " << m_node_id << ". Update ignored." << endl);
00044 return;
00045 }
00046
00047
00048
00049 if (msg.dlc != 1)
00050 {
00051 LOGGING_ERROR_C(CanOpen,NMT,"NMT Update called with illegal length message. Expected length: " << 1 << " got: " << msg.dlc << ". Update ignored." << endl);
00052 return;
00053 }
00054
00055 uint8_t payload = msg.data[0];
00056
00057
00058 if (payload == 0)
00059 {
00060 LOGGING_INFO_C(CanOpen,NMT,"NMT Bootup complete for node " << node_id << endl);
00061 m_state = NMTS_PRE_OPERATIONAL;
00062 }
00063 else
00064 {
00065
00066
00067
00068 uint8_t state_to_check = (payload & 0x7F);
00069 if (isValidNmtState(state_to_check))
00070 {
00071 if (m_state != static_cast<eNMT_State>(state_to_check))
00072 {
00073
00074 LOGGING_WARNING_C(CanOpen,NMT,"NMT Nodeguarding for node " << node_id << " detected a failure! State is supposed to be: " << nmtStateToString(m_state) << " but was detected as " << nmtStateToString(static_cast<eNMT_State>(state_to_check)) << ". State changed." << endl);
00075 m_state = static_cast<eNMT_State>(state_to_check);
00076 }
00077 else
00078 {
00079
00080 LOGGING_TRACE_C(CanOpen,NMT,"NMT Nodeguarding for node " << node_id << " received. ALL IS WELL!");
00081 }
00082 }
00083 else
00084 {
00085 LOGGING_DEBUG_C(CanOpen,NMT,"NMT Nodeguarding for node " << node_id << " received illegal NMT state information. Ignoring message" << endl);
00086 }
00087 }
00088
00089 }
00090
00091 void NMT::start()
00092 {
00093 sendCommand(NMT_STARTREMOTENODE);
00094 }
00095
00096 void NMT::stop()
00097 {
00098 sendCommand(NMT_STOPREMOTENODE);
00099 }
00100
00101 void NMT::preOperational()
00102 {
00103 sendCommand(NMT_ENTERPREOPERATIONAL);
00104 }
00105
00106 void NMT::reset()
00107 {
00108 sendCommand(NMT_RESETNODE);
00109 }
00110
00111 void NMT::resetCommunication()
00112 {
00113 sendCommand(NMT_RESETCOMMUNICATION);
00114 }
00115
00116 void NMT::sendCommand(const NMT::eNMT_Command& cmd)
00117 {
00118
00119
00120 switch (cmd)
00121 {
00122 case NMT_STARTREMOTENODE:
00123 m_state = NMTS_OPERATIONAL;
00124 break;
00125 case NMT_STOPREMOTENODE:
00126 m_state = NMTS_STOPPED;
00127 break;
00128 case NMT_ENTERPREOPERATIONAL:
00129 m_state = NMTS_PRE_OPERATIONAL;
00130 break;
00131 case NMT_RESETNODE:
00132 m_state = NMTS_INITIALISATION;
00133 break;
00134 case NMT_RESETCOMMUNICATION:
00135 m_state = NMTS_INITIALISATION;
00136 break;
00137 default:
00138 LOGGING_ERROR_C(CanOpen,NMT,"Illegal NMT command " << cmd << " was sent to node with id " << m_node_id << " . Command ignored." << endl);
00139 return;
00140 break;
00141 }
00142
00143
00144
00145 unsigned char msg[2];
00146 msg[0] = static_cast<unsigned char>(cmd);
00147 msg[1] = m_node_id;
00148 m_can_device->Send(CanMsg(ds301::ID_NMT,2,0,msg));
00149 }
00150
00151 }}