Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bindings for Multiple Analog Sensors interfaces #1756

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ if (_nested_build)
# bindings for other languages to be compiled from the build material.

# Install main CMakeLists and Swig input file
foreach(f CMakeLists.txt yarp.i README.md)
foreach(f CMakeLists.txt yarp.i macrosForMultipleAnalogSensors.i matlab/vectors_fromTo_matlab.i README.md)
install(FILES ${CMAKE_SOURCE_DIR}/bindings/${f}
COMPONENT development
DESTINATION ${CMAKE_INSTALL_DATADIR}/yarp/bindings)
Expand Down
58 changes: 58 additions & 0 deletions bindings/macrosForMultipleAnalogSensors.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (C) 2018 Istituto Italiano di Tecnologia (IIT)
// All rights reserved.
//
// This software may be modified and distributed under the terms of the
// BSD-3-Clause license. See the accompanying LICENSE file for details.

%define RESET_CONSTANTS_IN_EXTENDED_ANALOG_SENSOR_INTERFACE
#undef ThreeAxisGyroscopes_EXTENDED_INTERFACE
#undef ThreeAxisLinearAccelerometers_EXTENDED_INTERFACE
#undef ThreeAxisMagnetometers_EXTENDED_INTERFACE
#undef OrientationSensors_EXTENDED_INTERFACE
#undef TemperatureSensors_EXTENDED_INTERFACE
#undef SixAxisForceTorqueSensors_EXTENDED_INTERFACE
#undef ContactLoadCellArrays_EXTENDED_INTERFACE
#undef EncoderArrays_EXTENDED_INTERFACE
#undef SkinPatches_EXTENDED_INTERFACE
%enddef

%define EXTENDED_ANALOG_SENSOR_INTERFACE(sensor)

RESET_CONSTANTS_IN_EXTENDED_ANALOG_SENSOR_INTERFACE

#define sensor ## _EXTENDED_INTERFACE 1

std::string get ## sensor ## Name(int sens_index) const {
std::string name;
bool ok = self->get ## sensor ## Name(sens_index,name);
if (!ok) return "unknown";
return name;
}

#if !ContactLoadCellArray_EXTENDED_INTERFACE \
&& !EncoderArray_EXTENDED_INTERFACE \
&& !SkinPatch_EXTENDED_INTERFACE
std::string get ## sensor ## FrameName(int sens_index) const {
std::string frameName;
bool ok = self->get ## sensor ## FrameName(sens_index,frameName);
if (!ok) return "unknown";
return frameName;
}
#endif

#if OrientationSensor_EXTENDED_INTERFACE
double get ## sensor ## MeasureAsRollPitchYaw(int sens_index, yarp::sig::Vector& rpy) const {
double timestamp;
bool ok = self->get ## sensor ## MeasureAsRollPitchYaw(sens_index, rpy, timestamp);
#else
double get ## sensor ## Measure(int sens_index, yarp::sig::Vector& out) const {
double timestamp;
bool ok = self->get ## sensor ## Measure(sens_index, out, timestamp);
#endif
if (!ok) return -1;
return timestamp;
}

#undef sensor ## _EXTENDED_INTERFACE

%enddef
61 changes: 0 additions & 61 deletions bindings/matlab/IVector_fromTo_matlab.i

This file was deleted.

108 changes: 108 additions & 0 deletions bindings/matlab/vectors_fromTo_matlab.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (C) 2006-2018 Istituto Italiano di Tecnologia (IIT)
// All rights reserved.
//
// This software may be modified and distributed under the terms of the
// BSD-3-Clause license. See the accompanying LICENSE file for details.

%define RESET_CONSTANTS_IN_TO_MATLAB
#undef mxCreateDoubleMatrixHasComplexFlagParam
#undef mxCreateLogicalMatrixHasComplexFlagParam
%enddef

%define TO_MATLAB(matlabType,mxArrayInitializer)

// Convert to a Matlab vector
mxArray * toMatlab() const
{
// create Matlab vector and map it to pointer 'd'
size_t selfDim = self->size();
#if mxArrayInitializer ## HasComplexFlagParam
mxArray *p = mxArrayInitializer(selfDim, 1, mxREAL);
#else
mxArray *p = mxArrayInitializer(selfDim, 1);
#endif
matlabType* d = static_cast<matlabType*>(mxGetData(p));

// copy items from 'self' to 'd'
for(size_t i=0; i<selfDim; i++)
{
d[i] = static_cast<matlabType>(self->operator[](i));
}
return p;
}

%enddef


%define FROM_MATLAB(matlabType,cppType,mxArrayInitializer,vectorClass)

// Convert from a Matlab vector
void fromMatlab(mxArray * in)
{
// check size
const mwSize * dims = mxGetDimensions(in);
size_t selfDim = self->size();
size_t matlabVecDim = (dims[0] == 1 ? dims[1] : dims[0]);

if (matlabVecDim == selfDim)
{
// map Matlab vector to pointer 'd'
matlabType* d = static_cast<matlabType*>(mxGetData(in));

// copy items from 'd' to 'self'
for(size_t i=0; i<selfDim; i++)
{
self->operator[](i) = static_cast<cppType>(d[i]);
}
return;
} else {
mexErrMsgIdAndTxt("yarp::vectorClass::wrongDimension",
"Wrong vector size. Matlab size: %d. vectorClass size: %d", matlabVecDim, selfDim);
}
}

%enddef


%define RESET(resetValue)

// Reset values
void zero()
{
for(size_t i=0; i < self->size(); i++)
{
self->operator[](i) = resetValue;
}
return;
}

%enddef

RESET_CONSTANTS_IN_TO_MATLAB
#define mxCreateDoubleMatrixHasComplexFlagParam 1

%extend std::vector<double> {
TO_MATLAB(double,mxCreateDoubleMatrix)
FROM_MATLAB(double,double,mxCreateDoubleMatrix,DVector)
RESET(0)
}

%extend std::vector<bool> {
TO_MATLAB(bool,mxCreateLogicalMatrix)
FROM_MATLAB(bool,bool,mxCreateLogicalMatrix,BVector)
RESET(false)
}

%extend std::vector<int> {
TO_MATLAB(double,mxCreateDoubleMatrix)
FROM_MATLAB(double,int,mxCreateDoubleMatrix,IVector)
RESET(0)
}

%extend yarp::sig::Vector {
TO_MATLAB(double,mxCreateDoubleMatrix)
FROM_MATLAB(double,double,mxCreateDoubleMatrix,yarp::sig::Vector)
RESET(0)
}

RESET_CONSTANTS_IN_TO_MATLAB
Loading