iCub-main
fakeMotorDeviceComponents.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
3  * Author: Ugo Pattacini
4  * email: ugo.pattacini@iit.it
5  * Permission is granted to copy, distribute, and/or modify this program
6  * under the terms of the GNU General Public License, version 2 or any
7  * later version published by the Free Software Foundation.
8  *
9  * A copy of the license can be found at
10  * http://www.robotcub.org/icub/license/gpl.txt
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details
16 */
17 
18 #ifndef __FAKEMOTORDEVICECOMPONENTS_H__
19 #define __FAKEMOTORDEVICECOMPONENTS_H__
20 
21 #include <mutex>
22 
23 #include <yarp/os/all.h>
24 #include <yarp/dev/all.h>
25 #include <yarp/sig/all.h>
26 
27 #include <iCub/ctrl/pids.h>
28 
33 class fakeMotorDeviceServer : public yarp::dev::DeviceDriver,
34  public yarp::os::PeriodicThread,
35  public yarp::os::PortReader,
36  public yarp::dev::IControlLimits,
37  public yarp::dev::IEncoders,
38  public yarp::dev::IVelocityControl
39 {
40 protected:
41  yarp::os::BufferedPort<yarp::sig::Vector> statePort;
42  yarp::os::BufferedPort<yarp::os::Bottle> cmdPort;
43  yarp::os::Port rpcPort;
44 
45  std::mutex mtx;
46 
48  yarp::sig::Vector vel;
49  bool configured;
50 
51  void run();
56  bool read(yarp::os::ConnectionReader &connection);
57 
58 public:
60  bool open(yarp::os::Searchable &config);
61  bool close();
62 
67  /**********************************************************/
68  bool getLimits(int axis, double *min, double *max);
69 
70  // not implemented
71  /**********************************************************/
72  bool setLimits(int,double,double) { return false; }
73  bool setVelLimits(int,double,double) { return false; }
74  bool getVelLimits(int,double*,double*) { return false; }
75 
80  /**********************************************************/
81  bool getAxes(int *ax);
82 
83  // not implemented
84  /**********************************************************/
85  bool getEncoder(int,double*) { return false; }
86  bool getEncoders(double*) { return false; }
87  bool resetEncoder(int) { return false; }
88  bool resetEncoders() { return false; }
89  bool setEncoder(int,double) { return false; }
90  bool setEncoders(const double*) { return false; }
91  bool getEncoderSpeed(int,double*) { return false; }
92  bool getEncoderSpeeds(double*) { return false; }
93  bool getEncoderAcceleration(int,double*) { return false; }
94  bool getEncoderAccelerations(double*) { return false; }
95 
100  /**********************************************************/
101  bool velocityMove(int j, double sp);
102  bool setRefAcceleration(int j, double acc);
103  bool stop(int j);
104 
105  // not implemented
106  /**********************************************************/
107  bool velocityMove(const int,const int*,const double*) { return false; }
108  bool velocityMove(const double*) { return false; }
109  bool setRefAccelerations(const int,const int*,const double*) { return false; }
110  bool setRefAccelerations(const double*) { return false; }
111  bool getRefAccelerations(const int,const int*,double*) { return false; }
112  bool getRefAcceleration(int,double*) { return false; }
113  bool getRefAccelerations(double*) { return false; }
114  bool stop(const int,const int*) { return false; }
115  bool stop() { return false; }
116 };
117 
122 class fakeMotorDeviceClient : public yarp::dev::DeviceDriver,
123  public yarp::dev::IControlLimits,
124  public yarp::dev::IEncoders,
125  public yarp::dev::IVelocityControl
126 {
127 protected:
128  class StatePort : public yarp::os::BufferedPort<yarp::sig::Vector>
129  {
130  fakeMotorDeviceClient *owner;
131  void onRead(yarp::sig::Vector &encs)
132  {
133  if (owner!=NULL)
134  {
135  std::lock_guard<std::mutex> lg(owner->mtx);
136  owner->encs=encs;
137  }
138  }
139  public:
140  StatePort() : owner(NULL) { useCallback(); }
141  void setOwner(fakeMotorDeviceClient *owner) { this->owner=owner; }
142  };
143 
145  yarp::os::BufferedPort<yarp::os::Bottle> cmdPort;
146  yarp::os::RpcClient rpcPort;
147 
148  std::mutex mtx;
149 
150  yarp::sig::Vector encs;
152 
153  friend class StatePort;
154 
155 public:
157  bool open(yarp::os::Searchable &config);
158  bool close();
159 
164  /**********************************************************/
165  bool getLimits(int axis, double *min, double *max);
166 
167  // not implemented
168  /**********************************************************/
169  bool setLimits(int,double,double) { return false; }
170  bool setVelLimits(int,double,double) { return false; }
171  bool getVelLimits(int,double*,double*) { return false; }
172 
177  /**********************************************************/
178  bool getAxes(int *ax);
179  bool getEncoders(double *encs);
180 
181  // not implemented
182  /**********************************************************/
183  bool getEncoder(int,double*) { return false; }
184  bool resetEncoder(int) { return false; }
185  bool resetEncoders() { return false; }
186  bool setEncoder(int,double) { return false; }
187  bool setEncoders(const double*) { return false; }
188  bool getEncoderSpeed(int,double*) { return false; }
189  bool getEncoderSpeeds(double*) { return false; }
190  bool getEncoderAcceleration(int,double*) { return false; }
191  bool getEncoderAccelerations(double*) { return false; }
192 
197  /**********************************************************/
198  bool velocityMove(int j, double sp);
199  bool setRefAcceleration(int j, double acc);
200  bool stop(int j);
201 
202  // not implemented
203  /**********************************************************/
204  bool velocityMove(const int,const int*,const double*) { return false; }
205  bool velocityMove(const double*) { return false; }
206  bool setRefAccelerations(const int,const int*,const double*) { return false; }
207  bool setRefAccelerations(const double*) { return false; }
208  bool getRefAccelerations(const int,const int*,double*) { return false; }
209  bool getRefAcceleration(int,double*) { return false; }
210  bool getRefAccelerations(double*) { return false; }
211  bool stop(const int,const int*) { return false; }
212  bool stop() { return false; }
213 };
214 
215 #endif
216 
217 
fakeMotorDeviceServer::setEncoders
bool setEncoders(const double *)
Definition: fakeMotorDeviceComponents.h:90
fakeMotorDeviceServer::getRefAccelerations
bool getRefAccelerations(double *)
Definition: fakeMotorDeviceComponents.h:113
fakeMotorDeviceClient::setLimits
bool setLimits(int, double, double)
Definition: fakeMotorDeviceComponents.h:169
iCub::ctrl::Integrator
Definition: pids.h:47
fakeMotorDeviceClient::setRefAcceleration
bool setRefAcceleration(int j, double acc)
Definition: fakeMotorDeviceClient.cpp:159
fakeMotorDeviceClient::mtx
std::mutex mtx
Definition: fakeMotorDeviceComponents.h:148
fakeMotorDeviceClient::StatePort::StatePort
StatePort()
Definition: fakeMotorDeviceComponents.h:140
fakeMotorDeviceClient::velocityMove
bool velocityMove(const double *)
Definition: fakeMotorDeviceComponents.h:205
fakeMotorDeviceServer::open
bool open(yarp::os::Searchable &config)
Definition: fakeMotorDeviceServer.cpp:37
fakeMotorDeviceClient::setEncoder
bool setEncoder(int, double)
Definition: fakeMotorDeviceComponents.h:186
fakeMotorDeviceServer::getEncoder
bool getEncoder(int, double *)
Definition: fakeMotorDeviceComponents.h:85
fakeMotorDeviceClient::getEncoder
bool getEncoder(int, double *)
Definition: fakeMotorDeviceComponents.h:183
fakeMotorDeviceServer::getEncoderSpeeds
bool getEncoderSpeeds(double *)
Definition: fakeMotorDeviceComponents.h:92
fakeMotorDeviceServer::setRefAccelerations
bool setRefAccelerations(const int, const int *, const double *)
Definition: fakeMotorDeviceComponents.h:109
fakeMotorDeviceClient::getEncoders
bool getEncoders(double *encs)
Definition: fakeMotorDeviceClient.cpp:129
fakeMotorDeviceClient::getEncoderSpeeds
bool getEncoderSpeeds(double *)
Definition: fakeMotorDeviceComponents.h:189
fakeMotorDeviceServer::resetEncoder
bool resetEncoder(int)
Definition: fakeMotorDeviceComponents.h:87
strain::dsp::fsc::min
const FSC min
Definition: strain.h:49
fakeMotorDeviceServer::cmdPort
yarp::os::BufferedPort< yarp::os::Bottle > cmdPort
Definition: fakeMotorDeviceComponents.h:42
fakeMotorDeviceServer::rpcPort
yarp::os::Port rpcPort
Definition: fakeMotorDeviceComponents.h:43
fakeMotorDeviceClient
This class implements the client part of the fake motor device driver.
Definition: fakeMotorDeviceComponents.h:122
fakeMotorDeviceClient::setEncoders
bool setEncoders(const double *)
Definition: fakeMotorDeviceComponents.h:187
fakeMotorDeviceServer::getLimits
bool getLimits(int axis, double *min, double *max)
Definition: fakeMotorDeviceServer.cpp:195
fakeMotorDeviceClient::StatePort
Definition: fakeMotorDeviceComponents.h:128
fakeMotorDeviceServer::velocityMove
bool velocityMove(int j, double sp)
Definition: fakeMotorDeviceServer.cpp:226
fakeMotorDeviceServer::setRefAcceleration
bool setRefAcceleration(int j, double acc)
Definition: fakeMotorDeviceServer.cpp:241
fakeMotorDeviceServer::setVelLimits
bool setVelLimits(int, double, double)
Definition: fakeMotorDeviceComponents.h:73
strain::dsp::fsc::max
const FSC max
Definition: strain.h:48
fakeMotorDeviceServer::run
void run()
Definition: fakeMotorDeviceServer.cpp:108
fakeMotorDeviceClient::statePort
StatePort statePort
Definition: fakeMotorDeviceComponents.h:144
fakeMotorDeviceServer::fakeMotorDeviceServer
fakeMotorDeviceServer()
Definition: fakeMotorDeviceServer.cpp:30
fakeMotorDeviceServer::stop
bool stop()
Definition: fakeMotorDeviceComponents.h:115
fakeMotorDeviceClient::setVelLimits
bool setVelLimits(int, double, double)
Definition: fakeMotorDeviceComponents.h:170
fakeMotorDeviceClient::cmdPort
yarp::os::BufferedPort< yarp::os::Bottle > cmdPort
Definition: fakeMotorDeviceComponents.h:145
fakeMotorDeviceClient::getRefAccelerations
bool getRefAccelerations(double *)
Definition: fakeMotorDeviceComponents.h:210
fakeMotorDeviceServer::stop
bool stop(const int, const int *)
Definition: fakeMotorDeviceComponents.h:114
fakeMotorDeviceClient::open
bool open(yarp::os::Searchable &config)
Definition: fakeMotorDeviceClient.cpp:36
fakeMotorDeviceClient::getAxes
bool getAxes(int *ax)
Definition: fakeMotorDeviceClient.cpp:111
fakeMotorDeviceServer::velocityMove
bool velocityMove(const int, const int *, const double *)
Definition: fakeMotorDeviceComponents.h:107
fakeMotorDeviceServer::setLimits
bool setLimits(int, double, double)
Definition: fakeMotorDeviceComponents.h:72
fakeMotorDeviceClient::resetEncoder
bool resetEncoder(int)
Definition: fakeMotorDeviceComponents.h:184
fakeMotorDeviceServer::statePort
yarp::os::BufferedPort< yarp::sig::Vector > statePort
Definition: fakeMotorDeviceComponents.h:41
fakeMotorDeviceServer::velocityMove
bool velocityMove(const double *)
Definition: fakeMotorDeviceComponents.h:108
fakeMotorDeviceServer::getVelLimits
bool getVelLimits(int, double *, double *)
Definition: fakeMotorDeviceComponents.h:74
fakeMotorDeviceServer::motors
iCub::ctrl::Integrator * motors
Definition: fakeMotorDeviceComponents.h:47
fakeMotorDeviceServer
This class implements the server part of the fake motor device driver.
Definition: fakeMotorDeviceComponents.h:33
fakeMotorDeviceServer::getEncoderAccelerations
bool getEncoderAccelerations(double *)
Definition: fakeMotorDeviceComponents.h:94
fakeMotorDeviceClient::getLimits
bool getLimits(int axis, double *min, double *max)
Definition: fakeMotorDeviceClient.cpp:90
fakeMotorDeviceClient::velocityMove
bool velocityMove(int j, double sp)
Definition: fakeMotorDeviceClient.cpp:142
fakeMotorDeviceClient::resetEncoders
bool resetEncoders()
Definition: fakeMotorDeviceComponents.h:185
fakeMotorDeviceClient::rpcPort
yarp::os::RpcClient rpcPort
Definition: fakeMotorDeviceComponents.h:146
fakeMotorDeviceServer::configured
bool configured
Definition: fakeMotorDeviceComponents.h:49
fakeMotorDeviceServer::getEncoders
bool getEncoders(double *)
Definition: fakeMotorDeviceComponents.h:86
fakeMotorDeviceClient::setRefAccelerations
bool setRefAccelerations(const double *)
Definition: fakeMotorDeviceComponents.h:207
fakeMotorDeviceClient::getVelLimits
bool getVelLimits(int, double *, double *)
Definition: fakeMotorDeviceComponents.h:171
fakeMotorDeviceServer::getAxes
bool getAxes(int *ax)
Definition: fakeMotorDeviceServer.cpp:211
fakeMotorDeviceClient::stop
bool stop()
Definition: fakeMotorDeviceComponents.h:212
fakeMotorDeviceServer::getRefAcceleration
bool getRefAcceleration(int, double *)
Definition: fakeMotorDeviceComponents.h:112
fakeMotorDeviceServer::read
bool read(yarp::os::ConnectionReader &connection)
This method decodes the requests forwarded by the client and responds with corresponding replies.
Definition: fakeMotorDeviceServer.cpp:123
fakeMotorDeviceClient::close
bool close()
Definition: fakeMotorDeviceClient.cpp:71
fakeMotorDeviceClient::getEncoderSpeed
bool getEncoderSpeed(int, double *)
Definition: fakeMotorDeviceComponents.h:188
fakeMotorDeviceClient::configured
bool configured
Definition: fakeMotorDeviceComponents.h:151
fakeMotorDeviceServer::getEncoderAcceleration
bool getEncoderAcceleration(int, double *)
Definition: fakeMotorDeviceComponents.h:93
fakeMotorDeviceClient::encs
yarp::sig::Vector encs
Definition: fakeMotorDeviceComponents.h:150
acc
_3f_vect_t acc
Definition: dataTypes.h:3
fakeMotorDeviceServer::vel
yarp::sig::Vector vel
Definition: fakeMotorDeviceComponents.h:48
fakeMotorDeviceClient::fakeMotorDeviceClient
fakeMotorDeviceClient()
Definition: fakeMotorDeviceClient.cpp:29
fakeMotorDeviceClient::getEncoderAcceleration
bool getEncoderAcceleration(int, double *)
Definition: fakeMotorDeviceComponents.h:190
fakeMotorDeviceServer::setRefAccelerations
bool setRefAccelerations(const double *)
Definition: fakeMotorDeviceComponents.h:110
fakeMotorDeviceServer::close
bool close()
Definition: fakeMotorDeviceServer.cpp:76
fakeMotorDeviceClient::stop
bool stop(const int, const int *)
Definition: fakeMotorDeviceComponents.h:211
fakeMotorDeviceClient::getEncoderAccelerations
bool getEncoderAccelerations(double *)
Definition: fakeMotorDeviceComponents.h:191
fakeMotorDeviceClient::velocityMove
bool velocityMove(const int, const int *, const double *)
Definition: fakeMotorDeviceComponents.h:204
pids.h
fakeMotorDeviceClient::getRefAcceleration
bool getRefAcceleration(int, double *)
Definition: fakeMotorDeviceComponents.h:209
fakeMotorDeviceClient::setRefAccelerations
bool setRefAccelerations(const int, const int *, const double *)
Definition: fakeMotorDeviceComponents.h:206
fakeMotorDeviceClient::getRefAccelerations
bool getRefAccelerations(const int, const int *, double *)
Definition: fakeMotorDeviceComponents.h:208
fakeMotorDeviceClient::StatePort::setOwner
void setOwner(fakeMotorDeviceClient *owner)
Definition: fakeMotorDeviceComponents.h:141
fakeMotorDeviceServer::resetEncoders
bool resetEncoders()
Definition: fakeMotorDeviceComponents.h:88
fakeMotorDeviceServer::getEncoderSpeed
bool getEncoderSpeed(int, double *)
Definition: fakeMotorDeviceComponents.h:91
fakeMotorDeviceServer::mtx
std::mutex mtx
Definition: fakeMotorDeviceComponents.h:45
fakeMotorDeviceServer::getRefAccelerations
bool getRefAccelerations(const int, const int *, double *)
Definition: fakeMotorDeviceComponents.h:111
fakeMotorDeviceServer::setEncoder
bool setEncoder(int, double)
Definition: fakeMotorDeviceComponents.h:89