Serial readout of a rotary encoder for collecting running wheel data
- Counts Per Revolution (CPR):
- Number of counts (pulses) per full revolution of the encoder knob.
 - Each count corresponds to a small increment of rotation.
 
 - Which direction is the wheel turning?
- A positive 
positionChangeindicates rotation in one direction (e.g., forward), while a negative value indicates rotation in the opposite direction (e.g., backward). 
 - A positive 
 - Output:
- The script outputs the encoder counts sampled in 
positionChangewithin the 100 millisecond time window. - Used to determine how fast the wheel is turning and in which direction.
 
 - The script outputs the encoder counts sampled in 
 
To calculate speed and distance from the encoder output, you'll need to perform the following steps:
Necessary Parameters:
Wheel Circumference (C):  
Encoder Counts Per Revolution (CPR): encoder counts generated by one full rotation.
Sample Window Duration (T): 
- 
Calculate Distance Per Count (DPC):
$$\text{DPC} = \frac{\text{Circumference}}{\text{CPR}}$$  - 
Calculate Speed:
- 
Counts Per Second (CPS):
$$\text{CPS} = \frac{\text{positionChange}}{T}$$  - 
Speed (V; units of distance per second (e.g., meters per second)):
$$V = \text{CPS} \times \text{DPC}$$  
 - 
 - 
Calculate Distance Traveled:
- 
Distance for Each Sample Interval:
$$\text{Distance} = \text{positionChange} \times \text{DPC}$$  - 
Total Distance: Sum the distances calculated for each sample interval over time.
 
 - 
 
Assuming:
- Wheel Diameter (D) = 0.5 meters
 - Encoder CPR = 1024 counts/revolution
 - Sample Window Duration (T) = 0.1 seconds
 
- 
Wheel Circumference:
$$C = \pi \times 0.5 \text{ m} \approx 1.5708 \text{ m}$$  - 
Distance Per Count:
$$\text{Distance Per Count} = \frac{1.5708 \text{ m}}{1024} \approx 0.00153398 \text{ m/count}$$  - 
Assuming a
positionChangeof 150 counts:- 
Counts Per Second:
$$\text{CPS} = \frac{150 \text{ counts}}{0.1 \text{ s}} = 1500 \text{ counts/s}$$  - 
Speed:
$$V = 1500 \text{ counts/s} \times 0.00153398 \text{ m/count} \approx 2.30097 \text{ m/s}$$  - 
Distance Traveled in Sample Interval:
$$\text{Distance} = 150 \text{ counts} \times 0.00153398 \text{ m/count} \approx 0.230097 \text{ m}$$  
 - 
 
- The Direction of Rotation:
- Negative 
positionChangevalues indicate reverse rotation. - Ensure calculations account for the sign of 
positionChange. 
 - Negative 
 - The Encoder Resolution:
- Higher CPR provides more precise measurements but may require handling larger numbers.
 
 - The Sampling Rate:
- The 
sampleWindowshould be chosen based on the expected speed of the wheel and the encoder's capabilities. - A shorter 
sampleWindowprovides more frequent updates but may be more susceptible to noise. 
 - The