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')
64using namespace yarp::os;
65using namespace yarp::sig;
67using 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;
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;
385 bool goUp(Bottle &options,
const double h=std::numeric_limits<double>::quiet_NaN());
386 bool goHome(Bottle &options);
387 bool reach(Bottle &options);
389 bool push(Bottle &options);
390 bool point(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);
399 bool expect(Bottle &options);
400 bool give(Bottle &options);
#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 push(Bottle &options)
bool targetToCartesian(Bottle *target, Vector &xd)
bool calibTable(Bottle &options)
bool setArmInUse(int arm)
bool powerGrasp(Bottle &options)
void keepFixation(Bottle &options)
bool startLearningModeAction(Bottle &options)
virtual bool threadInit()
bool reach(Bottle &options)
bool grasp(const Bottle &options)
bool drawNear(Bottle &options)
bool isHolding(const Bottle &options)
void track(Bottle &options)
bool stereoToCartesian(const Vector &stereo, Vector &xd)
bool clearIt(Bottle &options)
bool getTableHeight(double *_table_height)
void getStatus(Bottle &status)
bool point(Bottle &options)
bool startLearningModeKinOffset(Bottle &options)
bool hand(const Bottle &options, const string &type="", bool *holding=NULL)
bool give(Bottle &options)
void lookAtHand(Bottle &options)
bool point_far(Bottle &options)
MotorThread(ResourceFinder &_rf, Initializer *initializer)
bool imitateAction(Bottle &options)
bool exploreHand(Bottle &options)
virtual void threadRelease()
bool look(Bottle &options)
bool deploy(Bottle &options)
bool suspendLearningModeKinOffset(Bottle &options)
bool exploreTorso(Bottle &options)
bool changeElbowHeight(const int arm, const double height, const double weight)
bool suspendLearningModeAction(Bottle &options)
bool setTorque(bool turn_on, int arm=ARM_IN_USE)
bool setImpedance(bool turn_on)
bool changeExecTime(const int arm, const double execTime)
bool release(const Bottle &options)
bool takeTool(Bottle &options)
bool calibFingers(Bottle &options)
bool setWaveing(bool _waveing)
void setGraspState(bool side)
bool shiftAndGrasp(Bottle &options)
bool getHandImagePosition(Bottle &hand_image_pos)
bool goHome(Bottle &options)
int setStereoToCartesianMode(const int mode, Bottle &reply)
bool goUp(Bottle &options, const double h=std::numeric_limits< double >::quiet_NaN())
bool expect(Bottle &options)
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
void init(Bottle &initializer, double thread_rate)