00001
00002
00003
00004
00005
00006
00007
00008 #ifndef BHANDFORCE_H
00009 #define BHANDFORCE_H
00010
00011 #include <string>
00012 #include <zbsplinefunction.h>
00013
00014 #include "bhand.h"
00015 #include "bhandext.h"
00016 #include "bhkinematic.h"
00017 #include "bhfinger.h"
00018 #include "bhspread.h"
00019
00020 #include "bhspeedgen.h"
00021
00022
00023 #define WITH_STOP
00024
00027 #define DEFAULT_LOOP_CTRL_VEL_COEFF 8
00028
00031 #define DEFAULT_MAX_POS_ERROR_FINGER 25
00032
00035 #define DEFAULT_MAX_POS_ERROR_SPREAD 50
00036
00037 #define MAX_PROFILE_ERROR 350
00038
00041 #define MAX_FORCE_ERROR 10
00042
00045 #define DELTA_POS_COEFF 6
00046
00062 class MoveStruct
00063 {
00064 public:
00072 MoveStruct(MotorFeedback *f1, MotorFeedback *f2, MotorFeedback *f3, MotorFeedback *spread);
00073
00078 ~MoveStruct();
00079
00082 BHFinger *Fingers[3];
00083
00086 BHSpread *Spread;
00087 };
00088
00089
00090
00091
00092 inline MoveStruct::MoveStruct(MotorFeedback *f1, MotorFeedback *f2, MotorFeedback *f3, MotorFeedback *spread)
00093 {
00094 Fingers[0] = new BHFinger(f1);
00095 Fingers[1] = new BHFinger(f2);
00096 Fingers[2] = new BHFinger(f3);
00097
00098 Spread = new BHSpread(spread);
00099 }
00100
00101
00102
00103
00104
00105 inline MoveStruct::~MoveStruct()
00106 {
00107 delete []Fingers;
00108 delete Spread;
00109 }
00110
00111
00120 enum RTFlags{
00121 UNKOWN,
00122 FINGER_MODE,
00123 FORCE_MODE,
00124 MOVE_MODE,
00125 GAIN_MODE
00126 };
00127
00156 class BHandForce : public BHandExt {
00157 public:
00158
00163 BHandForce(std::string devName);
00164
00165
00169 virtual ~BHandForce();
00170
00171
00178 void init() throw (BHandException);
00179
00192 void setRTDefaults();
00193
00194 void setRTGainDefaults();
00195
00199 void setRTForceDefaults();
00200
00201
00207
00216 void move(const Motors motors, const bool plotIt = false) throw (BHandException);
00217
00228 void move(const Motors motors, const int position) throw (BHandException);
00229
00241 void move(const Motors motors, const int position[4]) throw (BHandException);
00242
00243 #if 0
00244
00249 void moveG(const Motors motors, const int position[4])throw (BHandException){};
00250 #endif
00251 void moveG(const Motors motors) throw (BHandException);
00252 void moveG(const Motors motors, const int position) throw (BHandException);
00253
00254
00260 void openFingers(const Motors motors) throw (BHandException);
00261
00267 void closeFingers (const Motors motors) throw (BHandException);
00268
00273 void openSpread() throw (BHandException);
00274
00279 void closeSpread() throw (BHandException);
00280
00281
00298 void adjustForce(const Motors motors, const int force) throw (BHandException);
00299
00300
00315 void moveForce(const Motors motors) throw (BHandException);
00316
00329 void moveForce(const Motors motors, int force[3]) throw (BHandException);
00330
00331
00345 void force2File(std::string fileName, bool showPlot = false);
00346
00347 void forceCalib(std::string fileName, bool showPlot = false);
00348
00350
00355
00362 void setSpeed(const Motors motors, const int speed);
00363
00371 void setMaxForce(const short force, const Motors motors = FINGERS);
00372
00380 void setMinForce(const short force, const Motors motors = FINGERS);
00381
00386 void setMaxPosErrorF(const int error);
00387
00392 void setMaxPosErrorS(const int error);
00393
00402 void setPos(const Motors motors, const int position);
00403
00413 void setPropGain(const Motors motors, const unsigned short gain);
00414
00428 void setVelCoeff(const Motors motors, const unsigned short coeff);
00430
00439 void initForceSensor(const Motors motors = FINGERS);
00440
00449 void getForceReal(int *forces);
00450
00454 void getForce();
00455
00465 void getForce(int *forces, const bool update = true);
00466
00467 void writeForce(std::ostream *stream);
00468
00481 int forceInRange(const BHandKin::FINGER f, const bool update = true);
00482
00483
00487 RTFlags getRTFlags();
00488
00490
00491
00495
00501 BHFinger& getFinger(const BHandKin::FINGER f) const;
00502
00507 BHSpread& getSpread() const;
00508
00517 BHMotorExt& getMotor(const Motors motor) const;
00519
00520 protected:
00521
00528 int clipSpeed(int speed);
00529
00533 int clipSpeed(double speed);
00534
00535
00542 int clipSSpeed(double speed);
00543
00544
00551 int clipGain(int gain);
00552
00556 void resetDeltaPos();
00557
00558
00561 MoveStruct *MoveParams;
00562
00565 int MaxPosErrorFinger;
00566
00569 int MaxPosErrorSpread;
00570
00574 unsigned short LoopCtrlCoeff;
00575
00578 ControlBlock Cb;
00579
00582 FeedbackBlock Fb;
00583
00586 RTFlags RealTimeFlags;
00587
00588
00589 ZBSplineFunction *ForceSpline[NUM_FINGERS];
00590 };
00591
00592
00593
00594
00595
00596 inline int BHandForce::forceInRange(const BHandKin::FINGER f, const bool update)
00597 {
00598 if(update)
00599 getForce();
00600
00601 return MoveParams->Fingers[f-1]->forceInRange();
00602 }
00603
00604
00605
00606
00607
00608 inline void BHandForce::getForce(int *forces, const bool update)
00609 {
00610 if(update){
00611 try{
00612
00613 getForce();
00614
00615 }
00616 catch(...){
00617 std::cout << " ** getForce(int *forces, const bool update) " << std::endl;
00618 }
00619 }
00620
00621
00622
00623
00624
00625
00626 forces[0] = MoveParams->Fingers[0]->getForce();
00627 forces[1] = MoveParams->Fingers[1]->getForce();
00628 forces[2] = MoveParams->Fingers[2]->getForce();
00629
00630 }
00631
00632
00633
00634
00635
00636 inline int BHandForce::clipSpeed(int speed)
00637 {
00638 if(abs(speed) < MIN_FINGER_SPEED)
00639 return (speed < 0) ? -MIN_FINGER_SPEED : MIN_FINGER_SPEED;
00640
00641 if(abs(speed) > MAX_FINGER_SPEED)
00642 return (speed < 0) ? -MAX_FINGER_SPEED : MAX_FINGER_SPEED;
00643
00644 return speed;
00645 }
00646
00647
00648
00649
00650
00651 inline int BHandForce::clipSpeed(double speed)
00652 {
00653 if (fabs(speed) < MIN_FINGER_SPEED)
00654 return (speed < 0) ? -MIN_FINGER_SPEED : MIN_FINGER_SPEED;
00655
00656 if (fabs(speed) > MAX_FINGER_SPEED)
00657 return (speed < 0) ? -MAX_FINGER_SPEED : MAX_FINGER_SPEED;
00658
00659 return (int) speed;
00660 }
00661
00662
00663
00664
00665 inline int BHandForce::clipSSpeed(double speed)
00666 {
00667 if (fabs(speed) < MIN_SPREAD_SPEED)
00668 return (speed < 0) ? -MIN_SPREAD_SPEED : MIN_SPREAD_SPEED;
00669
00670 if (fabs(speed) > MAX_SPREAD_SPEED)
00671 return (speed < 0) ? -MAX_SPREAD_SPEED : MAX_SPREAD_SPEED;
00672
00673 return (int) speed;
00674 }
00675
00676
00677
00678
00679
00680 inline int BHandForce::clipGain(int gain)
00681 {
00682 int result = (gain > 255) ? 255 : gain;
00683 result = (gain < 1) ? 1 : result;
00684
00685 return result;
00686 }
00687
00688
00689
00690
00691
00692 inline void BHandForce::setMaxPosErrorF(const int error)
00693 {
00694 MaxPosErrorFinger = error;
00695 }
00696
00697
00698
00699
00700
00701 inline void BHandForce::setMaxPosErrorS(const int error)
00702 {
00703 MaxPosErrorSpread = error;
00704 }
00705
00706
00707
00708
00709
00710 inline void BHandForce::openFingers (const Motors motors) throw (BHandException)
00711 {
00712 Motors dummyM = Motors(motors & FINGERS);
00713 move(dummyM, MIN_FINGER_POS);
00714 }
00715
00716
00717
00718
00719
00720 inline void BHandForce::closeFingers (const Motors motors) throw (BHandException)
00721 {
00722 Motors dummyM = Motors(motors & FINGERS);
00723 move(dummyM, MAX_FINGER_POS);
00724 }
00725
00726
00727
00728
00729
00730 inline void BHandForce::openSpread() throw (BHandException)
00731 {
00732 BHand::open(SPREAD);
00733 }
00734
00735
00736
00737
00738 inline void BHandForce::closeSpread() throw (BHandException)
00739 {
00740 BHand::close(SPREAD);
00741 }
00742
00743
00744
00745
00746 inline BHFinger& BHandForce::getFinger(const BHandKin::FINGER f) const
00747 {
00748 return *(MoveParams->Fingers[f-1]);
00749 }
00750
00751
00752
00753
00754 inline BHSpread& BHandForce::getSpread() const
00755 {
00756 return *(MoveParams->Spread);
00757 }
00758
00759
00760
00761
00762 inline BHMotorExt& BHandForce::getMotor(const Motors motor) const
00763 {
00764 if(motor & F1)
00765 return *(MoveParams->Fingers[0]);
00766
00767 if(motor & F2)
00768 return *(MoveParams->Fingers[1]);
00769
00770 if(motor & F3)
00771 return *(MoveParams->Fingers[2]);
00772
00773 if(motor & SPREAD)
00774 return *(MoveParams->Spread);
00775 }
00776
00777
00778
00779
00780
00781 inline void BHandForce::setVelCoeff(const Motors motors, const unsigned short coeff)
00782 {
00783 LoopCtrlCoeff = coeff;
00784 if(RtModeEnabled)
00785 stop();
00786
00787 setParam(motors, LOOP_CTRL_VEL_COEFF, LoopCtrlCoeff);
00788 }
00789
00790
00791
00792
00793
00794 inline RTFlags BHandForce::getRTFlags()
00795 {
00796 return RealTimeFlags;
00797 }
00798
00799
00800
00801
00802
00803
00804 inline void BHandForce::resetDeltaPos()
00805 {
00806 for(int i = NUM_MOTORS-1; i >= 0; i--){
00807 Fb.mf[i].loopFeedbackDeltaPos = 0;
00808 }
00809 }
00810
00811 #if 1
00812
00813
00814
00815 inline void BHandForce::writeForce(std::ostream *stream)
00816 {
00817 int forces[3] = {0,0,0};
00818 getForce(forces,true);
00819 *stream << forces[0] << " " << forces[1] << " " << forces[2] << " ";
00820 }
00821 #endif
00822
00823 #endif