14 #include <yarp/os/ConnectionReader.h>
15 #include <yarp/os/ConnectionWriter.h>
16 #include <yarp/math/Math.h>
21 using namespace yarp::os;
22 using namespace yarp::sig;
23 using namespace yarp::math;
31 unsigned long dynContact::ID = 1;
33 dynContact::dynContact(){
37 dynContact::dynContact(
const BodyPart &_bodyPart,
unsigned int _linkNumber,
const Vector &_CoP){
38 init(_bodyPart, _linkNumber, _CoP);
41 dynContact::dynContact(
const BodyPart &_bodyPart,
unsigned int _linkNumber,
const Vector &_CoP,
const Vector &_Mu){
42 init(_bodyPart, _linkNumber, _CoP, _Mu);
45 dynContact::dynContact(
const BodyPart &_bodyPart,
unsigned int _linkNumber,
const Vector &_CoP,
const Vector &_Mu,
const Vector &_Fdir){
46 init(_bodyPart, _linkNumber, _CoP, _Mu, _Fdir);
49 void dynContact::init(
const BodyPart &_bodyPart,
unsigned int _linkNumber,
const Vector &_CoP,
const Vector &_Mu,
const Vector &_Fdir){
51 setBodyPart(_bodyPart);
52 setLinkNumber(_linkNumber);
67 fixForceDirection(_Fdir);
72 Vector dynContact::getForceMoment()
const{
return cat(
F, Mu); }
74 const Vector& dynContact::getForce()
const{
return F;}
76 const Vector& dynContact::getForceDirection()
const{
return Fdir;}
78 double dynContact::getForceModule()
const{
return Fmodule;}
80 const Vector& dynContact::getMoment()
const{
return Mu;}
82 const Vector& dynContact::getCoP()
const{
return CoP;}
84 unsigned int dynContact::getLinkNumber()
const{
return linkNumber;}
88 string dynContact::getBodyPartName()
const{
return BodyPart_s[bodyPart];}
90 unsigned long dynContact::getId()
const{
return contactId;}
95 bool dynContact::isMomentKnown()
const{
return muKnown;}
97 bool dynContact::isForceDirectionKnown()
const{
return fDirKnown;}
101 bool dynContact::setForce(
const Vector &_F){
102 if(!checkVectorDim(_F, 3,
"force"))
111 bool dynContact::setForceModule(
double _Fmodule){
114 fprintf(stderr,
"Error in dynContact: negative force module, %f\n", _Fmodule);
122 bool dynContact::setForceDirection(
const Vector &_Fdir){
123 if(!checkVectorDim(_Fdir, 3,
"force direction"))
125 double FdirNorm =
norm(_Fdir);
127 Fdir = _Fdir / FdirNorm;
132 bool dynContact::setMoment(
const Vector &_Mu){
133 if(!checkVectorDim(_Mu, 3,
"moment"))
139 bool dynContact::setForceMoment(
const yarp::sig::Vector &_F,
const yarp::sig::Vector &_Mu){
140 return setForce(_F) && setMoment(_Mu);
143 bool dynContact::setForceMoment(
const yarp::sig::Vector &_FMu){
144 if(!checkVectorDim(_FMu, 6,
"force moment"))
146 bool res = setForce(_FMu.subVector(0,2));
147 return res && setMoment(_FMu.subVector(3,5));
150 bool dynContact::setCoP(
const Vector &_CoP){
151 if(!checkVectorDim(_CoP, 3,
"Center of pressure"))
157 void dynContact::setLinkNumber(
unsigned int _linkNum){
158 linkNumber = _linkNum;
162 bodyPart = _bodyPart;
167 bool dynContact::fixForceDirection(
const Vector &_Fdir){
168 if(setForceDirection(_Fdir)){
175 bool dynContact::fixMoment(){
176 return fixMoment(
zeros(3));
179 bool dynContact::fixMoment(
const Vector &_Mu){
187 void dynContact::unfixForceDirection(){ fDirKnown=
false;}
189 void dynContact::unfixMoment(){ muKnown=
false;}
201 connection.appendInt32(BOTTLE_TAG_LIST);
202 connection.appendInt32(4);
204 connection.appendInt32(BOTTLE_TAG_LIST + BOTTLE_TAG_INT32);
205 connection.appendInt32(3);
206 connection.appendInt32(contactId);
207 connection.appendInt32(bodyPart);
208 connection.appendInt32(linkNumber);
210 connection.appendInt32(BOTTLE_TAG_LIST + BOTTLE_TAG_FLOAT64);
211 connection.appendInt32(3);
212 for(
int i=0;i<3;i++) connection.appendFloat64(CoP[i]);
214 connection.appendInt32(BOTTLE_TAG_LIST + BOTTLE_TAG_FLOAT64);
215 connection.appendInt32(3);
216 for(
int i=0;i<3;i++) connection.appendFloat64(
F[i]);
218 connection.appendInt32(BOTTLE_TAG_LIST + BOTTLE_TAG_FLOAT64);
219 connection.appendInt32(3);
220 for(
int i=0;i<3;i++) connection.appendFloat64(Mu[i]);
224 connection.convertTextMode();
226 return !connection.isError();
231 connection.convertTextMode();
238 if(connection.expectInt32()!= BOTTLE_TAG_LIST || connection.expectInt32()!=4)
241 if(connection.expectInt32()!=BOTTLE_TAG_LIST+BOTTLE_TAG_INT32 || connection.expectInt32()!=3)
243 contactId = connection.expectInt32();
244 bodyPart = (
BodyPart)connection.expectInt32();
245 linkNumber = connection.expectInt32();
247 if(connection.expectInt32()!=BOTTLE_TAG_LIST+BOTTLE_TAG_FLOAT64 || connection.expectInt32()!=3)
249 for(
int i=0;i<3;i++) CoP[i] = connection.expectFloat64();
251 if(connection.expectInt32()!=BOTTLE_TAG_LIST+BOTTLE_TAG_FLOAT64 || connection.expectInt32()!=3)
253 for(
int i=0;i<3;i++)
F[i] = connection.expectFloat64();
256 if(connection.expectInt32()!=BOTTLE_TAG_LIST+BOTTLE_TAG_FLOAT64 || connection.expectInt32()!=3)
258 for(
int i=0;i<3;i++) Mu[i] = connection.expectFloat64();
260 return !connection.isError();
265 res<<
"Contact id: "<< contactId<<
", Body part: "<<
BodyPart_s[bodyPart]<<
", link: "<< linkNumber<<
", CoP: "<<
266 CoP.toString(precision)<<
", F: "<<
F.toString(precision)<<
", M: "<< Mu.toString(precision);
270 void dynContact::setVerbose(
unsigned int verb){
274 bool dynContact::checkVectorDim(
const Vector &v,
unsigned int dim,
const string &descr){
275 if(v.length() != dim){
277 fprintf(stderr,
"Error in dynContact: unexpected dimension of vector %s, %d\n", descr.c_str(), (
int)v.length());
double norm(const yarp::sig::Matrix &M, int col)
Returns the norm of the vector given in the form: matrix(:,col).
BodyPart getBodyPart(SkinPart s)
Get the body part associated to the specified skin part.
bool read(yarp::os::Searchable &cfgtotal, pc104Data &pc104data)
const std::string BodyPart_s[]
std::string toString(const T &t)
bool write(const std::string filename, const FullRegulation ®)