-
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] Adding AS5047 driver and example
- Loading branch information
Showing
6 changed files
with
346 additions
and
14 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,79 @@ | ||
// coding: utf-8 | ||
/* | ||
* Copyright (c) 2024, Henrik Hose | ||
* | ||
* 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/driver/encoder/as5047.hpp> | ||
#include <modm/processing.hpp> | ||
|
||
using SpiMaster = SpiMaster1; | ||
|
||
using Cs = modm::platform::GpioB10; | ||
using Mosi = modm::platform::GpioB5; | ||
using Miso = modm::platform::GpioB4; | ||
using Sck = modm::platform::GpioB3; | ||
|
||
using namespace Board; | ||
using namespace modm::literals; | ||
|
||
class EncoderThread : public modm::pt::Protothread | ||
{ | ||
public: | ||
EncoderThread() : encoder(data) {} | ||
|
||
bool | ||
run() | ||
{ | ||
PT_BEGIN(); | ||
|
||
while (true) | ||
{ | ||
PT_CALL(encoder.readout()); | ||
|
||
MODM_LOG_INFO << "\nNew readout:" << modm::endl; | ||
MODM_LOG_INFO << " angle degree: " << data.getAngleDeg() << " degrees" << modm::endl; | ||
MODM_LOG_INFO << " angle rad: " << data.getAngleRad() << " radians" << modm::endl; | ||
MODM_LOG_INFO << " angle raw: " << data.getAngleRaw() << modm::endl; | ||
|
||
timeout.restart(std::chrono::milliseconds(500)); | ||
PT_WAIT_UNTIL(timeout.isExpired()); | ||
} | ||
|
||
PT_END(); | ||
} | ||
|
||
private: | ||
modm::as5047::Data data; | ||
modm::As5047<SpiMaster, Cs> encoder; | ||
|
||
modm::ShortTimeout timeout; | ||
} encoderThread; | ||
|
||
int | ||
main() | ||
{ | ||
Board::initialize(); | ||
|
||
Cs::setOutput(modm::Gpio::High); | ||
|
||
SpiMaster::connect<Miso::Miso, Mosi::Mosi, Sck::Sck>(); | ||
SpiMaster::initialize<Board::SystemClock, 1.3_MHz>(); | ||
|
||
MODM_LOG_INFO << "==========AS5047 Test==========" << modm::endl; | ||
MODM_LOG_DEBUG << "Debug logging here" << modm::endl; | ||
MODM_LOG_INFO << "Info logging here" << modm::endl; | ||
MODM_LOG_WARNING << "Warning logging here" << modm::endl; | ||
MODM_LOG_ERROR << "Error logging here" << modm::endl; | ||
MODM_LOG_INFO << "===============================" << modm::endl; | ||
|
||
while (true) { encoderThread.run(); } | ||
|
||
return 0; | ||
} |
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,14 @@ | ||
<library> | ||
<extends>modm:nucleo-g474re</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../build/nucleo_g474re/as5047</option> | ||
</options> | ||
<modules> | ||
<module>modm:driver:as5047</module> | ||
<module>modm:platform:gpio</module> | ||
<module>modm:platform:spi:1</module> | ||
<module>modm:processing:protothread</module> | ||
<module>modm:processing:timer</module> | ||
<module>modm:build:scons</module> | ||
</modules> | ||
</library> |
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,152 @@ | ||
// coding: utf-8 | ||
// ---------------------------------------------------------------------------- | ||
/* | ||
* Copyright (c) 2024, Henrik Hose | ||
* | ||
* 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_AS5047_HPP | ||
#define MODM_AS5047_HPP | ||
|
||
#include <array> | ||
#include <modm/architecture/interface/register.hpp> | ||
#include <modm/architecture/interface/spi_device.hpp> | ||
#include <modm/processing/resumable.hpp> | ||
#include <modm/processing/timer.hpp> | ||
#include <numbers> | ||
|
||
namespace modm | ||
{ | ||
|
||
constexpr uint16_t | ||
setMsbToEvenParity(const uint16_t num) | ||
{ | ||
uint16_t par = 0x7fff & num; | ||
par ^= par >> 8; | ||
par ^= par >> 4; | ||
par ^= par >> 2; | ||
par ^= par >> 1; | ||
return ((par & 1) << 15) | (0x7fff & num); | ||
} | ||
|
||
/// @ingroup modm_driver_as5047 | ||
struct as5047 | ||
{ | ||
enum class Errorfl : uint16_t | ||
{ | ||
Parerr = Bit2, | ||
Invcomm = Bit1, | ||
Frerr = Bit0, | ||
}; | ||
MODM_FLAGS16(Errorfl) | ||
|
||
enum class Prog : uint16_t | ||
{ | ||
Progver = Bit6, | ||
Progotp = Bit3, | ||
Otpref = Bit2, | ||
Progen = Bit0, | ||
}; | ||
MODM_FLAGS16(Prog) | ||
|
||
enum class Diaagc : uint16_t | ||
{ | ||
Magl = Bit11, | ||
Magh = Bit10, | ||
Cof = Bit9, | ||
Lf = Bit8, | ||
}; | ||
MODM_FLAGS16(Diaagc) | ||
|
||
enum class Register : uint16_t | ||
{ | ||
ReadNop = setMsbToEvenParity(((1 << 14) | 0x0000)), | ||
ReadErrfl = setMsbToEvenParity(((1 << 14) | 0x0001)), | ||
ReadProg = setMsbToEvenParity(((1 << 14) | 0x0003)), | ||
ReadDiaagc = setMsbToEvenParity(((1 << 14) | 0x3FFC)), | ||
ReadMag = setMsbToEvenParity(((1 << 14) | 0x3FFD)), | ||
ReadAngleunc = setMsbToEvenParity(((1 << 14) | 0x3FFE)), | ||
ReadAnglecom = setMsbToEvenParity(((1 << 14) | 0x3FFF)), | ||
|
||
}; | ||
|
||
struct modm_packed Data | ||
{ | ||
/// @return | ||
constexpr float | ||
getAngleRad() const | ||
{ | ||
const uint16_t angle = data & 0x3fff; | ||
return static_cast<float>(angle) / 16383.f * 2.f * std::numbers::pi_v<float>; | ||
} | ||
|
||
/// @return | ||
constexpr float | ||
getAngleDeg() const | ||
{ | ||
const uint16_t angle = data & 0x3fff; | ||
return static_cast<float>(angle) / 16383.f * 360.f; | ||
} | ||
|
||
/// @return | ||
constexpr uint16_t | ||
getAngleRaw() const | ||
{ | ||
const uint16_t angle = data & 0x3fff; | ||
return angle; | ||
} | ||
|
||
uint16_t data; | ||
}; | ||
}; // struct as5047 | ||
|
||
/** | ||
* @tparam SpiMaster | ||
* @tparam Cs | ||
* | ||
* @author Henrik Hose | ||
* @ingroup modm_driver_as5047 | ||
*/ | ||
template<typename SpiMaster, typename Cs> | ||
class As5047 : public as5047, public modm::SpiDevice<SpiMaster>, protected modm::NestedResumable<5> | ||
{ | ||
public: | ||
using Data = as5047::Data; | ||
|
||
/** | ||
* @param data pointer to buffer of the internal data of type Data | ||
*/ | ||
As5047(Data &data); | ||
|
||
/// Call this function once before using the device | ||
modm::ResumableResult<void> | ||
initialize(); | ||
|
||
/// Read the raw data from the sensor | ||
modm::ResumableResult<void> | ||
readout(); | ||
|
||
/// Get the data object for this sensor | ||
inline Data & | ||
getData() | ||
{ | ||
return data; | ||
} | ||
|
||
private: | ||
Data &data; | ||
uint8_t inBuffer[2]; | ||
uint8_t outBuffer[2]; | ||
}; | ||
|
||
} // namespace modm | ||
|
||
#include "as5047_impl.hpp" | ||
|
||
#endif // MODM_AS5047_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) 2024, Henrik Hose | ||
# | ||
# 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:as5047" | ||
module.description = """\ | ||
# AS5047 14 bit Absolute Encoder SPI Driver | ||
[Datasheet](https://ams.com/documents/20143/36005/AS5047D_DS000394_2-00.pdf) | ||
""" | ||
|
||
def prepare(module, options): | ||
module.depends( | ||
":architecture:gpio", | ||
":architecture:spi.device", | ||
":processing:resumable" | ||
) | ||
return True | ||
|
||
def build(env): | ||
env.outbasepath = "modm/src/modm/driver/encoder" | ||
env.copy("as5047.hpp") | ||
env.copy("as5047_impl.hpp") |
Oops, something went wrong.