Skip to content

sim_encoder: fix negative velocity at the thread rate cap; spindle sims use sim_spindle - #4583

Open
grandixximo wants to merge 2 commits into
LinuxCNC:masterfrom
grandixximo:sim-encoder-4582
Open

grandixximo wants to merge 2 commits into
LinuxCNC:masterfrom
grandixximo:sim-encoder-4582

Conversation

@grandixximo

@grandixximo grandixximo commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4582.

Root cause

sim_encoder runs its pulse generator in the base thread, so the quadrature count rate (rpm/60 × ppr × 4) cannot exceed the base thread frequency. update_speed() clamped the requested frequency at that limit (maxf), which makes the frequency-generator add value exactly +2^31. make_pulses() uses bit 31 of addval as the direction bit, so any positive speed at or above the cap was misread as negative rotation: the simulated counts ran backwards and encoder.N.velocity went negative (12500 rpm with the stock ppr 12 and a 100000 ns base period). spindle.0.at-speed then never asserts and a program dwells forever.

Changes

  • sim_encoder: clamp the add value magnitude below 2^31 so capped speeds saturate in the correct direction instead of flipping sign; add a saturated output pin that is true while the cap is engaged (suggested by @rmu75); print a one-shot warning naming the maximum supported speed; document the pin and the cap formula in the man page.
  • configs: the gmoccapy mill and lathe sims and lathe_multispindle now simulate the spindle with sim_spindle, as basic_sim.tcl does, so they have no speed cap. lathe_multispindle also gets its own index-enable per spindle (spindle 1 was on encoder 0). The scara spindle_sim.hal is removed: no INI loads it and it still used the old motion.spindle-* pins.
  • panelui-demo and spartan keep sim_encoder: they simulate a parport machine whose encoder comes in on parport pins, so the quadrature signal is the point. With the fix they saturate at their cap instead of running backwards.

Testing

  • halrun harness on sim_encoder: positive speeds saturate at the cap, negative speeds stay negative, saturated toggles correctly in both directions, warning fires once.
  • Headless G33 on the three converted configs at 1000 and 20000 rpm: all passes complete with the right speed-in. On master the 20000 rpm pass never starts (speed-in -12500 rpm on the gmoccapy sims, -2500 rpm on lathe_multispindle).

@rmu75

rmu75 commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

didn't try it, but LGTM.

@grandixximo

Copy link
Copy Markdown
Contributor Author

@DauntlessAq could you give it a test?

@BsAtHome

Copy link
Copy Markdown
Contributor

On hold until 64-bit HAL merge. May need to change again.

@andypugh

Copy link
Copy Markdown
Collaborator

I think we should park this until after the 64-bit change.
(also, this component seems to mainly exist to disappoint people looking to simulate a spindle etc, when all it really is is a strangely limited stepgen)

@rmu75

rmu75 commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

remove and replace with stepgen in velocity mode?

@grandixximo
grandixximo marked this pull request as draft September 28, 2026 04:51
@grandixximo

Copy link
Copy Markdown
Contributor Author

Parked, agreed. I looked into sim_encoder against stepgen in velocity mode before answering.

stepgen can't replace it: its quadrature output has A and B but no index, and these sims need the index for index-enable and G33/G76. Its pulses also run in the base thread, so it would hit the same kind of speed limit.

The replacement that fits is sim_spindle, which most stock sims already get from basic_sim.tcl. I ran G33 and G76 headless on three chains from 1000 to 80000 rpm: master's sim_encoder values, this PR's lower ppr, and sim_spindle. sim_spindle threads with exact pitch and no jitter at every speed. Master's values never start a thread from 12500 rpm up, since at-speed never comes. This PR's values work to 60000 rpm but quantize 4 times coarser.

So after the 64-bit work I'd rework this PR: switch the six sims that use sim_encoder (gmoccapy mill, lathe and scara, lathe_multispindle, panelui-demo, gscreen spartan) to sim_spindle, and keep sim_encoder with its direction fix. It is the only source of a real quadrature signal with an index pulse in the tree, which is what you want for exercising the encoder component.

@grandixximo
grandixximo force-pushed the sim-encoder-4582 branch 2 times, most recently from eaa8738 to c4b331e Compare December 1, 2026 05:36
@grandixximo grandixximo changed the title sim_encoder: fix negative velocity at the thread rate cap, raise sim spindle speed limits sim_encoder: fix negative velocity at the thread rate cap; spindle sims use sim_spindle Dec 1, 2026
@grandixximo
grandixximo marked this pull request as ready for review December 1, 2026 05:36
@grandixximo

Copy link
Copy Markdown
Contributor Author

@andypugh unparked now that the 64-bit work is in, and reworked as discussed: the gmoccapy mill and lathe sims and lathe_multispindle now use sim_spindle, and the ppr changes are dropped. panelui-demo and spartan stay on sim_encoder, since they simulate a parport machine whose encoder comes in on parport pins. sim_encoder keeps the direction fix and the saturated pin. Headless, G33 at 20000 rpm now completes on all three converted configs; on master it never starts.

Comment on lines +442 to +447
/* export pin for speed cap indication */
retval = hal_pin_new_bool(comp_id, HAL_OUT, &(addr->saturated), 0,
"%s.saturated", prefix);
if (retval != 0) {
return retval;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Indenting

Comment thread src/hal/components/sim_encoder.c Outdated
Comment on lines 379 to 384
(unsigned long)hal_get_uint(sim_enc->ppr), periodns);
}
hal_set_bool(sim_enc->saturated, clamped);
sim_enc->addval = aval;
sim_enc++;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Indenting

The frequency generator add value is freq * 2^31/maxf, and make_pulses()
uses bit 31 of addval as the direction bit.  The old freq clamp at
+/-maxf allowed addval to reach exactly +2^31, setting bit 31, so a
commanded speed at or above the cap was misread as negative rotation:
the simulated counts ran backwards and encoder.N.velocity went negative
(e.g. at >= 12500 rpm with ppr 12 and a 100000 ns base thread).  With a
spindle-at-speed interlock this makes a program dwell forever.

Clamp the add value magnitude to less than 2^31 instead, so the outputs
now keep turning in the commanded direction at the maximum rate.

Also add a 'saturated' output pin that is true while the cap is engaged,
and print a one-shot warning naming the maximum supported speed.
The gmoccapy mill and lathe sims and lathe_multispindle simulated the spindle with sim_encoder pulses counted by the encoder component in the base thread. That count rate caps at 12500 rpm with the stock settings; above it speed-in went wrong, at-speed never came and G33 never started. sim_spindle gives position and index directly with no speed cap, as basic_sim.tcl already does for most sims.

lathe_multispindle also put spindle 1's index-enable on encoder 0; each spindle now has its own.

The scara spindle_sim.hal is removed: no INI loads it and it still used the old motion.spindle-* pins.

Tested headless on the three configs: G33 at 1000 and 20000 rpm completes with the right speed-in; on master the 20000 rpm pass never starts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SIM: negative spindle speed with simulated encoder

4 participants