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

วันที่15 ที่Fibo

-เริ่มเขียน Encoder เพื่อจะไปปรับความเรียว Motor
ปัญหาที่เห็นได้คือ datasheet motor ละต้องใช้ millis ในการสั่งงาน
การคำนวนหารอบ
You will receive multiple pulses per rotation, yes? You know how many pulses that is.
So measure the elapsed time using millis() for 1/10 of those, or 1/4 of those, and do a little math.
Say you had 100 pulses, which is 1/10 of the pulses of the pulses
in complete rotation, and they occurred in 25mS.
A little math, and some unit conversions, and you have an answer:
RPM = (100 pulses/25mS) * (1 Revolution/1000 pulses) * (1000mS/S) * (60S/M) = 2640R/M or 2640 RPM

เว็ปอ้างอิง http://forum.arduino.cc/index.php?topic=167122.0
ตัวอย่างโค้ดที่นำมาใช้งาน
const int Encoder_Pin = 2; // output of encoder to interrupt #0, pin 2 of arduino
int Encoder_Count = 0;
long Time1 = 0;
long Time2 = 0;
long Time3 = 0;
void setup()
{
Serial.begin(9600);
attachInterrupt(0, Count_Pulses, FALLING); //attach interrupt to encoder pin
}
void Count_Pulses()
{
//this is the function that runs every time pin 2
//switches from high to low
Encoder_Count++;
}
void loop()
{
Time1 = millis();
Time3 = Time1 -= Time2;
if (Time3 >= 500)
{
Time2 = millis ();
int PulsSpeed = Encoder_Count;
Encoder_Count = 0;
Serial.println(PulsSpeed);
}
}

ส่วนโค้ดที่2 เป็นการใช้ millis หาpulses ของ encoder ว่าถ้าผ่านไป1 pulses มันจะใช้เวลาเท่าไหร่
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned long perv = 0;

void setup(){
  Serial.begin(9600);
  attachInterrupt(0, Millis, FALLING);
}
void loop(){

}

void Millis()
{
  perv = time1; 
   Serial.println("Time2: ");
   Serial.println(perv); 
  time1 = millis();
  //prints time since program started
   Serial.println("Time1: ");
   Serial.println(time1);        // wait a second so as not to send massive amounts of data
   int  x = time1 -= time2;
   Serial.println("x: ");
   Serial.println(x);
}
แหล่งที่มาของฟังชัน millis  http://ucexperiment.com/31
แหล่งที่มาจากarduino cc  http://www.arduino.cc/en/Reference/Millis
เรามาวิเคราะห์ Hardware ของ Arduino กันก่อน ในมือผมมี Arduino Mega 2560 ซึ่งรันด้วยความถี่คริสตัล 16MHz
พูดเป็นภาษาคนก็คือในหนึ่งวินาทีจะมีสัญญาณนาฬิกา(ซึ่งต่อไปขอเรียก Clock เพราะพอลองแปลแล้วรู้สึกส้นตีนมาก) เข้ามาในวงจรทั้งหมดสิบหกล้านพัลส์ ซึ่งเราประมาณได้ว่า 1 คำสั่งจะใช้เวลาทั้งหมด 1/16,000,000 วินาที ก็คือ 0.0000000625 วินาที (หรือ 0.0625 uS)

-การเขียน PWM 
ตัวอย่างโค้ด 
void setup()
{
  pinMode(13, OUTPUT);
}

void loop()
{
  digitalWrite(13, HIGH);
  delayMicroseconds(100); // Approximately 10% duty cycle @ 1KHz
  digitalWrite(13, LOW);
  delayMicroseconds(1000 - 100);
}

แหล่งที่มา http://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM

-l298n motor driver เรื่องวงจร drive motor










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

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