-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[driver] Add BitBangEncoderInput driver
- Loading branch information
Showing
4 changed files
with
183 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2021, Thomas Sommer | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
#ifndef MODM_BITBANG_ENCODER_INPUT_HPP | ||
#define MODM_BITBANG_ENCODER_INPUT_HPP | ||
|
||
#include <bit> | ||
#include <modm/platform.hpp> | ||
#include <type_traits> | ||
|
||
namespace modm | ||
{ | ||
/** | ||
* @ingroup modm_driver_bitbang_encoder_input | ||
* @author Thomas Sommer | ||
* | ||
* @brief This driver decodes a AB (incremental) encoder signal | ||
* | ||
* @tparam SignalA First modm::platform::Gpio pin to input the encoder signal. | ||
* @tparam SignalB Second modm::platform::Gpio pin to input the encoder signal. | ||
* @tparam POSTSCALER n_cycles to count as one in-/decrement. | ||
* @tparam DeltaType Must be signed integer and fit at least POSTSCALER. The Bigger | ||
* DeltaType, the more inc-/decrements can be stored temporarily. | ||
*/ | ||
template<typename SignalA, typename SignalB, uint8_t POSTSCALER = 4, | ||
std::signed_integral DeltaType = int8_t> | ||
class BitBangEncoderInput | ||
{ | ||
static_assert(std::popcount(POSTSCALER) == 1, | ||
"POSTSCALER must be an integer to basis 2 and not 0: 1, 2, 4, 8, 16, ..."); | ||
static_assert(POSTSCALER <= std::numeric_limits<DeltaType>::max(), | ||
"DeltaType is to small for POSTSCALER."); | ||
|
||
using Signals = modm::platform::SoftwareGpioPort<SignalA, SignalB>; | ||
|
||
static_assert(Signals::number_of_ports == 1, | ||
"Signal A/B must be on the same GPIO port to prevent signal tearing!"); | ||
|
||
uint8_t inline getRaw(); | ||
|
||
public: | ||
using ValueType = DeltaType; | ||
BitBangEncoderInput() : raw_last(0), delta(0){}; | ||
|
||
// Connect SingalA and SignalB and store power-up state | ||
inline void | ||
connect(); | ||
|
||
// Call @1kHz for manual movement | ||
inline void | ||
update(); | ||
|
||
ValueType | ||
getIncrement(); | ||
|
||
private: | ||
uint8_t raw_last; | ||
DeltaType delta; | ||
}; | ||
} // namespace modm | ||
|
||
#include "bitbang_encoder_input_impl.hpp" | ||
|
||
#endif // MODM_BITBANG_ENCODER_INPUT_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2021, Thomas Sommer | ||
# | ||
# This file is part of the modm project. | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# ----------------------------------------------------------------------------- | ||
|
||
def init(module): | ||
module.name = ":driver:encoder_input.bitbang" | ||
module.description = """ | ||
# Quadrature Encoder Input | ||
This driver decodes a AB (incremental) encoder signal. | ||
Ported from code created by Peter Dannegger: | ||
https://www.mikrocontroller.net/articles/Drehgeber. | ||
""" | ||
|
||
|
||
def prepare(module, options): | ||
module.depends(":architecture:atomic") | ||
return True | ||
|
||
|
||
def build(env): | ||
env.outbasepath = "modm/src/modm/driver/encoder" | ||
env.copy("bitbang_encoder_input.hpp") | ||
env.copy("bitbang_encoder_input_impl.hpp") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (c) 2021, Thomas Sommer | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
#ifndef MODM_BITBANG_ENCODER_INPUT_HPP | ||
#error "Don't include this file directly, use 'bitbang_encoder_input.hpp' instead!" | ||
#endif | ||
|
||
#include <cmath> | ||
|
||
template<typename SignalA, typename SignalB, uint8_t POSTSCALER, | ||
std::signed_integral DeltaType> | ||
inline uint8_t | ||
modm::BitBangEncoderInput<SignalA, SignalB, POSTSCALER, DeltaType>::getRaw() | ||
{ | ||
const uint8_t read = Signals::read(); | ||
// convert graycode to binary | ||
uint8_t raw = 0; | ||
if (read & 0b10) raw = 3; | ||
if (read & 0b01) raw ^= 1; | ||
return raw; | ||
} | ||
|
||
template<typename SignalA, typename SignalB, uint8_t POSTSCALER, | ||
std::signed_integral DeltaType> | ||
inline void | ||
modm::BitBangEncoderInput<SignalA, SignalB, POSTSCALER, DeltaType>::connect() | ||
{ | ||
Signals::setInput(::Gpio::InputType::PullUp); | ||
|
||
// Tare power-on state | ||
modm::delay(10us); | ||
raw_last = getRaw(); | ||
} | ||
|
||
template<typename SignalA, typename SignalB, uint8_t POSTSCALER, | ||
std::signed_integral DeltaType> | ||
inline void | ||
modm::BitBangEncoderInput<SignalA, SignalB, POSTSCALER, DeltaType>::update() | ||
{ | ||
uint8_t raw = getRaw(); | ||
const uint8_t diff = raw_last - raw; | ||
if (diff & 0b01) | ||
{ | ||
raw_last = raw; | ||
delta += (diff & 0b10) - 1; // bit 1 = direction (+/-) | ||
} | ||
} | ||
|
||
template<typename SignalA, typename SignalB, uint8_t POSTSCALER, | ||
std::signed_integral DeltaType> | ||
DeltaType | ||
modm::BitBangEncoderInput<SignalA, SignalB, POSTSCALER, DeltaType>::getIncrement() | ||
{ | ||
::modm::atomic::Lock _; | ||
DeltaType val = delta; | ||
|
||
delta &= (POSTSCALER - 1); // mask out higher bits | ||
|
||
constexpr uint8_t shift = std::log2(POSTSCALER); // Number of fraction bits | ||
return val >> shift; // return whats left without fractions | ||
} |