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

New class for return values of interface methods. #3051

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/devices/RemoteControlBoard/RemoteControlBoard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1567,12 +1567,12 @@ bool RemoteControlBoard::getNumberOfMotors(int *num)
return get1V1I(VOCAB_MOTORS_NUMBER, *num);
}

bool RemoteControlBoard::getTemperature (int m, double* val)
yarp_ret_value RemoteControlBoard::getTemperature (int m, double* val)
{
return get1V1I1D(VOCAB_TEMPERATURE, m, val);
}

bool RemoteControlBoard::getTemperatures (double *vals)
yarp_ret_value RemoteControlBoard::getTemperatures (double *vals)
{
return get1VDA(VOCAB_TEMPERATURES, vals);
}
Expand Down
4 changes: 2 additions & 2 deletions src/devices/RemoteControlBoard/RemoteControlBoard.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ class RemoteControlBoard :

// IMotor
bool getNumberOfMotors(int *num) override;
bool getTemperature(int m, double* val) override;
bool getTemperatures(double *vals) override;
yarp::dev::yarp_ret_value getTemperature(int m, double* val) override;
yarp::dev::yarp_ret_value getTemperatures(double *vals) override;
bool getTemperatureLimit (int m, double* val) override;
bool setTemperatureLimit (int m, const double val) override;
bool getGearboxRatio(int m, double* val) override;
Expand Down
4 changes: 2 additions & 2 deletions src/devices/controlBoardRemapper/ControlBoardRemapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1986,7 +1986,7 @@ bool ControlBoardRemapper::getNumberOfMotors(int *num)
return true;
}

bool ControlBoardRemapper::getTemperature(int m, double* val)
yarp::dev::yarp_ret_value ControlBoardRemapper::getTemperature(int m, double* val)
{
int off=(int)remappedControlBoards.lut[m].axisIndexInSubControlBoard;
size_t subIndex=remappedControlBoards.lut[m].subControlBoardIndex;
Expand All @@ -2006,7 +2006,7 @@ bool ControlBoardRemapper::getTemperature(int m, double* val)
return false;
}

bool ControlBoardRemapper::getTemperatures(double *vals)
yarp::dev::yarp_ret_value ControlBoardRemapper::getTemperatures(double *vals)
{
bool ret=true;
for(int l=0;l<controlledJoints;l++)
Expand Down
4 changes: 2 additions & 2 deletions src/devices/controlBoardRemapper/ControlBoardRemapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,9 @@ class ControlBoardRemapper :
/* IMotor */
bool getNumberOfMotors(int *num) override;

bool getTemperature(int m, double *val) override;
yarp::dev::yarp_ret_value getTemperature(int m, double *val) override;

bool getTemperatures(double *vals) override;
yarp::dev::yarp_ret_value getTemperatures(double *vals) override;

bool getTemperatureLimit(int m, double *val) override;

Expand Down
8 changes: 4 additions & 4 deletions src/devices/fakeMotionControl/fakeMotionControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3163,15 +3163,15 @@ bool FakeMotionControl::getNumberOfMotorsRaw(int* num)
return true;
}

bool FakeMotionControl::getTemperatureRaw(int m, double* val)
yarp_ret_value FakeMotionControl::getTemperatureRaw(int m, double* val)
{
*val = 37.5+double(m);
return true;
return yarp_ret_value(true);
}

bool FakeMotionControl::getTemperaturesRaw(double *vals)
yarp_ret_value FakeMotionControl::getTemperaturesRaw(double *vals)
{
bool ret = true;
yarp_ret_value ret = true;
for(int j=0; j< _njoints; j++)
{
ret &= getTemperatureRaw(j, &vals[j]);
Expand Down
4 changes: 2 additions & 2 deletions src/devices/fakeMotionControl/fakeMotionControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ class FakeMotionControl :

// IMotor interface
bool getNumberOfMotorsRaw(int * num) override;
bool getTemperatureRaw(int m, double* val) override;
bool getTemperaturesRaw(double *vals) override;
yarp::dev::yarp_ret_value getTemperatureRaw(int m, double* val) override;
yarp::dev::yarp_ret_value getTemperaturesRaw(double *vals) override;
bool getTemperatureLimitRaw(int m, double *temp) override;
bool setTemperatureLimitRaw(int m, const double temp) override;
bool getGearboxRatioRaw(int m, double* gearbox) override;
Expand Down
2 changes: 2 additions & 0 deletions src/libYARP_dev/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ set(YARP_dev_HDRS
yarp/dev/PolyDriverDescriptor.h
yarp/dev/PolyDriverList.h
yarp/dev/RGBDSensorParamParser.h
yarp/dev/ReturnValue.h
yarp/dev/ServiceInterfaces.h
yarp/dev/WrapperMultiple.h
yarp/dev/WrapperSingle.h
Expand Down Expand Up @@ -223,6 +224,7 @@ set(YARP_dev_SRCS
yarp/dev/PolyDriver.cpp
yarp/dev/PolyDriverDescriptor.cpp
yarp/dev/PolyDriverList.cpp
yarp/dev/ReturnValue.cpp
yarp/dev/RGBDSensorParamParser.cpp
yarp/dev/WrapperMultiple.cpp
yarp/dev/WrapperSingle.cpp
Expand Down
13 changes: 7 additions & 6 deletions src/libYARP_dev/src/yarp/dev/IMotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <yarp/os/Vocab.h>
#include <yarp/os/Log.h>
#include <yarp/dev/api.h>
#include <yarp/dev/ReturnValue.h>

namespace yarp::dev {
class IMotorRaw;
Expand Down Expand Up @@ -41,17 +42,17 @@ class YARP_dev_API yarp::dev::IMotorRaw
* @param val retrieved motor temperature
* @return true/false
*/
virtual bool getTemperatureRaw(int m, double* val)=0;
virtual yarp::dev::yarp_ret_value getTemperatureRaw(int m, double* val)=0;

/**
* Get temperature of all the motors.
* @param vals pointer to an array containing all motor temperatures
* @return true/false
*/
virtual bool getTemperaturesRaw(double *vals)=0;
virtual yarp::dev::yarp_ret_value getTemperaturesRaw(double *vals)=0;

/**
* Retreives the current temperature limit for a specific motor.
* Retrieves the current temperature limit for a specific motor.
* The specific behavior of the motor when the temperature limit is exceeded depends on the implementation (power off recommended)
* @param m motor number
* @param temp the current temperature limit.
Expand Down Expand Up @@ -111,17 +112,17 @@ class YARP_dev_API yarp::dev::IMotor
* @param val retrieved motor temperature
* @return true/false
*/
virtual bool getTemperature(int m, double *val)=0;
virtual yarp::dev::yarp_ret_value getTemperature(int m, double *val)=0;

/**
* Get temperature of all the motors.
* @param vals pointer to an array containing all motor temperatures
* @return true/false
*/
virtual bool getTemperatures(double *vals)=0;
virtual yarp::dev::yarp_ret_value getTemperatures(double *vals)=0;

/**
* Retreives the current temperature limit for a specific motor.
* Retrieves the current temperature limit for a specific motor.
* The specific behavior of the motor when the temperature limit is exceeded depends on the implementation (power off recommended)
* @param m motor number
* @param temp the current temperature limit.
Expand Down
8 changes: 4 additions & 4 deletions src/libYARP_dev/src/yarp/dev/ImplementMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ bool ImplementMotor::getNumberOfMotors(int *num)
return true;
}

bool ImplementMotor::getTemperature(int m, double* value)
yarp::dev::yarp_ret_value ImplementMotor::getTemperature(int m, double* value)
{
JOINTIDCHECK
bool ret;
yarp::dev::yarp_ret_value ret;
int k=castToMapper(helper)->toHw(m);

ret=imotor->getTemperatureRaw(k, value);
Expand Down Expand Up @@ -122,11 +122,11 @@ bool ImplementMotor::setGearboxRatio(int m, const double value)
return ret;
}

bool ImplementMotor::getTemperatures(double *v)
yarp::dev::yarp_ret_value ImplementMotor::getTemperatures(double *v)
{
yarp::dev::impl::Buffer<double> buffValues = doubleBuffManager->getBuffer();

bool ret = imotor->getTemperaturesRaw(buffValues.getData());
yarp::dev::yarp_ret_value ret = imotor->getTemperaturesRaw(buffValues.getData());
for (size_t i=0; i< buffValues.getSize(); i++)
{
int k = castToMapper(helper)->toHw(i);
Expand Down
4 changes: 2 additions & 2 deletions src/libYARP_dev/src/yarp/dev/ImplementMotor.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class YARP_dev_API yarp::dev::ImplementMotor: public IMotor

bool getNumberOfMotors(int *num) override;

bool getTemperature (int m, double* val) override;
bool getTemperatures (double *vals) override;
yarp::dev::yarp_ret_value getTemperature (int m, double* val) override;
yarp::dev::yarp_ret_value getTemperatures (double *vals) override;
bool getTemperatureLimit (int m, double* val) override;
bool setTemperatureLimit (int m, const double val) override;
bool getGearboxRatio (int m, double* val) override;
Expand Down
94 changes: 94 additions & 0 deletions src/libYARP_dev/src/yarp/dev/ReturnValue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <yarp/dev/ReturnValue.h>

using namespace yarp::dev;

yarp_ret_value::yarp_ret_value()
{
}

yarp_ret_value::yarp_ret_value(const bool& val)
{
if (val)
{
value_b = return_code::return_value_ok;
}
else
{
value_b = return_code::return_value_error_generic;
}
}

/*/yarp_ret_value::yarp_ret_value(const yarp_ret_value& other)
{
this->value_b=other.value_b;
}*/

yarp_ret_value& yarp_ret_value::operator&=(const yarp_ret_value& other)
{
if (other.operator bool() == true)
{
return *this;
}
else
{
value_b = return_code::return_value_error_generic;
}
return *this;
}

yarp_ret_value& yarp_ret_value::operator=(const bool& bool_val)
{
if (bool_val)
{
value_b = return_code::return_value_ok;
}
else
{
value_b = return_code::return_value_error_generic;
}
return *this;
}

std::string yarp_ret_value::toString()
{
switch (value_b)
{
case return_code::return_value_ok:
return std::string("ok");
case return_code::return_value_unitialized:
return std::string("return_value_unitialized");
Comment on lines +63 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "uninitialized".

Suggestion: isn't the "return_value_" prefix a bit verbose? I'm also thinking of an overload to LogStream so that this would be possible:

double val;
auto ret = iMotor->getTemperature(0, &val);
yInfo() << ret; // prints "ok", "error_generic" "error_not_implemented" and so on

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a nice idea! Just give me a little more time before proceeding since we need to edit a few things more before.

case return_code::return_value_error_deprecated:
return std::string("return_value_error_deprecated");
case return_code::return_value_error_generic:
return std::string("return_value_error_generic");
case return_code::return_value_error_method_failed:
return std::string("return_value_error_method_fail");
case return_code::return_value_error_not_implemented_by_device:
return std::string("return_value_error_not_implemented_by_device");
case return_code::return_value_error_nws_nwc_communication_error:
return std::string("return_value_error_nws_nwc_communication_error");
default:
return std::string("unknown");
}
}

yarp_ret_value::operator bool() const
{
return value_b == return_code::return_value_ok;
}

yarp_ret_value::yarp_ret_value(return_code code)
{
value_b = code;
}

bool yarp_ret_value::operator == (const return_code& code) const
{
if (code == this->value_b) return true;
return false;
}
46 changes: 46 additions & 0 deletions src/libYARP_dev/src/yarp/dev/ReturnValue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
* SPDX-License-Identifier: BSD-3-Clause
*/

#ifndef YARP_RET_VALUE_H
#define YARP_RET_VALUE_H

#include <yarp/dev/api.h>
#include <string>

namespace yarp::dev {

class YARP_dev_API yarp_ret_value
{
public:
enum class YARP_dev_API return_code
{
return_value_ok = 0,
return_value_error_generic = 1,
return_value_error_not_implemented_by_device = 2,
return_value_error_nws_nwc_communication_error = 3,
return_value_error_deprecated = 4,
return_value_error_method_failed = 5,
return_value_unitialized = 100
};

private:
return_code value_b = return_code::return_value_unitialized;

public:
yarp_ret_value();
yarp_ret_value(const bool& val);
yarp_ret_value(return_code code);
yarp_ret_value(const yarp_ret_value& other) = default;
yarp_ret_value& operator&=(const yarp_ret_value& other);
yarp_ret_value& operator=(const bool& bool_val);
bool operator == (const return_code& code) const;
std::string toString();
operator bool() const;
};

}

#endif // YARP_RET_VALUE_H
1 change: 1 addition & 0 deletions src/libYARP_dev/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ target_sources(harness_dev
PRIVATE
MapGrid2DTest.cpp
PolyDriverTest.cpp
ReturnValueTest.cpp
)

target_link_libraries(harness_dev
Expand Down
Loading
Loading