[Logo] TCC discussion forum
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Top Downloads] Top Downloads   [Groups] Back to home page 
[Register] Register /  [Login] Login 

Encoder Rollover Possible? RSS feed
Forum Index » IoAbstraction & TaskManagerIO
Author Message
milesg


Joined: Oct 8, 2021
Messages: 11
Offline
Hello!
I'm looking to set the angle of a high-end servo motor with an encoder I have setup through IOAbstraction.

I am controlling this angle in 1/10degree increments, so I use the following line to setup the encoder when I want to adjust that value:

switches.changeEncoderPrecision(3599, rotationTarget);

I would like to setup some code so that when I rotate up and my encoder's rotationTarget is already at 3599 it would go back to 0 (likewise, go to 3599 when going down from 0), but since there is no newValue produced in my onEncoderChanged() function when I'm already at either limit, I can't seem to figure out how to call switches.getEncoder()->setCurrentReading(*0 or 3599*) at the right time.

Alternately I was using the DIRECTION_ONLY option and I was handling the incrementing/limits in my own code, but that way I lose the acceleration function which I can't really live without when dealing with such large numbers.

Any suggestions? Thank you very much.
davetcc


Joined: Jan 19, 2019
Messages: 542
Offline
EDIT:

Are you planning to use IoAbstraction to handle some kind of encoder attached to the stepper motor? Because, if I'm honest, I don't think that the encoder support is designed to do what you want with it. It is designed for regular manual adjustments in menu systems, joysticks, and other similar uses. It does not guarantee to capture every single detent. For example in the event of a long-running task, a tick may be missed. For most intended cases this doesn't matter, but it would really matter when counting out degrees of movement.

However, if you are just using it to set the degrees from a menu system, you can raise an issue in IoAbstraction but honestly, unless prioritized it is unlikely to be done as it is not really needed by tcMenu.

https://github.com/davetcc/IoAbstraction/issues
milesg


Joined: Oct 8, 2021
Messages: 11
Offline
Ah, sorry I wasn't clear... The servo motor counting/positioning will be handled by the servo-amp. Those counts don't need to be handled by IOAbstraction.

I have a regular 15 PPR quadrature encoder on my interface panel that I'm using to set the target angle. That angle setting is eventually to be sent to the servo-amp via serial commands. I just wanted to be able to rollover that setting since I'm allowing for a full 360.

It sounds like there's no way to do it currently. I thought maybe you'd have some shortcut that I was overlooking. It still seems like a useful feature in TCMenu; Any menu item that has a large value range could benefit from rolling over to save time for the user when making changes. I'll go ahead and raise the issue, but as you suggested, I won't expect a quick update. However, thank you very much for your help anyway, your libraries are such great tools!
milesg


Joined: Oct 8, 2021
Messages: 11
Offline
Hey all,
I had some time to tinker over the weekend and I just thought I'd share the solution I cobbled into IOAbstraction:

I added a "rollover" parameter to the changeEncoderPrecision function (this seemed the most logical place for it.)

Then, I just needed some small changes in the increment function in SwitchInput.cpp:

void RotaryEncoder::increment(int8_t incVal) {
    // first check if we are in direction only mode (max = 0)
    if(maximumValue == 0) {
        callback(incVal);
        return;
    }
	// otherwise run through all the possibilities
	if(incVal >= 0) {
		currentReading = rollover?currentReading+=incVal:min((uint16_t)(currentReading + incVal), maximumValue);
		if (currentReading>maximumValue) currentReading=currentReading-maximumValue-1;
		callback(currentReading);
	}
	else if(currentReading < abs(incVal)) {
		currentReading = rollover?maximumValue-abs(incVal)+1:0;
		callback(currentReading);
	}
	else if(currentReading != 0) {
		currentReading += incVal; 
		callback(currentReading);
	}
}


A smattering of updates to make the prototype functions work when not using the new parameter in SwitchInput.h and SwitchInput.cpp finished up the modification.

I'm certainly no pro when it comes to C++ but this seems to work and none of my existing encoder/tcmenu programs stopped working after this mod. Hope it helps someone if they need it.
 Filename rollover.zip [Disk] Download
 Description No description given
 Filesize 9 Kbytes
 Downloaded:  797 time(s)

davetcc


Joined: Jan 19, 2019
Messages: 542
Offline
Many thanks for this, I've got to do a release of IoAbstraction soon anyway, to pick up some changes required for platformIO, so I'll incorporate this change at the same time.
davetcc


Joined: Jan 19, 2019
Messages: 542
Offline
I've started integrating that change now. Hopefully, I'll get an hour or two over the weekend and get it committed.

Thanks again for the code.
davetcc


Joined: Jan 19, 2019
Messages: 542
Offline
Just remembered that I had not updated here, I checked the change into github master a day or two back, after testing it on a SAMD board. I've got another couple of small changes for ESP32 to go in before the next release.
milesg


Joined: Oct 8, 2021
Messages: 11
Offline
Very cool. No hurry on my end, but glad it's working out so far.

Thank you so much!

davetcc


Joined: Jan 19, 2019
Messages: 542
Offline
Actually, after trying it with tcMenu it integrates really well, so I'll be making it possible in menuMgr to enable it globally, and override it on a per item where it isn't a good fit. I'll probably release a patch of tcMenuLib at the same time.
 
Forum Index » IoAbstraction & TaskManagerIO
Go to:   
Mobile view
Powered by JForum 2.7.0 © 2020 JForum Team • Maintained by Andowson Chang and Ulf Dittmer

This site uses cookies to analyse traffic, serve ads by Google AdSense (non-personalized in EEA/UK), and to record consent. We also embed Twitter, Youtube and Disqus content on some pages, these companies have their own privacy policies.

Our privacy policy applies to all pages on our site

Should you need further guidance on how to proceed: External link for information about cookie management.