29
29
#define UR_CLIENT_LIBRARY_REVERSE_INTERFACE_H_INCLUDED
30
30
31
31
#include " ur_client_library/comm/tcp_server.h"
32
+ #include " ur_client_library/comm/control_mode.h"
32
33
#include " ur_client_library/types.h"
33
34
#include " ur_client_library/log.h"
34
35
#include < cstring>
37
38
38
39
namespace urcl
39
40
{
40
- namespace comm
41
+ namespace control
41
42
{
42
- /* !
43
- * \brief Control modes as interpreted from the script runnning on the robot.
44
- */
45
- enum class ControlMode : int32_t
46
- {
47
- MODE_STOPPED = -2 , // /< When this is set, the program is expected to stop and exit.
48
- MODE_UNINITIALIZED = -1 , // /< Startup default until another mode is sent to the script.
49
- MODE_IDLE = 0 , // /< Set when no controller is currently active controlling the robot.
50
- MODE_SERVOJ = 1 , // /< Set when servoj control is active.
51
- MODE_SPEEDJ = 2 // /< Set when speedj control is active.
52
- };
53
-
54
43
/* !
55
44
* \brief The ReverseInterface class handles communication to the robot. It starts a server and
56
45
* waits for the robot to connect via its URCaps program.
@@ -65,67 +54,23 @@ class ReverseInterface
65
54
* \param port Port the Server is started on
66
55
* \param handle_program_state Function handle to a callback on program state changes.
67
56
*/
68
- ReverseInterface (uint32_t port, std::function<void (bool )> handle_program_state)
69
- : client_fd_(-1 ), server_(port), handle_program_state_(handle_program_state), keepalive_count_(1 )
70
- {
71
- handle_program_state_ (false );
72
- server_.setMessageCallback (
73
- std::bind (&ReverseInterface::messageCallback, this , std::placeholders::_1, std::placeholders::_2));
74
- server_.setConnectCallback (std::bind (&ReverseInterface::connectionCallback, this , std::placeholders::_1));
75
- server_.setDisconnectCallback (std::bind (&ReverseInterface::disconnectionCallback, this , std::placeholders::_1));
76
- server_.setMaxClientsAllowed (1 );
77
- server_.start ();
78
- }
57
+ ReverseInterface (uint32_t port, std::function<void (bool )> handle_program_state);
58
+
79
59
/* !
80
60
* \brief Disconnects possible clients so the reverse interface object can be safely destroyed.
81
61
*/
82
- ~ReverseInterface ()
83
- {
84
- }
62
+ ~ReverseInterface () = default ;
85
63
86
64
/* !
87
65
* \brief Writes needed information to the robot to be read by the URCaps program.
88
66
*
89
67
* \param positions A vector of joint targets for the robot
90
- * \param control_mode Control mode assigned to this command. See documentation of ::ControlMode
68
+ * \param control_mode Control mode assigned to this command. See documentation of comm ::ControlMode
91
69
* for details on possible values.
92
70
*
93
71
* \returns True, if the write was performed successfully, false otherwise.
94
72
*/
95
- bool write (const vector6d_t * positions, const ControlMode control_mode = ControlMode::MODE_IDLE)
96
- {
97
- if (client_fd_ == -1 )
98
- {
99
- return false ;
100
- }
101
- uint8_t buffer[sizeof (int32_t ) * 8 ];
102
- uint8_t * b_pos = buffer;
103
-
104
- // The first element is always the keepalive signal.
105
- int32_t val = htobe32 (keepalive_count_);
106
- b_pos += append (b_pos, val);
107
-
108
- if (positions != nullptr )
109
- {
110
- for (auto const & pos : *positions)
111
- {
112
- int32_t val = static_cast <int32_t >(pos * MULT_JOINTSTATE);
113
- val = htobe32 (val);
114
- b_pos += append (b_pos, val);
115
- }
116
- }
117
- else
118
- {
119
- b_pos += 6 * sizeof (int32_t );
120
- }
121
-
122
- val = htobe32 (toUnderlying (control_mode));
123
- b_pos += append (b_pos, val);
124
-
125
- size_t written;
126
-
127
- return server_.write (client_fd_, buffer, sizeof (buffer), written);
128
- }
73
+ bool write (const vector6d_t * positions, const comm::ControlMode control_mode = comm::ControlMode::MODE_IDLE);
129
74
130
75
/* !
131
76
* \brief Set the Keepalive count. This will set the number of allowed timeout reads on the robot.
@@ -138,36 +83,14 @@ class ReverseInterface
138
83
}
139
84
140
85
private:
141
- void connectionCallback (const int filedescriptor)
142
- {
143
- if (client_fd_ < 0 )
144
- {
145
- URCL_LOG_INFO (" Robot connected to reverse interface. Ready to receive control commands." );
146
- client_fd_ = filedescriptor;
147
- handle_program_state_ (true );
148
- }
149
- else
150
- {
151
- URCL_LOG_ERROR (" Connection request to ReverseInterface received while connection already established. Only one "
152
- " connection is allowed at a time. Ignoring this request." );
153
- }
154
- }
86
+ void connectionCallback (const int filedescriptor);
155
87
156
- void disconnectionCallback (const int filedescriptor)
157
- {
158
- URCL_LOG_INFO (" Connection to reverse interface dropped." , filedescriptor);
159
- client_fd_ = -1 ;
160
- handle_program_state_ (false );
161
- }
88
+ void disconnectionCallback (const int filedescriptor);
162
89
163
- void messageCallback (const int filedescriptor, char * buffer)
164
- {
165
- URCL_LOG_WARN (" Messge on ReverseInterface received. The reverse interface currently does not support any message "
166
- " handling. This message will be ignored." );
167
- }
90
+ void messageCallback (const int filedescriptor, char * buffer);
168
91
169
92
int client_fd_;
170
- TCPServer server_;
93
+ comm:: TCPServer server_;
171
94
172
95
static const int32_t MULT_JOINTSTATE = 1000000 ;
173
96
@@ -183,7 +106,7 @@ class ReverseInterface
183
106
uint32_t keepalive_count_;
184
107
};
185
108
186
- } // namespace comm
109
+ } // namespace control
187
110
} // namespace urcl
188
111
189
112
#endif // UR_CLIENT_LIBRARY_REVERSE_INTERFACE_H_INCLUDED
0 commit comments