/**************************************/ // 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}