Arduino 如何将输出从循环函数传递到设置函数?
我尝试使用Arduino 如何将输出从循环函数传递到设置函数?,arduino,output,parameter-passing,Arduino,Output,Parameter Passing,我尝试使用Arduino并使用模糊逻辑为机器人制作控制器。该代码的输入为2个LDR传感器(由RSensor和LSensor标记),输出为RMotor和LMotor。我研究了一些示例,我的代码抛出了以下错误: tryingagain:12: error: expected constructor, destructor, or type conversion before '*' token tryingagain.ino: In function 'void setup()': tryingag
Arduino
并使用模糊逻辑为机器人制作控制器。该代码的输入为2个LDR传感器(由RSensor和LSensor标记),输出为RMotor和LMotor。我研究了一些示例,我的代码抛出了以下错误:
tryingagain:12: error: expected constructor, destructor, or type conversion before '*' token
tryingagain.ino: In function 'void setup()':
tryingagain:38: error: 'FuzzyInput' was not declared in this scope
tryingagain:38: error: 'RSensor' was not declared in this scope
tryingagain:38: error: expected type-specifier before 'FuzzyInput'
tryingagain:38: error: expected `;' before 'FuzzyInput'
tryingagain:39: error: 'whiteRS' was not declared in this scope
tryingagain:40: error: 'greyRS' was not declared in this scope
tryingagain:41: error: 'blackRS' was not declared in this scope
tryingagain:43: error: 'fuzzy' was not declared in this scope
tryingagain:46: error: 'LSensor' was not declared in this scope
使用的每个变量都会重复这些错误。
我的问题是,我之前已经在setup函数中声明了这些变量。它不能在循环函数中调用?如果不是,如何将这些变量值从一个函数传递到另一个函数
这是我的arduino代码:
#include <FuzzyRule.h>
#include <FuzzyComposition.h>
#include <Fuzzy.h>
#include <FuzzyRuleConsequent.h>
#include <FuzzyOutput.h>
#include <FuzzyInput.h>
#include <FuzzyIO.h>
#include <FuzzySet.h>
#include <FuzzyRuleAntecedent.h>
Fuzzy* fuzzy = new Fuzzy();
FuzzySet* whiteRS = new FuzzySet(0, 0, 0.1, 0.45);
FuzzySet* greyRS = new FuzzySet(0.15, 0.4, 0.6, 0.85);
FuzzySet* blackRS = new FuzzySet(0.55, 0.9, 1, 1);
FuzzySet* whiteLS = new FuzzySet(0, 0, 0.1, 0.45);
FuzzySet* greyLS = new FuzzySet(0.15, 0.4, 0.6, 0.85);
FuzzySet* blackLS = new FuzzySet(0.55, 0.9, 1, 1);
int LDR0 = 0; //analog pin to which LDR is connected, here we set it to 0 so it means A0
int LDR1 = 1; //analog pin to which LDR is connected, here we set it to 1 so it means A1
int LDRValue0 = 0; //that’s a variable to store LDR0 values
int LDRValue1 = 0; //that’s a variable to store LDR1 values
int LDRMax=800;
int LDRMin=100;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT); //we mostly use13 because there is already a built in yellow LED in arduino which shows output when 13 pin is enabled
pinMode(12, OUTPUT); //we mostly use12 because there is already a built in yellow LED in arduino which shows output when 12 pin is enabled
// FuzzyInput
FuzzyInput* RSensor = new FuzzyInput(1);
RSensor->addFuzzySet(whiteRS);
RSensor->addFuzzySet(greyRS);
RSensor->addFuzzySet(blackRS);
fuzzy->addFuzzyInput(RSensor);
// FuzzyInput
FuzzyInput* LSensor = new FuzzyInput(2);
LSensor->addFuzzySet(whiteLS);
LSensor->addFuzzySet(greyLS);
LSensor->addFuzzySet(blackLS);
fuzzy->addFuzzyInput(LSensor);
// FuzzyOutput
FuzzyOutput* RMotor= new FuzzyOutput(1);
FuzzySet* slowRM = new FuzzySet(0, 0, 0.2, 0.8);
RMotor->addFuzzySet(slowRM);
FuzzySet* fastRM = new FuzzySet(0.2, 0.8, 1, 1);
RMotor->addFuzzySet(fastRM);
fuzzy->addFuzzyOutput(RMotor);
FuzzyOutput* LMotor= new FuzzyOutput(2);
FuzzySet* slowLM = new FuzzySet(0, 0, 0.2, 0.8);
LMotor->addFuzzySet(slowLM);
FuzzySet* fastLM = new FuzzySet(0.2, 0.8, 1, 1);
LMotor->addFuzzySet(fastLM);
fuzzy->addFuzzyOutput(LMotor);
// Building FuzzyRule1
FuzzyRuleAntecedent* ifRSWhiteAndLSBlack = new FuzzyRuleAntecedent();
ifRSWhiteAndLSBlack->joinWithAND(whiteRS, blackLS);
FuzzyRuleConsequent* thenRMSlow = new FuzzyRuleConsequent();
thenRMSlow->addOutput(slowRM);
FuzzyRule* fuzzyRule1 = new FuzzyRule(1, ifRSWhiteAndLSBlack, thenRMSlow);
fuzzy->addFuzzyRule(fuzzyRule1);
// Building FuzzyRule2
FuzzyRuleAntecedent* ifRSGreyAndLSWhite = new FuzzyRuleAntecedent();
ifRSGreyAndLSWhite->joinWithAND(greyRS, whiteLS);
FuzzyRuleConsequent* thenLMfast = new FuzzyRuleConsequent();
thenLMfast->addOutput(fastLM);
FuzzyRule* fuzzyRule2 = new FuzzyRule(2, ifRSGreyAndLSWhite, thenLMfast);
fuzzy->addFuzzyRule(fuzzyRule2);
// Building FuzzyRule3
FuzzyRuleAntecedent* ifRSGreyAndLSGrey = new FuzzyRuleAntecedent();
ifRSGreyAndLSGrey->joinWithAND(greyRS, greyLS);
FuzzyRuleConsequent* thenLMslowAndRMslow = new FuzzyRuleConsequent();
thenLMslowAndRMslow->addOutput(slowLM);
thenLMslowAndRMslow->addOutput(slowRM);
FuzzyRule* fuzzyRule3 = new FuzzyRule(3, ifRSGreyAndLSGrey, thenLMslowAndRMslow);
fuzzy->addFuzzyRule(fuzzyRule3);
// Building FuzzyRule4
FuzzyRuleAntecedent* ifRSBlackAndLSWhite = new FuzzyRuleAntecedent();
ifRSBlackAndLSWhite->joinWithAND(blackRS, whiteLS);
FuzzyRuleConsequent* thenLMslow = new FuzzyRuleConsequent();
thenLMslow->addOutput(slowLM);
FuzzyRule* fuzzyRule4 = new FuzzyRule(4, ifRSBlackAndLSWhite, thenLMslow);
fuzzy->addFuzzyRule(fuzzyRule4);
// Building FuzzyRule5
FuzzyRuleAntecedent* ifRSBlackAndLSGrey = new FuzzyRuleAntecedent();
ifRSBlackAndLSGrey->joinWithAND(blackRS, greyLS);
FuzzyRuleConsequent* thenRMslow = new FuzzyRuleConsequent();
thenRMslow->addOutput(slowRM);
FuzzyRule* fuzzyRule5 = new FuzzyRule(5, ifRSBlackAndLSGrey, thenRMslow);
fuzzy->addFuzzyRule(fuzzyRule5);
// Building FuzzyRule6
FuzzyRuleAntecedent* ifRSBlackAndLSBlack = new FuzzyRuleAntecedent();
ifRSBlackAndLSBlack->joinWithAND(blackRS, blackLS);
FuzzyRuleConsequent* thenLMslowAndRMslow1 = new FuzzyRuleConsequent();
thenLMslowAndRMslow1 ->addOutput(slowLM);
thenLMslowAndRMslow1 ->addOutput(slowRM);
FuzzyRule* fuzzyRule6 = new FuzzyRule(6, ifRSBlackAndLSBlack, thenLMslowAndRMslow1 );
fuzzy->addFuzzyRule(fuzzyRule6);
// Building FuzzyRule7
FuzzyRuleAntecedent* ifRSWhiteAndLSGrey = new FuzzyRuleAntecedent();
ifRSWhiteAndLSGrey->joinWithAND(whiteRS, greyLS);
FuzzyRuleConsequent* thenRMfast = new FuzzyRuleConsequent();
thenRMfast->addOutput(fastRM);
FuzzyRule* fuzzyRule7 = new FuzzyRule(7, ifRSWhiteAndLSGrey, thenRMfast);
fuzzy->addFuzzyRule(fuzzyRule7);
// Building FuzzyRule8
FuzzyRuleAntecedent* ifRSGreyAndLSBlack = new FuzzyRuleAntecedent();
ifRSGreyAndLSBlack->joinWithAND(greyRS, blackLS);
FuzzyRuleConsequent* thenRMslow1 = new FuzzyRuleConsequent();
thenRMslow1->addOutput(slowRM);
FuzzyRule* fuzzyRule8 = new FuzzyRule(8, ifRSGreyAndLSBlack, thenRMslow1);
fuzzy->addFuzzyRule(fuzzyRule8);
}
void loop()
{
LDRValue0 = analogRead(LDR0); //reads the ldr’s value through LDR which we have set to Analog input 0 “A0″
delay(50); //This is the output value by which LDR sends value to arduino
//right sensor
LDRValue1 = analogRead(LDR1); //reads the ldr’s value through LDR which we have set to Analog input 1 “A1″
delay(50); //This is the output value by which LDR sends value to arduino
//leftsensor
//Finding normalization value for LDRValue0 and LDRValue1
RSensor=(LDRValue0-LDRMin)/(LDRMax-LDRMin);
LSensor=(LDRValue1-LDRMin)/(LDRMax-LDRMin);
fuzzy->setInput(1, RSensor);
fuzzy->setInput(2, LSensor);
fuzzy->fuzzify();
float output1 = fuzzy->defuzzify(1);
float output2 = fuzzy->defuzzify(2);
delay(100000);
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
Fuzzy*Fuzzy=新Fuzzy();
FuzzySet*whiteRS=新的FuzzySet(0,0,0.1,0.45);
FuzzySet*greyRS=新的FuzzySet(0.15,0.4,0.6,0.85);
FuzzySet*blackRS=新的FuzzySet(0.55,0.9,1,1);
FuzzySet*whiteLS=新的FuzzySet(0,0,0.1,0.45);
FuzzySet*greyLS=新的FuzzySet(0.15,0.4,0.6,0.85);
FuzzySet*blackLS=新的FuzzySet(0.55,0.9,1,1);
int LDR0=0//LDR连接的模拟管脚,这里我们将其设置为0,因此表示A0
int LDR1=1//LDR连接的模拟管脚,这里我们将其设置为1,因此表示A1
int LDRValue0=0//这是一个用于存储LDR0值的变量
int-LDRValue1=0//这是一个存储LDR1值的变量
int-LDRMax=800;
int-LDRMin=100;
无效设置()
{
Serial.begin(9600);
pinMode(13,输出);//我们主要使用13,因为arduino中已经有一个内置的黄色LED,当启用13针时,它会显示输出
pinMode(12,输出);//我们主要使用12,因为arduino中已经有一个内置的黄色LED,当12针被启用时显示输出
//模糊输入
FuzzyInput*r传感器=新的FuzzyInput(1);
R传感器->添加模糊设置(白色);
R传感器->添加模糊集(灰色);
R传感器->添加模糊设置(黑色);
fuzzy->addFuzzyInput(RSensor);
//模糊输入
FuzzyInput*l传感器=新的FuzzyInput(2);
l传感器->添加模糊设置(白色);
l传感器->添加模糊集(灰色);
l传感器->添加模糊设置(blackLS);
fuzzy->addFuzzyInput(LSensor);
//模糊输出
FuzzyOutput*RMotor=新的FuzzyOutput(1);
FuzzySet*slowRM=新的FuzzySet(0,0,0.2,0.8);
RMotor->addFuzzySet(slowRM);
FuzzySet*fastRM=新的FuzzySet(0.2,0.8,1,1);
RMotor->addFuzzySet(fastRM);
fuzzy->addFuzzyOutput(RMotor);
FuzzyOutput*LMotor=新的FuzzyOutput(2);
FuzzySet*slowLM=新的FuzzySet(0,0,0.2,0.8);
l电机->添加模糊设置(slowLM);
FuzzySet*fastLM=新的FuzzySet(0.2,0.8,1,1);
l电机->添加模糊设置(fastLM);
fuzzy->addFuzzyOutput(电机);
//建筑物模糊规则1
FuzzyRuleAntecedent*ifRSWhiteAndLSBlack=新的FuzzyRuleAntecedent();
IFRS白色和黑色->连接和(白色和黑色);
FuzzyRuleConcerent*thenRMSlow=新的FuzzyRuleConcerent();
然后rmslow->addOutput(slowRM);
FuzzyRule*fuzzyRule1=新的FuzzyRule(1,IFRS白色和黑色,然后是rmslow);
fuzzy->addFuzzyRule(fuzzyRule1);
//建筑模糊规则2
FuzzyRuleAntecedent*ifRSGreyAndLSWhite=新的FuzzyRuleAntecedent();
IFRS灰色和白色->连接和(灰色、白色);
FuzzyRuleConcerent*thenLMfast=新的FuzzyRuleConcerent();
然后单击lmfast->addOutput(fastLM);
FuzzyRule*fuzzyRule2=新的FuzzyRule(2,IFRSgreyandswhite,thenLMfast);
fuzzy->addFuzzyRule(fuzzyRule2);
//建筑物模糊规则3
FuzzyRuleAntecedent*ifRSGreyAndLSGrey=新的FuzzyRuleAntecedent();
IFRSGREDLSGREY->joinWithAND(GRYRS,GRYLS);
FuzzyRuleConcertinent*thenLMslowAndRMslow=新的FuzzyRuleConcertinent();
然后单击lmslowandrmslow->addOutput(slowLM);
然后单击LMSLOWANDRMSLOW->添加输出(slowRM);
FuzzyRule*fuzzyRule3=新的FuzzyRule(3,ifRSGreyAndLSGrey,Thenlmslow和rmslow);
fuzzy->addFuzzyRule(fuzzyRule3);
//建筑物模糊规则4
FuzzyRuleAntecedent*ifRSBlackAndLSWhite=新的FuzzyRuleAntecedent();
IFRS Black和LSWhite->joinWithAND(黑色和白色);
FuzzyRuleConcerent*thenLMslow=新的FuzzyRuleConcerent();
然后单击lmslow->addOutput(slowLM);
FuzzyRule*fuzzyRule4=新的FuzzyRule(4,黑色和白色,然后是慢速);
fuzzy->addFuzzyRule(fuzzyRule4);
//建筑物模糊规则5
FuzzyRuleAntecedent*ifRSBlackAndLSGrey=新的FuzzyRuleAntecedent();
IFRS Black和LSGrey->joinWithAND(黑色、灰色);
FuzzyRuleConcerent*thenRMslow=新的FuzzyRuleConcerent();
然后rmslow->addOutput(slowRM);
FuzzyRule*fuzzyRule5=新的FuzzyRule(5,黑色和灰色,然后是rmslow);
fuzzy->addFuzzyRule(fuzzyRule5);
//建筑物模糊规则6
FuzzyRuleAntecedent*ifRSBlackAndLSBlack=新的FuzzyRuleAntecedent();
IFRS Black和LSBlack->joinWithAND(blackRS、blackLS);
FuzzyRuleConcertinent*thenLMslowAndRMslow1=新的FuzzyRuleConcertinent();
然后单击lmslowandrmslow1->addOutput(slowLM);
然后单击LMSLOWANDRMSLOW1->添加输出(slowRM);
FuzzyRule*fuzzyRule6=新的FuzzyRule(6,IFRSBLACK和LSBLACK,然后是LMSLO和RMSLOW1);
fuzzy->addFuzzyRule(fuzzyRule6);
//建筑物模糊规则7
FuzzyRuleAntecedent*ifRSWhiteAndLSGrey=新的FuzzyRuleAntecedent();
IFRS白色和灰色->连接和(白色、灰色);
FuzzyRuleConcerent*thenRMfast=新的FuzzyRuleConcerent();
然后单击rmfast->addOutput(fastRM);
FuzzyRule*fuzzyRule7=新的FuzzyRule(7,IFRS白色和LS灰色,然后是RMFAST);
fuzzy->addFuzzyRule(fuzzyRule7);
//建筑物模糊规则8
FuzzyRuleAntecedent*ifRSGreyAndLSBlack=新的FuzzyRuleAntecedent();
IFRS灰色和黑色->连接和(灰色、黑色);
FuzzyRuleConcertinent*thenRMslow1=新的FuzzyRuleConcertinent();
然后单击RMSLOW1->添加输出(slowRM);
FuzzyRule*FuzzyRule 8=新的FuzzyRule(8,IFRSGRE和DLSBLACK,然后是RMSLOW1);
fuzzy->addFuzzyRule(fuzzyRule8);
}
void循环()
{
LDRValue0=analogRead(LDR0);//通过我们设置为模拟输入0“A0”的ldr读取ldr的值
延迟(50);//这是LDR向arduino发送值的输出值
//右传感器
LDRValue1=模拟读取(LDR1);