ATINetFTUnit.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package VisionX::ArmarXObjects::ATINetFTUnit
19  * @author Jonas Rauber ( kit at jonasrauber dot de )
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "ATINetFTUnit.h"
26 #include <sys/socket.h>
27 #include <arpa/inet.h>
28 #include <iostream>
29 #include <string>
30 #include <netdb.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <vector>
34 #include <inttypes.h>
35 namespace armarx
36 {
38  {
39 
40  }
41 
43  {
44  /*
45  * Read ATINetFT hostname and port from properties
46  * std::string receiverHostName("192.168.1.1:49152");
47  */
48 
49  std::string ATINetFTReceiverHostName = getProperty<std::string>("ATINetFTReceiveHostName").getValue();
50  std::string captureName = getProperty<std::string>("CaptureName").getValue();
51  ARMARX_INFO << "Capture Name " << captureName;
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);
60  ftDataSize = 6;
61  sendPacketSize = 8;
62  receivePacketSize = 36;
63  recordingStopped = true;
64  recordingCompleted = true;
65  recordingStarted = false;
66 
67  /*
68  * Connect to ATINetFT
69  */
70  /*if (ATINetFT.Connect(hostName).Result != Result::Success)
71  {
72  ARMARX_ERROR << "Could not connect to ATINetFT at " << hostName << flush;
73  return;
74  }
75  else
76  {
77  ARMARX_IMPORTANT << "Connected to ATINetFT at " << hostName << flush;
78  }*/
79 
80  /*
81  * Enable Marker Data
82  */
83  /*Result::Enum result = ATINetFT.EnableMarkerData().Result;
84 
85  if (result != Result::Success)
86  {
87  ARMARX_ERROR << "ATINetFT EnableMarkerData() returned error code " << result << flush;
88  return;
89  }*/
90  }
91 
93  {
94  ARMARX_IMPORTANT << "Disconnecting from ATINetFT" << flush;
95 
96  }
97 
98 
100  {
101 
102  }
103 
104  void ATINetFTUnit::periodicExec()
105  {
106  if (recordingStarted)
107 
108 
109  {
110  //ISO C++ forbids variable length array [-Werror=vla] => use a vector (the array will be on the heap anyways)
111  std::vector<float> ftdata(ftDataSize);
112  std::vector<char> receivePacketContent(receivePacketSize);
113 
114  if (!receivePacket(receivePacketContent.data()))
115  {
116  ARMARX_INFO << "recvfrom() failed :";
117  }
118  else
119  {
120  convertToFTValues(receivePacketContent.data(), ftdata.data(), ftdata.size());
121  writeFTValuesToFile(ftdata.data(), ftdata.size());
122  sampleIndex++;
123  }
124 
125  }
126 
127  }
128 
130  {
133  }
134 
135  void ATINetFTUnit::startRecording(const std::string& customName, const Ice::Current& c)
136  {
137  if (remoteTriggerEnabled)
138  {
139  if (recordingCompleted)
140  {
141  int bpSize = sendPacketSize;
142  //ISO C++ forbids variable length array [-Werror=vla] => use a vector (the array will be on the heap anyways)
143  std::vector<char> bytePacket(bpSize);
144  *(uint16_t*)&bytePacket[0] = htons(0x1234); /* standard header. */
145  *(uint16_t*)&bytePacket[2] = htons(2); /* per table 9.1 in Net F/T user manual. */
146  *(uint32_t*)&bytePacket[4] = htonl(0); /* see section 9.1 in Net F/T user manual. */
147  recordingStarted = sendPacket(bytePacket.data(), bytePacket.size());
148 
149  if (recordingStarted)
150  {
151  recordingStopped = false;
152  recordingCompleted = false;
153  //TODO build filename captureNamePrefic+customName
154  recordingFile.open(captureNamePrefix);
155  sampleIndex = 0;
156  }
157  }
158  }
159  }
160 
161  void ATINetFTUnit::stopRecording(const Ice::Current& c)
162  {
163  if (remoteTriggerEnabled)
164  {
165  int bpSize = 8;
166  //ISO C++ forbids variable length array [-Werror=vla] => use a vector (the array will be on the heap anyways)
167  std::vector<char> bytePacket(bpSize);
168  *(uint16_t*)&bytePacket[0] = htons(0x1234); /* standard header. */
169  *(uint16_t*)&bytePacket[2] = htons(0); /* per table 9.1 in Net F/T user manual. */
170  *(uint32_t*)&bytePacket[4] = htonl(0); /* see section 9.1 in Net F/T user manual. */
171 
172  recordingStopped = sendPacket(bytePacket.data(), bytePacket.size());
173  if (recordingStopped)
174  {
175  recordingFile.close();
176  recordingStarted = false;
177  }
178 
179  }
180  }
181 
182  bool ATINetFTUnit::sendPacket(char* bytePacket, int bpSize)
183  {
184  if (remoteTriggerEnabled)
185  {
186  if (sendto(senderSocket, bytePacket, bpSize, 0, (struct sockaddr*)&receiveHostAddr, sizeof(receiveHostAddr)) != bpSize)
187  {
188 
189  return false;
190  }
191  return true;
192  }
193  return false;
194  }
195 
196  bool ATINetFTUnit::receivePacket(char* receiveBuf)
197  {
198 
199  int test = sizeof(receiveHostAddr);
200 
201  int receiveint = recvfrom(senderSocket, receiveBuf, receivePacketSize, 0, (struct sockaddr*) &receiveHostAddr, ((socklen_t*)&test));
202 
203  if (receiveint < 0)
204  {
205 
206  return false;
207  }
208 
209 
210  return true;
211  }
212 
213  void ATINetFTUnit::convertToFTValues(char* receiveBuf, float* ftdata, int ftdataSize)
214  {
215  //uint32_t rdt_sequence = ntohl(*(uint32_t*)&receiveBuf[0]); //was an unused variable
216  //uint32_t ft_sequence = ntohl(*(uint32_t*)&receiveBuf[4]); //was an unused variable
217  //uint32_t status = ntohl(*(uint32_t*)&receiveBuf[8]); //was an unused variable
218 
219  for (int i = 0; i < ftdataSize; i++)
220  {
221  ftdata[i] = float(int32_t(ntohl(*(uint32_t*)&receiveBuf[12 + i * 4]))) / 1000000.0f;
222  }
223  }
224 
225  void ATINetFTUnit::writeFTValuesToFile(float* ftdata, int ftdataSize)
226  {
227  recordingFile << sampleIndex << " ";
228  for (int i = 0; i < ftdataSize; i++)
229  {
230  recordingFile << ftdata[i] << " ";
231  }
232  recordingFile << "\n";
233  }
234 
235  void ATINetFTUnit::establishATINetFTReceiveHostConnection(std::string receiverHostName)
236  {
237  remoteTriggerEnabled = false;
238 
239  //Construct the server sockaddr_ structure
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);
244 
245  //Create the socket
246  if ((senderSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
247  {
248  return;
249  }
250 
251  if (bind(senderSocket, (struct sockaddr*) &senderHostAddr, sizeof(senderHostAddr)) < 0)
252  {
253  return;
254  }
255 
256  //Construct the server sockaddr_ structure
257  memset(&receiveHostAddr, 0, sizeof(receiveHostAddr));
258  size_t pos = receiverHostName.find(":");
259  std::string hostname = receiverHostName.substr(0, pos);
260 
261  std::string hostport = receiverHostName.substr(pos + 1, receiverHostName.size());
262 
263  struct hostent* he;
264  struct in_addr** addr_list;
265  int i;
266 
267  if ((he = gethostbyname(hostname.c_str())) == NULL)
268  {
269  // get the host info
270  herror("gethostbyname");
271  return;
272  }
273 
274  addr_list = (struct in_addr**) he->h_addr_list;
275 
276  for (i = 0; addr_list[i] != NULL; i++)
277  {
278 
279  char ip[100];
280  //Return the first one;
281  strcpy(ip, inet_ntoa(*addr_list[i]));
282 
283  inet_pton(AF_INET, ip, &receiveHostAddr.sin_addr.s_addr);
284 
285  receiveHostAddr.sin_port = htons(atoi(hostport.c_str()));
286  remoteTriggerEnabled = true;
287  ARMARX_INFO << "Connection established to " << hostname << ":" << hostport;
288  //if((receiverSocket=socket(AF_INET, SOCK_DGRAM, 0))<0) {
289  // return;
290  //}
291  //if(bind(receiverSocket,( struct sockaddr *) &receiveHostAddr, sizeof(receiveHostAddr))<0) {
292  // return;
293  //}
294 
295  return;
296  }
297  return;
298 
299  }
300 }
armarx::ATINetFTUnit::startRecording
void startRecording(const std::string &customName, const Ice::Current &c) override
Definition: ATINetFTUnit.cpp:135
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::ATINetFTUnit::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ATINetFTUnit.cpp:129
armarx::ATINetFTUnit::onInitComponent
void onInitComponent() override
Definition: ATINetFTUnit.cpp:37
armarx::ATINetFTUnit::onDisconnectComponent
void onDisconnectComponent() override
Definition: ATINetFTUnit.cpp:92
armarx::ATINetFTUnit::onExitComponent
void onExitComponent() override
Definition: ATINetFTUnit.cpp:99
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
ATINetFTUnit.h
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
armarx::ATINetFTUnit::onConnectComponent
void onConnectComponent() override
Definition: ATINetFTUnit.cpp:42
armarx::ATINetFTUnitPropertyDefinitions
Definition: ATINetFTUnit.h:40
armarx::ATINetFTUnit::stopRecording
void stopRecording(const Ice::Current &c) override
Definition: ATINetFTUnit.cpp:161
for
for(;yybottom<=yytop;yybottom++)
Definition: Grammar.cpp:790
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
float
#define float
Definition: 16_Level.h:22
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28