วันจันทร์ที่ 22 มิถุนายน พ.ศ. 2558

วันที่25 ที่Fibo

-โค้ดเดิมที่ได้รับการปรับแต่งนิดหน่อย แต่ยังคงไม่สมบูรณ์ หรือ ยังไม่เสร็จ 
    -ติดปัญหาคือการคำนวนที่จะเอารอบ rpm มาลบกับค่าเดิมเพื่อจะให้ PWM เพิ่มขึ้น

int ledPin = 9;      // PWM connected to digital pin 9
int valum = 1;

float time1 = 0.0;
float time2 = 0.0;
float s=0.0;
float counter = 0.0;
float j = 0.0;
float k=0.0;
float rpm=0.0;
float rpm_set = 0.0;
unsigned long Cur_time=0;
unsigned long Prev_time=0;

int latter1 = 0;

void setup() {
  
  Serial.begin(9600);
  attachInterrupt(0,Millis,RISING);
  //LOW to trigger the interrupt whenever the pin is low,
  //CHANGE to trigger the interrupt whenever the pin changes value
  //RISING to trigger when the pin goes from low to high,
  //FALLING for when the pin goes from high to low.
  pinMode(ledPin,OUTPUT); //PWM
  digitalWrite(7,HIGH);
  digitalWrite(8,LOW);
  
}

void loop() 
  String latter ="";
  if(Serial.available()>0)
  {
    while(Serial.available()>0)
    {
  latter += char(Serial.read());
    }   
    
  Serial.println(latter);
  latter1=latter.toInt();
  }
  analogWrite(ledPin,latter1);
  delay(200); 
  Cur_time = millis();
  int s = Cur_time-Prev_time;
  if(s >= 100)
      {
  j = (counter*10.0)/360.0;
  k = j/(s*0.001);
  rpm = k*60.0;    //rpm
  Serial.print("rpm:"); Serial.print(rpm); Serial.println("\t");  

   rpm_set = analogWrite(latter1);
   err = rpm_set - rpm;
   
   counter=0;
   Prev_time = Cur_time;
   Serial.print("latter:"); Serial.print(latter); Serial.println("\t"); 
       }
}


void Millis()
{
  counter++;
}

      /********************************************************
* PID RelayOutput Example
* Same as basic example, except that this time, the output
* is going to a digital pin which (we presume) is controlling
* a relay. The pid is designed to output an analog value,
* but the relay can only be On/Off.
*
* To connect them together we use "time proportioning
* control" Tt's essentially a really slow version of PWM.
* First we decide on a window size (5000mS say.) We then
* set the pid to adjust its output between 0 and that window
* size. Lastly, we add some logic that translates the PID
* output into "Relay On Time" with the remainder of the
* window being "Relay Off Time"
********************************************************/
#include <PID_v1.h>
#define RelayPin 6
//Define Variables we'll be connecting to
double Setpoint, Input, Output;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
int WindowSize = 5000;
unsigned long windowStartTime;
void setup()
{
windowStartTime = millis();
//initialize the variables we're linked to
Setpoint = 100;
//tell the PID to range between 0 and the full window size
myPID.SetOutputLimits(0, WindowSize);
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
Input = analogRead(0);
myPID.Compute();
/************************************************
* turn the output pin on/off based on pid output
************************************************/
unsigned long now = millis();
if(now - windowStartTime>WindowSize)
{ //time to shift the Relay Window
windowStartTime += WindowSize;
}
if(Output > now - windowStartTime) digitalWrite(RelayPin,HIGH);
else digitalWrite(RelayPin,LOW);
}

แหล่งที่มา http://playground.arduino.cc/Code/PIDLibraryRelayOutputExample




// (Really Simple) PID Class by Ivan Seidel
// GitHub.com/ivanseidel
// Use as you want. Leave credits
class PID{
public:
double error;
double sample;
double lastSample;
double kP, kI, kD;
double P, I, D;
double pid;
double setPoint;
long lastProcess;
PID(double _kP, double _kI, double _kD){
kP = _kP;
kI = _kI;
kD = _kD;
}
void addNewSample(double _sample){
sample = _sample;
}
void setSetPoint(double _setPoint){
setPoint = _setPoint;
}
double process(){
// Implementação P ID
error = setPoint - sample;
float deltaTime = (millis() - lastProcess) / 1000.0;
lastProcess = millis();
//P
P = error * kP;
//I
I = I + (error * kI) * deltaTime;
//D
D = (lastSample - sample) * kD / deltaTime;
lastSample = sample;
// Soma tudo
pid = P + I + D;
return pid;
}
};
#define pSENSOR A1
#define pCONTROLE 3
PID meuPid(1.0, 0, 0);
void setup() {
Serial.begin(9600);
pinMode(pSENSOR, INPUT);
pinMode(pCONTROLE, OUTPUT);
}
int controlePwm = 50;
void loop() {
// Lê temperatura
double temperature = map(analogRead(pSENSOR), 0, 1023, 0, 100);
// Manda pro objeto PID!
meuPid.addNewSample(temperature);
// Converte para controle
controlePwm = (meuPid.process() + 50);
// Saída do controle
analogWrite(pCONTROLE, controlePwm);
}
-ออกแบบ LM298 โดยใช้proteus เพื่อจะหาโปรแกรมออกแบบ PCB ช่วยเพื่อน

ไม่มีความคิดเห็น:

แสดงความคิดเห็น