|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 TU Dresden |
| 3 | + * SPDX-License-Identifier: LGPL-2.1-only |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * @ingroup drivers_inc_encoder |
| 8 | + * @{ |
| 9 | + * |
| 10 | + * @file |
| 11 | + * @brief Device driver implementation for a generic incremental rotary encoder |
| 12 | + * |
| 13 | + * @author Leonard Herbst <[email protected]> |
| 14 | + * |
| 15 | + * @} |
| 16 | + */ |
| 17 | + |
| 18 | +#include "inc_encoder.h" |
| 19 | +#include "inc_encoder_params.h" |
| 20 | +#include "inc_encoder_constants.h" |
| 21 | + |
| 22 | +#include <errno.h> |
| 23 | +#include "log.h" |
| 24 | +#include "ztimer.h" |
| 25 | +#include "time_units.h" |
| 26 | + |
| 27 | +/* The maximum delta_count that does not cause an overflow in the RPM calculation */ |
| 28 | +#define DELTA_COUNT_MAX (INT32_MAX / (SEC_PER_MIN * MS_PER_SEC * GEAR_RED_RATIO_SCALE)) |
| 29 | + |
| 30 | +/* Maximum RPM before we accumulate more than DELTA_COUNT_MAX pulses per calculation period, |
| 31 | + * which would cause an overflow. */ |
| 32 | +#define MAX_RPM ((DELTA_COUNT_MAX * SEC_PER_MIN * MS_PER_SEC * GEAR_RED_RATIO_SCALE) \ |
| 33 | + / (CONFIG_INC_ENCODER_PPR \ |
| 34 | + * CONFIG_INC_ENCODER_GEAR_RED_RATIO \ |
| 35 | + * CONFIG_INC_ENCODER_HARDWARE_PERIOD_MS \ |
| 36 | + * 4)) |
| 37 | + |
| 38 | +#if (MAX_RPM < CONFIG_INC_ENCODER_MAX_RPM) |
| 39 | +# error With the current configuration the RPM calculation can overflow. \ |
| 40 | + Please reduce the period, pulses per revolution, gear reduction ratio, or the max RPM. |
| 41 | +#endif |
| 42 | + |
| 43 | +/* Prototypes */ |
| 44 | +static bool _rpm_calc_timer_cb(void *arg); |
| 45 | +static void _acc_overflow_cb(void *args); |
| 46 | + |
| 47 | +/* Public API */ |
| 48 | +int inc_encoder_init(inc_encoder_t *dev, const inc_encoder_params_t *params) |
| 49 | +{ |
| 50 | + dev->params = *params; |
| 51 | + |
| 52 | + if (qdec_init(dev->params.qdec_dev, QDEC_X4, _acc_overflow_cb, (void *) dev)) { |
| 53 | + LOG_ERROR("[inc_encoder] Qdec mode not supported!\n"); |
| 54 | + return -EINVAL; |
| 55 | + } |
| 56 | + |
| 57 | + dev->extended_count = 0; |
| 58 | + dev->prev_count = 0; |
| 59 | + dev->leftover_count = 0; |
| 60 | + dev->last_rpm = 0; |
| 61 | + |
| 62 | + /* Task to periodically calculate RPM */ |
| 63 | + ztimer_periodic_init(ZTIMER_MSEC, &dev->rpm_timer, _rpm_calc_timer_cb, (void *) dev, |
| 64 | + CONFIG_INC_ENCODER_HARDWARE_PERIOD_MS); |
| 65 | + |
| 66 | + ztimer_periodic_start(&dev->rpm_timer); |
| 67 | + |
| 68 | + return 0; |
| 69 | +} |
| 70 | + |
| 71 | +int inc_encoder_read_rpm(inc_encoder_t *dev, int32_t *rpm) |
| 72 | +{ |
| 73 | + int irq_state = irq_disable(); |
| 74 | + *rpm = dev->last_rpm; |
| 75 | + irq_restore(irq_state); |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +int inc_encoder_read_reset_milli_revs(inc_encoder_t *dev, int32_t *rev_counter) |
| 80 | +{ |
| 81 | + int32_t total_count; |
| 82 | + int32_t delta_count; |
| 83 | + |
| 84 | + int irq_state = irq_disable(); |
| 85 | + total_count = qdec_read_and_reset(dev->params.qdec_dev); |
| 86 | + total_count += dev->extended_count; |
| 87 | + delta_count = total_count - dev->prev_count; |
| 88 | + |
| 89 | + /* We reset the counter but we need to keep the number |
| 90 | + * of pulses since last read for the RPM calculation */ |
| 91 | + dev->leftover_count = delta_count; |
| 92 | + dev->extended_count = 0; |
| 93 | + dev->prev_count = 0; |
| 94 | + irq_restore(irq_state); |
| 95 | + |
| 96 | + /* The 4X mode counts all falling and rising edges */ |
| 97 | + *rev_counter = (int32_t) total_count / 4; |
| 98 | + |
| 99 | + *rev_counter *= UNIT_SCALE * GEAR_RED_RATIO_SCALE; |
| 100 | + *rev_counter /= CONFIG_INC_ENCODER_PPR; |
| 101 | + *rev_counter /= CONFIG_INC_ENCODER_GEAR_RED_RATIO; |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | +/* Private API */ |
| 106 | +static bool _rpm_calc_timer_cb(void *arg) |
| 107 | +{ |
| 108 | + inc_encoder_t *dev = (inc_encoder_t *) arg; |
| 109 | + int32_t delta_count; |
| 110 | + int32_t rpm; |
| 111 | + int32_t total_count; |
| 112 | + |
| 113 | + total_count = dev->extended_count + qdec_read(dev->params.qdec_dev); |
| 114 | + delta_count = total_count - dev->prev_count; |
| 115 | + if (dev->leftover_count != 0) { |
| 116 | + /* Add leftover count from last reset */ |
| 117 | + delta_count += dev->leftover_count; |
| 118 | + dev->leftover_count = 0; |
| 119 | + } |
| 120 | + dev->prev_count = total_count; |
| 121 | + |
| 122 | + rpm = (int32_t)(SEC_PER_MIN * MS_PER_SEC * GEAR_RED_RATIO_SCALE * delta_count) / |
| 123 | + (int32_t)(CONFIG_INC_ENCODER_PPR * CONFIG_INC_ENCODER_GEAR_RED_RATIO |
| 124 | + * CONFIG_INC_ENCODER_HARDWARE_PERIOD_MS * 4); /* 4X mode counts all edges */ |
| 125 | + |
| 126 | + dev->last_rpm = rpm; |
| 127 | + return true; |
| 128 | +} |
| 129 | + |
| 130 | +static void _acc_overflow_cb(void *args) |
| 131 | +{ |
| 132 | + inc_encoder_t *dev = (inc_encoder_t *) args; |
| 133 | + |
| 134 | + dev->extended_count += qdec_read_and_reset(dev->params.qdec_dev); |
| 135 | +} |
0 commit comments