3D printed Arduino Airsoft Chronograph
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

timing.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. void interrupt_init() {
  31. // trigger both on rising edge
  32. EICRA = (1 << ISC00) | (1 << ISC01);
  33. EICRA |= (1 << ISC10) | (1 << ISC11);
  34. // enable interrupts
  35. EIMSK = (1 << INT0) | (1 << INT1);
  36. }
  37. /*
  38. * this is supposed to be the "input" sensor,
  39. * the one that triggers first on firing.
  40. */
  41. ISR(INT0_vect) {
  42. time_a = timer_get();
  43. trigger_a = 1;
  44. }
  45. /*
  46. * this is supposed to be the "output" sensor,
  47. * the one that triggers after the other sensor.
  48. */
  49. ISR(INT1_vect) {
  50. time_b = timer_get();
  51. trigger_b = 1;
  52. // we now need to turn on the UV led
  53. // and make sure it will only be on shortly!
  54. timer_start();
  55. digitalWrite(UV_LED_PIN, HIGH);
  56. }
  57. // --------------------------------------
  58. static void timer1_init() {
  59. // normal mode
  60. TCCR1A = 0;
  61. // prescaler
  62. #if TIMER_PRESCALER == 1
  63. TCCR1B = (1 << CS10);
  64. #elif TIMER_PRESCALER == 8
  65. TCCR1B = (1 << CS11);
  66. #elif TIMER_PRESCALER == 64
  67. TCCR1B = (1 << CS11) | (1 << CS10);
  68. #elif TIMER_PRESCALER == 256
  69. TCCR1B = (1 << CS12);
  70. #elif TIMER_PRESCALER == 1024
  71. TCCR1B = (1 << CS12) | (1 << CS10);
  72. #else
  73. #error Invalid Prescaler for Timer1
  74. #endif
  75. }
  76. static void timer2_init() {
  77. // normal mode, no clock source
  78. TCCR2A = 0;
  79. TCCR2B = 0;
  80. // enable overflow interrupt
  81. TIMSK2 = (1 << TOIE2);
  82. }
  83. void timer_init() {
  84. timer1_init();
  85. timer2_init();
  86. }
  87. uint16_t timer_get() {
  88. return TCNT1;
  89. }
  90. void timer_start() {
  91. /*
  92. * the distance between the second IR sensor
  93. * and the UV LEDs is 7.5mm.
  94. * Our bullet will travel with a speed of
  95. * ~10m/s up to ~300m/s approximately.
  96. * So it will move the 7.5mm in
  97. * 750us to 25us respectively.
  98. * So it makes sense to keep the UV LED
  99. * on for 1ms.
  100. *
  101. * We reach exactly 1ms when counting to 250
  102. * with a prescaler of 64 at 16MHz.
  103. *
  104. * If you __really__ want to increase the brightness
  105. * of the tracer, reduce the pulse length here.
  106. * Then you can also reduce the UV LED resistor for
  107. * higher currents, according to the datasheet of
  108. * your UV LED.
  109. * Make sure to keep within 40mA the AVR GPIO can provide.
  110. * Otherwise you need to add a transistor for switching.
  111. */
  112. const static uint8_t pulse_length = 250;
  113. // initial value we count up from
  114. TCNT2 = 0xFF - pulse_length;
  115. // prescaler 64
  116. TCCR2B = (1 << CS22);
  117. }
  118. ISR(TIMER2_OVF_vect) {
  119. // turn off UV LED
  120. digitalWrite(UV_LED_PIN, LOW);
  121. // and also stop timer
  122. TCCR2B = 0;
  123. }