Register / Login  |  Desktop view  |  Jump to bottom of page

IoAbstraction & TaskManagerIO » Encoder Rollover Possible?

Author: milesg
05/11/2021 23:01:54
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.

Author: davetcc
09/11/2021 08:21:06
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

Author: milesg
09/11/2021 17:36:18
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!

Author: milesg
22/11/2021 18:37:05
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
Description No description given
Filesize 9 Kbytes
Downloaded 1246 time(s)
[Disk] Download


Author: davetcc
23/11/2021 09:02:46
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.

Author: davetcc
26/11/2021 07:58:03
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.

Author: davetcc
30/11/2021 09:50:07
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.

Author: milesg
30/11/2021 17:11:27
Very cool. No hurry on my end, but glad it's working out so far.

Thank you so much!


Author: davetcc
02/12/2021 10:57:46
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.




Register / Login  |  Desktop view  |  Jump to top of page