forked from ros-industrial/abb_libegm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
egm_controller_interface.h
218 lines (192 loc) · 6.84 KB
/
egm_controller_interface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/***********************************************************************************************************************
*
* Copyright (c) 2015, ABB Schweiz AG
* All rights reserved.
*
* Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that
* the following conditions are met:
*
* * Redistributions of source code must retain the
* above copyright notice, this list of conditions
* and the following disclaimer.
* * Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions
* and the following disclaimer in the documentation
* and/or other materials provided with the
* distribution.
* * Neither the name of ABB nor the names of its
* contributors may be used to endorse or promote
* products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
***********************************************************************************************************************
*/
#ifndef EGM_CONTROLLER_INTERFACE_H
#define EGM_CONTROLLER_INTERFACE_H
#include "egm_base_interface.h"
namespace abb
{
namespace egm
{
/**
* \brief Class for an EGM contoller user interface.
*
* The class provides behavior for motion control, and this includes:
* - Processing asynchronous callbacks from an UDP server.
* - Notifying an external control loop about new messages.
* - Incorporating external control loop inputs, and sending them to the robot controller.
*
* Pseudocode for the usage of the class methods (inside an external control loop);
* 1. if(waitForMessage(...))
* 1.1. read(...)
* 1.2. write(...)
* 1.3. Repeat from 1.
*/
class EGMControllerInterface : public EGMBaseInterface
{
public:
/**
* \brief A constructor.
*
* \param io_service for operating boost asio's asynchronous functions.
* \param port_number for the server's UDP socket.
* \param configuration for the interface's configuration.
*/
EGMControllerInterface(boost::asio::io_service& io_service,
const unsigned short port_number,
const BaseConfiguration& configuration = BaseConfiguration());
/**
* \brief Wait for the next EGM message.
*
* \param timeout_ms for specifying a timeout in [ms]. If omitted, then the method waits forever.
*
* \return bool indicating if the wait was successful or not. I.e. returns false if a timeout has occurred.
*/
bool waitForMessage(const unsigned int timeout_ms = 0);
/**
* \brief Read EGM inputs received from the robot controller.
*
* \param p_inputs for containing the inputs.
*/
void read(wrapper::Input* p_inputs);
/**
* \brief Write EGM outputs to send to the robot controller.
*
* \param outputs containing the outputs.
*/
void write(const wrapper::Output& outputs);
private:
/**
* \brief Class for managing controller motion data, between the inner loop and an external control loop.
*/
class ControllerMotion
{
public:
/**
* \brief Default constructor.
*/
ControllerMotion() : read_data_ready_(false), write_data_ready_(false) {}
/**
* \brief Initialize the motion data for a new communication session.
*
* \param first_message indicating if it is the first message in a communication session.
*/
void initialize(const bool first_message);
/**
* \brief Wait for the next message.
*
* \param timeout_ms for specifying a timeout in [ms]. If omitted, then the method waits forever.
*
* \return bool indicating if the wait was successful or not. I.e. returns false if a timeout has occurred.
*/
bool waitForMessage(const unsigned int timeout_ms);
/**
* \brief Write the current inputs (from the inner loop, to the intermediate storage).
*
* \param inputs for containing the inputs.
*/
void writeInputs(const wrapper::Input& inputs);
/**
* \brief Read the current inputs (from the intermediate storage, to the external loop).
*
* \param p_inputs for containing the inputs.
*/
void readInputs(wrapper::Input* p_inputs);
/**
* \brief Write the current outputs (from the external loop, to the intermediate storage).
*
* \param outputs for containing the outputs.
*/
void writeOutputs(const wrapper::Output& outputs);
/**
* \brief Read the current outputs (from the intermediate storage, to the inner loop).
*
* \param p_outputs for containing the outputs.
*/
void readOutputs(wrapper::Output* p_outputs);
private:
/**
* \brief Static constant timeout [ms] for waiting on external control loop inputs.
*/
static const unsigned int WRITE_TIMEOUT_MS = 24;
/**
* \brief Mutex for protecting read data.
*/
boost::mutex read_mutex_;
/**
* \brief Mutex for protecting write data.
*/
boost::mutex write_mutex_;
/**
* \brief Condition variable for waiting on read data.
*/
boost::condition_variable read_condition_variable_;
/**
* \brief Condition variable for waiting on write data.
*/
boost::condition_variable write_condition_variable_;
/**
* \brief Flag indicating if read data is ready.
*/
bool read_data_ready_;
/**
* \brief Flag indicating if write data is ready.
*/
bool write_data_ready_;
/**
* \brief Container for the inputs received from the robot controller.
*/
wrapper::Input inputs_;
/**
* \brief Container for the outputs to send to the robot controller.
*/
wrapper::Output outputs_;
};
/**
* \brief Handle callback requests from an UDP server.
*
* \param server_data containing the UDP server's callback data.
*
* \return string& containing the reply.
*/
const std::string& callback(const UDPServerData& server_data);
/**
* \brief The interface's controller motion data (between internal loop and external controller loop).
*/
ControllerMotion controller_motion_;
};
} // end namespace egm
} // end namespace abb
#endif // EGM_CONTROLLER_INTERFACE_H