26 #include <sys/socket.h>
27 #include <arpa/inet.h>
49 std::string ATINetFTReceiverHostName = getProperty<std::string>(
"ATINetFTReceiveHostName").getValue();
50 std::string captureName = getProperty<std::string>(
"CaptureName").getValue();
52 size_t pos = captureName.find(
"0");
53 captureNamePrefix = captureName.substr(0, pos);
54 ARMARX_INFO <<
"Capture Name Prefix " << captureNamePrefix;
55 std::string captureNameSuffix = captureName.substr(pos, captureName.size());
56 ARMARX_INFO <<
"Capture Name Suffix " << captureNameSuffix;
57 nPaddingZeros = captureNameSuffix.size();
58 captureNumber = std::stoi(captureNameSuffix);
59 establishATINetFTReceiveHostConnection(ATINetFTReceiverHostName);
62 receivePacketSize = 36;
63 recordingStopped =
true;
64 recordingCompleted =
true;
65 recordingStarted =
false;
104 void ATINetFTUnit::periodicExec()
106 if (recordingStarted)
111 std::vector<float> ftdata(ftDataSize);
112 std::vector<char> receivePacketContent(receivePacketSize);
114 if (!receivePacket(receivePacketContent.data()))
120 convertToFTValues(receivePacketContent.data(), ftdata.data(), ftdata.size());
121 writeFTValuesToFile(ftdata.data(), ftdata.size());
137 if (remoteTriggerEnabled)
139 if (recordingCompleted)
141 int bpSize = sendPacketSize;
143 std::vector<char> bytePacket(bpSize);
144 *(uint16_t*)&bytePacket[0] = htons(0x1234);
145 *(uint16_t*)&bytePacket[2] = htons(2);
146 *(uint32_t*)&bytePacket[4] = htonl(0);
147 recordingStarted = sendPacket(bytePacket.data(), bytePacket.size());
149 if (recordingStarted)
151 recordingStopped =
false;
152 recordingCompleted =
false;
154 recordingFile.open(captureNamePrefix);
163 if (remoteTriggerEnabled)
167 std::vector<char> bytePacket(bpSize);
168 *(uint16_t*)&bytePacket[0] = htons(0x1234);
169 *(uint16_t*)&bytePacket[2] = htons(0);
170 *(uint32_t*)&bytePacket[4] = htonl(0);
172 recordingStopped = sendPacket(bytePacket.data(), bytePacket.size());
173 if (recordingStopped)
175 recordingFile.close();
176 recordingStarted =
false;
182 bool ATINetFTUnit::sendPacket(
char* bytePacket,
int bpSize)
184 if (remoteTriggerEnabled)
186 if (sendto(senderSocket, bytePacket, bpSize, 0, (
struct sockaddr*)&receiveHostAddr,
sizeof(receiveHostAddr)) != bpSize)
196 bool ATINetFTUnit::receivePacket(
char* receiveBuf)
199 int test =
sizeof(receiveHostAddr);
201 int receiveint = recvfrom(senderSocket, receiveBuf, receivePacketSize, 0, (
struct sockaddr*) &receiveHostAddr, ((socklen_t*)&test));
213 void ATINetFTUnit::convertToFTValues(
char* receiveBuf,
float* ftdata,
int ftdataSize)
219 for (
int i = 0; i < ftdataSize; i++)
221 ftdata[i] =
float(int32_t(ntohl(*(uint32_t*)&receiveBuf[12 + i * 4]))) / 1000000.0f;
225 void ATINetFTUnit::writeFTValuesToFile(
float* ftdata,
int ftdataSize)
227 recordingFile << sampleIndex <<
" ";
228 for (
int i = 0; i < ftdataSize; i++)
230 recordingFile << ftdata[i] <<
" ";
232 recordingFile <<
"\n";
235 void ATINetFTUnit::establishATINetFTReceiveHostConnection(std::string receiverHostName)
237 remoteTriggerEnabled =
false;
240 memset(&senderHostAddr, 0,
sizeof(senderHostAddr));
241 senderHostAddr.sin_family = AF_INET;
242 senderHostAddr.sin_addr.s_addr = htonl(INADDR_ANY);
243 senderHostAddr.sin_port = htons(0);
246 if ((senderSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
251 if (bind(senderSocket, (
struct sockaddr*) &senderHostAddr,
sizeof(senderHostAddr)) < 0)
257 memset(&receiveHostAddr, 0,
sizeof(receiveHostAddr));
258 size_t pos = receiverHostName.find(
":");
259 std::string hostname = receiverHostName.substr(0, pos);
261 std::string hostport = receiverHostName.substr(pos + 1, receiverHostName.size());
264 struct in_addr** addr_list;
267 if ((he = gethostbyname(hostname.c_str())) == NULL)
270 herror(
"gethostbyname");
274 addr_list = (
struct in_addr**) he->h_addr_list;
276 for (i = 0; addr_list[i] != NULL; i++)
281 strcpy(ip, inet_ntoa(*addr_list[i]));
283 inet_pton(AF_INET, ip, &receiveHostAddr.sin_addr.s_addr);
285 receiveHostAddr.sin_port = htons(atoi(hostport.c_str()));
286 remoteTriggerEnabled =
true;
287 ARMARX_INFO <<
"Connection established to " << hostname <<
":" << hostport;