我使用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
#定义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;


}

最佳答案

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

运行下面的示例程序,您将看到一次pwm可以正确打印。在第一个错误 call 之后,它将输出零。占用的内存也不再更改。

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);
}

关于c++ - Arduino加法作业,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14903753/

10-12 21:47