YARP
Yet Another Robot Platform
dev/fake_motor/fake_motor.cpp

Some tips on how to create a device for a new motor control board.

/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* Copyright (C) 2006-2010 RobotCub Consortium
* 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.
*/
#include <cstdio>
class FakeMotor :
public DeviceDriver,
public IPositionControl
{
public:
bool getAxes(int* ax) override
{
*ax = 2;
printf("FakeMotor reporting %d axes present\n", *ax);
return true;
}
bool positionMove(int j, double ref) override
{
return true;
}
bool positionMove(const double* refs) override
{
YARP_UNUSED(refs);
return true;
}
bool positionMove(const int n_joint, const int* joints, const double* refs) override
{
YARP_UNUSED(n_joint);
YARP_UNUSED(joints);
YARP_UNUSED(refs);
return true;
}
bool relativeMove(int j, double delta) override
{
YARP_UNUSED(delta);
return true;
}
bool relativeMove(const double* deltas) override
{
YARP_UNUSED(deltas);
return true;
}
bool relativeMove(const int n_joint, const int* joints, const double* deltas) override
{
YARP_UNUSED(n_joint);
YARP_UNUSED(joints);
YARP_UNUSED(deltas);
return true;
}
bool checkMotionDone(int j, bool* flag) override
{
YARP_UNUSED(flag);
return true;
}
bool checkMotionDone(bool* flag) override
{
YARP_UNUSED(flag);
return true;
}
bool checkMotionDone(const int n_joint, const int* joints, bool* flag) override
{
YARP_UNUSED(n_joint);
YARP_UNUSED(joints);
YARP_UNUSED(flag);
return true;
}
bool setRefSpeed(int j, double sp) override
{
return true;
}
bool setRefSpeeds(const double* spds) override
{
YARP_UNUSED(spds);
return true;
}
bool setRefSpeeds(const int n_joint, const int* joints, const double* spds) override
{
YARP_UNUSED(n_joint);
YARP_UNUSED(joints);
YARP_UNUSED(spds);
return true;
}
bool setRefAcceleration(int j, double acc) override
{
return true;
}
bool setRefAccelerations(const double* accs) override
{
YARP_UNUSED(accs);
return true;
}
bool setRefAccelerations(const int n_joint, const int* joints, const double* accs) override
{
YARP_UNUSED(n_joint);
YARP_UNUSED(joints);
YARP_UNUSED(accs);
return true;
}
bool getRefSpeed(int j, double* ref) override
{
return true;
}
bool getRefSpeeds(double* spds) override
{
YARP_UNUSED(spds);
return true;
}
bool getRefSpeeds(const int n_joint, const int* joints, double* spds) override
{
YARP_UNUSED(n_joint);
YARP_UNUSED(joints);
YARP_UNUSED(spds);
return true;
}
bool getRefAcceleration(int j, double* acc) override
{
return true;
}
bool getRefAccelerations(double* accs) override
{
YARP_UNUSED(accs);
return true;
}
bool getRefAccelerations(const int n_joint, const int* joints, double* accs) override
{
YARP_UNUSED(n_joint);
YARP_UNUSED(joints);
YARP_UNUSED(accs);
return true;
}
bool stop(int j) override
{
return true;
}
bool stop() override
{
return true;
}
bool stop(const int n_joint, const int* joints) override
{
YARP_UNUSED(n_joint);
YARP_UNUSED(joints);
return true;
}
bool open(Searchable& config) override
{
YARP_UNUSED(config);
return true;
}
bool close() override
{
return true;
}
};
void testMotor(PolyDriver& driver)
{
IPositionControl* pos;
if (driver.view(pos)) {
int ct = 0;
pos->getAxes(&ct);
printf(" number of axes is: %d\n", ct);
} else {
printf(" could not find IPositionControl interface\n");
}
}
int main(int argc, char* argv[])
{
YARP_UNUSED(argc);
YARP_UNUSED(argv);
Drivers::factory().add(new DriverCreatorOf<FakeMotor>("motor",
"controlboard",
"FakeMotor"));
printf("============================================================\n");
printf("check our device can be instantiated directly\n");
PolyDriver direct("motor");
if (direct.isValid()) {
printf("Direct instantiation worked\n");
testMotor(direct);
} else {
printf("Direct instantiation failed\n");
}
direct.close();
// check our device can be wrapped in the controlboard network wrapper
printf("\n\n");
printf("============================================================\n");
printf("check our device can be wrapped in controlboard\n");
PolyDriver indirect("(device controlboard) (subdevice motor)");
if (indirect.isValid()) {
printf("Indirect instantiation worked\n");
} else {
printf("Indirect instantiation failed\n");
}
indirect.close();
// check our device can be wrapped in the controlboard network wrapper
// and accessed remotely
printf("\n\n");
printf("============================================================\n");
printf("check our device can be accessed via remote_controlboard\n");
PolyDriver server("(device controlboard) (subdevice motor) (name /server)");
if (server.isValid()) {
printf("Server instantiation worked\n");
PolyDriver client("(device clientcontrolboard) (local /client) (remote /server)");
if (client.isValid()) {
printf("Client instantiation worked\n");
testMotor(client);
} else {
printf("Client instantiation failed\n");
}
client.close();
}
server.close();
return 0;
}
FakeMotor::positionMove
bool positionMove(int j, double ref) override
Set new reference point for a single axis.
Definition: FakeMotor.h:85
Network.h
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
Drivers.h
yarp::dev::DriverCreatorOf
A factory for creating driver objects of a particular type.
Definition: Drivers.h:85
yarp::dev::DeviceDriver
Interface implemented by all device drivers.
Definition: DeviceDriver.h:38
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:261
FakeMotor::relativeMove
bool relativeMove(int j, double delta) override
Set relative position.
Definition: FakeMotor.h:105
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
FakeMotor::setRefAcceleration
bool setRefAcceleration(int j, double acc) override
Set reference acceleration for a joint.
Definition: FakeMotor.h:156
FakeMotor::getRefSpeed
bool getRefSpeed(int j, double *ref) override
Get reference speed for a joint.
Definition: FakeMotor.h:174
Searchable.h
ControlBoardInterfaces.h
define control board standard interfaces
FakeMotor::open
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition: FakeMotor.h:66
FakeMotor::setRefSpeed
bool setRefSpeed(int j, double sp) override
Set reference speed for a joint, this is the speed used during the interpolation of the trajectory.
Definition: FakeMotor.h:138
FakeMotor::getRefAccelerations
bool getRefAccelerations(double *accs) override
Get reference acceleration of all joints.
Definition: FakeMotor.h:201
PolyDriver.h
FakeMotor::getRefAcceleration
bool getRefAcceleration(int j, double *acc) override
Get reference acceleration for a joint.
Definition: FakeMotor.h:192
yarp::dev::PolyDriver
A container for a device driver.
Definition: PolyDriver.h:27
FakeMotor
fakeMotor: A fake motor control board for testing.
Definition: FakeMotor.h:38
FakeMotor::checkMotionDone
bool checkMotionDone(int j, bool *flag) override
Check if the current trajectory is terminated.
Definition: FakeMotor.h:125
FakeMotor::close
bool close() override
Close the DeviceDriver.
Definition: FakeMotor.h:281
FakeMotor::getAxes
bool getAxes(int *ax) override
Get the number of controlled axes.
Definition: FakeMotor.h:59
FakeMotor::setRefSpeeds
bool setRefSpeeds(const double *spds) override
Set reference speed on all joints.
Definition: FakeMotor.h:147
yarp::os::Network
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:786
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
FakeMotor::stop
bool stop() override
Stop motion, multiple joints.
Definition: FakeMotor.h:216
yarp::dev::Drivers
Global factory for devices.
Definition: Drivers.h:175
FakeMotor::setRefAccelerations
bool setRefAccelerations(const double *accs) override
Set reference acceleration on all joints.
Definition: FakeMotor.h:165
yarp::dev::IPositionControl
Interface for a generic control board device implementing position control.
Definition: IPositionControl.h:257
FakeMotor::getRefSpeeds
bool getRefSpeeds(double *spds) override
Get reference speed of all joints.
Definition: FakeMotor.h:183