48 DEBUG_LOG(
"Set NMT state of "<<(
unsigned)node_id<<
" to "<<static_cast<uint32_t>(cmd));
49 const Message message = { 0x0000,
false, 2, {
static_cast<uint8_t
>(cmd),node_id,0,0,0,0,0,0} };
60 const auto pause = std::chrono::milliseconds(CONSECUTIVE_SEND_PAUSE_MS);
61 for (
size_t node_id = 1; node_id < 239; ++node_id) {
63 std::this_thread::sleep_for(pause);
69 const auto pause = std::chrono::milliseconds(CONSECUTIVE_SEND_PAUSE_MS);
70 for (
size_t node_id = 1; node_id < 239; ++node_id) {
72 uint16_t cob_id = 0x700+node_id;
73 const Message message = { cob_id,
true, 0, {0,0,0,0,0,0,0,0} };
75 std::this_thread::sleep_for(pause);
81 DEBUG_LOG(
"NMT Error Control message from node "
84 uint8_t data = message.
data[0];
85 uint8_t state = data&0x7F;
88 DEBUG_LOG(
"NMT: Ignoring remote transmission request.");
89 DEBUG_DUMP(message.
cob_id);
105 if (m_cleanup_futures) {
106 std::lock_guard<std::mutex> scoped_lock(m_callback_futures_mutex);
107 m_callback_futures.remove_if([](
const std::future<void>& f) {
109 return (f.wait_for(std::chrono::steady_clock::duration::zero())==std::future_status::ready);
114 std::lock_guard<std::mutex> scoped_lock(m_device_alive_callbacks_mutex);
115 for (
const auto& callback : m_device_alive_callbacks) {
116 DEBUG_LOG(
"Calling new device callback (async)");
120 std::lock_guard<std::mutex> scoped_lock(m_callback_futures_mutex);
121 m_callback_futures.push_front(
122 std::async(std::launch::async, callback, message.
get_node_id())
138 DEBUG_LOG(
"New state is Initialising");
143 DEBUG_LOG(
"New state is Disconnected");
148 DEBUG_LOG(
"New state is Connecting");
153 DEBUG_LOG(
"New state is Preparing");
158 DEBUG_LOG(
"New state is Stopped");
163 DEBUG_LOG(
"New state is Operational");
168 DEBUG_LOG(
"New state is Pre-operational");
173 DEBUG_LOG(
"New state is unknown: "<<(
unsigned)state);
182 std::lock_guard<std::mutex> scoped_lock(m_device_alive_callbacks_mutex);
183 m_device_alive_callbacks.push_back(callback);
uint16_t cob_id
Message ID aka COB-ID.
void send_nmt_message(uint8_t node_id, Command cmd)
Sends a NMT message to a given device.
void broadcast_nmt_message(Command cmd)
Sends a broadcast NMT message.
void send(const Message &message)
Sends a message.
void register_device_alive_callback(const DeviceAliveCallback &callback)
Registers a callback which will be called when a slave sends it's state via NMT and the state indicat...
DeviceAliveCallback NewDeviceCallback
Type of a new device callback function.
This class implements the Core of KaCanOpen It communicates with the CAN driver, sends CAN messages a...
void discover_nodes()
Discovers nodes in the network via node guard protocol.
void register_new_device_callback(const NewDeviceCallback &callback)
Registers a callback which will be called when a new slave device is discovered.
void process_incoming_message(const Message &message)
Process incoming NMT message.
uint8_t get_node_id() const
Extracts the node id from the COB-ID.
NMT(Core &core)
Constructor.
uint8_t rtr
Remote transmission request (0 if it's not an RTR message, 1 if it is an RTR message) ...
std::function< void(const uint8_t node_id) > DeviceAliveCallback
Type of a device alive callback function Important: Never call register_device_alive_callback() from ...
This struct represents a CANOpen message.
uint8_t data[8]
Data bytes.
void reset_all_nodes()
Resets all nodes in the network.