我使用 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;
}