3D printed Arduino Airsoft Chronograph
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timing.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * timing.cpp
  3. *
  4. * OpenChrono BB speed measurement device.
  5. *
  6. * Copyright (c) 2022 Thomas Buck <thomas@xythobuz.de>
  7. *
  8. * OpenChrono is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * OpenChrono is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with OpenChrono. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. * Two phototransistors connected to external interrupts 0 and 1.
  22. * Timer1 (16bit) used to count time between triggers.
  23. * Timer2 (8bit) used for timing UV LED pulse.
  24. */
  25. #include <Arduino.h>
  26. #include "timing.h"
  27. #include "config.h"
  28. volatile uint8_t trigger_a = 0, trigger_b = 0;
  29. volatile uint16_t time_a = 0, time_b = 0;
  30. #ifdef DEBUG_LONG_UV_TIME
  31. volatile static uint8_t led_runs = 0;
  32. #endif
  33. void interrupt_init() {
  34. // trigger both on rising edge
  35. EICRA = (1 << ISC00) | (1 << ISC01);
  36. EICRA |= (1 << ISC10) | (1 << ISC11);
  37. // enable interrupts
  38. EIMSK = (1 << INT0) | (1 << INT1);
  39. }
  40. /*
  41. * this is supposed to be the "input" sensor,
  42. * the one that triggers first on firing.
  43. */
  44. ISR(INT0_vect) {
  45. time_a = timer_get();
  46. trigger_a = 1;
  47. }
  48. /*
  49. * this is supposed to be the "output" sensor,
  50. * the one that triggers after the other sensor.
  51. */
  52. ISR(INT1_vect) {
  53. time_b = timer_get();
  54. trigger_b = 1;
  55. // we now need to turn on the UV led
  56. // and make sure it will only be on shortly!
  57. timer_start();
  58. digitalWrite(UV_LED_PIN, HIGH);
  59. }
  60. // --------------------------------------
  61. static void timer1_init() {
  62. // normal mode
  63. TCCR1A = 0;
  64. // prescaler
  65. #if TIMER_PRESCALER == 1
  66. TCCR1B = (1 << CS10);
  67. #elif TIMER_PRESCALER == 8
  68. TCCR1B = (1 << CS11);
  69. #elif TIMER_PRESCALER == 64
  70. TCCR1B = (1 << CS11) | (1 << CS10);
  71. #elif TIMER_PRESCALER == 256
  72. TCCR1B = (1 << CS12);
  73. #elif TIMER_PRESCALER == 1024
  74. TCCR1B = (1 << CS12) | (1 << CS10);
  75. #else
  76. #error Invalid Prescaler for Timer1
  77. #endif
  78. }
  79. static void timer2_init() {
  80. // normal mode, no clock source
  81. TCCR2A = 0;
  82. TCCR2B = 0;
  83. // enable overflow interrupt
  84. TIMSK2 = (1 << TOIE2);
  85. }
  86. void timer_init() {
  87. timer1_init();
  88. timer2_init();
  89. }
  90. uint16_t timer_get() {
  91. return TCNT1;
  92. }
  93. void timer_start() {
  94. /*
  95. * the distance between the second IR sensor
  96. * and the UV LEDs is 7.5mm.
  97. * Our bullet will travel with a speed of
  98. * ~10m/s up to ~300m/s approximately.
  99. * So it will move the 7.5mm in
  100. * 750us to 25us respectively.
  101. * So it makes sense to keep the UV LED
  102. * on for 1ms.
  103. *
  104. * We reach exactly 1ms when counting to 250
  105. * with a prescaler of 64 at 16MHz.
  106. *
  107. * If you __really__ want to increase the brightness
  108. * of the tracer, reduce the pulse length here.
  109. * Then you can also reduce the UV LED resistor for
  110. * higher currents, according to the datasheet of
  111. * your UV LED.
  112. * Make sure to keep within 40mA the AVR GPIO can provide.
  113. * Otherwise you need to add a transistor for switching.
  114. */
  115. const static uint8_t pulse_length = 250;
  116. // initial value we count up from
  117. TCNT2 = 0xFF - pulse_length;
  118. #ifdef DEBUG_LONG_UV_TIME
  119. led_runs = 0;
  120. #endif
  121. // prescaler 64
  122. TCCR2B = (1 << CS22);
  123. }
  124. ISR(TIMER2_OVF_vect) {
  125. /*
  126. * When a BB is only dropped through the device it moves
  127. * with something like 0.1m/s to 1m/s of velocity.
  128. * So in this case it only moves the 7.5mm to the UV LEDs
  129. * in 75ms to 7.5ms respectively.
  130. * So our 1ms pulse is not illuminating it at all!
  131. * In this case, we have to increase our pulse time by
  132. * approx. factor 100.
  133. */
  134. #ifdef DEBUG_LONG_UV_TIME
  135. led_runs++;
  136. if (led_runs < 100) {
  137. // keep running for another millisecond
  138. TCNT2 = 0xFF - 250;
  139. return;
  140. }
  141. #endif
  142. // turn off UV LED
  143. digitalWrite(UV_LED_PIN, LOW);
  144. // and also stop timer
  145. TCCR2B = 0;
  146. }