<commit_before>#include "Pid.hpp"

#include <cstring>
#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;


Pid::Pid(float p, float i, float d, unsigned int windup)
{
	_windupLoc = 0;
	_errSum = 0;
	_oldErr = 0;
	
	_lastErr = 0;

	kp = p;
	ki = i;
	kd = d;
	
	_windup = 0;
	setWindup(windup);
}

Pid::~Pid()
{
	if (_oldErr)
	{
		delete[] _oldErr;
		_oldErr = nullptr;
	}
}

void Pid::setWindup(unsigned int w)
{
	if (w != _windup) 
	{
		if (_oldErr) 
			delete[] _oldErr;

		if (w > 0)
		{
			if (w != _windup) {
				_windup = w;
				_oldErr = new float[_windup]{};
				_errSum=0;
			}
		}
	}
}

float Pid::run(const float err)
{
	if (isnan(err))
	{
		return 0;
	}
	float dErr = err - _lastErr;
	_lastErr = err;

	_errSum += err;
	
	if (_oldErr)
	{
		_errSum -= _oldErr[_windupLoc];<commit_msg>Set w to new value even if w=0<commit_after>#include "Pid.hpp"

#include <cstring>
#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;


Pid::Pid(float p, float i, float d, unsigned int windup)
{
	_windupLoc = 0;
	_errSum = 0;
	_oldErr = 0;
	
	_lastErr = 0;

	kp = p;
	ki = i;
	kd = d;
	
	_windup = 0;
	setWindup(windup);
}

Pid::~Pid()
{
	if (_oldErr)
	{
		delete[] _oldErr;
		_oldErr = nullptr;
	}
}

void Pid::setWindup(unsigned int w)
{
	if (w != _windup) 
	{

		_windup = w;

		if (_oldErr) 
			delete[] _oldErr;

		if (w > 0)
		{	
			_oldErr = new float[_windup]{};
			_errSum=0;
		}
	}
}

float Pid::run(const float err)
{
	if (isnan(err))
	{
		return 0;
	}
	float dErr = err - _lastErr;
	_lastErr = err;

	_errSum += err;
	
	if (_oldErr)
	{
		_errSum -= _oldErr[_windupLoc];
{'fraction_non_alphanumeric': 0.1588957055214724, 'fraction_numerical': 0.010429447852760737, 'mean_word_length': 2.9683698296836982, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4175070', 'n_tokens_mistral': 845, 'n_tokens_neox': 760, 'n_words': 181}