Skip to content
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

QMC5883L support for hmc5883l.c module (#2187) #2188

Closed
wants to merge 1 commit into from

Conversation

infrat
Copy link

@infrat infrat commented Nov 29, 2017

Fixes #2187 .

  • This PR is for the dev branch rather than for master.
  • This PR is compliant with the other contributing guidelines as well (if not, please describe why).
  • I have thoroughly tested my contribution.
  • The code changes are reflected in the documentation at docs/en/*.

QMC5883L chip support introduced to hmc5883l.c library. Unfortunately, I don't have magnetometer based on HMC5883L chip to test it. My changes are tested ONLY with QMC5883L!

uint32_t sda;
uint32_t scl;

platform_print_deprecation_note("hmc5883l.init() is replaced by hmc5883l.setup()", "in the next version");
platform_print_deprecation_note("hmc5883l.init() is replacd by hmc5883l.setup()", "in the next version");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces a spelling mistake.

@@ -106,11 +176,22 @@ static int hmc5883_read(lua_State* L) {
return 3;
}

static int xmc5883_read(lua_State* L) {
uint8_t i2c_addr;
i2c_addr = xmc5883_identify();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces a delay on each call of read(). Why not save the i2c_addr in xmc5883_i2c_addr (as part of setup/init) and then just access the right address immediately.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was considering approach you suggested, but I wasn't really sure if nodemcu modules stores their states between each LUA command call - that's why I decided to determine chip type each time. But if it's possible to store such value, then I'll do it the way you suggest.

@infrat
Copy link
Author

infrat commented Nov 30, 2017

Changes commited

@devsaurus
Copy link
Member

I'd merge this one if there's confirmation that HMC5883L isn't broken. @pjsg maybe?

@infrat
Copy link
Author

infrat commented Dec 17, 2017

@devsaurus hold on for a moment, please. Few days ago I've found a minor issue with my change, when used with HMC/QMC disconnected from I2C bus. By default in such case it should throw error luaL_error(L, "device not found") but it doesn't happen (I suspect it's because module identification done here: https://github.com/infrat/nodemcu-firmware/blob/ff7bb2d525815ab65a56b03ec19776a3387f12d8/app/modules/hmc5883l.c#L55 )
After Christmas I should prepare fix for that.

It doesn't affect the library in way making it unusable, but it's a bug and I should fix it.

@pjsg
Copy link
Member

pjsg commented Dec 18, 2017

I don't have either device....

@edtamarin
Copy link

edtamarin commented Dec 22, 2017

I have used the HMC library from the latest master build successfully, and the "device not connected" error is thrown when the HMC is not on the bus. I have not tested this particular HMC build.

However, I had some additions to the module in mind, since, at least for the HMC chip, I think the module is too simple and can be made much better. I have opened #2206 for that and planned to make some changes over the Christmas holidays myself.

My idea is that for the HMC, and really for any compass module, it is good to have the library output the heading value, since the NodeMCU does not have built-in trigonometry and have more configuration options than just reading the raw data with some given presets.

Unfortunately, I only have the HMC chip so I can add features only for that at this time. I would like to hear your thoughts on this.

@marcelstoer marcelstoer mentioned this pull request Jan 8, 2018
@marcelstoer
Copy link
Member

A fundamental problem is that the module name is equal to the chip name. Hence, someone looking for support for QMC5883L would be disappointed because he/she would only see HMC5883L in the modules list. The same applies for search engines of course.

We can change the title on line 1 in hmc5883l.md and we can change the navigation item name to something like "hmc5883l / qmc5883l" (I suggest we do both) but you would still say hmc5883l.read() even if you run it against a QMC5883L. I'm not sure if we just have to live with that.

@infrat
Copy link
Author

infrat commented Jan 8, 2018

Yeah, I agree with you but in fact it's quite hard to deal with that. For the moment, I see two ways: we can do it as is done in this PR, or create separate library for QMC. The question is which is better? Even I can't say which is better to me. Both solutions has pros and cons.

Copy link
Member

@marcelstoer marcelstoer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change title in documentation and navigation item name as proposed at #2188 (comment)

@devsaurus
Copy link
Member

I studied the datasheets of the two chips and understand the headaches here better now. Both only share the same model number 5883 and the fact that they're magnetometers. Apart from these, register interface and operation modes differ. Initially I though that QMC5883 would be the bigger/newer/extended version of HMC5883 but no - they're different architectures IMO.

For the moment, I see two ways: we can do it as is done in this PR

I would avoid that for the reasons above.

or create separate library for QMC

Not my favorite solution but provides a better separation.

Third alternative (same topic as #1242 (comment)): Remove the chip-specific module and integrate HMC5883 and QMC5883 (and future ones) in a magnetometer module. Users derive objects from this class and run the setup() and read() methods on the object

hmc = magnetometer.hmc5883l(i2c_id)
newconfig = hmc:setup({MS=0, DO=4, MA=3, GN=1, MD=1}) -- normal measurement, 15Hz, 8 avg, single-shot mode
-- wait some time or wait for GPIO trigger by DRDY
x, y, z = hmc:read()

qmc = magnetometer.qmc5883l(i2c_id)

Implementations on C level would be quite specific to the chip type, same for the setup() arguments.

Pros

  • Single magnetometer module (or any other swift name)
  • Clear separation, no need to support multiple architectures with one piece of code
  • Extensible with further chip types
  • Enables consistent module doc

Cons

  • Additional command to derive an object
  • RAM usage for Lua object
  • All-or-nothing option, qmc5883 routines use flash memory even if you only need hmc eg.
  • Increased complexity on C level (slightly, IMO)

What do you think - would this third option be a feasible way out?

@infrat
Copy link
Author

infrat commented Jan 9, 2018

@devsaurus if you compare block diagrams of both chips - they're very similar. From architectural point of view there are not too much differences between them. They have different registers addressing, and probably clock is little bit different too. Sad thing is that many manufacturers use QMC and HMC interchangeably what makes customers little bit confused (like in my case).

Regarding module code, third option you suggested I like the most. It seems to be clear enough and straightforward. Do we have any modules using such architectural approach?

@devsaurus
Copy link
Member

Regarding module code, third option you suggested I like the most. It seems to be clear enough and straightforward. Do we have any modules using such architectural approach?

u8g and ucg are the only ones if I remember correctly, but they are quite complex to serve as a blueprint. Given there's consensus to head into this direction (@marcelstoer, @pjsg?) then I'd volunteer to set up such a module including current hmc5883l code. I don't intend to offload this maintenance effort to you 😃.

@marcelstoer marcelstoer dismissed their stale review January 9, 2018 21:16

we're heading into a different (better) direction

@infrat
Copy link
Author

infrat commented Jan 10, 2018

we're heading into a different (better) direction

OFC direction! because it's a magnetometer ;)

@devsaurus I think I can try to setup both modules in the way you suggested, so don't worry. I'd be really happy to have some influence on this project. At least, if I wouldn't manage I'll ask you for some help.
In this moment I'm focused on other ESP-related project, but hopefully I'll be able to find some time to do this soon. Just give me few weeks. I hope it's not really urgent?

@marcelstoer
Copy link
Member

Ok, then let's close this PR and either @infrat or @devsaurus will create a new one.

@devsaurus
Copy link
Member

I think I can try to setup both modules in the way you suggested

Thanks you very for your support! It's highly appreciated.

It's not urgent at all from my point of view. Feel free to come up even with a prototype if you look for early feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants