vicon
Public Types | Public Member Functions | List of all members
vicon::Driver Class Reference

#include <ViconDriver.hpp>

Public Types

enum  GetResult { UNKNOWN, SUCCESS, INVALID_SUBJECT_NAME, INVALID_SEGMENT_NAME }
 

Public Member Functions

 Driver ()
 
bool connect (const std::string &hostname, const unsigned int port=801)
 
bool isConnected ()
 
void disconnect ()
 
bool getFrame (const base::Time &timeout=base::Time::fromSeconds(1.0))
 
base::Time getTimestamp ()
 
Eigen::Affine3d getSegmentTransform (const std::string &subjectName, const std::string &segmentName, bool &inFrame)
 
Eigen::Affine3d getSegmentTransform (const std::string &subjectName, const std::string &segmentName)
 
GetResult getLastResult ()
 
std::vector< base::Vector3d > getUnlabeledMarkers ()
 
void getAxesDir (int &x_dir, int &y_dir, int &z_dir)
 Get axes direction. More...
 
void setAxesDir (int x_dir, int y_dir, int z_dir)
 Set axes directions. More...
 

Detailed Description

Definition at line 32 of file ViconDriver.hpp.

Member Enumeration Documentation

◆ GetResult

Enumerator
UNKNOWN 
SUCCESS 
INVALID_SUBJECT_NAME 
INVALID_SEGMENT_NAME 

Definition at line 35 of file ViconDriver.hpp.

Constructor & Destructor Documentation

◆ Driver()

Driver::Driver ( )

Definition at line 48 of file ViconDriver.cpp.

49  : impl( new DriverImpl() ), mLastResult(UNKNOWN)
50 {
51 }

Member Function Documentation

◆ connect()

bool Driver::connect ( const std::string &  hostname,
const unsigned int  port = 801 
)

Open a connection to the Vicon DataStream host

Parameters
hostnamename of the network host
portport numer
Returns
true of the connection was successful, false otherwise

Definition at line 53 of file ViconDriver.cpp.

54 {
55  std::ostringstream host;
56  host << hostname << ":" << port;
57  LOG_INFO_S << "connecting to " << host.str();
58 
59  impl->client.Connect( host.str() );
60 
61  if( isConnected() )
62  {
63  impl->client.EnableSegmentData();
64  impl->client.EnableUnlabeledMarkerData();
65 
66  //impl->client.SetStreamMode( ViconDataStreamSDK::CPP::StreamMode::ClientPull );
67  impl->client.SetStreamMode( ViconDataStreamSDK::CPP::StreamMode::ClientPullPreFetch );
68  // MyClient.SetStreamMode( ViconDataStreamSDK::CPP::StreamMode::ServerPush );
69 
70  // Set the global up axis
71  impl->client.SetAxisMapping(
74  Direction::Up ); // Z-up
75 
76  return true;
77  }
78  else
79  return false;
80 }
bool isConnected()
Definition: ViconDriver.cpp:82

◆ disconnect()

void Driver::disconnect ( )

disconnect from the server

Definition at line 87 of file ViconDriver.cpp.

88 {
89  LOG_INFO_S << "disconnecting";
90  impl->client.Disconnect();
91 }

◆ getAxesDir()

void Driver::getAxesDir ( int &  x_dir,
int &  y_dir,
int &  z_dir 
)

Get axes direction.

Definition at line 188 of file ViconDriver.cpp.

189 {
190  Output_GetAxisMapping vicon_axes = impl->client.GetAxisMapping ();
191  x_dir = axesMap( vicon_axes.XAxis );
192  y_dir = axesMap( vicon_axes.YAxis );
193  z_dir = axesMap( vicon_axes.ZAxis );
194 }
int axesMap(Direction::Enum axis)
Definition: ViconDriver.cpp:19

◆ getFrame()

bool Driver::getFrame ( const base::Time &  timeout = base::Time::fromSeconds(1.0))

Step to the next frame in the data stream. Always call this method before accessing any of the data methods.

Parameters
timeouttime in milliseconds to wait for a new frame
Returns
true if a frame has been received

Definition at line 93 of file ViconDriver.cpp.

94 {
95  base::Time start = base::Time::now();
96  Result::Enum result;
97  while( ((result=impl->client.GetFrame().Result) == Result::NoFrame) && (start+timeout > base::Time::now()) )
98  {
99  const unsigned long wait_ms = 10;
100 #ifdef WIN32
101  Sleep( wait_ms );
102 #else
103  usleep( wait_ms );
104 #endif
105  }
106 
107  if( result == Result::Success )
108  {
109  LOG_DEBUG_S << "Got Frame!";
110  return true;
111  }
112  else
113  {
114  if ( timeout.toSeconds() > 0 ) LOG_ERROR_S << "No Frame received!";
115  return false;
116  }
117 }

◆ getLastResult()

GetResult vicon::Driver::getLastResult ( )
inline

Get the result o the last getSegmentTransform operation.

Definition at line 90 of file ViconDriver.hpp.

90 { return mLastResult; }

◆ getSegmentTransform() [1/2]

Eigen::Affine3d Driver::getSegmentTransform ( const std::string &  subjectName,
const std::string &  segmentName,
bool &  inFrame 
)

Get the transformation which transforms the given segment from the local to the global coordinate frame.

Parameters
subjectNamename of the subject
segmentNamename of the segment in the subject
inFrameis true if segment is present in the current frame (not occluded).
Returns
transformation from the segments local to the global reference frame

Definition at line 125 of file ViconDriver.cpp.

126 {
127 
128  // Get the segment name
129  std::string SegmentName = impl->client.GetSubjectRootSegmentName(subjectName).SegmentName;
130 
131  LOG_DEBUG_S << " Name: " << SegmentName;
132 
133  LOG_DEBUG_S << "get transform: " << subjectName << "." << segmentName;
135  impl->client.GetSegmentGlobalTranslation( subjectName, segmentName );
136 
138  impl->client.GetSegmentGlobalRotationQuaternion( subjectName, segmentName );
139 
140  Eigen::Vector3d trans_m( trans.Translation[0], trans.Translation[1], trans.Translation[2] );
141 
142  // vicon data is in mm, but we prefer standard si units...
143  Eigen::Affine3d result = Eigen::Translation3d( trans_m * 1e-3 ) *
144  Eigen::Quaterniond( quat.Rotation[3], quat.Rotation[0], quat.Rotation[1], quat.Rotation[2] );
145 
146  inFrame = !trans.Occluded;
147 
148  switch(trans.Result) {
150  mLastResult = INVALID_SUBJECT_NAME;
151  break;
153  mLastResult = INVALID_SEGMENT_NAME;
154  break;
155  case(Result::Success):
156  mLastResult = SUCCESS;
157  break;
158  default:
159  mLastResult = UNKNOWN;
160  break;
161  }
162 
163  return result;
164 }

◆ getSegmentTransform() [2/2]

Eigen::Affine3d vicon::Driver::getSegmentTransform ( const std::string &  subjectName,
const std::string &  segmentName 
)
inline

For backcompatibility.

Definition at line 84 of file ViconDriver.hpp.

84  {
85  bool lin_frame;
86  return getSegmentTransform ( subjectName, segmentName, lin_frame );
87  }
Eigen::Affine3d getSegmentTransform(const std::string &subjectName, const std::string &segmentName, bool &inFrame)

◆ getTimestamp()

base::Time Driver::getTimestamp ( )
Returns
the timestamp of the current frame

Definition at line 119 of file ViconDriver.cpp.

120 {
121  // for now, just return the current time
122  return base::Time::now();
123 }

◆ getUnlabeledMarkers()

std::vector< base::Vector3d > Driver::getUnlabeledMarkers ( )

Returns a list of unlabeled markers found by the system in global coordinates.

Returns
a vector of unlabeled markers

Definition at line 166 of file ViconDriver.cpp.

167 {
168  std::vector<base::Vector3d> result;
169 
170  unsigned int UnlabeledMarkerCount = impl->client.GetUnlabeledMarkerCount().MarkerCount;
171  for( unsigned int UnlabeledMarkerIndex = 0 ; UnlabeledMarkerIndex < UnlabeledMarkerCount ; ++UnlabeledMarkerIndex )
172  {
173  Output_GetUnlabeledMarkerGlobalTranslation _Output_GetUnlabeledMarkerGlobalTranslation =
174  impl->client.GetUnlabeledMarkerGlobalTranslation( UnlabeledMarkerIndex );
175 
176  Eigen::Vector3d marker_pos(
177  _Output_GetUnlabeledMarkerGlobalTranslation.Translation[ 0 ],
178  _Output_GetUnlabeledMarkerGlobalTranslation.Translation[ 1 ],
179  _Output_GetUnlabeledMarkerGlobalTranslation.Translation[ 2 ] );
180 
181  result.push_back( marker_pos * 1e-3 );
182  }
183 
184  return result;
185 }

◆ isConnected()

bool Driver::isConnected ( )
Returns
true if there is a connection to the server

Definition at line 82 of file ViconDriver.cpp.

83 {
84  return impl->client.IsConnected().Connected;
85 }

◆ setAxesDir()

void Driver::setAxesDir ( int  x_dir,
int  y_dir,
int  z_dir 
)

Set axes directions.

Mapping is as follows: Forward=1, Backward=-1, Left=2, Right=-2, Up=3, Down=-3

Definition at line 196 of file ViconDriver.cpp.

197 {
198  impl->client.SetAxisMapping( axesMap(x_dir), axesMap(y_dir), axesMap(z_dir) );
199 
200 }
int axesMap(Direction::Enum axis)
Definition: ViconDriver.cpp:19

The documentation for this class was generated from the following files: