1

我使用 Arduino 进行了一些编程,但我遇到了问题。在loop()函数中,我想在pwm每次loop()执行函数时为变量添加一个值。我从我编写的某个函数中获取该值并pwm使用命令将其添加到其中pwm += result,但它不起作用。如果我打印pwm,我总是得到零。即使打印result给了我一些非零值。整个代码是:

float pwm = 0;
float result = 0;

void loop(){

    ....

    errV = w - cm;
    errDtV = errOldV - errV;

    result = flc->cog(errV, errDtV);

    Serial.print("RESULT: ");
    Serial.println(result);

    pwm += result;
    Serial.println(pwm);
}

输出是这样的:

RESULT: 31.98
0.00

可能有什么问题?

编辑:这是整个草图

#include "FSet.h"
#include "FRule.h"
#include "Flc.h"
#include <NewPing.h>
#include "MotorControl.h"

MotorControl* m;
Flc* flc;
FRule* rule1,*rule2,*rule3,*rule4,*rule5,*rule6,*rule7,*rule8,*rule9;

NewPing sonar1(32,33,200);

static int dirA = 9;
static int pwmA = 8;

// Setup the FSets
FSet errZ(0,5,0);
FSet errMP(-15,15,0);
FSet errLP(-30,15,-1);

FSet errDtLN(-4,2,-1);
FSet errDtMN(-2,2,0);
FSet errDtZ(0,2,0);
FSet errDtMP(2,2,0);
FSet errDtLP(4,2,1);

FSet cntLN(-40,20,0);
FSet cntMN(-20,20,0);
FSet cntZ(0,20,0);
FSet cntMP(20,20,0);
FSet cntLP(40,20,0);

void setup(){
  Serial.begin(4800); 
  pinMode(dirA,INPUT);

  //Creating FRules for test    
  rule1 = new FRule(&errZ,&errDtMP,&cntMN);
  rule2 = new FRule(&errZ,&errDtZ,&cntZ);
  rule3 = new FRule(&errZ,&errDtMN,&cntMP);
  rule4 = new FRule(&errMP,&errDtLP,&cntMN);
  rule5 = new FRule(&errMP,&errDtMN,&cntMP);
  rule6 = new FRule(&errLP,&errDtMP,&cntMP);
  rule7 = new FRule(&errLP,&errDtZ,&cntLP);  
  rule8 = new FRule(&errLP,&errDtMN,&cntLP);  
  rule9 = new FRule(&errLP,&errDtLN,&cntLP);  

  flc = new Flc(9);
  flc->addRule(rule1);
  flc->addRule(rule2);
  flc->addRule(rule3);
  flc->addRule(rule4);
  flc->addRule(rule5);
  flc->addRule(rule6);
  flc->addRule(rule7); 
  flc->addRule(rule8);
  flc->addRule(rule9); 

}

int errV = 0;
int errOldV = 0;
int errDtV = 0;
int w = 30;
unsigned int uS;
unsigned int cm;

float pwm1 = 0;
float result = 0;

void loop(){   
  uS = sonar1.ping();
  cm = (uS / US_ROUNDTRIP_CM);

  errV = w - cm;
  errDtV = errOldV - errV;    

  result = flc->cog(errV,errDtV);

  Serial.print("RESULT: ");
  Serial.println(result);

  pwm1 = pwm1 + result;  

  Serial.println(pwm1);
  analogWrite(pwmA,pwm1);

  errOldV = errV;

}

FLC 类头文件:#ifndef FLC_H #define FLC_H #include "Arduino.h" #include "FRule.h"

class Flc {
    public:
        Flc(int size);
        ~Flc();
        int addRule(FRule* rule);
        int mom(float x1,float x2);
        float cog(float x1,float x2);

        FRule** rules;

    private:
        int last;
        int size;
        float h;
        float numerator = 0;
        float denominator = 0;
        float result = 0;

};

#endif

FLC 类源:#include "Arduino.h" #include "Flc.h"

Flc::Flc(int size){
    this->rules = (FRule**) malloc(sizeof(FRule*) * size);

    this->size = size;
    last = -1;
}

Flc::~Flc(){
    free(rules);
}

int Flc::addRule(FRule* rule){
    this->rules[++last] = rule;
    return last;
}

int Flc::mom(float x1,float x2){
    return 0.0;a
}

float Flc::cog(float x1, float x2){

    for(int i = 0; i < size;i++){

        h = rules[i]->dof(x1,x2);
        float area = rules[i]->widthOfCon() * ( h - h*h/2);

        numerator += rules[i]->cntrOfCon() * area;
        denominator += area;

    }

    result = numerator / denominator;

    return result;


}
4

1 回答 1

1

函数 cog() 中的某些东西踩到了变量 pwm。一旦 float 变量损坏, Serial.print() 将只显示零。下面的示例显示,将浮点数设置为 0xffffff,浮点数学库停止对变量进行任何操作。

运行下面的示例程序,您将看到一次 pwm 正确打印。在第一次错误调用之后,它会打印零。占用的内存也不再变化。

pwm=0.50 zpwm=0.50
pwm=0.00 zpwm=3.95 result=3.45
255-255-255-255
pwm=0.00 zpwm=4.45
pwm=0.00 zpwm=7.90 result=3.45
255-255-255-255

示例程序显示了写入错误内存位置的函数。查看链接器输出的内存映射,变量按列出的顺序放置在内存中。所以写超过 var 的结尾,会损坏 pwm。

float zpwm = 0;
byte var = 0;
float pwm = 0;
float result = 0;


float badactor() {
  *((long*)(&var+1)) = -1;
  return 3.45;
}

void setup() {
  Serial.begin(57600);
}

void loop() {

  zpwm += 0.5;
  pwm += 0.5;

  Serial.print("pwm=");
  Serial.print(pwm);
  Serial.print(" zpwm=");
  Serial.println(zpwm);

  result = badactor();
  pwm += result;
  zpwm += result;

  Serial.print("pwm=");
  Serial.print(pwm);
  Serial.print(" zpwm=");
  Serial.print(zpwm);
  Serial.print(" result=");
  Serial.println(result);

  uint8_t* ptr;
  ptr = (uint8_t*)&pwm;
  Serial.print((int)*(ptr));
  Serial.print("-");
  Serial.print((int)*(ptr+1));
  Serial.print("-");
  Serial.print((int)*(ptr+2));
  Serial.print("-");
  Serial.println((int)*(ptr+3));

  delay(1000);
}
于 2013-02-17T04:55:58.570 回答