RoboAide
Project to improve a DIY robotic arm used for mobility assistance
SpeedTest.ino
Go to the documentation of this file.
1 /* Encoder Library - SpeedTest - for measuring maximum Encoder speed
2  * http://www.pjrc.com/teensy/td_libs_Encoder.html
3  *
4  * This example code is in the public domain.
5  */
6 
7 
8 // This SpeedTest example provides a simple way to verify how much
9 // CPU time Encoder is consuming. Connect a DC voltmeter to the
10 // output pin and measure the voltage while the encoder is stopped
11 // or running at a very slow speed. Even though the pin is rapidly
12 // pulsing, a DC voltmeter will show the average voltage. Due to
13 // software timing, it will read a number much less than a steady
14 // logic high, but this number will give you a baseline reading
15 // for output with minimal interrupt overhead. Then increase the
16 // encoder speed. The voltage will decrease as the processor spends
17 // more time in Encoder's interrupt routines counting the pulses
18 // and less time pulsing the output pin. When the voltage is
19 // close to zero and will not decrease any farther, you have reached
20 // the absolute speed limit. Or, if using a mechanical system where
21 // you reach a speed limit imposed by your motors or other hardware,
22 // the amount this voltage has decreased, compared to the baseline,
23 // should give you a good approximation of the portion of available
24 // CPU time Encoder is consuming at your maximum speed.
25 
26 // Encoder requires low latency interrupt response. Available CPU
27 // time does NOT necessarily prove or guarantee correct performance.
28 // If another library, like NewSoftSerial, is disabling interrupts
29 // for lengthy periods of time, Encoder can be prevented from
30 // properly counting the intput signals while interrupt are disabled.
31 
32 
33 // This optional setting causes Encoder to use more optimized code,
34 // but the downside is a conflict if any other part of your sketch
35 // or any other library you're using requires attachInterrupt().
36 // It must be defined before Encoder.h is included.
37 //#define ENCODER_OPTIMIZE_INTERRUPTS
38 
39 #include <Encoder.h>
40 #include "pins_arduino.h"
41 
42 // Change these two numbers to the pins connected to your encoder
43 // or shift register circuit which emulates a quadrature encoder
44 // case 1: both pins are interrupts
45 // case 2: only first pin used as interrupt
46 Encoder myEnc(5, 6);
47 
48 // Connect a DC voltmeter to this pin.
49 const int outputPin = 12;
50 
51 /* This simple circuit, using a Dual Flip-Flop chip, can emulate
52  quadrature encoder signals. The clock can come from a fancy
53  function generator or a cheap 555 timer chip. The clock
54  frequency can be measured with another board running FreqCount
55  http://www.pjrc.com/teensy/td_libs_FreqCount.html
56 
57  +5V
58  | Quadrature Encoder Signal Emulator
59  Clock |
60  Input o----*-------------------------- ---------------------------o Output1
61  | |14 | |
62  | _______|_______ | | _______________
63  | | CD4013 | | | | CD4013 |
64  | 5 | | 1 | | 9 | | 13
65  ---------| D Q |-----|----*----| D Q |------o Output2
66  | | | | | | |
67  | | 3 | | | 11 | |
68  | ----|> Clk | ---------|> Clk |
69  | | | | |
70  | 6 | | 8 | |
71  | ----| S | ----| S |
72  | | | | | | |
73  | | 4 | _ | 2 | 10 | _ | 12
74  | *----| R Q |--- *----| R Q |----
75  | | | | | | | |
76  | | |_______________| | |_______________| |
77  | | | | |
78  | | | 7 | |
79  | | | | |
80  --------------------------------------------------------------
81  | | |
82  | | |
83  ----- ----- -----
84  --- --- ---
85  - - -
86 */
87 
88 
89 void setup() {
90  pinMode(outputPin, OUTPUT);
91 }
92 
93 #if defined(__AVR__) || defined(TEENSYDUINO)
94 #define REGTYPE unsigned char
95 #else
96 #define REGTYPE unsigned long
97 #endif
98 
99 void loop() {
100  volatile int count = 0;
101  volatile REGTYPE *reg = portOutputRegister(digitalPinToPort(outputPin));
102  REGTYPE mask = digitalPinToBitMask(outputPin);
103 
104  while (1) {
105  myEnc.read(); // Read the encoder while interrupts are enabled.
106  noInterrupts();
107  *reg |= mask; // Pulse the pin high, while interrupts are disabled.
108  count = count + 1;
109  *reg &= ~mask;
110  interrupts();
111  }
112 }
113 
#define REGTYPE
Definition: SpeedTest.ino:96
const int outputPin
Definition: SpeedTest.ino:49
Encoder myEnc(5, 6)
void setup()
Definition: SpeedTest.ino:89
int32_t read()
Definition: Encoder.h:104
void loop()
Definition: SpeedTest.ino:99