|
| 1 | +/* The Encoder class reads encoder periods calculated by the |
| 2 | + encoder2 pio program, stores the calculated encoder periods |
| 3 | + and keeps track of encoder tick count. |
| 4 | +
|
| 5 | + Copyright (C) 2024 Brian LePage |
| 6 | +
|
| 7 | + Redistribution and use in source and binary forms, with or without |
| 8 | + modification, are permitted provided that the following conditions |
| 9 | + are met: |
| 10 | + 1. Redistributions of source code must retain the above copyright |
| 11 | + notice, this list of conditions and the following disclaimer. |
| 12 | + 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + notice, this list of conditions and the following disclaimer in the |
| 14 | + documentation and/or other materials provided with the distribution. |
| 15 | + 3. The name of the author may not be used to endorse or promote products |
| 16 | + derived from this software without specific prior written permission. |
| 17 | +
|
| 18 | + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + */ |
| 29 | + |
| 30 | +#pragma once |
| 31 | +#include <Arduino.h> |
| 32 | +#include <limits> |
| 33 | +#include <vector> |
| 34 | +#include "encoder2.pio.h" |
| 35 | + |
| 36 | +namespace xrp { |
| 37 | + |
| 38 | +class Encoder { |
| 39 | +public: |
| 40 | + |
| 41 | + Encoder() : period_queue(samples_to_average()) {} |
| 42 | + |
| 43 | +/**************************************************************** |
| 44 | +* |
| 45 | +* Encoder::init() |
| 46 | +* Initialize PIO program that measures encoder period. |
| 47 | +* |
| 48 | +* Returns true on sucess. |
| 49 | +* |
| 50 | +*****************************************************************/ |
| 51 | + bool init(const int pin); |
| 52 | + |
| 53 | +/**************************************************************** |
| 54 | +* |
| 55 | +* Encoder::enable() |
| 56 | +* Enable the encoder. |
| 57 | +* |
| 58 | +* |
| 59 | +*****************************************************************/ |
| 60 | + void enable(); |
| 61 | + |
| 62 | +/**************************************************************** |
| 63 | +* |
| 64 | +* Encoder::disable() |
| 65 | +* Disable the encoder. |
| 66 | +* |
| 67 | +* |
| 68 | +*****************************************************************/ |
| 69 | + void disable(); |
| 70 | + |
| 71 | +/**************************************************************** |
| 72 | +* |
| 73 | +* Encoder::update() |
| 74 | +* Get the latest encoder period(s) from the PIO if available |
| 75 | +* store the latest, and updated the tick count. |
| 76 | +* |
| 77 | +* Returns number of samples that were retrieved. |
| 78 | +* |
| 79 | +*****************************************************************/ |
| 80 | + int update(); |
| 81 | + |
| 82 | +/**************************************************************** |
| 83 | +* |
| 84 | +* Encoder::setSamplesToAverage() |
| 85 | +* Set the number of Encoder period samples to average together to |
| 86 | +* calculate period. |
| 87 | +* |
| 88 | +*****************************************************************/ |
| 89 | + |
| 90 | +void setSamplesToAverage(const int n); |
| 91 | + |
| 92 | +/**************************************************************** |
| 93 | +* |
| 94 | +* Encoder::getPeriod() |
| 95 | +* Return the period calculated by the PIO in the following format: |
| 96 | +* 31 1 0 |
| 97 | +* | Period in 16-cycle ticks | dir | |
| 98 | +* |
| 99 | +* This is the same format return by the PIO. |
| 100 | +* |
| 101 | +*****************************************************************/ |
| 102 | + uint getPeriod(); |
| 103 | + |
| 104 | +/**************************************************************** |
| 105 | +* |
| 106 | +* Encoder::getCount() |
| 107 | +* Return the number of encoder ticks since the robot started. |
| 108 | +* |
| 109 | +*****************************************************************/ |
| 110 | + int getCount() const; |
| 111 | + |
| 112 | +/**************************************************************** |
| 113 | +* |
| 114 | +* Encoder::getDivisor() |
| 115 | +* Get divisor for encoder period in ticks per second. |
| 116 | +* |
| 117 | +*****************************************************************/ |
| 118 | + |
| 119 | + static constexpr uint getDivisor() { |
| 120 | + return F_CPU / encoder2_CYCLES_PER_COUNT; |
| 121 | + } |
| 122 | + |
| 123 | +private: |
| 124 | + uint period = 0; |
| 125 | + int period_fraction = 0; |
| 126 | + uint saved_period = UINT_MAX; |
| 127 | + bool direction = true; |
| 128 | + bool saved_direction = true; |
| 129 | + int samples_to_average_shift = 3; |
| 130 | + int sample_count = 0; |
| 131 | + int sample_index = 0; |
| 132 | + std::vector<uint> period_queue; |
| 133 | + uint count = 0; |
| 134 | + int StateMachineIdx = -1; |
| 135 | + unsigned long last_sample_time = 0; |
| 136 | + PIO PioInstance = nullptr; |
| 137 | + int pin = 0; |
| 138 | + int offset = -1; |
| 139 | + bool enabled = false; |
| 140 | + void clearPeriodQueue(); |
| 141 | + uint getFraction(const uint count) const; |
| 142 | + uint getWholeNumber(const uint count) const; |
| 143 | + constexpr uint samples_to_average() const { |
| 144 | + return 1 << samples_to_average_shift; |
| 145 | + } |
| 146 | +}; |
| 147 | + |
| 148 | +} |
0 commit comments