-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[driver] Adding AS5047 driver and example #1138
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,157 @@ | ||
// 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 | ||
{ | ||
|
||
/// @cond | ||
namespace detail | ||
{ | ||
constexpr uint16_t | ||
as5047_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); | ||
} | ||
} // namespace detail | ||
/// @endcond | ||
|
||
/// @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 = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x0000)), | ||
ReadErrfl = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x0001)), | ||
ReadProg = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x0003)), | ||
ReadDiaagc = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x3FFC)), | ||
ReadMag = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x3FFD)), | ||
ReadAngleunc = detail::as5047_setMsbToEvenParity(((1 << 14) | 0x3FFE)), | ||
ReadAnglecom = detail::as5047_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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the modm namespace which is quite generic. Would be good to move to
struct as5047
or put into the relevant:math
module.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure, how to move it into the struct?
If I declare `setMsbToEvenParity` as `static constexpr` in the `as5047` struct, I get an error saying `error: 'static constexpr uint16_t modm::as5047::setMsbToEvenParity(uint16_t)' used before its definition`
I think this might be related to this:
Do you have any suggestions?
Should I put everything into an
as5047
namespace?I don't think moving the function as is to the
math:
is appropriate, since the function specifically calculates and sets bit 15 of auint16_t
, if moved tomath:
, a more generic version might be more appropriate. Also, other modm modules likeetlcpp
have their own functions to calculate parity bits...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, C++ amiright!1!!
You can put it into a
modm::details
namespace then and use/// @cond
and/// @endcond
to block doxypress from indexing it (Examples). Prolly prefix the function withas5047_
to avoid collisions if you wanna be super careful.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐈 No we are super careful 🐱