Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

Iunctus.cpp

Go to the documentation of this file.
00001 // Iunctus.cpp: implementation of the Iunctus class.
00002 //
00004 
00005 #include "Iunctus.h"
00006 #include "Simulator.h"
00007 #include "Circle.h"
00008 #include "CappedRectangle.h"
00009 
00011 // Construction/Destruction
00013 
00014 
00015 Iunctus::Iunctus(real x, real y, std::string label):
00016    ArticulatedAgentQuasistatic(label),myBody(NULL),controller(NULL){
00017    eye=NULL;
00018    build(x, y);
00019    controller=new RandomController(24, 16, 56789);
00020 }
00021 
00022 Iunctus::~Iunctus(){
00023    delete eye;
00024    delete controller;
00025 }
00026 
00027 void Iunctus::build(real x, real y){
00028    ArticulatedLink* link;
00029    ArticulatedLink* hand;
00030    ArticulatedLimb* limb;
00031    const real kFactorFirstLink=20.0f;
00032    const real kFactorSecondLink=10.0f;
00033    const real kFactorThirdLink=5.0f;
00034    int sign;
00035    r.setXY(x,y);
00036    //addObject(eye,0.0f,0.0f,0.0f);
00037    //create body
00038    myBody=new Circle(0.2f, 0, 0, "body", 10);
00039    addObject(myBody,0.0f,0.0f,0.0f);
00040    computeMemberPositions();
00041    computeIStar0();
00042    //add eye
00043    eye=new VisualSensor(myBody, 0,0, M_PI/2, 0.2f, M_PI/3, 10);
00044    for(int i=0;i<2;i++){
00045       sign= i==0 ? 1 : -1;
00046       limb=addLimb(0.25f, i==0 ? 10.0f*degrees : 170.0f*degrees);
00047       //create link 0 - first arm segment
00048       link=limb->addOneObjectLink(0.40f,sign*10.0f*degrees,
00049                                     new CappedRectangle(0.20f, 0.05f, 0, 0, 0, "arm 1", 10, 5), 
00050                                     0.20f,0.0f, 0.0f, NULL);
00051       link->k=kFactorFirstLink*DEFAULT_K;
00052       //create link 1 - second arm segment
00053       link=limb->addOneObjectLink(0.40f,sign*40.0f*degrees,
00054                                     new CappedRectangle(0.15f,0.05f, 0, 0, 0, "arm 2", 10, 5), 
00055                                     0.25f,0.0f, 0.0f, link);
00056       link->k=kFactorSecondLink*DEFAULT_K;
00057       //link->setNChildLinks(1);
00058       //create link 2 - hand
00059       link=limb->addOneObjectLink(0.10f,sign*10.0f*degrees,
00060                                     new Circle(0.05f, 0, 0, "hand", 5), 
00061                                     0.10f,0.0f, 0.0f, link);
00062         link->theta0Min=-30.0f*degrees;
00063         link->theta0Max=30.0f*degrees;
00064       
00065       link->k=kFactorThirdLink*DEFAULT_K;
00066       hand=link;
00067       //link->setNChildLinks(2);
00068       //create link 3 - finger right 1
00069       link=limb->addOneObjectLink(0.17f,-sign*60.0f*degrees,
00070                                     new CappedRectangle(0.05f,0.02f, 0, 0, 0, "finger right 1", 10, 5), 
00071                                     0.12f,0.0f, 0.0f, hand);
00072       if(i==0){
00073          link->theta0Min=-90.0*degrees;
00074          link->theta0Max=-30.0*degrees;
00075       } else {
00076          link->theta0Min=30.0f*degrees;
00077          link->theta0Max=90.0f*degrees;
00078       }
00079       //link->setNChildLinks(1);
00080       //create link 4 - finger right 2
00081       link=limb->addOneObjectLink(0.14f,sign*60.0f*degrees,
00082                                     new CappedRectangle(0.05f,0.02f, 0, 0, 0, "finger right 2", 10, 5), 
00083                                     0.09f,0.0f, 0.0f, link);
00084       if(i==0){
00085          link->theta0Min=30.0*degrees;
00086          link->theta0Max=90.0*degrees;
00087       } else {
00088          link->theta0Min=-90.0f*degrees;
00089          link->theta0Max=-30.0f*degrees;
00090       };
00091       //end finger right
00092       //create link 5 - finger left 1
00093       link=limb->addOneObjectLink(0.17f,sign*60.0f*degrees,
00094                                     new CappedRectangle(0.05f, 0.02f, 0, 0, 0, "finger left 1", 10, 5), 
00095                                     0.12f,0.0f, 0.0f, hand);
00096       link->theta0=sign*60.0*degrees;
00097       if(i==0){
00098          link->theta0Min=30.0*degrees;
00099          link->theta0Max=90.0*degrees;
00100       } else {
00101          link->theta0Min=-90.0f*degrees;
00102          link->theta0Max=-30.0f*degrees;
00103       };
00104       //create link 6 - finger left 2
00105       link=limb->addOneObjectLink(0.14f,-sign*60.0f*degrees,
00106                                     new CappedRectangle(0.05f,0.02f, 0, 0, 0, "finger left 2", 10, 5), 
00107                                     0.09f,0.0f, 0.0f, link);
00108       if(i==0){
00109          link->theta0Min=-90.0*degrees;
00110          link->theta0Max=-30.0*degrees;
00111       } else {
00112          link->theta0Min=30.0f*degrees;
00113          link->theta0Max=90.0f*degrees;
00114       }
00115    }
00116    //we call forwardKinematics() for updating the absolute positions of the objects
00117    forwardKinematics();
00118    setFillColor(Color("wheat"));
00119    setOutlineColor(Color("slateGray"));
00120 }
00121 
00122 
00123 void Iunctus::proprioception(){
00124    unsigned int k;
00125    ArticulatedLink* link;
00126    ArticulatedLimb* limb;
00127    ArticulatedLimbPVector::iterator limbsI, limbsEnd;
00128    ArticulatedLinkPVector::iterator linksI, linksEnd; 
00129 
00130    k=0;
00131    for(limbsI=limbs.begin(), limbsEnd=limbs.end(); limbsI!=limbsEnd; limbsI++){
00132       limb=*limbsI;
00133       for (linksI=limb->links.begin(), linksEnd=limb->links.end(); linksI!=linksEnd; linksI++){
00134          link=*linksI;
00135          assert(link);
00136          controller->setInput(k, (link->theta-link->theta0Min)/(link->theta0Max-link->theta0Min) );
00137          k++;
00138       }
00139    }
00140 }
00141 
00142 
00143 void Iunctus::controll(){
00144    int i, k;
00145    ArticulatedLink* link;
00146    ArticulatedLimb* limb;
00147    ArticulatedLimbPVector::iterator limbsI, limbsEnd;
00148    ArticulatedLinkPVector::iterator linksI, linksEnd;
00149    
00150    const real rocketForceFactor=10.0; //was 2.0 before
00151 
00152    //***get the output of the controller
00153 
00154    //update motor commands issued by the controller, for link movement
00155    k=0;
00156    i=0;
00157    for(limbsI=limbs.begin(), limbsEnd=limbs.end(); limbsI!=limbsEnd; limbsI++){
00158       i++;
00159       limb=*limbsI;
00160       for (linksI=limb->links.begin(), linksEnd=limb->links.end(); linksI!=linksEnd; linksI++){
00161          link=*linksI;
00162          assert(link);
00163          /*
00164          link->theta0+=((real)rand()/RAND_MAX-0.5)/10.0;
00165          if(link->theta0<link->theta0Min) link->theta0=link->theta0Min;
00166          if(link->theta0>link->theta0Max) link->theta0=link->theta0Max;
00167          */
00168          if(i==1)
00169             link->theta0=link->theta0Min+(link->theta0Max-link->theta0Min)*controller->getOutput(k);
00170          else
00171             link->theta0=link->theta0Max-(link->theta0Max-link->theta0Min)*controller->getOutput(k);
00172          k++;
00173       }
00174    }
00175    //update motor commands for body movement
00176    //the torque
00177    betaExternal.x=(controller->getOutput(14)-0.5)*rocketForceFactor*myBody->R;
00178    betaExternal.y=0; //for resetting mouse force
00179    //the forward "rocket" force
00180    betaExternal.z=(controller->getOutput(15)-0.5)*rocketForceFactor;
00181 
00182    //***set the input of the controller
00183    //...
00184    
00185    //***advance controller time
00186    controller->advanceTime();
00187 }
00188 
00189 
00190 
00191 void Iunctus::draw(GUI *gui){
00192    ArticulatedAgentQuasistatic::draw(gui);
00193    /*
00194    //draw body axis
00195    p->dc->DrawLine(p->panX+p->zoomX*myBody->r.x,p->panY+p->zoomY*myBody->r.y, 
00196       p->panX+p->zoomX*(myBody->r.x+myBody->R*cos(myBody->alpha+M_PI/2)),
00197       p->panY+p->zoomY*(myBody->r.y+myBody->R*sin(myBody->alpha+M_PI/2)));
00198       */
00199    /*
00200    //draw body force
00201    Vector2 f;
00202    f.y=betaExternal.z;
00203    f.rotate(myBody->alpha);
00204    p->drawForce(myBody->r,f);
00205    */
00206 
00207    //draw body torque
00208    //p->drawTorque(myBody->r,myBody->alpha+M_PI/2,betaExternal.x);
00209    
00210    //draw body eye
00211    real eyeAngle=M_PI/6;
00212    real x, y, xOld, yOld;
00213    xOld = myBody->r.x;
00214    yOld = myBody->r.y;
00215    for(int i = 0; i <= 10; ++i) {
00216       x = myBody->r.x+myBody->R*0.75*cos(myBody->alpha+M_PI/2+eyeAngle*(2*i/10.0-1));
00217       y = myBody->r.y+myBody->R*0.75*sin(myBody->alpha+M_PI/2+eyeAngle*(2*i/10.0-1));
00218       gui->drawLine(xOld, yOld, x, y);
00219       xOld=x; yOld=y;
00220    }
00221    x = myBody->r.x;
00222    y = myBody->r.y;
00223    gui->drawLine(xOld, yOld, x, y);
00224    //if(eye) eye->draw(gui);
00225 }

Thyrix homepageUsers' guide

(C) Arxia 2004-2005