奇怪的方法行为

时间:2012-11-22 22:09:31

标签: c++ arduino fuzzy-logic

我正在尝试为我的Arduino编写一个使用模糊逻辑控制机器人汽车的程序。但我遇到了C ++的问题(因为我是新手)。我的类FRule中的方法看起来像这样:

double FRule::dof(double x1,double x2){
    pi1 = ant1->getFSet(U1)->lom(x1); //ant1 is a TermSet       
    pi2 = ant2->getFSet(U2)->lom(x2); //ant2 is a TermSet           
    Serial.println(pi1);
    Serial.println(pi2);
    return min(pi1,pi2);;
}

如果我直接调用lom对象上的FSet方法,则会给出正确的结果。但是,如果我尝试通过FRule方法dof调用它们,它会返回错误的结果,我不确定,为什么会发生这种情况。

有什么想法吗?

整个计划的来源:

**我根据评论**进行了一些更改

FSet类:

FSet::FSet(double center, double delta, int inf){

this->inf = inf;
this->delta = delta;
this->a = center - delta;
this->b = center + delta;
this->center = center;
}

double FSet::lom(double crispValue) {
if(inf == 0){
    if((crispValue < this->a) || (crispValue > this->b)){
        return 0.0;
    } else {

        return 1 - abs(crispValue-center)*(1.0/delta);
    }
} else {
    if( ((crispValue < this->a) && (inf > 0)) ||
            ((crispValue > this->b) && (inf < 0))){
            return 0.0;
    }

    if((inf > 0) && (crispValue < center)){
        return 1 - abs(crispValue-center)*(1.0/delta);
    } else if((inf < 0) && (crispValue > center)){
        return 1 - abs(crispValue-center)*(1.0/delta);
    } else {
        return 1.0;
    }

}
}

TermSet类:

TermSet::TermSet(float a, float b, float step, int setsSize){
this->a = a;
this->b = b;
this->step = step;
this->last = -1;

this->setsSize = setsSize;
this->sets = (FSet**) malloc(sizeof(FSet*) * setsSize);
this->size = (b-a)/step + 1;
}

TermSet::~TermSet(){
    free(this->sets);
}

FSet* TermSet::getFSet(int index){
        return this->sets[index];
}

int TermSet::addFSet(FSet* set){
    sets[++last] = set;
    return last;
}

double TermSet::getUODValue(int index){
    return a + index*step;
}

int TermSet::UODSize(){
    return size;
}

FRule class

FRule::FRule(TermSet* ant1, int u1, TermSet* ant2, int u2, TermSet* conTermSet, int d){
    this->ant1 = ant1;
    this->ant2 = ant2;
    this->con = conTermSet;
    this->U1 = u1;
    this->U2 = u2;
    this->D = d;

    pi1 = 0.0;
    pi2 = 0.0;
    cdof = 0.0;
}

double FRule::dof(double x1,double x2){
    pi1 = ant1->getFSet(U1)->lom(x1); //ant1 is a TermSet       
    pi2 = ant2->getFSet(U2)->lom(x2); //ant2 is a TermSet           
    Serial.println(pi1);
    Serial.println(pi2);
    return min(pi1,pi2);;
}

最后是应用程序代码

FSet errN(-2,2,-1);
FSet errZ(0,2,0);
FSet errP(2,2,1);

FSet errDtN(-0.5,0.5,-1);
FSet errDtZ(0,0.5,0);
FSet errDtP(0.5,0.5,1);

FSet cntN(-4,4,-1);
FSet cntZ(0,4,0);
FSet cntP(4,4,1);

TermSet errDt(-1,1,0.1,3);
TermSet err(-3,3,1,3);
TermSet cnt(-6,6,1,3);


void loop(){  

  errDt.addFSet(&errDtN);
  int ierrDt = errDt.addFSet(&errDtZ);
  errDt.addFSet(&errDtP); 

  err.addFSet(&errN);
  int ierr = err.addFSet(&errZ);
  err.addFSet(&errP);  

  cnt.addFSet(&cntN);
  cnt.addFSet(&cntZ);
  cnt.addFSet(&cntP);

  FRule rule1(&err,ierr,&errDt,ierrDt,&cnt,1);

  Serial.print("UOD SIZE: ");
  Serial.println(errDt.UODSize());

  Serial.print("LOM1: ");
  Serial.println(errZ.lom(0));

  Serial.print("LOM2: ");
  Serial.println(errDtZ.lom(-0.1));

  Serial.print("DOF: ");
  Serial.println(rule1.dof(0,-0.1));  

  delay(10000);  

}

标题文件:

class FSet {
    public:
        FSet(double center, double delta, int inf);         
        // Returns Level Of Matching (value of the membership function) for the given x
        double lom(double crispValue);          
        // Start of UOD
        double a;
        // End of UOD
        double b;           
    private:          
        double center;
        ouble delta;
        int inf;        
}; 

class TermSet {

public:
    TermSet(float uodA, float uodB, float step, int setsSize);
    ~TermSet();
    FSet* getFSet(int index);
    int addFSet(FSet* set);
    double getUODValue(int index);
    int UODSize();

    FSet** sets;
    int setsSize;
    float a = 0;
    float b = 0;
    float step;
    int size = 0;
private:
    int last;
};

class FRule {
   public:
       FRule(TermSet* ant1, int u1, TermSet* ant2, int u2, TermSet* conTermSet, int d);
       double dof(double x1,double x2);
       double* ruleOutput(double x1, double x2);
       TermSet* ant1;
       TermSet* ant2;
       TermSet* con;
       double cdof;
       double pi1;
       double pi2;
   private:
       int U1;
       int U2;
       int D;

};

1 个答案:

答案 0 :(得分:1)

这是一个错误

TermSet::TermSet(float a, float b, float step, int setsSize){
this->a = a;
this->b = b;
this->step = step;
this->last = -1;

this->setsSize = size;

此时size是未初始化的变量。您的编译器应该已经警告过您。注意编译器警告。我想你的意思是

this->setsSize = setsSize;

不知道这是否是您遇到问题的原因,但您没有给我们太多的帮助。