-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisual_servo_motion.cpp
205 lines (199 loc) · 5.99 KB
/
visual_servo_motion.cpp
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
#include "DATA_STRUCT.h"
#include "Data_Log.hpp"
#include "Multi_Process.h"
#include "Transform.hpp"
#include "driverSever.h"
// #define BasicTest
// #define operationalControlTest
#define VISUAL
// #define jacobeMotionTest
using namespace std;
auto getVisualSocket() -> shared_ptr<SOCKET>
{
const int DEFAULT_BUFLEN = 56;
const string DEFAULT_PORT = "8899";
WSADATA wsaData;
auto ConnectSocket = make_shared<SOCKET>(INVALID_SOCKET);
struct addrinfo *result = NULL, *ptr = NULL, hints;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed with error: %d\n", iResult);
return make_shared<SOCKET>(INVALID_SOCKET);
}
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo("127.0.0.1", DEFAULT_PORT.data(), &hints, &result);
if (iResult != 0)
{
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return make_shared<SOCKET>(INVALID_SOCKET);
}
// Attempt to connect to an address until one succeeds
for (ptr = result; ptr != NULL; ptr = ptr->ai_next)
{
// Create a SOCKET for connecting to server
*ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (*ConnectSocket == INVALID_SOCKET)
{
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return make_shared<SOCKET>(INVALID_SOCKET);
}
// Connect to server.
iResult = connect(*ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
closesocket(*ConnectSocket);
*ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (*ConnectSocket == INVALID_SOCKET)
{
printf("Unable to connect to server!\n");
WSACleanup();
return make_shared<SOCKET>(INVALID_SOCKET);
}
// Receive until the peer closes the connection
cout << "start receiving!" << endl;
return ConnectSocket;
// while (TRUE) {
// iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
// cout << "received data" << endl;
// auto c_vecs = (double *) &recvbuf;
// cout << c_vecs[0] << "," << c_vecs[1] << "," << c_vecs[2] << "," << c_vecs[3] << ","
// << c_vecs[4] << "," << c_vecs[5] << endl;
// }
}
int main(int argc, char* argv[])
{
Tc_Ads ads_ptr;
Multi_Process p;
auto pi = p.safety_monitor_build("SAFE-CHECK.exe vservoing.exe");
auto d = make_shared<MotionV1>(ads_ptr);
// auto fl = file_log();
// fl.writeFile(*d);
d->Enable();
#ifdef BasicTest
d->setSyncrpm(100);
d->Write('x', -10.0F, 20.0F, 20.0F, 20.0F, 20.0F, 20.0F);
cout << "Finished!" << endl;
#endif
#ifdef operationalControlTest
d->setSyncrpm(10);
if (argc == 7)
{
vector<double> c_vecs {atof(argv[1]), atof(argv[2]), atof(argv[3]),
atof(argv[4]), atof(argv[5]), atof(argv[6])};
for (int i {}; i < 100 * 6; i++)
{
d->opSpaceMotionByJacobe(vector<float> {c_vecs.begin(), c_vecs.begin() + 6});
Sleep(5);
}
}
# ifdef jacobeMotionTest
{
vector<float> c_vecs {0, 1, 0, 0, 0, 0};
cout << "move y axis!" << endl;
for (int i {}; i < 100 * 6; i++)
{
d->opSpaceMotionByJacobe(c_vecs);
Sleep(5);
vec c_end = mat(fkine_W(MDT::getAngles(*d, d->MotGetData)));
// c_end.t().print("operational position end:");
}
Sleep(1000);
cout << "move -y axis!" << endl;
c_vecs = {0, -1, 0, 0, 0, 0};
for (int i {}; i < 100 * 6; i++)
{
d->opSpaceMotionByJacobe(c_vecs);
Sleep(5);
vec c_end = mat(fkine_W(MDT::getAngles(*d, d->MotGetData)));
// c_end.t().print("operational position end:");
}
Sleep(1000);
cout << "move x axis!" << endl;
c_vecs = {1, 0, 0, 0, 0, 0};
for (int i {}; i < 100 * 6; i++)
{
d->opSpaceMotionByJacobe(c_vecs);
Sleep(5);
vec c_end = mat(fkine_W(MDT::getAngles(*d, d->MotGetData)));
// c_end.t().print("operational position end:");
}
Sleep(1000);
cout << "move -x axia!" << endl;
c_vecs = {-1, 0, 0, 0, 0, 0};
for (int i {}; i < 100 * 6; i++)
{
d->opSpaceMotionByJacobe(c_vecs);
Sleep(5);
vec c_end = mat(fkine_W(MDT::getAngles(*d, d->MotGetData)));
// c_end.t().print("operational position end:");
}
Sleep(1000);
cout << "rotate with x" << endl;
c_vecs = {0, 0, 0, 1, 0, 0};
for (int i {}; i < 100 * 6; i++)
{
d->opSpaceMotionByJacobe(c_vecs);
Sleep(5);
vec c_end = mat(fkine_W(MDT::getAngles(*d, d->MotGetData)));
// c_end.t().print("operational position end:");
}
Sleep(1000);
cout << "rotate with -x" << endl;
c_vecs = {0, 0, 0, -1, 0, 0};
for (int i {}; i < 100 * 6; i++)
{
d->opSpaceMotionByJacobe(c_vecs);
Sleep(5);
vec c_end = mat(fkine_W(MDT::getAngles(*d, d->MotGetData)));
// c_end.t().print("operational position end:");
}
return 0;
}
# endif
// vec c_end = mat(fkine_W(MDT::getAngles(*d, d->MotGetData)));
// c_end.print("operational position end:");
// vec c_delta = {-0.1, 0, 0, 0, 0, 0};
// vec c_newTarget = c_end + c_delta;
// c_newTarget.print("c_New target:");
// d->setSyncrpm(10);
// d->opSpaceMotion(vector<double>{c_newTarget.begin(), c_newTarget.end()});
// Sleep(20000);
// vec c_newEnd = fkine(MDT::getAngles(*d, d->MotGetData));
// c_newEnd.print("c_new end: ");
#endif
#ifdef VISUAL
d->setSyncrpm(1);
auto socketptr = getVisualSocket();
if (*socketptr != INVALID_SOCKET)
{
char recvbuf[56] = {};
while (recv(*socketptr, recvbuf, 56, 0))
{
auto c_vecs = (double*)&recvbuf;
cout << c_vecs[0] << "," << c_vecs[1] << "," << c_vecs[2] << "," << c_vecs[3] << ","
<< c_vecs[4] << "," << c_vecs[5] << endl;
d->opSpaceMotionByJacobe(vector<float> {(float)c_vecs[1], -(float)c_vecs[0],
(float)c_vecs[2], (float)c_vecs[3],
(float)c_vecs[4], (float)c_vecs[5]});
Sleep(5);
}
}
#endif
p.processDelete(pi);
// system("pause");
return 0;
}