gpt4 book ai didi

c++ - Arduino C++计算触发点

转载 作者:搜寻专家 更新时间:2023-10-31 02:10:51 27 4
gpt4 key购买 nike

有人可以帮我写代码吗?我有一个 24 齿触发轮。每颗 dentry 都由霍尔传感器记录,我需要 Arduino 模拟对应的 24 脉冲输入的 36 脉冲输出。

这是我的 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

最佳答案

只要您了解您永远无法通过 24 脉冲/转获得每转 36 个脉冲的精度,您就可以做到这一点,这是从 Bresenham 算法派生的常见技巧。此解决方案假设您关心职位。

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

此代码不会均匀地生成脉冲,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.
}

关于c++ - Arduino C++计算触发点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44530390/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com