Arduino C++计算触发点

Arduino C++ calculating trigger points

本文关键字:计算 C++ Arduino      更新时间:2023-10-16

有人可以帮我写代码吗?我有一个 24 齿扳机轮。每颗牙齿都由霍尔传感器注册,我需要 Arduino 模拟相应的 36 个脉冲输入的 24 个脉冲输出。

这是我的延迟微秒代码,但我不能使用延迟微秒,因为 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);
}

如果有助于理解这里是框图

只要你明白你永远无法获得每圈 36 个脉冲的精度和 24 个脉冲/圈,你就可以做到这一点,这是从布雷森汉姆算法得出的常见技巧。 此解决方案假定您关心该职位。

现在,这将实时生成脉冲,而不是您的代码,它以阻塞方式生成脉冲,我不认为丢失脉冲是您的初衷。

此代码不会均匀地生成脉冲,1 个读数中有 3 个将产生 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.
}