Calling a TimerChannel method in Timer callback
Posted: Sat Jan 11, 2020 6:22 pm
Hello.
I'm currently writing some code that will let me control an incrementally encoded DC motor using the PID algorithm and a Timer. The error that's being fed to the algorithm is the difference between the chosen target position and the incremental encoder's count. The encoder's count is updated in the background by means of two pins X3 and X4 and a callback applied to X3 using machine.Pin.irq.
I want to use the Timer so moving the motor can be a non-blocking action.
I had the motor setup with an L298N and an external power source and was able to rotate it one way or the other at fixed speeds appropriately by setting up two channels on Timer(5) for pins X1 and X2 and using their TimerChannel.pulse_width_percent method. The encoder's count was being correctly updated.
I've also been able to implement the PID algorithm as a Timer callback by writing it in inline assembly (which was the recommended workaround for floating-point arithmetic in a callback) and it seems to be working fine. The algorithm does try to match the 'fake' data I input.
I wanted to merge these two together and have written code that makes use of a single Timer and one callback which calls the PID update assembly function and then calls the TimerChannel.pulse_width_percent methods appropriately with the newly computed percentage.
The percentage is being computed correctly (between 0 and 100 as required) however the motor isn't responding to the TimerChannel.pulse_width_percent commands at all, so I wanted to ask:
what are the effects of calling TimerChannel.pulse_width_percent within a timer callback?
Thanks.
I'm currently writing some code that will let me control an incrementally encoded DC motor using the PID algorithm and a Timer. The error that's being fed to the algorithm is the difference between the chosen target position and the incremental encoder's count. The encoder's count is updated in the background by means of two pins X3 and X4 and a callback applied to X3 using machine.Pin.irq.
I want to use the Timer so moving the motor can be a non-blocking action.
I had the motor setup with an L298N and an external power source and was able to rotate it one way or the other at fixed speeds appropriately by setting up two channels on Timer(5) for pins X1 and X2 and using their TimerChannel.pulse_width_percent method. The encoder's count was being correctly updated.
I've also been able to implement the PID algorithm as a Timer callback by writing it in inline assembly (which was the recommended workaround for floating-point arithmetic in a callback) and it seems to be working fine. The algorithm does try to match the 'fake' data I input.
I wanted to merge these two together and have written code that makes use of a single Timer and one callback which calls the PID update assembly function and then calls the TimerChannel.pulse_width_percent methods appropriately with the newly computed percentage.
The percentage is being computed correctly (between 0 and 100 as required) however the motor isn't responding to the TimerChannel.pulse_width_percent commands at all, so I wanted to ask:
what are the effects of calling TimerChannel.pulse_width_percent within a timer callback?
Thanks.