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
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "arm2segm.h"
00048
00049 namespace lpzrobots{
00050
00051 Arm2Segm::Arm2Segm(const OdeHandle& odeHandle, const OsgHandle& osgHandle, const Arm2SegmConf armConf):
00052 OdeRobot(odeHandle, osgHandle), conf(armConf){
00053
00054
00055 Configurable::insertCVSInfo(name, "$RCSfile: arm2segm.cpp,v $",
00056 "$Revision: 1.6.4.6 $");
00057 parentspace=odeHandle.space;
00058
00059 speed=1.0;
00060 factorSensors=2.0;
00061 sensorno=conf.segmentsno;
00062 motorno=conf.segmentsno;
00063 this->osgHandle.color = Color(1, 0, 0, 1.0f);
00064
00065 created=false;
00066 };
00067
00068
00069
00070
00071
00072 void Arm2Segm::setMotors(const motor* motors, int motornumber){
00073 assert(created);
00074
00075
00076
00077 int len = (motornumber < motorno)? motornumber : motorno;
00078
00079
00080
00081 for (int i=0; i<len; i++){
00082 amotors[i]->set(1, motors[i]*speed);
00083 }
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 };
00095
00096
00097
00098
00099
00100
00101
00102 int Arm2Segm::getSensors(sensor* sensors, int sensornumber){
00103 assert(created);
00104
00105
00106
00107
00108 int len = (sensornumber < sensorno)? sensornumber : sensorno;
00109
00110
00111 for (int i=0; i<len; i++){
00112 sensors[i]=amotors[i]->get(1);
00113
00114
00115 sensors[i]/=speed;
00116 }
00117
00118 return len;
00119 };
00120
00121
00122
00123
00124
00125 void Arm2Segm::place(const osg::Matrix& pose){
00126
00127
00128
00129 osg::Matrix p2;
00130 p2 = pose
00131
00132
00133 * osg::Matrix::translate(osg::Vec3(0, 0, conf.base_length* 0.5));
00134 create(p2);
00135
00136
00137
00138
00139
00140
00141 };
00142
00143
00144
00145
00146
00147
00148 int Arm2Segm::getSegmentsPosition(vector<Position> &poslist){
00149 for (int i=0; i<conf.segmentsno; i++){
00150 Pos p = objects[i]->getPosition();
00151 poslist.push_back(p.toPosition());
00152 }
00153 return conf.segmentsno;
00154 };
00155
00156
00157 void Arm2Segm::update(){
00158 assert(created);
00159 for (vector<Primitive*>::iterator i = objects.begin(); i!=objects.end(); i++) {
00160 if (*i) (*i)->update();
00161 }
00162 for (vector<Joint*>::iterator i = joints.begin(); i!=joints.end(); i++) {
00163 if (*i) (*i)->update();
00164 }
00165 };
00166
00167
00168 void Arm2Segm::doInternalStuff(const GlobalData& globalData){
00169 };
00170
00171 void Arm2Segm::mycallback(void *data, dGeomID o1, dGeomID o2){
00172
00173 };
00174
00175 bool Arm2Segm::collisionCallback(void *data, dGeomID o1, dGeomID o2){
00176
00177 if( o1 == (dGeomID)odeHandle.space || o2 == (dGeomID)odeHandle.space){
00178
00179
00180 int i,n;
00181 const int N = 10;
00182 dContact contact[N];
00183
00184 n = dCollide (o1,o2,N,&contact[0].geom,sizeof(dContact));
00185 for (i=0; i<n; i++){
00186 contact[i].surface.mode = dContactSlip1 | dContactSlip2 |
00187 dContactSoftERP | dContactSoftCFM | dContactApprox1;
00188 contact[i].surface.slip1 = 0.005;
00189 contact[i].surface.slip2 = 0.005;
00190 contact[i].surface.mu = 0.5;
00191 contact[i].surface.soft_erp = 0.9;
00192 contact[i].surface.soft_cfm = 0.001;
00193 dJointID c = dJointCreateContact( odeHandle.world, odeHandle.jointGroup, &contact[i]);
00194 dJointAttach ( c , dGeomGetBody(contact[i].geom.g1) , dGeomGetBody(contact[i].geom.g2)) ;
00195 }
00196 return true;
00197 }
00198 return false;
00199 };
00200
00201
00202 Primitive* Arm2Segm::getMainPrimitive() const{
00203
00204
00205 return objects[conf.segmentsno-1];
00206 }
00207
00208
00209
00210
00211
00212 void Arm2Segm::create(const osg::Matrix& pose){
00213 if (created) {
00214 destroy();
00215 }
00216
00217
00218 odeHandle.space = dSimpleSpaceCreate (parentspace);
00219
00220
00221 Primitive* o = new Box(conf.base_length, conf.base_width, conf.base_length);
00222 o -> init(odeHandle, conf.base_mass, osgHandle);
00223 o->setPose( pose);
00224
00225 objects.push_back(o);
00226
00227
00228 for (int i=0; i<(conf.segmentsno-1); i++){
00229 o = new Box(conf.arm_length, conf.arm_width, conf.arm_width);
00230 o -> init(odeHandle, conf.arm_mass, osgHandle);
00231
00232 o -> setPose(osg::Matrix::translate(osg::Vec3(
00233 (i+0.5)*conf.arm_length - (i+1)*conf.joint_offset,
00234
00235 (i+1)*conf.arm_offset
00236 + (i+0.5)*conf.arm_width + 0.5*conf.base_width,
00237
00238 0) ) * pose);
00239 objects.push_back(o);
00240 }
00241
00242
00243 Pos p1(objects[0]->getPosition());
00244 HingeJoint* j = new HingeJoint(0, objects[0], p1, osg::Vec3(0,0,1) );
00245 j -> init(odeHandle, osgHandle,true);
00246 joints.push_back(j);
00247 AngularMotor1Axis* a=new AngularMotor1Axis(odeHandle, (OneAxisJoint *)joints[0], conf.max_force);
00248 amotors.push_back(a);
00249
00250
00251 Pos p2(objects[1]->getPosition());
00252 p1[1]=(p1[1]+p2[1])/2;
00253 j = new HingeJoint(objects[0], objects[1], p1, osg::Vec3(0,1,0) );
00254 j -> init(odeHandle, osgHandle,true);
00255 joints.push_back(j);
00256 a=new AngularMotor1Axis(odeHandle, (OneAxisJoint *)joints[1], conf.max_force);
00257 amotors.push_back(a);
00258
00259
00260 for (int i=2; i<conf.segmentsno; i++) {
00261 Pos po1(objects[i-1]->getPosition());
00262 Pos po2(objects[i]->getPosition());
00263 Pos po3( (po1+po2)/2);
00264 j = new HingeJoint(objects[i-1], objects[i], po3, osg::Vec3(0,1,0) );
00265 j -> init(odeHandle, osgHandle,true);
00266 joints.push_back(j);
00267 a=new AngularMotor1Axis(odeHandle, (OneAxisJoint *)joints[i], conf.max_force);
00268 amotors.push_back(a);
00269 }
00270
00271
00272
00273 osg::Matrix ps;
00274 ps.makeIdentity();
00275 Primitive* o1 = new Sphere(conf.arm_width*0.8);
00276 Primitive* o2 = new Transform(objects[objects.size()-1], o1, osg::Matrix::translate(0, conf.arm_length*0.5, 0) * ps);
00277 o2->init(odeHandle, 0, osgHandle, false);
00278
00279
00280 created=true;
00281 };
00282
00283
00284
00285
00286 void Arm2Segm::destroy(){
00287 if (created){
00288 for (vector<Primitive*>::iterator i=objects.begin(); i!=objects.end(); i++){
00289 if (*i) delete *i;
00290 }
00291 objects.clear();
00292 for (vector<Joint*>::iterator i=joints.begin(); i!=joints.end(); i++){
00293 if (*i) delete *i;
00294 }
00295 joints.clear();
00296 for (vector<AngularMotor1Axis*>::iterator i=amotors.begin(); i!=amotors.end(); i++){
00297 if (*i) delete *i;
00298 }
00299 amotors.clear();
00300 dSpaceDestroy(odeHandle.space);
00301 }
00302 created=false;
00303 };
00304
00305
00306
00307 Configurable::paramlist Arm2Segm::getParamList() const{
00308 paramlist list;
00309 list.push_back(pair<paramkey, paramval> (string("speed"), speed));
00310 list.push_back(pair<paramkey, paramval> (string("factorSensors"), factorSensors));
00311 return list;
00312 }
00313
00314 Configurable::paramval Arm2Segm::getParam(const paramkey& key) const{
00315 if(key == "speed") return speed;
00316 else if(key == "factorSensors") return factorSensors;
00317 else return Configurable::getParam(key) ;
00318 };
00319
00320 bool Arm2Segm::setParam(const paramkey& key, paramval val){
00321 if(key == "speed") speed=val;
00322 else if(key == "factorSensors") factorSensors = val;
00323 else return Configurable::setParam(key, val);
00324 return true;
00325 };
00326
00327 }