gpt4 book ai didi

c++ - Arduino加法作业

转载 作者:太空宇宙 更新时间:2023-11-04 11:59:05 26 4
gpt4 key购买 nike

我使用 Arduino 进行了一些编程,但遇到了问题。在 loop() 函数中,我想在每次执行 loop() 函数时向变量 pwm 添加一个值。我从我编写的某个函数中获取该值,并使用命令 pwm += result 将其添加到 pwm,但它不起作用。如果我打印 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;


}

最佳答案

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

运行下面的示例程序,您将看到一次 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);
}

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com