mnh86 / NimbleTCodeSerial

Firmware that implements the Toy Code (T-Code) v0.3 protocol over USB/Serial Port for the Nimble Connectivity Module, an ESP32 controller for the NimbleStroker.
https://twitter.com/mnh867
GNU General Public License v3.0
9 stars 0 forks source link

Confused about A0 Axis Behavior in MFP #1

Open MardukVII opened 1 year ago

MardukVII commented 1 year ago

Awesome firmware firstly, I've been getting a lot of enjoyment out of it!

What I'm trying to do is hotkey the air valves by creating shortcuts. The MFP program by default maps this to a %. If I set one key to put it at 0%, another 50% and a third at 100%, I would think 0 would open air out fully, 50 would close the valves, and 100 would open air in fully, but it doesn't seem to be working that way.

-For one, the axis doesn't immediately go to the target value, it motion smooths it so i have to set the action to last 4+ seconds.

-When i press the key to go to 0%, after a brief delay i can hear the valve click open and can hear escaping because i can hear the whine it does normally

-When the action times out and returns to home position (50%), or i press the key to manual set to 50%, i still hear the valve whining like it's not fully closed.

So % values seem to work fine for L0, but i'm struggling to figure out what to set A0 to in order for it to work properly.

Thanks for your time.

mnh86 commented 1 year ago

Hi, I'm glad to hear you're getting use out of it! I haven't tested controlling A0 from MFP, but lets walk through the A0 axis logic in this firmware's code and see what might be an issue...

  1. Initial setup - Register A0 and reset state to default value (5000): https://github.com/mnh86/NimbleTCodeSerial/blob/main/include/NimbleTCode.h#L68C1-L69

    tcode->axisRegister("A0", F("Air")); // Air in/out valve
    tcode->axisWrite("A0", 5000, ' ', 0); // 0: air out, 5000: stop, 9999: air in

    Main thing here to note is that when the module is turned on, it sets the initial internal TCode value for A0 to 5000.

  2. TCode change handler - This checks if current A0 TCode input value is greater than, less than, or equal to the "stop" value 5000: https://github.com/mnh86/NimbleTCodeSerial/blob/main/include/NimbleTCode.h#L139-L151

    if (!tcode->axisChanged("A0")) return;
    int val = tcode->axisRead("A0");
    
    if (val < 5000) {
        frame.air = -1;
    } else if (val > 5000) {
        frame.air = 1;
    } else {
        frame.air = 0;
    }

    Here, whenever the TCode serial input changes, the firmware will map the TCode value (0-9999) to three distinct states (-1: air out, 0: closed, 1: air in).

  3. During a NimbleStroker Actuator update frame (which is a separate schedule than input data reads), set the actuator frame data with the current state: https://github.com/mnh86/NimbleTCodeSerial/blob/main/include/NimbleTCode.h#L170-L171

            actuator.airIn = (frame.air > 0);
            actuator.airOut = (frame.air < 0);
            ...
            sendToAct();
  4. Within the nimbleConModule SDK, the airIn/airOut boolean values are converted to a bit-packed statusByte and sent to the actuator over serial as an outgoingPacket (according to its spec): https://github.com/mnh86/NimbleTCodeSerial/blob/main/include/nimbleConModule.h#L245-L246

    statusByte |= actuator.airOut << 1;
    statusByte |= actuator.airIn << 2;

Some thoughts:

  1. Could something be off with MFP's method of using percentages? Where it may not return to the exact 5000 value required in the current firmware code to turn off the valve? Could you debug from MFP's side of things what value it uses when it goes back to 50%? Perhaps to work around MFP's percentages the mapping in step 2 above could be changed to be more forgiving, like: 0-3333 = out, 3334-6666 = off, and 6667-9999 = in.
  2. When it returns to home position but the valve does not seem fully closed, try clicking the module's encoder button to toggle reset of the internal firmware state, to see if you hear the valve close. This may help narrow down the issue between the TCode values and firmware. https://github.com/mnh86/NimbleTCodeSerial/blob/main/src/main.cpp#L19-L22
MardukVII commented 1 year ago

Yeah I think I will do some testing. I can setup some shortcut keys to move the axis at 10% increments and document at which points the valves click open and closed, then narrow it down farther from there to 5% increments etc. Might be a bit before I get a chance to test again tho.

MardukVII commented 1 year ago

some quick testing today: -valve will click open and start squeaking with as little as 5% adjustment of the slider from 50%-45% -valve does not click when returning to 50% and squeaking continues -valve will click closed and stop squeaking when going from 45%-55%

I will adjust my funscript to home at 50% then every 5min or so adjust to 40% for a few seconds before ramping back up to 55% and settling back at 50% and report back after my next session!

mnh86 commented 1 year ago

Can give this code change a try in branch fix-A0-ranges.

PR #2

MardukVII commented 1 year ago

Can give this code change a try in branch fix-A0-ranges.

PR #2

cloned and built. Will have some time to test tomorrow :)

MardukVII commented 1 year ago

Can give this code change a try in branch fix-A0-ranges.

PR #2

Sorry this took so long, life and all...

So tried out this branch with my funscript, and happy to report that it works very well! My script does home at 50%, then drops to 30% for a few sec to open the valve, then up to 60% for 1 sec to close it before homing back to 50%. I noticed NO squeaking after the valve closed this time.

Should I have been testing using 0%-100% though?

mnh86 commented 1 year ago

Glad that PR helped.

Should I have been testing using 0%-100% though?

Yes, I would. Mapping the MFP percentages to the TCode values would be:

Worried that if you use 60% it may not be doing "air in" (if that was the expectation).

I'd use values: 0% for air out, 100% for air in, and 50% for valve off