Skip to main content

Closed Loop Angle Control

In this tutorial, we will explore how to run closed loop angle control on the Aotenjo One board.

Prerequisites

In this tutorial, the following hardware is used:

  • Aotenjo One Board
    MCU: STM32G431CBU6
    Version: Aotenjo One V1.4
  • Diametrically magnetized disc magnet (included with Aotenjo One)
  • ST-LINK V2 Clone
  • BlackPill STM32 Board
  • BE4108 75T Gimbal Motor
  • Power Supply (12V)

You need a ST-LINK V2 Clone to upload the firmware to the board. You can purchase a cheap clone ST-LINK V2 under $5.

USB to Serial Adapter

Since the Aotenjo One board doesn't have an USB port, you need a USB to Serial adapter to communicate with the board via serial. In this tutorial, we will use a Blackpill STM32 board as a USB to Serial adapter to communicate with the Aotenjo One board, but you can use a cheap USB to Serial adapter or another STM32 board like the Blackpill as an USB to Serial adapter.

tip

You can use the following development boards as a USB to Serial adapter:

  • BlackPill STM32 board
  • BluePill STM32 board
  • ESP32-DevKitC

USB to Serial Adapter Diagram

If you're not sure how to use the BlackPill STM32 board as a USB to Serial adapter, you can refer to the Serial Communication tutorial.

caution

Make sure your BLDC motor's internal resistance is greater than 10 Ω; otherwise, the board may burn out.

ref. How to measure internal resistance

Installation

VScode with PlatformIO

You can install PlatformIO from the VSCode extension marketplace.

STM32CubeProgrammer

You need this to flash the firmware to the board.

Clone the repository

git clone https://github.com/aotenjo-xyz/one.git
cd one

The example code for the LED blink is located in the examples/closed_loop_angle_control directory, so run the following command if you use linux or macOS to copy the example code to the src directory:

rm src/*
cp examples/closed_loop_angle_control/main.cpp src/
cp examples/closed_loop_angle_control/platformio.ini platformio.ini

For Windows, you can copy the files manually.

Quickstart

  1. Place the magnet on the motor shaft.

    Magnet on Motor Shaft

  2. Connect the Aotenjo One board to the ST-LINK V2 programmer.

    ST-LINK V2 <-> Aotenjo One
    3.3V <---> 3.3V
    GND <---> GND
    SWDIO <---> SWDIO
    SWCLK <---> SWCLK
    RST <---> NRST
    info

    Unlike the blackpill board, you need to connect the NRST pin to the ST-LINK V2 programmer for STM32G4 series.

  3. Connect the ST-LINK V2 programmer to your computer.

  4. Open the project folder in VSCode.

  5. Click on the PlatformIO tab on the left sidebar and click on the Upload button to upload the firmware to the board.

  6. Disconnect the ST-LINK V2 programmer, then connect the power supply and the BlackPill STM32 board to the Aotenjo One board. Finally, connect the BlackPill STM32 board to your computer.

    Power Supply <-> Aotenjo One
    12V <---> VCC
    GND <---> GND

    Aotenjo One <-> Blackpill STM32
    TX <---> PA3 (RX)
    RX <---> PA2 (TX)
    GND <---> GND

    Blackpill STM32 <-> Computer
    USB <---> USB

    Open Loop Control Setup

  7. Click on the PlatformIO tab on the left sidebar and click on the Monitor button to open the serial monitor. PlatformIO Monitor

  8. Turn on the power supply, and the motor will start rotating to initialize the FOC (Field Oriented Control).

  9. Input the following command in the serial monitor and press enter:

    T6.28

    The motor will complete one full rotation and stop at the initial position. You can adjust the target angle by changing the value in radians. You can also rotate the motor in the opposite direction by entering a negative value, such as T-6.28.

    Here is the video demonstration:

    BLDC motor closed-loop angle control

Code

Here are the relevant code snippets for the closed-loop angle control implementation:

main.cpp
  // set motion control loop to be used
motor.controller = MotionControlType::angle;

// velocity PI controller parameters
motor.PID_velocity.P = 0.2;
motor.PID_velocity.I = 20;
motor.PID_velocity.D = 0.001;
// maximal voltage to be set to the motor
motor.voltage_limit = 3.0;

// velocity low pass filtering time constant
// the lower the less filtered
motor.LPF_velocity.Tf = 0.01f;

// angle P controller
motor.P_angle.P = 20;
// maximal velocity of the position control
motor.velocity_limit = 10;

You can play around with the PID controller parameters to see how they affect the motor's behavior.

References