Practical 7: Servomotor Control

In this practical we will use the knowledge you have gained from the previous practicals to build and control a servomotor from constituent parts. You will use a brushed DC motor and a feedback potentiometer as the elements of the servomotor. These will need to be driven with a appropriate H-Bridge and level adjusting circuitry. A position command signal will be generated using an onboard potentiometer from your UCT STM32 dev board. You will also be required to implement an already designed PI controller to control the position of the servomotor.

This practical is extensive and will require you to complete a number of tasks, building several circuits and writing a significant amount of code. Accordingly, this practical will run over the course of two weeks. During the first in-lab session you will be given a motor rig which contains the brushed DC motor coupled to a feedback potentiometer. This will need to be returned once you are ready to demonstrate your circuit during the second week.

Please use the feedback form to give us feedback on this practical and to report broken/faulty equipment.

Table of Contents

The hardware setup for this practical is shown in the following hardware block diagram:

Hardware Block Diagram

This figure shows the main components of the system:

  • The STM32 microcontroller
  • The comparator-based level-shifting circuit
  • The H-Bridge
  • The brushed DC motor
  • The feedback potentiometer
  • The position command potentiometer

The brushed DC motor is driven by the H-Bridge circuit which is controlled by the STM32 microcontroller and level shifting circuitry. The position command signal is generated using the position command potentiometer. The feedback potentiometer is used to feedback the position of the motor to the STM32 microcontroller. This diagram also shows the power supply levels required for the various components in the system, and the number of and type of connections required between the components.

The STM32 microcontroller will be programmed using the standard STM32 programming template, and will need to perform the tasks shown in the diagram below:

STM32 Microcontroller Tasks

At a functional level, during the operation of the system, the STM32 needs to:

  • Calculate the error between the position command signal and the feedback signal.
  • Perform the PI control calculation.
  • Output a control signal used to drive the H-Bridge and the motor.

The motor rig

Each person will be given a motor rig during the first practical session. This rig is shown in the following figures:

Motor Rig Top View Motor Rig Iso View 1 Motor Rig Iso View 2

Interface electronics

The interface electronics for this system should be designed to meet the following requirements:

  • The STM32 microcontroller will generate a single PWM signal to drive the motor on PB4.
  • The PWM signal should be converted into two anti-phase PWM signals to drive the H-Bridge and be of a voltage level compatible with the H-Bridge.
  • A maximum voltage of 7 V may be applied across the motor terminals. This should be generated by the H-Bridge circuit which must take the form of an emmitter-follower type H-Bridge.
  • The feedback potentiometer is connected to PA5. (Ensure that the potentiometer is appropriately labelled on your circuit diagram.)
  • The position command potentiometer is connected to PA6. (Ensure that the potentiometer is appropriately labelled on your circuit diagram. Check the schemati for the UCT board if necessary.)

Question 1

Draw a circuit diagram, based on the block diagram and the requirements above, of the interface electronics for the motor rig. Ensure that your circuit diagram includes all the components needed to build the drive electronics and that the circuit is compatible with the STM32 microcontroller. Ensure all supply voltages are appropriate for the components and functional requirements.

Question 2

Using a table populated with parameters measured from your circuitry, demonstrate that your comparator-based level-shifting circuit works correctly.

Question 3

Using a table populated with parameters measured from your circuitry, demonstrate that your H-Bridge circuit works correctly.

Programming the STM32

Using this template file, write the code needed to implement the measurement and control of the motor rig. Your code should implement the following functionality and be tracked in a git repository hosted on GitHub:

  • Initialise the ADC to read the position command potentiometer on PA6 and feedback signal on PA5. The resolution of the ADC should be chosen such that a precision of 1 degree is achieved.
  • Initialise the appropriate timer to generate a PWM signal to drive the motor from PB4. This signal should have a frequency of 20 kHz and an appropriate initial duty cycle. The configuration of the timer should be such that the result of the ADC conversions are matched in scale to the duty cycle control of the PWM signal.
  • Implement a PI controller to control the motor. The controller should take the form of an ideal PI controller with the following form:

    $$ \frac{O(s)}{E(s)} = K_p \left(\frac{s+I}{s}\right) $$

    where $K_p$ is the proportional gain and $I$ is the integral time constant. When converted to a discrete time domain implementation, the controller becomes:

    $$ \frac{O(z)}{E(z)} = K_p \left(\frac{(2+IT_s)z + (IT_s-2)}{2z - 2}\right) $$

    where $T_s$ is the sample time of the controller. Ensure that the controller is executed at a frequency of 1 kHz. Further, $K_p$ should be set to 50 and $I$ should be set to 1. Implement the controller as a difference equation in a function called PI_control(). You will need to choose an appropriate architecture for this function in terms of arguments and return values.

Question 4

Plan and describe your code structure using a flowchart or pseudocode.

Demonstration and Submission

By demonstrating and submitting this practical you agree that:

  • You know that plagiarism is a serious form of academic dishonesty.
  • You have read the document about avoiding plagiarism, are familiar with its contents and have avoided all forms of plagiarism mentioned there.
  • Where you have used the words of others, you have indicated this by the use of quotation marks.
  • You have referenced all quotations and other ideas borrowed from others.
  • You have not and shall not allow others to plagiarise your work.

Before your demonstration, upload your code and answers to Gradescope. Your answers should be contained within a single PDF file added to your GitHub repository.

Submitting to Gradescope

Use the following recipe to submit your code from GitHub to Gradescope:

  1. Log in to Gradescope.
  2. From your Dashboard, open the programming assignment. The Submit option will appear.
  3. Select GitHub as your Submission Method.
  4. Link your GitHub account with Gradescope:
    1. Authorize Gradescope as a third party app in your GitHub account.
    2. Choose the Repository and Branch.
  5. Select upload.
  • Make sure that you use the template file provided.
  • Ensure that at least your student number is located within the file.
  • The time at which you demonstrate will be the time taken for submission. If you do not demonstrate before 17:00 on Monday (19/05/2025), your mark will be capped to 50% as dictated by the course outline.
  • No demonstration will result in no mark.

When you are ready to demonstrate, call over tutor. You will then be asked to run your C code on a UCT STM32 Dev board. The tutor will then assign a demonstration mark as described by the marksheet. The tutor will then ask a series of questions based on your submitted C code and ask to see your Git commit history on Github.

Marksheet

The marks awarded for this practical remain TBD but will be updated here and alongside each question as soon as they are available.