20 #ifndef __MOTOR_THREAD__
21 #define __MOTOR_THREAD__
23 #include <yarp/os/all.h>
24 #include <yarp/dev/all.h>
25 #include <yarp/sig/all.h>
26 #include <yarp/math/SVD.h>
27 #include <yarp/math/Math.h>
37 #include <iCub/utils.h>
39 #define HEAD_MODE_IDLE 0
40 #define HEAD_MODE_GO_HOME 1
41 #define HEAD_MODE_TRACK_HAND 2
42 #define HEAD_MODE_TRACK_TEMP 3
43 #define HEAD_MODE_TRACK_CART 4
44 #define HEAD_MODE_TRACK_FIX 5
46 #define ARM_MODE_IDLE 0
47 #define ARM_MODE_LEARN_ACTION 1
48 #define ARM_MODE_LEARN_KINOFF 2
49 #define ARM_MODE_FINE_REACHING 3
51 #define GRASP_STATE_IDLE 0
52 #define GRASP_STATE_ABOVE 1
53 #define GRASP_STATE_SIDE 2
55 #define ARM_HOMING_PERIOD 1.5
58 #define S2C_HOMOGRAPHY yarp::os::createVocab32('h','o','m','o')
59 #define S2C_DISPARITY yarp::os::createVocab32('d','i','s','p')
60 #define S2C_NETWORK yarp::os::createVocab32('n','e','t','w')
64 using namespace yarp::os;
65 using namespace yarp::sig;
67 using namespace yarp::math;
104 void init(Bottle &initializer,
double thread_rate);
118 PolyDriver *drv_head;
119 PolyDriver *drv_torso;
120 PolyDriver *drv_ctrl_gaze;
121 PolyDriver *drv_arm[2];
122 PolyDriver *drv_car_arm[2];
124 IEncoders *enc_arm[2];
126 IEncoders *enc_torso;
127 IGazeControl *ctrl_gaze;
129 IPositionControl *pos_torso;
130 IVelocityControl *vel_torso;
131 IControlMode *ctrl_mode_torso;
132 IInteractionMode *int_mode_torso;
133 IImpedanceControl *ctrl_impedance_torso;
135 IControlMode *ctrl_mode_arm[2];
136 IPositionControl *pos_arm[2];
137 IInteractionMode *int_mode_arm[2];
138 IImpedanceControl *ctrl_impedance_arm[2];
142 bool gazeUnderControl;
143 bool status_impedance_on;
151 int action_context[2];
153 bool homeFixCartType;
155 Vector reachAboveDisp;
156 Vector graspAboveRelief;
157 Vector pushAboveRelief;
158 double extForceThresh[2];
159 double default_exec_time;
160 double reachingTimeout;
163 Vector takeToolPos[2];
164 Vector takeToolOrient[2];
167 Vector homeOrient[2];
168 Vector reachSideDisp[2];
169 Vector reachAboveOrient[2];
170 Vector reachAboveCata[2];
171 Vector reachSideOrient[2];
173 Vector deployOrient[2];
174 Vector drawNearPos[2];
175 Vector drawNearOrient[2];
178 Vector expectOrient[2];
186 bool neuralNetworkAvailable;
187 RpcClient disparityPort;
189 vector<Vector> pos_torsoes;
190 vector<Vector> handPoses;
191 vector<Vector> headPoses;
194 string kinematics_file;
195 Vector defaultKinematicOffset[2];
196 Vector currentKinematicOffset[2];
198 double go_up_distance;
200 double table_height_tolerance;
221 Vector track_cartesian_target;
222 double avoid_table_height[2];
226 bool storeContext(
const int arm);
227 bool restoreContext(
const int arm);
228 bool deleteContext(
const int arm);
234 bool loadExplorationPoses(
const string &file_name);
235 int checkArm(
int arm);
236 int checkArm(
int arm, Vector &xd,
const bool applyOffset=
true);
237 bool checkOptions(
const Bottle &options,
const string ¶meter);
238 Vector eye2root(
const Vector &
out,
bool forehead);
239 bool stereoToCartesianHomography(
const Vector &stereo, Vector &xd);
240 bool stereoToCartesianDisparity(
const Vector &stereo, Vector &xd);
241 bool stereoToCartesianNetwork(
const Vector &stereo, Vector &xd);
242 Vector randomDeployOffset();
243 bool getGeneralOptions(Bottle &b);
244 bool loadKinematicOffsets();
245 bool saveKinematicOffsets();
246 bool getArmOptions(Bottle &b,
const int &arm);
247 void goWithTorsoUpright(
ActionPrimitives *action,
const Vector &xin,
const Vector &oin);
248 bool avoidTable(
bool avoid);
253 :PeriodicThread(0.02),rf(_rf),stereo_target(initializer->stereo_target),opcPort(initializer->port_opc),
254 track_cartesian_target(3,0.0)
257 ctrl_mode_torso=NULL;
259 drv_head=drv_torso=drv_ctrl_gaze=NULL;
261 drv_car_arm[
LEFT]=drv_car_arm[
RIGHT]=NULL;
262 ctrl_mode_arm[
LEFT]=ctrl_mode_arm[
RIGHT]=NULL;
263 int_mode_arm[
LEFT]=int_mode_arm[
RIGHT]=NULL;
268 virtual bool threadInit();
270 virtual void threadRelease();
271 virtual void onStop();
275 if (wbdPort.getOutputCount()>0)
278 cmd.addString(
"calib");
279 cmd.addString(
"all");
280 wbdPort.write(
cmd,reply);
290 Bottle *bTarget=options.find(
"target").asList();
291 if (!gazeUnderControl)
293 ctrl_gaze->stopControl();
294 ctrl_gaze->restoreContext(gaze_context);
296 if (checkOptions(options,
"no_sacc"))
297 ctrl_gaze->setSaccadesMode(
false);
304 gazeUnderControl=
true;
308 if (Bottle *bCartesian=bTarget->find(
"cartesian").asList())
309 if ((
size_t)bCartesian->size()==track_cartesian_target.length())
310 for (
size_t i=0; i<track_cartesian_target.length(); i++)
311 track_cartesian_target[i]=bCartesian->get(i).asFloat64();
317 if(checkOptions(options,
"no_sacc"))
318 ctrl_gaze->setSaccadesMode(
false);
324 gazeUnderControl=
true;
325 ctrl_gaze->setTrackingMode(
true);
326 if(checkOptions(options,
"no_sacc"))
327 ctrl_gaze->setSaccadesMode(
false);
334 ctrl_gaze->stopControl();
335 gazeUnderControl=
false;
340 if(action[arm]==NULL)
353 if(action[
RIGHT]!=NULL)
356 if(action[
LEFT]!=NULL)
361 if(action[
RIGHT]!=NULL)
363 if(action[
LEFT]!=NULL)
376 *_table_height=table_height-table_height_tolerance;
381 bool stereoToCartesian(
const Vector &stereo, Vector &xd);
382 bool targetToCartesian(Bottle *target, Vector &xd);
385 bool goUp(Bottle &options,
const double h=std::numeric_limits<double>::quiet_NaN());
386 bool goHome(Bottle &options);
387 bool reach(Bottle &options);
388 bool powerGrasp(Bottle &options);
389 bool push(Bottle &options);
390 bool point(Bottle &options);
391 bool point_far(Bottle &options);
392 bool look(Bottle &options);
393 bool hand(
const Bottle &options,
const string &type=
"",
bool *holding=NULL);
394 bool grasp(
const Bottle &options);
395 bool release(
const Bottle &options);
396 bool deploy(Bottle &options);
397 bool drawNear(Bottle &options);
398 bool shiftAndGrasp(Bottle &options);
399 bool expect(Bottle &options);
400 bool give(Bottle &options);
402 bool clearIt(Bottle &options);
405 bool exploreTorso(Bottle &options);
406 bool exploreHand(Bottle &options);
409 bool takeTool(Bottle &options);
411 bool getHandImagePosition(Bottle &hand_image_pos);
412 bool isHolding(
const Bottle &options);
413 bool calibTable(Bottle &options);
414 bool calibFingers(Bottle &options);
416 bool startLearningModeAction(Bottle &options);
417 bool suspendLearningModeAction(Bottle &options);
418 bool imitateAction(Bottle &options);
420 bool startLearningModeKinOffset(Bottle &options);
421 bool suspendLearningModeKinOffset(Bottle &options);
423 bool changeElbowHeight(
const int arm,
const double height,
const double weight);
424 bool changeExecTime(
const int arm,
const double execTime);
426 bool setImpedance(
bool turn_on);
427 bool setTorque(
bool turn_on,
int arm=
ARM_IN_USE);
429 int setStereoToCartesianMode(
const int mode, Bottle &reply);
430 int setStereoToCartesianMode(
const int mode);
440 void getStatus(Bottle &status);
#define HEAD_MODE_TRACK_TEMP
#define GRASP_STATE_ABOVE
#define HEAD_MODE_TRACK_HAND
#define HEAD_MODE_TRACK_CART
#define HEAD_MODE_TRACK_FIX
bool setArmInUse(int arm)
void keepFixation(Bottle &options)
void track(Bottle &options)
bool getTableHeight(double *_table_height)
void lookAtHand(Bottle &options)
MotorThread(ResourceFinder &_rf, Initializer *initializer)
bool setWaveing(bool _waveing)
void setGraspState(bool side)
The base class defining actions.
virtual bool disableArmWaving()
Disable the waving mode.
virtual bool enableArmWaving(const yarp::sig::Vector &restPos)
Enable the waving mode that keeps on moving the arm around a predefined position.
A class for defining a saturated integrator based on Tustin formula: .
Feed-Forward 2 layers Neural Network with a tansig function for the hidden nodes and a purelin for th...
An abstract class that provides basic methods for interfacing with the data acquisition.
iCub::ctrl::Integrator * I