x: the number to constrain, all data types
a: the lower end of the range, all data types
b: the upper end of the range, all data types
x: if x is between a and b
a: if x is less than a
b: if x is greater than b
a: the lower end of the range, all data types
b: the upper end of the range, all data types
x: if x is between a and b
a: if x is less than a
b: if x is greater than b
-เว็ปอธิบายเกี่ยวกับ Speed Control of DC Motor Using PID Method
http://www.engineersgarage.com/contribution/speed-control-dc-motor-using-pid-method
http://www.engineersgarage.com/contribution/speed-control-dc-motor-using-pid-method
วีดีโอตัวอย่าง จาก youtube
-วันนี้ทดลองเขียนโค้ดหลายตัวเลย เพื่อจะแก้ปัญหา control PWM
-โค้ดแรกPWM
-โค้ดแรกPWM
int ledPin = 9; // LED connected to digital pin 9
int time100 = 0;
int time1 = 0;
void setup() {
}
void loop() {
analogWrite(ledPin, 40); // analogWrite values from 0 to 255
}
-โค้ดที่สอง อ่านค่าencoder โดยใช้ interrupt แล้วเอาค่าที่หาได้จาก millis มาควบคุม
int ledPin = 9; // LED connected to digital pin 9
int in1A = 7;
int in2A = 8;
unsigned long timeNow = 0.00; //unsigned long 4294967295.00
unsigned long timeDif = 0.00;
unsigned long perv = 0.00;
int i = 0;
void setup()
{
attachInterrupt(0, Millis, FALLING); //ใช้interrup ที่pin2 หรือinterrup 0
//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); // sets the pin as output
pinMode(in1A, OUTPUT); //swts pin as output
pinMode(in2A, OUTPUT);
digitalWrite(in1A,HIGH);
digitalWrite(in2A,LOW);
Serial.begin(9600);
}
void loop()
{
analogWrite(ledPin, 40); // analogWrite values from 0 to 255
Serial.print("perv: "); Serial.print(perv); Serial.print("\t");
Serial.print("timeNow: "); Serial.print(timeNow); Serial.print("\t");
Serial.print("time2: "); Serial.print(timeDif); Serial.println("\t");
}
void Millis()
{
timeNow = millis();
timeDif = timeNow - perv;
perv = timeNow;
}
ตอนแรกที่เขียนผมมีข้อผิดพลาดก็คือผมสั่งแสดง monitor ที่ interrupt ทำให้การทำงงานช้าไปอย่างมากและ pulses มันเร็วมากทำให้ interrupt เร็วจนอ่านค่าไม่ได้จึงจะมีการแก้ไข้ปัญหาโดยหาinterupt เป็นช่วงๆ ให้ได้ก่อนแล้วถึงจะทำการ กำหนดความเร็วของ PWM ได้
int ledPin = 9; // LED connected to digital pin 9
int in1A = 7;
int in2A = 8;
unsigned long timeNow = 0.00; //unsigned long 4294967295.00
unsigned long timeDif = 0.00;
unsigned long perv = 0.00;
int i = 0;
void setup()
{
attachInterrupt(0, Millis, FALLING); //ใช้interrup ที่pin2 หรือinterrup 0
//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); // sets the pin as output
pinMode(in1A, OUTPUT); //swts pin as output
pinMode(in2A, OUTPUT);
digitalWrite(in1A,HIGH);
digitalWrite(in2A,LOW);
Serial.begin(9600);
}
void loop()
{
analogWrite(ledPin, 40); // analogWrite values from 0 to 255
Serial.print("perv: "); Serial.print(perv); Serial.print("\t");
Serial.print("timeNow: "); Serial.print(timeNow); Serial.print("\t");
Serial.print("time2: "); Serial.print(timeDif); Serial.println("\t");
}
void Millis()
{
timeNow = millis();
timeDif = timeNow - perv;
perv = timeNow;
}
ตอนแรกที่เขียนผมมีข้อผิดพลาดก็คือผมสั่งแสดง monitor ที่ interrupt ทำให้การทำงงานช้าไปอย่างมากและ pulses มันเร็วมากทำให้ interrupt เร็วจนอ่านค่าไม่ได้จึงจะมีการแก้ไข้ปัญหาโดยหาinterupt เป็นช่วงๆ ให้ได้ก่อนแล้วถึงจะทำการ กำหนดความเร็วของ PWM ได้


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