วันพุธที่ 10 มิถุนายน พ.ศ. 2558

วันที่17 ที่Fibo

-ยังคงคิดปัยหากับการที่เราจะนำสัญญาณมาควบคุมPWM และในตอนแรก เขียน PWM โดยไม่ใช้ poten ไม่ได้แต่ทำได้โดยใช้ map และไม่ใช้
โค้ดตัวอย่าง
int val ;
void setup() {
Serial.begin(9600);
}

void loop()
{
  
  val = map(210, 0, 1023, 0, 500);
  analogWrite(9, val);
  Serial.println(val);
}

-การใช้ map() ในarduino map(value, fromLow, fromHigh, toLow, toHigh)
แหล่งที่มา http://www.arduino.cc/en/Reference/Map

-วิธีการควบคุม DC motor speed control using PID controler

แหล่งที่มา https://www.youtube.com/watch?v=pdYqjlY9jdA
ตัวอย่างโค้ดที่นำมาในการศึกษา
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
int sensorValue = 0;        // value read from the pot
float v=0.0;
float vf=0.0;
float u=0.0;
float ui=0.0;
float e1=0.0;
float e1_dotf=0.0;
float e1_old=0.0;

float kp=0.105;//0.101-0.515
float ki=0.152;//0.102-0.852
float kd=0.021;//0.011-0.081

// reading a PWM signal using interrupts
int pin1 = 2;       // arduino pin number numbers 0 (on digital pin 2)
int intrnum1 = 0;   // interrupt number 0-1 for others//and 1 (on digital pin 3) 
volatile unsigned long width1 = 4294967295;  // width of most recent signal
volatile unsigned long start1 = 0;  // start time of rising signal
//Used for timing
int i = 0;
unsigned long timer=0;
unsigned long dtime=1;

// myisr -- interrupt handler
void myisr1()  
{
  unsigned long now1 = micros();
  width1= now1 - start1;
  start1 = now1;

void setup(){
  Serial.begin(9600);
  pinMode(pin1,INPUT); 
  pinMode(9, OUTPUT); 
  attachInterrupt(intrnum1, myisr1, 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.
  timer = millis();//start timing
}

void loop(){
  
  dtime = millis()-timer;
  if(dtime >= 20)
  {
   timer = millis();//start timing
   sensorValue = analogRead(analogInPin); 
   sensorValue = map(sensorValue, 0, 1023, 0, 290);  //0-290 rpm
 
   v = 60000000.0/(width1*2.0); //rpm
   vf = vf + ((v-vf)*0.251);//filter
   //PID control
   e1 = sensorValue - vf;
   float e1_dot = (e1 - e1_old)/0.02;
   e1_old = e1;
   e1_dotf = e1_dotf + ((e1_dot - e1_dotf)*0.251);//filter
   ui = ui + (e1*ki*0.02);
   ui = constrain(ui, -60, 60);
   u = (kp*e1) + ui + (e1_dotf*kd);
   
   if(sensorValue == 0)
   {
     width1 = 4294967295;
     u = 0;
   }
   u = constrain(u, 0, 255);
   analogWrite(9, u);
 
   i++;
   if(i > 10)
   {
    i=0;
    Serial.print(e1); Serial.print("\t");
    //Serial.print(v);Serial.print("\t");
    Serial.print(vf);Serial.print("\t");
    Serial.print(ui);Serial.print("\t");
    Serial.println(dtime);
   }
  }
}





โค้ดที่เขียนและยังไม่สมบูรณ์
int sensorValue = 0;        // value read from the pot
float v=0.0;
float vf=0.0;
float u=0.0;
float ui=0.0;
float e1=0.0;
float e1_dotf=0.0;
float e1_old=0.0;

float kp=0.105;//0.101-0.515
float ki=0.152;//0.102-0.852
float kd=0.021;//0.011-0.081

// reading a PWM signal using interrupts

int motor = 9;

volatile unsigned long width1 = 4294967295;  // width of most recent signal
volatile unsigned long start1 = 0;  // start time of rising signal

int i = 0;
unsigned long timer=0;
unsigned long dtime=1;

// myisr -- interrupt handler
void myisr1()  
{
  unsigned long now1 = micros();
  width1= now1 - start1;
  start1 = now1;

void setup(){
  Serial.begin(9600);

  pinMode(motor, OUTPUT); 
  attachInterrupt(0, myisr1, RISING);
  timer = millis();//start timing
}

void loop(){
  
  dtime = millis()-timer;
  if(dtime >= 20)
  {
   timer = millis();//start timing
  
   sensorValue = map(1023, 0, 1023, 0, 500);  //0-500 rpm
 
   v = 60000000.0/(width1*2.0); //rpm
   vf = vf + ((v-vf)*0.251);//filter
   //PID control
   e1 = sensorValue - vf;
   float e1_dot = (e1 - e1_old)/0.02;
   e1_old = e1;
   e1_dotf = e1_dotf + ((e1_dot - e1_dotf)*0.251);//filter
   ui = ui + (e1*ki*0.02);
   ui = constrain(ui, -60, 60);
   u = (kp*e1) + ui + (e1_dotf*kd);
   
   if(sensorValue == 0)
   {
     width1 = 4294967295;
     u = 0;
   }
   u = constrain(u, 0, 255);
   analogWrite(motor, u);
 
   i++;
   if(i > 10)
   {
    i=0;
    Serial.print(e1); Serial.print("\t");
    //Serial.print(v);Serial.print("\t");
    Serial.print(vf);Serial.print("\t");
    Serial.print(ui);Serial.print("\t");
    Serial.println(dtime);
   }
  }
}

-การใช้งาน Module L298N
ชุดขับมอเตอร์อเนกประสงค์ด้วย IC เบอร์ L298 / L298N ซึ่งเป็น IC ขับมอเตอร์แบบ H-Bridge - สามารถใช้ขับมอเตอร์ DC ได้พร้อมกันจำนวน 2 ตัว
การต่อวงจร
ENA >> PWM 9 ของ arduino
IN1 >> PIN7
IN2  >>PIN8
ENAB>> PWM 9 ของ arduino
IN1 >> PIN7
IN2  >>PIN8


การทดลองใช้งาน
const int analogOutPin = 9; // Analog output pin that the LED is attached to
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
}
void loop() {
  analogWrite(analogOutPin, 255);
  digitalWrite(7,HIGH);
  digitalWrite(8,LOW);
  delay(5000);
  analogWrite(analogOutPin, 127);
  digitalWrite(7,HIGH);
  digitalWrite(8,LOW);
  delay(5000);
  analogWrite(analogOutPin, 0);
  digitalWrite(7,HIGH);
  digitalWrite(8,LOW);
  delay(5000);
  analogWrite(analogOutPin, 255);
  digitalWrite(7,LOW);
  digitalWrite(8,HIGH);
  delay(5000);
  analogWrite(analogOutPin, 127);
  digitalWrite(7,LOW);
  digitalWrite(8,HIGH);
  delay(5000);
  analogWrite(analogOutPin, 0);
  digitalWrite(7,LOW);
  digitalWrite(8,HIGH);
  delay(5000);
  analogWrite(analogOutPin, 0);
  digitalWrite(7,HIGH);
  digitalWrite(8,HIGH);
  delay(5000);
  
}


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

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