Simulated PID Controller

For this part of the project you will be implementing a PID controller in scripts/student_pid_class.py and using a simulator, z_sim/sim.py to test its behavior.

PID

A PID (proportional, integral, derivative) controller is a control algorithm that generates a control signal based on error. You will be implementing PID control for a simulated drone that can only move in one dimension, the vertical dimension. You can control the speed the motors spin on the drone which sets the thrust being generated by the propellers. So in this case, the error is the distance in meters the drone is away from the desired altitude (called the set point), and the control signal is a PWM (pulse-width modulation) value between 1100 and 1900 which is sent to the flight controller to set the throttle.

From Wikipedia, the general algorithm is:

PID continuous

However for this project you should implement the discrete version:

\[K_P, K_I, K_D, K = \mbox{Constants and Offset Term}\] \[e_t = \mbox{error at time } t\] \[\Delta t = \mbox{time elapsed from previous iteration}\] \[u_t = K_P(e_t) + K_I \sum_{j=1}^t(e_t \Delta t_t) + K_D (\frac{e_t - e_{t-1}}{\Delta t_t}) + K\]

You should run the simulator on your base station or on a department computer and not on your drone, since it requires a graphical user interface to visualize the output. You can run the git clone command wherever you plan to work. You will need numpy and matplotlib: pip install numpy matplotlib pyyaml. To run the simulator, use python sim.py and the PID class in student_pid_class.py will automatically be used to control the simulated drone. The up and down arrow keys will change the set point, and r resets the sim.

Problem 1: Implement Idealized PID

Write brief (one sentence is fine) answers to the following questions in your altitude_answers.pdf. You will implement student_pid_class.py and tune the parameters in z_pid.yaml:

  1. Implement the step function to return a constant $K$. At what value of K does the drone takeoff? Can you get it to hover? Set $K$ to 1300 for questions 2 and 3.
  2. Implement the P term. What happens when the absolute value of the P term is very large? What happens when its absolute value is very small? Can you tune the P term to stop oscillations? Why or why not?
  3. Implement the D term. Set the $K_I$ and $K_P$ terms to zero. What happens?
  4. Now implement the derivative term. Set $K$ to 1300 and tune the $K_P$ and $K_D$ so that the drone comes a steady hover. Describe the trade off as you change the ratio of $K_P$ to $K_D$. Does the drone stabilize at its target? Why or why not?
  5. Implement the integral term and observe the difference between PD and PID control. What role does the i term play in this system? Set the D and P terms to zero. What happens?
  6. Finally, try setting $K$ higher and lower. What is the relationship between $K$ and $K_I$ (especially when the drone first takes off)? What could happen if $K$were set too high on the real drone?

Tune the constants in your PID controller to the best of your abilities. The drone should chase the set point very closely, and should not wobble when the set point is still.

NOTE: Pay careful attention to the behavior of reset. This has been the source of a lot of crashes in our lab; see how many problems you can anticipate reset causing and how many you can prevent.

Problem 2: Implement PID with Latency

Now, we introduce latency! Run the sim as python sim.py -l 6 to introduce 24 milliseconds of latency (six steps of latency running at 25 hz). Tune the constants in your PID controller to the best of your abilities. The drone should chase the set point very closely but converge more slowly and you should get it to stop oscillations. Report your PID terms before and after. Which terms changed the most? Why would latency affect that term/those terms in particular?

Run python sim.py -h to see the other simulator parameters. We encourage you to experiment with those as well and observe their effects on your controller.

Problem 3: Implement PID with Latency, Noise, and Drag

In the most realistic mode, you will tune a controller with latency, noise, and a drag coefficient. You can do this with the command line arguments python sim.py -l 3 -n 0.5 -d 0.02 to be most realistic to real world flight. Tune with these arguments to be as good as possible.

Checkoff:

Come see a TA to show off your PID controller from problem 3! We will assess the speed it converges (dynamic performance after a change to the set point) and the steady-state performance. It should converge without oscillations in the steady state, and it should quickly (a few seconds) converge to the new set point.