Timers, interrupts and other such delights

I have a mechatronic device which has a couple of ‘jog’ buttons, these are working nicely on a interrupt. The interrupt code fires on level change then checks the value - corresponding code sections send a command to the PRU via /dev/rpmsg_pru31.

Part of the command sent is a numeric value representing the frequency of the pru_ecap PWM generation (this connects to an AC servo motor ‘step’ input)…

As you can now see, press the button, the motor moves, release the button & the frequency command sent is reduced to ‘0’ and the PRU code stops the pwm generation.

What I’ve been playing around with today is making this a ramped (accelerated) jog. I can put a loop inside the interrupt code that increases the value of the numeric value, works fine, but I would like to delay the loop so the acceleration is more gradual. I’ve played around with a setTimeout function inside the loop, doesn’t work.

I’ve moved this delayed loop to a separate function outside of the ISR. Inside the ISR I just loop in a do->while until the ramp generation code has incremented the value to a maximum, I then cancel the interval timer - this doesn’t work either.

Is there a better way to do this that I’ve overlooked?

I’m working on commercial drive much more complex than what your doing but jog is a use case. we use rtos not Linux but concept is the same.

While loops in interrupts bad practice might be causing problem and won’t work in multithreaded environment.

We have a high speed periodic servo thread calculating next command looking at desired speed etc. In a real drive this requires a PID and some control theory that you wont need right now for your simple use case

you also need a state machine the start state is fed by button pressed Interrupt
Then you poll the button after press is first detected for release you need to communicate the release to the servo loop

A periodic task can be driven by software timers in a barebones system

The details of implementing this in Linux I can’t help with

Good luck