Arduino添加任务

时间:2013-02-15 21:39:42

标签: c++ arduino

我使用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&#34; Arduino.h&#34;     #include&#34; FRule.h&#34;

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&#34; Arduino.h&#34;     #include&#34; Flc.h&#34;

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;


}

1 个答案:

答案 0 :(得分: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);
}