PID controller integral term causing extreme instability

前端 未结 4 1791
慢半拍i
慢半拍i 2021-02-07 21:19

I have a PID controller running on a robot that is designed to make the robot steer onto a compass heading. The PID correction is recalculated/applied at a rate of 20Hz.

4条回答
  •  滥情空心
    2021-02-07 21:26

    I'm not sure why your code isn't working, but I'm almost positive you can't test it to see why, either. You might inject a timer service so you can mock it out and see what's happening:

    public interace ITimer 
    {
         long GetCurrentTicks()
    }
    
    public class Timer : ITimer
    {
        public long GetCurrentTicks() 
        {
            return DateTime.Now.Ticks;
        }
    }
    
    public class TestTimer : ITimer
    {
        private bool firstCall = true;
        private long last;
        private int counter = 1000000000;
    
        public long GetCurrentTicks()
        {
            if (firstCall)
                last = counter * 10000;
            else
                last += 3500;  //ticks; not sure what a good value is here
    
            //set up for next call;
            firstCall = !firstCall;
            counter++;
    
            return last;
        }
    }
    

    Then, replace both calls to DateTime.Now.Ticks with GetCurrentTicks(), and you can step through the code and see what the values look like.

提交回复
热议问题