-
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] Driver for AB (incremental) encoder output
- Loading branch information
Showing
4 changed files
with
220 additions
and
7 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,86 @@ | ||
// coding: utf-8 | ||
/* | ||
* Copyright (c) 2019, Raphael Lehmann | ||
* | ||
* 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_ENCODER_OUTPUT_HPP | ||
#define MODM_ENCODER_OUTPUT_HPP | ||
|
||
#include <modm/processing/timer/periodic_timer.hpp> | ||
|
||
namespace modm | ||
{ | ||
|
||
/** | ||
* @ingroup modm_driver_encoder_output | ||
* @author Raphael Lehmann | ||
* | ||
* @brief This driver generates a AB (incremental) encoder signal to | ||
* emulate a hardware encoder with a microcontroller. | ||
* | ||
* @tparam PinA First modm::platform::Gpio pin to output the encoder signal. | ||
* @tparam PinB Second modm::platform::Gpio pin to output the encoder signal. | ||
* @tparam PositionType Data type (integer) of the position value, default `int32_t`. | ||
* @tparam PeriodicTimer Defaults to `modm::PeriodicTimer`, must be replaced for | ||
* encoder frequencies above 1kHz by a class that offers | ||
* time steps less than 1ms, e.g. `modm::PrecisePeriodicTimer`. | ||
* @tparam period Timebase for the output signal. This limits the maximal | ||
* frequency of the encoder signal. Defaults to `1`. | ||
*/ | ||
template < | ||
class PinA, | ||
class PinB, | ||
typename PositionType = int32_t, | ||
class PeriodicTimer = modm::PeriodicTimer, | ||
uint32_t period = 1 | ||
> | ||
class EncoderOutput { | ||
public: | ||
/** | ||
* @brief Initializes the Encoder. | ||
* | ||
* Sets pins (PinA and PinB) as output pins. | ||
* | ||
* @param initialValue for the encoder. Useful with unsigned PositionType | ||
* data types. Defaults to 0. | ||
*/ | ||
EncoderOutput(PositionType initialValue); | ||
|
||
/** | ||
* @brief Update method. Generates the encoder signal. | ||
* | ||
* Call this function in your main loop. | ||
*/ | ||
void update(); | ||
|
||
/** | ||
* @brief Set the position for the encoder. | ||
* @param position The position setpoint for the encoder. | ||
*/ | ||
void setPosition(PositionType position) { setpoint = position; }; | ||
|
||
private: | ||
PositionType setpoint; | ||
PositionType actualValue; | ||
PeriodicTimer timer; | ||
enum class State : uint8_t { | ||
State0, | ||
State1, | ||
State2, | ||
State3, | ||
}; | ||
State state; | ||
}; | ||
|
||
} | ||
|
||
#include "encoder_output_impl.hpp" | ||
|
||
#endif |
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,33 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2019, Raphael Lehmann | ||
# | ||
# 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.parent = "driver" | ||
module.name = "encoder.output" | ||
module.description = """\ | ||
# Encoder Output | ||
This driver generates a AB (incremental) encoder signal to | ||
emulate a hardware encoder with a microcontroller. | ||
""" | ||
|
||
def prepare(module, options): | ||
module.depends( | ||
":architecture:gpio", | ||
":processing:timer") | ||
return True | ||
|
||
def build(env): | ||
env.outbasepath = "modm/src/modm/driver/encoder" | ||
env.copy("encoder_output.hpp") | ||
env.copy("encoder_output_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,92 @@ | ||
// coding: utf-8 | ||
/* | ||
* Copyright (c) 2019, Raphael Lehmann | ||
* | ||
* 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_ENCODER_OUTPUT_HPP | ||
# error "Don't include this file directly, use 'encoder_output.hpp' instead!" | ||
#endif | ||
#include "encoder_output.hpp" | ||
|
||
template < | ||
class PinA, | ||
class PinB, | ||
typename PositionType, | ||
class PeriodicTimer, | ||
uint32_t period | ||
> | ||
modm::EncoderOutput<PinA, PinB, PositionType, PeriodicTimer, period>::EncoderOutput(PositionType initialValue) : | ||
setpoint(initialValue), actualValue(initialValue), timer(period), state(State::State0) | ||
{ | ||
PinA::setOutput(); | ||
PinB::setOutput(); | ||
PinA::set(); | ||
PinB::set(); | ||
setpoint = initialValue; | ||
actualValue = initialValue; | ||
} | ||
|
||
template < | ||
class PinA, | ||
class PinB, | ||
typename PositionType, | ||
class PeriodicTimer, | ||
uint32_t period | ||
> | ||
void | ||
modm::EncoderOutput<PinA, PinB, PositionType, PeriodicTimer, period>::update() | ||
{ | ||
if(timer.execute()){ | ||
if(setpoint > actualValue) { | ||
// generate forward tick | ||
switch(state) { | ||
case State::State0: | ||
PinA::reset(); | ||
state = State::State1; | ||
break; | ||
case State::State1: | ||
PinB::reset(); | ||
state = State::State2; | ||
break; | ||
case State::State2: | ||
PinA::set(); | ||
state = State::State3; | ||
break; | ||
case State::State3: | ||
PinB::set(); | ||
state = State::State0; | ||
break; | ||
} | ||
actualValue++; | ||
} | ||
else if(setpoint < actualValue) { | ||
// generate backward tick | ||
switch(state) { | ||
case State::State0: | ||
PinB::reset(); | ||
state = State::State3; | ||
break; | ||
case State::State1: | ||
PinA::reset(); | ||
state = State::State0; | ||
break; | ||
case State::State2: | ||
PinB::set(); | ||
state = State::State1; | ||
break; | ||
case State::State3: | ||
PinA::set(); | ||
state = State::State2; | ||
break; | ||
} | ||
actualValue--; | ||
} | ||
} | ||
} |