ArmSim.H
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef ArmSim_H_DEFINED
00039 #define ArmSim_H_DEFINED
00040 #include "ArmControl/RobotArm.H"
00041 #include "Component/ModelComponent.H"
00042 #include "Component/ModelParam.H"
00043 #include "Devices/Serial.H"
00044 #include "Controllers/PID.H"
00045 #include "GUI/ViewPort.H"
00046 #include "Util/Types.H"
00047 #include <deque>
00048 #include <pthread.h>
00049 #include <stdarg.h>
00050 #include <ode/ode.h>
00051
00052 #define MAX_PWM 90
00053
00054 #define NUM 5
00055
00056 #define INIT_ELBOW_ANGLE 50.0
00057 #define INIT_SHOLDER_ANGLE 75.0
00058
00059
00060
00061 class ArmSim : public RobotArm
00062 {
00063 public:
00064 #define ARM_GRAVITYMODE 0
00065 #define ARM_BITFIELD 1
00066 #define GRIPPER_BITFIELD 2
00067 #define GROUND_BITFIELD 4
00068 #define OBJ_BITFIELD 8
00069
00070
00071
00072
00073 struct ArmParam {
00074 double armLoc[3];
00075
00076 double base[2];
00077 double baseMass;
00078
00079 double body[3];
00080 double bodyMass;
00081
00082 double upperarm[3];
00083 double upperarmMass;
00084
00085 double forearm[3];
00086 double forearmMass;
00087
00088 double wrist1[2];
00089 double wrist1Mass;
00090
00091 double wrist2[3];
00092 double wrist2Mass;
00093
00094 double gripper[3];
00095 double gripperMass;
00096
00097 };
00098
00099 enum OBJECT_TYPE {BOX, CYLINDER, SPHERE};
00100
00101
00102 ArmSim(OptionManager& mgr,
00103 const std::string& descrName = "ArmSim",
00104 const std::string& tagName = "ArmSim",
00105 const double l0 = 0.8,const double l1 = 0.9,const double l2 = 1.00,
00106 const double l3 = 1.00,const double l4 = 1.00
00107 );
00108 ~ArmSim();
00109
00110
00111 void start2();
00112
00113
00114 void setMotor(MOTOR m, int val);
00115 void setMotorPos(MOTOR m, int pos){}
00116 int getEncoder(MOTOR m);
00117 void resetEncoders();
00118 void resetMotorPos(){}
00119 void stopAllMotors();
00120 void homeMotor();
00121
00122
00123 void setSafety(bool val);
00124
00125 void simLoop();
00126 void startSim(void);
00127 float getPWM(MOTOR m);
00128
00129 dWorldID getWorld() { return world; }
00130 dJointGroupID getContactgroup() { return contactgroup; }
00131
00132 float toDegree(float r){return (180*r)/M_PI;}
00133 float toRadius(float d){return (d*M_PI)/180;}
00134 Image<PixRGB<byte> > getFrame(int camera);
00135
00136
00137 void getArmLoc(double loc[3]);
00138
00139 void getObjLoc(const int x, const int y, double loc[3]);
00140
00141 double getGradient(MOTOR m,double *targetPos);
00142
00143 int getGradientEncoder(MOTOR m,double *targetPos);
00144
00145 void addObject(OBJECT_TYPE objType,double initPos[3]);
00146 void addObject(OBJECT_TYPE objType,double initPos[3],double objSize[3]);
00147
00148 void addDrawObject(OBJECT_TYPE objType,double pos[3]);
00149
00150 void drawObjects();
00151
00152 void moveObject(double pos[3],int objID = 0);
00153 dReal * getEndPos();
00154
00155 int ang2encoder(double ang,MOTOR m);
00156
00157 double encoder2ang(int eng,MOTOR m);
00158
00159 void getJointAxis(MOTOR m,dReal axis[3]);
00160
00161 void getJointCenter(MOTOR m,dReal joint[3]);
00162
00163 dJointID getJointID(MOTOR m);
00164
00165 void drawLine(double pos[3]);
00166 private:
00167
00168 void makeArm();
00169 void drawArm();
00170 void drawTable();
00171
00172 void pwmControl();
00173
00174 dWorldID world;
00175 dSpaceID space;
00176 dGeomID ground;
00177 dJointGroupID contactgroup;
00178
00179 typedef struct{
00180 dBodyID body;
00181 dGeomID geom;
00182 dJointID joint;
00183 OBJECT_TYPE type;
00184 float color[3];
00185 } Object;
00186
00187 ArmParam itsArmParam;
00188 Object itsBase;
00189 Object itsBody;
00190 Object itsUpperArm;
00191 Object itsForearm;
00192 Object itsWrist1;
00193 Object itsWrist2;
00194 Object itsGripper1;
00195 Object itsGripper2;
00196
00197 double itsTableSize[3];
00198 dReal endPoint[3];
00199 std::vector<Object> itsObjects;
00200
00201 typedef struct {
00202 OBJECT_TYPE type;
00203 double loc[3];
00204 double rot[12];
00205 } DrawObject;
00206 std::vector<DrawObject> itsDrawObjects;
00207
00208 ViewPort *vp;
00209 void crossproduct(dReal *v1,dReal *v2,dReal *result)
00210 {
00211 result[0] = v1[1]*v2[2]-v1[2]*v2[1];
00212 result[1] = v1[2]*v2[0]-v1[0]*v2[2];
00213 result[2] = v1[0]*v2[1]-v1[1]*v2[0];
00214 }
00215 dReal dotproduct(dReal *v1,dReal *v2)
00216 {
00217 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
00218 }
00219 double getDistance(const double* pos, const double* desire)
00220 {
00221 double sum = 0;
00222 for(int i = 0; i < 3; i++)
00223 sum+= (pos[i]-desire[i])*(pos[i]-desire[i]);
00224 return sqrt(sum);
00225 }
00226 };
00227
00228 #endif
00229
00230
00231
00232
00233
00234