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.
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:
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.
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:
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.
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.
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.
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.