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