{"action":"created","issue":{"url":"https://api.github.com/repos/Smoothieware/Smoothieware/issues/639","labels_url":"https://api.github.com/repos/Smoothieware/Smoothieware/issues/639/labels{/name}","comments_url":"https://api.github.com/repos/Smoothieware/Smoothieware/issues/639/comments","events_url":"https://api.github.com/repos/Smoothieware/Smoothieware/issues/639/events","html_url":"https://github.com/Smoothieware/Smoothieware/issues/639","id":60551601,"number":639,"title":"PID Derivative calculation requires input filtering","user":{"login":"tempo502","id":11251919,"avatar_url":"https://avatars.githubusercontent.com/u/11251919?v=3","gravatar_id":"","url":"https://api.github.com/users/tempo502","html_url":"https://github.com/tempo502","followers_url":"https://api.github.com/users/tempo502/followers","following_url":"https://api.github.com/users/tempo502/following{/other_user}","gists_url":"https://api.github.com/users/tempo502/gists{/gist_id}","starred_url":"https://api.github.com/users/tempo502/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tempo502/subscriptions","organizations_url":"https://api.github.com/users/tempo502/orgs","repos_url":"https://api.github.com/users/tempo502/repos","events_url":"https://api.github.com/users/tempo502/events{/privacy}","received_events_url":"https://api.github.com/users/tempo502/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":40,"created_at":"2015-03-10T19:03:26Z","updated_at":"2015-04-05T13:12:42Z","closed_at":null,"body":"Summary:\r\nThe PID loop is extremely sensitive to noise in the derivative term, which is fairly common due to ADC sampling error / measurement jitter. Some simple input filtering would greatly improve the stability and consistency of PWM output.\r\n\r\nDetails:\r\nThe default 20hz sample rate is around a hundred times faster than the time constant of typical thermistors, so the derivative term is being recalculated WAY more often than is necessary or physically meaningful. The resolution of the temperature sensing is sufficiently low relative to the sample frequency that d_factor*dT/dt appears to be either zero or an extremely large number for most PID loop ticks. In particular, renormalizing the D gain to suit the 20hz tick frequency greatly amplifies the D response on ticks where the temp measurably changes. This puts a lot of undesired junk values in the PID PWM output. And it actually clamps / diminishes the impact of the D term on the heater control since it's being applied in very brief impulses that exceed the max/min PWM range. \r\n\r\nAny sort of low-pass filter on the D term would significantly improve the stability of the temperature control. An easy option (used by Makerbot firmware & Sailfish) is to calculate a moving average (eg store last n dT/dt values in a ring buffer and recalculate a new average each PID tick) and use that average to calculate the D parameter output. For example, see \r\nhttps://github.com/jetty840/Sailfish-MightyBoardFirmware/blob/418058e57e63eec62d90572d49ac56b6b366692a/firmware/src/MightyBoard/shared/PID.cc#L72\r\nThis implementation uses a 2.5 second moving average (PID tick every half second, five point moving average). Ideally, you want to use a window somewhat shorter than the time constant of the temperature sensor, but long enough to adequately smooth out most measurement noise. A half second or one second window would probably work well for Smoothie.\r\n\r\nHere's an example log of bad heater PWM behavior (note intermittent 0 and 255 values):\r\n\r\nB:62.2 /70.0 @255\r\nB:62.1 /70.0 @83\r\nB:61.9 /70.0 @87\r\nB:61.8 /70.0 @91\r\nB:61.7 /70.0 @94\r\nB:61.6 /70.0 @97\r\nB:61.6 /70.0 @100\r\n[...]\r\nB:61.9 /70.0 @155\r\nB:62.0 /70.0 @157\r\nB:62.1 /70.0 @0\r\nB:62.2 /70.0 @160\r\n[...]\r\nB:63.2 /70.0 @174\r\nB:63.3 /70.0 @0\r\nB:63.4 /70.0 @176\r\n[...]\r\nB:64.8 /70.0 @183\r\nB:64.9 /70.0 @184\r\nB:65.1 /70.0 @0\r\nB:65.2 /70.0 @184\r\n[...]\r\nB:66.0 /70.0 @185\r\nB:66.2 /70.0 @0\r\nB:66.3 /70.0 @184"},"comment":{"url":"https://api.github.com/repos/Smoothieware/Smoothieware/issues/comments/89769626","html_url":"https://github.com/Smoothieware/Smoothieware/issues/639#issuecomment-89769626","issue_url":"https://api.github.com/repos/Smoothieware/Smoothieware/issues/639","id":89769626,"user":{"login":"arthurwolf","id":108821,"avatar_url":"https://avatars.githubusercontent.com/u/108821?v=3","gravatar_id":"","url":"https://api.github.com/users/arthurwolf","html_url":"https://github.com/arthurwolf","followers_url":"https://api.github.com/users/arthurwolf/followers","following_url":"https://api.github.com/users/arthurwolf/following{/other_user}","gists_url":"https://api.github.com/users/arthurwolf/gists{/gist_id}","starred_url":"https://api.github.com/users/arthurwolf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arthurwolf/subscriptions","organizations_url":"https://api.github.com/users/arthurwolf/orgs","repos_url":"https://api.github.com/users/arthurwolf/repos","events_url":"https://api.github.com/users/arthurwolf/events{/privacy}","received_events_url":"https://api.github.com/users/arthurwolf/received_events","type":"User","site_admin":false},"created_at":"2015-04-05T13:12:41Z","updated_at":"2015-04-05T13:12:41Z","body":"On Sun, Apr 5, 2015 at 3:10 PM, Chris <notifications@github.com> wrote:\n\n> All in all it seems Smoothie is broken firmware on top of broken hardware.\n>\n\nWell that's not very nice ...\n\n\n> Time to bin mine and move on I think.\n>\n> —\n> Reply to this email directly or view it on GitHub\n> <https://github.com/Smoothieware/Smoothieware/issues/639#issuecomment-89769538>\n> .\n>\n\n\n\n-- \nCourage et bonne humeur.\n"}}