I'e seen a lot of examples of simple PWM on the Pi where relatively high-precision PWM signals at a fixed duty cycle can be generated using WiringPi or pigpio in hardware PWM mode. I have tried implementing something similar but need to update the duty cycle at a fixed sample period Ts. The idea is to basically hack together a DAC using the PWM output and a one-pole RC to have a signal generator capable of emulating signals recorded from a much larger set up. The bandwidth is pretty low, so I'm trying for an effective sample rate of 200 Hz (update the PWM duty cycle every 5 ms).
I'm using pigpio in hardwarePWM mode with Python3 on GPIO 18 and have tried using time.sleep() and time.perf_counter() to get the delays but it's clear that between the Python interpreter and Linux scheduler the drift accumulates significantly within just a few seconds of signal playback.
Next step will be to try doing it in C instead of Python, but I was wondering if anyone had any input on how to potentially do this with more reliable inter-sample spacing (ie, microsecond levels of drift per "sample" update instead of ms).
One other solution that came to mind is converting the entire signal to a train of square waves first (basically call a function first to re-produce the entire "PWM signal" that would be getting written to the GPIO lines on each sample update and then just outputting that as serial data, if the clock is more reliable this way). Can also always just buy a cheap DAC but was hoping to be able to throw this together in code to keep footprint down, especially since the sample rate is pretty low. Thanks for any insight