Last active
August 29, 2015 14:08
-
-
Save theodox/3c956ccffd3d5d060b15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
/* | |
This is a simple example of the PIDContoller class. | |
For a discussion of PID theory see http://techartsurvival.blogspot.com/-address-tbd | |
Legalese: | |
Copyright (c) 2014 Steve Theodore | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
// This is a simple example of the PIDContoller class. | |
// the Follower will try to track the Y position of a target | |
// using a PID controller to demonstrate PID tuning. | |
// The controller itself is a general-purpose reusable | |
// component that can be used to tweak any single-input | |
// single-output series. | |
public class Follower : MonoBehaviour { | |
public Transform _Target; | |
public PIDController _Controller; | |
public float _Friction; | |
public float _Power; | |
private Vector3 _delta; | |
private Vector3 _momentum; | |
public Vector3 _localPos; | |
// Use this for initialization | |
void Start () { | |
_Controller.Start(); | |
} | |
// Update is called once per frame | |
void Update () { | |
_delta = _Target.position - transform.position; | |
_Controller.Update(_delta.y); | |
Vector3 thrust = Vector3.up * _Controller._Response * _Power; | |
_momentum *= (1-_Friction); | |
_momentum += thrust; | |
transform.Translate(_momentum); | |
} | |
} | |
// The PID controller applies th | |
[System.Serializable] | |
public class PIDController | |
{ | |
public float _Response { get { return _response;} } | |
private float _response; | |
public float _Gain; // Kp in most docs | |
public float _IntegralGain; // Ki | |
public float _DifferentialGain; // Kd | |
public float _Droop; // intentional error | |
private float _integral; | |
private float _differential; | |
private float _backOne; | |
private float _backTwo; | |
private float _error = 0; | |
public void Start() | |
{ | |
} | |
public void Update(float e) | |
{ | |
// rotate the last samples | |
_backTwo = _backOne; | |
_backOne = _error; | |
// current error + random extra error scaled by droop | |
_error = Time.deltaTime * (e + Random.RandomRange(-1 * _Droop, _Droop)); | |
_integral += _error; | |
_differential = ((_backOne - _backTwo) + (_error - _backOne)) / 2; | |
_response = (_Gain * e) + (_IntegralGain * _integral) + (_DifferentialGain * _differential ) ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Abandoned the fixed window history - with proper signed errors the problem it fixed is no longer an issue