# PID control of a 1D helicopter¶

Note: This is a minor rewrite of an existing PIC32 lab for ECE 4760 created by Bruce Land.

## Introduction¶

In this lab, you will construct and control of a one-degree-of-freedom helicopter. A small drone motor is rigidly attached to the end of a lever-arm, the other end of which is attached to a low-torque potentiometer. The drone motor can lift the arm, and the angle of the arm is measured by the potentiometer. The user can select a target arm-angle and a PID controller will drive the arm to that angle.

The target and measured angle are displayed on the oscilloscope or the VGA screen and the user specifies new target angles through a serial interface to the RP2040.

## Demonstration¶

A demo is shown below.

Experience shows that students prefer these webpages short. For that reason, please find the reading and background materials on the webpages linked below. Please note that the information in these readings will be critical for completing the lab.

### Theoretical background¶

• Phenomenological introduction to PID controllers: This document focuses on building a phenomenological understanding of PID controllers through demos. The hope is that this document will help you debug your system based on the behavior that you observe in lab, and the one below will help you understand that behavior.
• Analytical introduction to PID controllers: This document focuses on building an analytical understanding of these controllers. The hope is that the phenomenological document will help you debug your system based on the behavior that you observe in lab, and this one will help you understand that behavior.

### Engineering background¶

• Mechanical contruction of the lever arm: This webpage provides instructions for the mechanical construction of the lever arm.
• Motor circuit and PWM: This webpage describes the circuit that you will construct to safely drive the DC motor, and how to generate PWM.
• Angle sensing circuit: This webpage describes the potentiometer circuit that you will use to measure the arm's position, and how to setup/read the ADC.
• Data display: This webpage describes the data that you must visualize in realtime, and some strategies for visualizing that data.
• RP2040 datasheet: See chapters on PWM and ADC.
• RP2040 C SDK: See chapters on PWM and ADC.

## Program organization¶

You may organize your program however you like. Here is a suggestion:

• Protothreads maintains the ISR-driven, millisecond-scale timing as part of the supplied system. Use this for all low-precision timing (can have several milliseconds jitter).
• PID ISR uses a timer or PWM interrupt to ensure an exact 1kHz control rate.
• Clears the interrupt flag
• Runs the PID control loop at 1000/sec using the angle measurements from the potentiometer
• Sets a hardware PWM signal using output-compare unit to control the motor using the command: pwm_set_chan_level(slice_num, PWM_CHAN_X[A or B], pwm_on_time);.
• (possibly) communicates measured angle and low-passed control input to DAC
• Main sets up peripherals and protothreads then just schedules tasks, round-robin
• Takes user input from the serial interface to setup PID parameters and the desired angle.
• (possibly) diplays measured angle and low-passed control input on VGA display

## Weekly checkpoints and lab report¶

Note that these checkpoints are cumulative. In week 2, for example, you must have also completed all of the requirements from week 1.

#### Week one checkpoint¶

By the end of the lab session in week one of the lab you must have:

• The mechanical assembly finished and be able to control motor speed open loop, from the PWM output. Here is some demo code to get you started.
• Display of the actual beam angle and the low-passed motor command signal using the two channel DAC or the VGA display. This is critical for debugging.
• Finishing a checkpoint does NOT mean you can leave lab early!

#### Week two checkpoint¶

By the end of the lab session in week two of the lab you must have:

• Full closed-loop control of the motor (just proportional control is fine).
• Serial command interface to set PID parameters.
• Finishing a checkpoint does NOT mean you can leave lab early!

#### Week three assignment¶

• Measure the angle of the beam supporting the lift-motor, display the beam position either on the oscilloscope or the VGA screen.
• Format the set angle and PID parameters to display appropriate messages on the VGA display or the serial console.
• At any time, take commands from the serial interface to:
• Set the desired beam angle
• Set the PID proportional gain
• Set the PID differential gain
• Set the PID integral gain
• The new values should take effect immediately
• One set of coefficients should produce stable behavior over the range of desired hover angles
• Use a PID control algorithm to control the speed of the motor by producing a PWM drive to the optoisolator. PWM setup example.
• Tune the PID algorithm so that you can change the angle of the beam quickly and accurately without excessive angle oscillations.
• The user should be able to enter a desired hover angle and the motor should quickly change the beam to the new angle. The initial angle of the beam should be around -1.57 radians (hanging straight down).
• Display the motor control value (not the raw PWM signal) on the scope or VGA. Noise control on this signal is essential. If you cannot see the shape of the control signal, you will not get credit for this feature! You may need to combine DSP and analog filtering, depending on exactly how you build the circuit.
• Display the actual beam angle, from the angle sensor, on the scope or VGA. If this signal is noisy, you need to find out why! Poor wire routing, loose connections, and motor noise are all posibilities. A noisy input makes the control PID algorithm very hard to tune.
• When a button is pushed (not the reset button), the beam should go through a quick sequence of defined angle changes, using the current set of PID parameters:
• Before time=0, while holding the button, the beam should be hanging vertically down (motor off)
• When the button is released at time=0, target angle should be set to horizontal
• At time=5 seconds, target angle should be set to approximately 30 degrees above horizontal
• At time=10, target angle should be set to approximately 30 degrees below horizontal
• At time=15, target angle should be set to horizontal

You will demo all of the features above to a course staff member. You program should not need to be reset during the demo.

#### Lab report¶

Your written lab report should include the sections mentioned on the policy page, and:

• A schematic of the circuit you built
• Scope screen dumps of typical two-trace: (1) motor-control and (2) actual angle. Include the demo sequence of four angles, as shown below
• A summary of the accuracy of your measurements. How accurately can you measure the angle?
• How you selected the three PID gains.
• What are the maximum/minimum angles that produce stable behavior for your PID gains?
• What are the settling times at different angles?
• A heavily commented listing of your code.