Simple implementation of positional PID, streamlined

Source: Internet
Author: User
/**************************************/ // Positional PID algorithm /********************************* * *** // float set; // set the value to u32 Kp = 2; // The ratio constant u32 ki = 0; // The Integral Constant u32 Kd = 0; // The differential constant u32 le; // last error value u32 se; // cumulative error value u32 pid_ctl (u32 set, u32 samp_data) {s32 new, E, de, out; // define the signed temporary variable new = samp_data; // read the sample value e = set-New; // calculate the current deviation se ++ = E; // calculate the error integral value de = E-LE; // calculate the current error difference value out = Kp * E + Ki * Se + KD * de; // calculate the control signal out = out + 2048; // 0-2048 reverse, 2048-4095 forward to if (out <= 0) // the output voltage 0 has no meaning out = 0; if (Out> = 4095) // output voltage greater than 4095 meaningless out = 4095; write_da (out); // calculates the output result Le = E; // stores the error value, return out for one calculation; // return the current sample value, that is, the actual output value}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.