Skip to content

Commit

Permalink
added example for blue-pill, improved example for arduino-nano
Browse files Browse the repository at this point in the history
  • Loading branch information
TomSaw committed Nov 15, 2021
1 parent 7e7d2e9 commit d46c09d
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/driver/encoder/bitbang_encoder_input.hpp>
#include <modm/math/algorithm/prescaler.hpp>
#include <modm/processing/timer.hpp>
#include <modm/math/algorithm/prescaler.hpp>
#include <modm/driver/encoder/bitbang_encoder_input.hpp>

using namespace modm::platform;

// Connect the encoders outputs to D7 and D8 Pins (usually the outer pins)
// The common third pin (usually in the middle) is connected to GND.
// Don't add any resistors or filters. It's all in the MCU and the driver.
modm::BitBangEncoderInput<Board::D11, Board::D12, 4> encoder;

MODM_ISR(TIMER2_COMPA)
Expand Down Expand Up @@ -48,22 +47,19 @@ main()
Board::initialize();
LedD13::setOutput();

encoder.connect();

encoder.initialize();
init_Timer2();
enableInterrupts();

int value(0);

modm::ShortPeriodicTimer heartbeat(500ms);
modm::ShortPeriodicTimer outputValue(1000ms);

while (true)
{
if (heartbeat.execute()) Board::LedD13::toggle();
if (outputValue.execute())
{
value += encoder.getIncrement();
if (heartbeat.execute()) {
Board::LedD13::toggle();
value += encoder.getDelta();
MODM_LOG_INFO << "value: " << value << modm::endl;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<library>
<extends>modm:arduino-nano</extends>
<options>
<option name="modm:build:build.path">../../../build/arduino_nano/encoder_input</option>
<option name="modm:build:build.path">../../../build/arduino_nano/encoder_input_bitbang</option>
</options>
<modules>
<module>modm:build:scons</module>
Expand Down
74 changes: 74 additions & 0 deletions examples/blue_pill_f103/encoder_input/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/debug/logger.hpp>
#include <modm/processing/timer.hpp>
#include <modm/driver/encoder/encoder_input.hpp>

// ----------------------------------------------------------------------------
// Set the log level
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::INFO

// Create an IODeviceWrapper around the Uart Peripheral we want to use
modm::IODeviceWrapper< Usart2, modm::IOBuffer::BlockIfFull > loggerDevice;

// Set all four logger streams to use the UART
modm::log::Logger modm::log::debug(loggerDevice);
modm::log::Logger modm::log::info(loggerDevice);
modm::log::Logger modm::log::warning(loggerDevice);
modm::log::Logger modm::log::error(loggerDevice);

int
main()
{
Board::initialize();

Usart2::connect<GpioOutputA2::Tx>();
Usart2::initialize<Board::SystemClock, 115200_Bd>();

// Each Timer can drive one Encoder
// For Timer2 and Timer3 you have 2 Gpio options
// When using one of PB3 or PB4 you also have to disable JTAG debugging during shared pins

// Timer1:
modm::EncoderInput<Timer1, GpioInputA8, GpioInputA9> encoder;

// Timer2:
// modm::EncoderInput<Timer2, GpioInputA0, GpioInputA1> encoder;
// modm::EncoderInput<Timer2, GpioInputA15, GpioInputB3> encoder;
// AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE;

// Timer3:
// modm::EncoderInput<Timer3, GpioInputA6, GpioInputA7> encoder;
// modm::EncoderInput<Timer3, GpioInputB4, GpioInputB5> encoder;
// Disable JTAG to make PB3, PB4 available as Gpio
// AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE;

// Timer4:
// modm::EncoderInput<Timer4, GpioInputB6, GpioInputB7> encoder;

encoder.initialize(true);

modm::ShortPeriodicTimer heartbeat(500ms);

while (true)
{
if(heartbeat.execute()) {
Board::LedGreen::toggle();
MODM_LOG_INFO << "Encoder Delta: " << encoder.getDelta() << modm::endl;
MODM_LOG_INFO << "Encoder Absolut: " << encoder.getValue() << modm::endl;
}
}

return 0;
}
2 changes: 2 additions & 0 deletions examples/blue_pill_f103/encoder_input/openocd.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Replace this with your custom programmer
source [find interface/stlink-v2.cfg]
18 changes: 18 additions & 0 deletions examples/blue_pill_f103/encoder_input/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<library>
<extends>modm:blue-pill-f103</extends>
<options>
<option name="modm:build:build.path">../../../build/blue_pill_f103/encoder_input</option>
<option name="modm:build:openocd.cfg">openocd.cfg</option>
</options>
<modules>
<module>modm:debug</module>
<module>modm:platform:uart:2</module>
<module>modm:processing:timer</module>
<module>modm:driver:encoder_input</module>
<module>modm:platform:timer:1</module>
<module>modm:platform:timer:2</module>
<module>modm:platform:timer:3</module>
<module>modm:platform:timer:4</module>
<module>modm:build:scons</module>
</modules>
</library>
82 changes: 82 additions & 0 deletions examples/blue_pill_f103/encoder_input_bitbang/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/debug/logger.hpp>
#include <modm/processing/timer.hpp>
#include <modm/driver/encoder/bitbang_encoder_input.hpp>

// ----------------------------------------------------------------------------
// Set the log level
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::INFO

// Create an IODeviceWrapper around the Uart Peripheral we want to use
modm::IODeviceWrapper< Usart2, modm::IOBuffer::BlockIfFull > loggerDevice;

// Set all four logger streams to use the UART
modm::log::Logger modm::log::debug(loggerDevice);
modm::log::Logger modm::log::info(loggerDevice);
modm::log::Logger modm::log::warning(loggerDevice);
modm::log::Logger modm::log::error(loggerDevice);

// Connect the encoders outputs to D7 and D8 Pins (usually the outer pins)
// The common third pin (usually in the middle) is connected to GND.
modm::BitBangEncoderInput<GpioInputB6, GpioInputB7, 4, int16_t> encoder;

MODM_ISR(TIM2)
{
Timer2::acknowledgeInterruptFlags(Timer2::InterruptFlag::Update);
encoder.update();
}

void
init_Timer2(const uint16_t period)
{
Timer2::enable();
Timer2::setMode(Timer2::Mode::UpCounter);

Timer2::template setPeriod<Board::SystemClock>(period);
Timer2::enableInterruptVector(true, 10);
Timer2::enableInterrupt(Timer2::Interrupt::Update);

Timer2::applyAndReset();
Timer2::start();
}

int
main()
{
Board::initialize();

Usart2::connect<GpioOutputA2::Tx>();
Usart2::initialize<Board::SystemClock, 115200_Bd>();

encoder.initialize();
init_Timer2(1000); // 1ms period

int value(0);

modm::ShortPeriodicTimer heartbeat(1s);

while (true)
{
if (heartbeat.execute()) {
Board::LedGreen::toggle();

const auto delta = encoder.getDelta();
MODM_LOG_INFO << "Delta: " << delta << modm::endl;

value += delta;
MODM_LOG_INFO << "Encoder Absolut: " << value << modm::endl;
}
}
}
2 changes: 2 additions & 0 deletions examples/blue_pill_f103/encoder_input_bitbang/openocd.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Replace this with your custom programmer
source [find interface/stlink-v2.cfg]
15 changes: 15 additions & 0 deletions examples/blue_pill_f103/encoder_input_bitbang/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<library>
<extends>modm:blue-pill-f103</extends>
<options>
<option name="modm:build:build.path">../../../build/blue_pill_f103/encoder_input_bitbang</option>
<option name="modm:build:openocd.cfg">openocd.cfg</option>
</options>
<modules>
<module>modm:debug</module>
<module>modm:platform:uart:2</module>
<module>modm:processing:timer</module>
<module>modm:driver:encoder_input.bitbang</module>
<module>modm:platform:timer:2</module>
<module>modm:build:scons</module>
</modules>
</library>

0 comments on commit d46c09d

Please sign in to comment.