yarp-devices
EmulatedControlBoard.hpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 #ifndef __EMULATED_CONTROL_BOARD_HPP__
4 #define __EMULATED_CONTROL_BOARD_HPP__
5 
6 #include <vector>
7 
8 #include <yarp/os/PeriodicThread.h>
9 #include <yarp/os/Semaphore.h>
10 
11 #include <yarp/dev/Drivers.h>
12 #include <yarp/dev/PolyDriver.h>
13 #include <yarp/dev/ControlBoardInterfaces.h>
14 
15 namespace roboticslab
16 {
17 
28 class EmulatedControlBoard : public yarp::dev::DeviceDriver,
29  public yarp::dev::IControlLimits,
30  public yarp::dev::IControlMode,
31  public yarp::dev::IEncodersTimed,
32  public yarp::dev::IPositionControl,
33  public yarp::dev::IPositionDirect,
34  public yarp::dev::IVelocityControl,
35  public yarp::os::PeriodicThread
36 {
37 public:
38 
39  // Set the thread period in the class constructor
40  EmulatedControlBoard() : PeriodicThread(1.0) {} // In seconds
41 
42  // ------- IPositionControl declarations. Implementation in IPositionControlImpl.cpp -------
43 
44  bool getAxes(int *ax) override;
45  bool positionMove(int j, double ref) override;
46  bool positionMove(const double *refs) override;
47  bool relativeMove(int j, double delta) override;
48  bool relativeMove(const double *deltas) override;
49  bool checkMotionDone(int j, bool *flag) override;
50  bool checkMotionDone(bool *flag) override;
51  bool setRefSpeed(int j, double sp) override;
52  bool setRefSpeeds(const double *spds) override;
53  bool setRefAcceleration(int j, double acc) override;
54  bool setRefAccelerations(const double *accs) override;
55  bool getRefSpeed(int j, double *ref) override;
56  bool getRefSpeeds(double *spds) override;
57  bool getRefAcceleration(int j, double *acc) override;
58  bool getRefAccelerations(double *accs) override;
59  bool stop(int j) override;
60  bool stop() override;
61  bool positionMove(int n_joint, const int *joints, const double *refs) override;
62  bool relativeMove(int n_joint, const int *joints, const double *deltas) override;
63  bool checkMotionDone(int n_joint, const int *joints, bool *flags) override;
64  bool setRefSpeeds(int n_joint, const int *joints, const double *spds) override;
65  bool setRefAccelerations(int n_joint, const int *joints, const double *accs) override;
66  bool getRefSpeeds(int n_joint, const int *joints, double *spds) override;
67  bool getRefAccelerations(int n_joint, const int *joints, double *accs) override;
68  bool stop(int n_joint, const int *joints) override;
69  bool getTargetPosition(int joint, double *ref) override;
70  bool getTargetPositions(double *refs) override;
71  bool getTargetPositions(int n_joint, const int *joints, double *refs) override;
72 
73  // ------- IPositionDirect declarations. Implementation in IPositionDirectImpl.cpp -------
74 
75  bool setPosition(int j, double ref) override;
76  bool setPositions(int n_joint, const int *joints, const double *refs) override;
77  bool setPositions(const double *refs) override;
78  bool getRefPosition(int joint, double *ref) override;
79  bool getRefPositions(double *refs) override;
80  bool getRefPositions(int n_joint, const int *joints, double *refs) override;
81 
82  // ---------- IEncodersTimed Declarations. Implementation in IEncoderImpl.cpp ----------
83 
84  bool resetEncoder(int j) override;
85  bool resetEncoders() override;
86  bool setEncoder(int j, double val) override;
87  bool setEncoders(const double *vals) override;
88  bool getEncoder(int j, double *v) override;
89  bool getEncoders(double *encs) override;
90  bool getEncoderSpeed(int j, double *sp) override;
91  bool getEncoderSpeeds(double *spds) override;
92  bool getEncoderAcceleration(int j, double *spds) override;
93  bool getEncoderAccelerations(double *accs) override;
94  bool getEncodersTimed(double *encs, double *time) override;
95  bool getEncoderTimed(int j, double *encs, double *time) override;
96 
97  // --------- IVelocityControl Declarations. Implementation in IVelocityControlImpl.cpp ---------
98 
99  bool velocityMove(int j, double sp) override;
100  bool velocityMove(const double *sp) override;
101  bool velocityMove(int n_joint, const int *joints, const double *spds) override;
102  bool getRefVelocity(int joint, double *vel) override;
103  bool getRefVelocities(double *vels) override;
104  bool getRefVelocities(int n_joint, const int *joints, double *vels) override;
105 
106  // --------- IControlLimits Declarations. Implementation in IControlLimitsImpl.cpp ---------
107 
108  bool setLimits(int axis, double min, double max) override;
109  bool getLimits(int axis, double *min, double *max) override;
110  bool setVelLimits(int axis, double min, double max) override;
111  bool getVelLimits(int axis, double *min, double *max) override;
112 
113  // --------- IControlMode Declarations. Implementation in IControlModeImpl.cpp ---------
114 
115  bool getControlMode(int j, int *mode) override;
116  bool getControlModes(int *modes) override;
117  bool getControlModes(int n_joint, const int *joints, int *modes) override;
118  bool setControlMode(int j, const int mode) override;
119  bool setControlModes(int n_joint, const int *joints, int *modes) override;
120  bool setControlModes(int *modes) override;
121 
122  // -------- DeviceDriver declarations. Implementation in DeviceDriverImpl.cpp --------
123 
124  bool open(yarp::os::Searchable& config) override;
125  bool close() override;
126 
127  // -------- PeriodicThread declarations. Implementation in PeriodicThreadImpl.cpp --------
128 
129  bool threadInit() override;
130  void run() override;
131 
132  // ----- Shared Area Funcion declarations. Implementation in SharedArea.cpp -----
133 
134  void setEncRaw(const int index, const double position);
135  void setEncsRaw(const std::vector<double> & positions);
136 
137  double getEncRaw(const int index);
138  std::vector<double> getEncsRaw();
139 
140  double getEncExposed(const int index);
141  std::vector<double> getEncsExposed();
142 
143  // ------------------------------- Private -------------------------------------
144 
145 private:
146 
147  enum jmc_state { NOT_CONTROLLING, POSITION_MOVE, RELATIVE_MOVE, VELOCITY_MOVE };
148  enum jmc_mode { POSITION_MODE, VELOCITY_MODE, POSITION_DIRECT_MODE };
149 
150  bool setPositionMode(int j);
151  bool setVelocityMode(int j);
152  bool setTorqueMode(int j);
153  bool setPositionDirectMode(int j);
154 
155  // General Joint Motion Controller parameters //
156  unsigned int axes;
157  double jmcMs;
158  jmc_mode controlMode;
159  double lastTime;
160 
161  yarp::os::Semaphore encRawMutex; // SharedArea
162 
163  std::vector<jmc_state> jointStatus;
164 
165  std::vector<double> encRaw;
166  std::vector<double> encRawExposed; // For conversion.
167  std::vector<double> initPos; // Exposed.
168  std::vector<double> jointTol; // Exposed.
169  std::vector<double> maxLimit; // Exposed.
170  std::vector<double> minLimit; // Exposed.
171  std::vector<double> refAcc; // Exposed.
172  std::vector<double> refSpeed; // Exposed.
173  std::vector<double> targetExposed; // Exposed.
174  std::vector<double> velRawExposed; // For conversion.
175  std::vector<double> velRaw;
176 };
177 
178 } // namespace roboticslab
179 
180 #endif // __EMULATED_CONTROL_BOARD_HPP__
Implements several motor interfaces.
Definition: EmulatedControlBoard.hpp:36
The main, catch-all namespace for Robotics Lab UC3M.
Definition: groups.dox:6