Arduino C ++计算触发点

时间:2017-06-13 19:55:10

标签: c++ arduino

有人可以帮我代码吗?我有一个24齿触发轮。每个牙齿都由霍尔传感器记录,我需要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

1 个答案:

答案 0 :(得分:2)

只要您了解到24圈/转就无法获得每回合36脉冲的精度,您可以这样做,这是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.
}