communication.cpp
Go to the documentation of this file.
1 
8 
9 Communication::Communication(const std::string& configPath)
10 {
11  // load config files
12  comCfg = CommunicationConfig(configPath);
25 }
26 
28 {
29  delete (dispatcher);
30  delete (rxPolling);
31  delete (sensorGroupThread);
32 }
33 
35 {
37  si.setBaudRate(serialParams->baudRate);
38  si.setDeviceTag(serialParams->deviceTag);
39  si.setMaxLineLength(serialParams->maxLineLength);
40  si.setSerialDevicesFolder(serialParams->serialDevicesFolder);
41  si.setSerialTimeout(serialParams->serialTimeout);
42 }
43 
45 {
47  si.connect();
48 }
49 
51 
53 
55 {
57  si.disconnect();
58 }
59 
61 {
64 }
65 
67 {
69 }
70 
71 void Communication::sendRawMessage(const std::string& msg)
72 {
74  throw std::runtime_error("Raw communication mode not enabled!");
76  std::string send = msg;
77  si.send(send);
78 }
79 
80 // timeout in microseconds
81 bool Communication::sendCommand(const std::string& command,
82  const Parameter::ParameterMap& inputParams,
83  Parameter::ParameterMap& outputParams,
84  unsigned int timeout)
85 {
86  if(commands.find(command)==commands.end()) throw std::out_of_range("Command: "+command+" does not exist!");
87  ros::Time t = ros::Time::now();
89  std::unique_lock<std::mutex> lck(mtx);
90  std::string cmd;
91  std::string response;
92  commands[command]->generateCommand(inputParams, cmd);
94  cmd = cmd + syntax->endOfFrame;
95  si.send(cmd);
96  cv.wait_for(lck, std::chrono::microseconds(timeout));
98  return false;
100  {
101  dispatcher->dequeueResponse(response);
102  if (commands[command]->verifyResponse(inputParams, response, outputParams))
103  {
104  return true;
105  }
106  }
107  return false;
108 }
109 // timeout in microseconds
110 bool Communication::sendCommand(const std::string& command,
111  const std::vector<std::string>& options,
112  const Parameter::ParameterMap& inputParams,
113  Parameter::ParameterMap& outputParams,
114  unsigned int timeout)
115 {
116  if(commands.find(command)==commands.end()) throw std::out_of_range("Command: "+command+" does not exist!");
117  ros::Time t = ros::Time::now();
119  std::unique_lock<std::mutex> lck(mtx);
120  std::string cmd;
121  std::string response;
122  commands[command]->generateCommand(inputParams,options, cmd);
124  cmd = cmd + syntax->endOfFrame;
125  si.send(cmd);
126  cv.wait_for(lck, std::chrono::microseconds(timeout));
128  return false;
129  while (!dispatcher->IsResponseQueueEmpty())
130  {
131  dispatcher->dequeueResponse(response);
132  if (commands[command]->verifyResponse(inputParams, options, response, outputParams))
133  {
134  return true;
135  }
136  }
137  return false;
138 }
139 
140 bool Communication::registerSensorGroups(const std::string& cmdName,
141  unsigned int timeout)
142 {
143  if(commands.find(cmdName)==commands.end()) throw std::out_of_range("Command: "+cmdName+" does not exist!");
144  bool success = true;
146  for (auto grp : sensorGroups)
147  {
148  std::unique_lock<std::mutex> lck(mtx);
149  std::string cmd;
150  std::string response;
151  grp.second->createSensorGroupCommand(*commands[cmdName], cmd);
153  cmd = cmd + syntax->endOfFrame;
154  si.send(cmd);
155  cv.wait_for(lck, std::chrono::microseconds(timeout));
157  {
158  success = false;
159  continue;
160  }
161  dispatcher->dequeueResponse(response);
162  if (!grp.second->verifyResponseOnComand(*commands[cmdName], response))
163  success = false;
164  }
165  return success;
166 }
167 
169 {
171  for(auto grp : sensorGroups){
172  grp.second->registerErrorCallback(error);
173  }
174 }
175 
177 {
179 }
180 
181 void Communication::registerSensorGroupCallback(const unsigned char& grpNumber,
182  responseCallback cbPtr)
183 {
184  sensorGroups[grpNumber]->setResponseCallback(cbPtr);
185 }
Header file for the Communication class.
const bool IsResponseQueueEmpty() const
Is there a response in the response queue?
std::unordered_map< std::string, std::shared_ptr< Command > > commands
void startCommunication()
Start reading the serial input buffer and start parsing threads.
bool registerSensorGroups(const std::string &cmdName, unsigned int timeout=100000)
Register all predefined sensor groups on the serial device.
static SerialInterface & instance()
Returns a singleton auf this class.
boost::function< void(SensorGroup *)> responseCallback
Definition: sensorgroup.h:63
void enableRawCommunication()
Enables the transmission of raw messages to the serial device.
void sendRawMessage(const std::string &msg)
Method to send a string directly to the serial device.
The ThreadDispatcher class coordinates other communication threads and handles start up and shut down...
void stopCommunication()
Stop reading the serial input buffer and shutdown parsing threads.
void connect()
Tries to setup a connection to a serial devive with a certain device-tag and specified baudrate...
std::condition_variable cv
SensorGroupThread * sensorGroupThread
The ReadingThread class implements threadded polling of the serial input buffer.
Definition: readingthread.h:31
void enableDebugMessages(debugCallbackPtr debug)
Enable pass through of every line in the input buffer.
The Parameter::ParameterMap class provides a map functionality for Parameter::Parameter objects conta...
Definition: parameter.h:163
bool sendCommand(const std::string &command, const Parameter::ParameterMap &inputParams, Parameter::ParameterMap &outputParams, unsigned int timeout=100000)
Send a command to the servial device.
void registerErrorCallback(debugCallbackPtr error)
Register a callback to handle transmission/communication errors.
void registerErrorCallback(debugCallbackPtr error)
Register a callback to handle transmission/communication errors.
void disconnect()
Terminates a connection to a serial devive with a certain device-tag.
void configSerialInterface()
Configures the serial connection setup with the provided parameters.
void enableDebugMessages(debugCallbackPtr debug)
Enable pass through of every line in the input buffer.
void setSerialTimeout(unsigned int serialTimeout)
Setter for the serial timeout.
ReadingThread * rxPolling
void setMaxLineLength(unsigned int maxLineLength)
Setter for the maximum line length.
void registerTextCallback(debugCallbackPtr text)
Register a callback to handle plain text messages from the serial device.
void registerSensorGroupCallback(const unsigned char &grpNumber, responseCallback cbPtr)
Register a callback to handle sensor group messages from the serial device.
void disconnect()
try to close the connection to the serial device
void setCommunicationCondVar(std::condition_variable *condVar)
Register the condition variable of the Communication object in order to wakeup a waiting command resp...
void setReadingThread(ReadingThread *rxThread)
Register ReadingThread with the thread dispatcher.
void startThread()
Start dispatching thread, SensorGroupThread and ReadingThread.
const std::unordered_map< unsigned char, std::shared_ptr< SensorGroup > > & getSensorGroups() const
SensorGroup map object getter.
void setBaudRate(unsigned int baudRate)
Setter for the baudrate, which defines the bandwith of the communication.
const std::shared_ptr< SerialInterfaceParams > getSerialInterfaceParams() const
SerialInterfaceParams object getter.
~Communication()
Communication default destructor.
void setDeviceTag(const std::string &deviceTag)
Setter for the device tag, which is an identifier a the connected serial device.
void connect()
try to open a connection to the serial device
void stopThread()
Stop dispatching thread, SensorGroupThread and ReadingThread.
void setSerialDevicesFolder(const std::string &serialDevicesFolder)
Setter for the path to the devices folder.
boost::function< void(const std::string &)> debugCallbackPtr
const std::shared_ptr< Syntax > getSyntax() const
Syntax object getter.
void setSensorGroupThread(SensorGroupThread *grpThread)
Register SensorGroupThread with the thread dispatcher.
CommunicationConfig comCfg
The CommunicationConfig class parses config files and provides configuration parameters to a Communca...
void send(std::string &message)
try to send a message to the serial device
void setCommunicationWakeUp(bool wakeUp)
Set wether there is a command response thread waiting or not.
ThreadDispatcher * dispatcher
std::shared_ptr< SerialInterfaceParams > serialParams
std::shared_ptr< Syntax > syntax
The SerialInterface class provides an interface to a connected serial device.
std::unordered_map< unsigned char, std::shared_ptr< SensorGroup > > sensorGroups
void enableRawCommunication()
Enables the transmission of raw messages to the serial device.
void dequeueResponse(std::string &response)
Pop a received command response from the response queue.
The SensorGroupThread class implements functionality to extract the group identifier of an incoming s...
const std::unordered_map< std::string, std::shared_ptr< Command > > & getCommands() const
Command map getter.
std::mutex mtx
Communication(const std::string &configPath)
Communication constructor.
bool rawCommunicationEnabled
void registerTextCallback(debugCallbackPtr text)
Register a callback to handle plain text messages from the serial device.


pses_ucbridge
Author(s): Sebastian Ehmes
autogenerated on Sat Oct 28 2017 19:16:13