CanOpenReceiveThread.cpp
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 //----------------------------------------------------------------------
23 //----------------------------------------------------------------------
24 #include "CanOpenReceiveThread.h"
25 
26 
27 #include "Logging.h"
28 
29 namespace icl_hardware {
30 namespace canopen_schunk {
31 
34  ReceivedPacketCallback const& received_callback)
35  : m_period_time_ms(period.toMSec()),
36  m_can_device (can_device),
37  m_received_callback(received_callback)
38 {
39  m_thread = boost::thread(&CanOpenReceiveThread::workerFunction, this);
40 }
41 
43 {
44  stop();
45  m_thread.join();
46 }
47 
48 
50 {
51  int32_t receive_result;
52  while (true)
53  {
54  if (m_can_device)// != NULL)
55  {
56  if (m_can_device->IsInitialized())
57  {
58  receive_result = receiveData();
59  if (receive_result == -ENODATA)
60  {
61  // currently do nothing
62  }
63  else if (receive_result != 0)
64  {
65  LOGGING_ERROR_C (CanOpen, CanOpenReceiveThread, "Reading CAN message failed, received error code " << receive_result << ". Doing nothing." << endl);
66  }
67  }
68  else
69  {
70  LOGGING_WARNING_C(CanOpen, CanOpenReceiveThread, "Cannot read data from can device. It is not initialized!" << endl);
71  }
72  }
73 
74  // Wait for the thread period so that the timing is in sync.
75  try
76  {
77  boost::this_thread::sleep(boost::posix_time::milliseconds(m_period_time_ms));
78  }
79  catch(boost::thread_interrupted&)
80  {
81  return;
82  }
83  }
84 }
85 
87 {
88  m_thread.interrupt();
89 }
90 
92 {
93  int32_t receive_result = m_can_device->Receive(m_can_msg);
94  if ( receive_result > 0)
95  {
97  }
98  else
99  {
100  return receive_result;
101  }
102  return 0;
103 }
104 
105 
106 
107 }} // end of NS
signed int int32_t
boost::thread m_thread
Feedback thread handle.
icl_hardware::can::tCanMessage m_can_msg
Can message for storing and transmission of received data.
ReceivedPacketCallback m_received_callback
Callback for parsing of correctly received can messages (protocoll will be evaluated there) ...
int32_t m_period_time_ms
Cycle period of the receive thread.
#define LOGGING_WARNING_C(streamname, classname, arg)
virtual ~CanOpenReceiveThread()
~CanOpenReceiveThread Destructor
int32_t receiveData()
receiveData Reads Data from the can driver and calls the data receivec callback if it was valid ...
ThreadStream & endl(ThreadStream &stream)
The CanOpenReceiveThread class handles incoming canOpen messages.
void workerFunction()
state machine processing received data
CanOpenReceiveThread(const icl_core::TimeSpan &period, const boost::shared_ptr< icl_hardware::can::tCanDevice > &can_device, const ReceivedPacketCallback &received_callback)
CanOpenReceiveThread constructor.
boost::function< void(const icl_hardware::can::tCanMessage &msg)> ReceivedPacketCallback
definition of boost function callback for received packages
boost::shared_ptr< icl_hardware::can::tCanDevice > m_can_device
Can handle for message receiving.
#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