p_error = (state - target) which is an unconventional definition of the error. Please use computeCommand instead, which assumes error = (target - state) . Note that calls to computeCommand should not be mixed with calls to updatePid. p_error = (state - target) which is an unconventional definition of the error. Please use computeCommand instead, which assumes error = (target - state) . Note that calls to computeCommand should not be mixed with calls to updatePid.