Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 奇怪的方法行为_C++_Arduino_Fuzzy Logic - Fatal编程技术网

C++ 奇怪的方法行为

C++ 奇怪的方法行为,c++,arduino,fuzzy-logic,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 TermS

我正试图为我的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);;
}
如果我直接调用
FSet
对象上的
lom
方法,它会给出正确的结果。但是如果我试图通过
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;
    }

}
}
圆台类

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;

};
这里有一只虫子

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;

不知道这是否是您出现问题的原因,但您没有给我们太多的支持。

它返回了什么“错误”结果?它应该返回什么?您作为
x1
x2
传递的是什么?我传递的是模糊集的讨论宇宙中的一些值,它们是一些双重值。您能给我们展示一下类声明吗?也许有些类型不符合预期?是的,我已将它们添加到原始帖子中。但是现在有点乱了。提到墙和类似的选项吗?(不,这只是逻辑中的“模糊”部分)@john你是对的,应该是这个->setsize=setsize;但这并没有造成问题。
this->setsSize = setsSize;