2017-06-13 109 views
2

有人可以幫我用代碼嗎?我有一個24齒的扳機輪。每顆牙齒都由霍爾傳感器記錄,我需要Arduino模擬相應的24脈衝輸入的36個脈衝輸出。Arduino C++計算觸發點

這裏是我的代碼與delayMicroseconds,但我不能使用delayMicroseconds,因爲Arduino不明白大於16k微秒延遲。

const int hall = 2; // hall sensor 
const int ledPin = 13;  // the pin that the LED is attached to 

// Variables will change: 
int teethCounter = 0; 
int hallState = 0; 
int lasthallState = 0; 
long cycles=0; 
boolean cycle = false; 
unsigned long microsStart = 0; 
unsigned long microsStop = 0; 
unsigned long usElapsed = 0; 
unsigned long usElapsedUp = 0; 
unsigned long usInterval; 


void setup() { 
// initialize the button pin as a input: 
pinMode(hall, INPUT); 
// initialize the LED as an output: 
pinMode(ledPin, OUTPUT); 
// initialize serial communication: 
Serial.begin(9600); 
} 


void loop() { 
hallState = digitalRead(hall); 
if(cycle==true){ 
microsStart=micros(); 
} 
if(cycle==true){ 
usInterval = usElapsedUp/72; 
for (int i=0; i <= 36; i++){ 
digitalWrite(13,HIGH); 
delayMicroseconds(usInterval); 
digitalWrite(13,LOW); 
delayMicroseconds(usInterval); 
cycle = false; 
} 
} 

// compare the hallState to its previous state 
if (hallState != lasthallState) { 
// if the state has changed, increment the counter 
if (hallState == HIGH) { 
    teethCounter++; 
    if(teethCounter==24){ 
    cycle = true; 
    cycles++; 
    teethCounter=0; 
    usElapsedUp = usElapsed; 

    } 

    Serial.print("Tooth count: "); 
    Serial.print(teethCounter); 
    Serial.print(" Cycles: "); 
    Serial.print(cycles); 
    Serial.print(" Time: "); 
    Serial.print(usElapsedUp); 
    Serial.print(" Interval: "); 
    Serial.println(usInterval); 
    } 
    microsStop=micros(); 
    usElapsed=microsStop-microsStart; 
    } 
// save the current state as the last state, 
//for next time through the loop 

    lasthallState = hallState; 
    } 

我該如何計算並從哪裏獲取觸發點?

If(event happens==true){ 
digitalWrite(13,HIGH); 
} 
If(event happens==false){ 
digitalWrite(13,LOW); 
} 

If it helps to understand here is a block diagram

+0

你需要某種功能,從24轉換爲36;因爲這不是1:1映射。額外的12個脈衝來自哪裏? –

+0

是否要將24脈衝*頻率*轉換爲36脈衝頻率? –

+0

是的,那正是我需要的 –

回答

2

只要你明白,你將永遠無法得到每轉的精度36個脈衝,24脈衝/轉,你能做到這一點,這是從Bressenham算法得出一個共同的伎倆。這個解決方案假定你關心這個位置。

現在,這將實時生成脈衝,而不是您的代碼,它以阻塞的方式生成脈衝,我認爲不會丟失脈衝是您的初衷。

該代碼不會均勻生成脈衝,3個讀數中的1個會生成2個脈衝。

另一種方式是,以計算平均速度和編程硬件計時器來模擬每匝36個脈衝,使用中斷,但打算這條路線將可能(總是在我的經驗)之間的同步的總損失最終車輪的實際位置以及您校正的滴答計數報告的內容。還有嚴格的速度範圍,你必須尊重,如果去那條路線,這也會引入嚴重的延遲問題到您的應用程序。

1:將增量值更改爲36,並將整個轉數計爲24/36。
2:將步驟檢測更改爲閾值24. 3:我試圖理解爲什麼要做這個36/24的事情,而不能。所以,你的里程可能會有所不同

// compare the hall State to its previous state 
// to declared outside of loop() 
// int smallCounter; 
// PULSE_WIDTH as small positive pulse with in us 
// 
if (hallState != lasthallState) { 
    // if the state has changed, increment the counter 
    smallCounter += ((hallState == HIGH) ? 36 : 0); 
    // ... I'm assuming that the serial prints you had here were just here for debugging. 
    lasthallState = hallState; 
} 
// 
// reporting for each step below 
// 
if (smallCounter >= 24) 
{ 
    smallCounter -= 24; 
    if (++teethCounter >= 36) { 
    cycle = true; 
    cycles++; 
    teethCounter=0; 
    usElapsedUp = usElapsed; 

    } 
    digitalWrite(13,HIGH); 
    delayMicroseconds(PULSE_WIDTH); 
    digitalWrite(13,LOW); 
    delayMicroseconds(PULSE_WIDTH); // this is probably not needed. 
}