PCB Project

FishyServo

Summary

Closed-Loop Servo Controller for DC motors

Source
GitHub

Description

FishyServo - Closed-Loop Servo Controller for DC motors

FishyServo Logo

Something is Fishy; looks like a DC motor driver on the outside, but actually a Servo driver.

FishyServo is an open-source motor controller board based on the STM32H503KBU6 microcontroller (Arm Cortex-M33, up to 250 MHz) and dual Texas Instruments DRV8411A H-bridge drivers. The board provides 4 independent half/full H-bridge channels with low-side current sensing on each channel. When connected to an external Texas Instruments TMAG5273 3D Hall-effect magnetic sensor via a Flexible Printed Circuit (FPC) ribbon cable, the system operates as a closed-loop servo controller with absolute angular position feedback, dual isolated I2C buses, and Katapult / BareBoot bootloader support.

Repository Scope: This repository is dedicated strictly to hardware design, CAD assets, schematics, PCB fabrication files, and production test scripts. The production firmware has been upstreamed into the core BareFOC ecosystem and is canonically maintained at bareFOC/examples/fishyservo_stm32h5.


Key Features

  • Dual-Mode Operation: Configurable as a 4-channel independent DC motor/load driver or as a closed-loop servo controller with angular feedback.
  • Microcontroller: STM32H503KBU6 (Arm Cortex-M33 with FPU/DSP, 64 to 250 MHz, 128 KB Flash, 32 KB SRAM, UFQFPN-32).
  • H-Bridge Motor Drivers: Two TI DRV8411ARTE ICs providing 4 H-bridge channels (up to 1.5 A peak per bridge) for micro DC motors and directional loads.
  • External Magnetic Sensor Interface: Connects an external TI TMAG5273 3D Hall-effect sensor over an FPC ribbon cable to dedicated internal bus I2C1 for angular tracking.
  • Dual Isolated I2C Buses:
    • Host Bus (I2C2 on PB3 / PB4): External interface for host communication and firmware updates via Katapult / BareBoot (Target Address 0x40).
    • Sensor Bus (I2C1 on PB6 / PB7): Dedicated internal channel isolating sensor transactions from host traffic.
  • Four-Channel Current Sensing: Low-side current mirror circuits (AIPROPI=200 μA/AA_{IPROPI} = 200\ \mu\text{A/A}) with 1 kΩ shunt resistors on channels A through D, sampled via 12-bit ADC1.
  • Bus Voltage Monitoring: Resistor divider on PB1 (VSENS) for DC bus voltage measurement.
  • #![no_std] Rust Firmware: Implemented using the Embassy asynchronous framework (embassy-stm32) for task scheduling, motion control, and telemetry streaming.
  • N20 Form Factor: Board dimensions matched to standard N20 micro gearmotors.

Hardware Specifications

Core Specifications

SpecificationValueNotes
MicrocontrollerSTM32H503KBU6Arm Cortex-M33 @ 64–250 MHz, 128 KB Flash, 32 KB SRAM
Motor Drivers2 × TI DRV8411ARTEDual H-bridge drivers (4 channels: A, B, C, D), 1.5 A peak per bridge
Logic Voltage3.3 V nominalOnboard LDO supply, decoupled analog VDDA rail
Motor Supply3.0 V to 6.0 VSensed via resistor divider on PB1 (VSENS)
Position SensorTI TMAG5273 (FPC)3D Hall-effect magnetic sensor on internal I2C1 (PB6/PB7) via FPC ribbon
Host BusI2C (I2C2)PB3 (SCL) / PB4 (SDA), Target Address 0x40
Current Sensing4 × Low-Side Mirrors200 μA/A200\ \mu\text{A/A} mirror into 1 kΩ resistor (VISENS=0.2 V/AV_{ISENS} = 0.2\text{ V/A})
Programming & DebugSWDSWDIO (PA13), SWCLK (PA14), USART1 (PA11/PA12)
Status IndicatorsDual LEDsBlue (PA0), Red (PA1), active-LOW common anode

Operating Modes

ModeExternal Sensor FPCActive ChannelsControl ParadigmTarget Application
Quad DC DriverDisconnected / UnfittedChannels A, B, C, DOpen-loop PWM / Voltage / VelocityMulti-motor drive, directional DC actuators, solenoids, or resistive loads
Closed-Loop ServoConnected via FPC (I2C1)Channel D (or A–C configured)Cascaded Position/Velocity PID with 3D angle feedbackAngular position control, robotic joints, micro linear actuators, N20 gearmotors

H-Bridge Channel Allocation & Pin Mapping

ChannelDriver ICInput 1 (IN_x1)Input 2 (IN_x2)Timer PWM ChannelsCurrent Sense PinADC ChannelDefault Boot State
Bridge AU1 (DRV8411A)PA15PB5TIM2_CH1 / TIM3_CH2PA2ADC1_INP14Hi-Z Coast (0, 0)
Bridge BU1 (DRV8411A)PA8PB15TIM1_CH1 / TIM1_CH3NPA3ADC1_INP15Hi-Z Coast (0, 0)
Bridge CU2 (DRV8411A)PA6PA7TIM3_CH1 / TIM3_CH2PA4ADC1_INP18Hi-Z Coast (0, 0)
Bridge DU2 (DRV8411A)PA9PB0TIM1_CH2 / TIM3_CH3PA5ADC1_INP19Active Servo PWM

DRV8411A IN/IN Control Logic

IN_x1IN_x2Output StateFunctionDescription
00Hi-ZCoastOutputs disabled, high impedance; zero quiescent drive current
10OUT1=H, OUT2=LForwardForward rotation / high-side to low-side conduction
01OUT1=L, OUT2=HReverseReverse rotation / low-side to high-side conduction
11OUT1=L, OUT2=LBrakeLow-side slow decay; dynamic braking

Dual I2C Bus Topology & Address Isolation

Bus DomainPeripheralClock PinData PinTarget / SpeedDevices & RolesIsolation Function
Host BusI2C2PB3 (AF8)PB4 (AF8)Target 0x40
100–400 kHz
Host master, BareBoot Katapult bootloader, barefoc-multibus register serverIsolates host register transactions and bootloader communications from sensor bus traffic
Sensor BusI2C1PB6 (AF4)PB7 (AF4)Master
50–100 kHz
TI TMAG5273 3D Hall sensor (FPC connector)Maintains deterministic sensor polling rates without host arbitration delays or clock stretching

Power & Clock Regimes

RegimeSystem Clock SourceFrequencyVoltage ScaleTypical Die TempTarget Operating Condition
NominalInternal HSI (RCC_HSI_DIV1)64 MHzScale 3 (Low Power)~40–45 °CContinuous closed-loop servo operation with lower thermal dissipation
High FrequencyPLL1 via CSI / HSI48Up to 250 MHzScale 0 (Boost)~60–70 °CHigh-rate current control loops and high-throughput telemetry

Implementation Notes

  • Current Sensing Calibration:
    • Current mirror ratio: AIPROPI=200 μA/AA_{IPROPI} = 200\ \mu\text{A/A} (1:5000 ratio).
    • Shunt resistor: RSENS=1000 ΩR_{SENS} = 1000\ \Omega.
    • Transfer function: VISENS=Imotor×AIPROPI×RSENS=Imotor×(200×10−6 A/A)×1000 Ω=Imotor×0.2 V/AV_{ISENS} = I_{motor} \times A_{IPROPI} \times R_{SENS} = I_{motor} \times (200 \times 10^{-6}\text{ A/A}) \times 1000\ \Omega = I_{motor} \times 0.2\text{ V/A} Imotor=VISENS0.2 V/A=5.0×VISENSI_{motor} = \frac{V_{ISENS}}{0.2\text{ V/A}} = 5.0 \times V_{ISENS}
    • Resolution: 12-bit ADC count ≈3.3 V4095×0.2 V/A≈4.029 mA/count\approx \frac{3.3\text{ V}}{4095 \times 0.2\text{ V/A}} \approx 4.029\text{ mA/count} with full-scale range up to 16.5 A.
  • Safe Boot Coast Mode:
    • To prevent startup transients during power-up, all bridge inputs (IN_A1, IN_A2, IN_B1, IN_B2, IN_C1, IN_C2) are initialized to logic LOW (Level::Low) before PWM timer configuration.
  • Active-Low LED Polarity:
    • LED_B (PA0) and LED_R (PA1) are tied to 3.3V via 1 kΩ series resistors. Driving the GPIO LOW illuminates the LED.
  • SWD Debug Preservation:
    • PA13 (SWDIO) and PA14 (SWCLK) are preserved from GPIO reconfiguration. DBGMCU stop/standby debug clocks remain enabled on boot to maintain SWD connection during Embassy WFE/WFI sleep periods.

Hardware Architecture & Pinout

System Architecture

flowchart TD
    Host["Host Controller (I2C Master)"] -->|"I2C2: PB3 / PB4 (0x40)"| MCU["STM32H503KBU6 (Arm Cortex-M33)"]

    subgraph sensing_stage ["Sensing and Telemetry"]
        direction TB
        TMAG["TI TMAG5273 3D Magnetic Sensor (FPC Module)"]
        CS["4x Current Mirrors (PA2-PA5)"]
        VS["Bus Voltage Divider (PB1)"]
    end

    subgraph driver_stage ["Motor Actuation"]
        direction TB
        Drv1["TI DRV8411A 1 (Channels A and B)"]
        Drv2["TI DRV8411A 2 (Channels C and D)"]
        Motor["N20 Micro DC Gearmotor / Loads"]
    end

    MCU -->|"I2C1 FPC Ribbon: PB6 / PB7"| TMAG
    CS -->|"ADC1 Current"| MCU
    VS -->|"ADC1 Voltage"| MCU

    MCU -->|"TIM1 / TIM2 / TIM3 PWM"| Drv1
    MCU -->|"TIM1 / TIM3 PWM"| Drv2
    Drv1 -->|"Drive Outputs"| Motor
    Drv2 -->|"Drive Outputs"| Motor
    Motor -.->|"Magnetic Flux"| TMAG

Complete Pinout Table

Pin #Pin NameNet LabelMode / PeripheralFunctionDescription
1VDD3V3Power SupplyPowerDigital 3.3V supply (Decoupled by C2 4.7µF, C9 100nF)
2PC14-OSC32_INNCNoneUnusedNo external crystal fitted
3PC15-OSC32_OUTNCNoneUnusedNo external crystal fitted
4NRSTRSTResetControlActive-low system reset (10 kΩ pull-up R31 to 3V3)
5VDDA/VREF+3V3Analog PowerPowerAnalog supply & reference (Decoupled by C37 4.7µF, C20 100nF)
6PA0LED_BGPIO OutputIndicatorBlue Status LED (Active LOW, common anode to 3V3)
7PA1LED_RGPIO OutputIndicatorRed Status LED (Active LOW, common anode to 3V3)
8PA2ISENS_AADC1_INP14TelemetryDriver Channel A Current Sense feedback
9PA3ISENS_BADC1_INP15TelemetryDriver Channel B Current Sense feedback
10PA4ISENS_CADC1_INP18TelemetryDriver Channel C Current Sense feedback
11PA5ISENS_DADC1_INP19TelemetryDriver Channel D Current Sense feedback
12PA6IN_C1TIM3_CH1 / GPIOMotor DriverChannel C PWM / Direction Input 1
13PA7IN_C2TIM3_CH2 / GPIOMotor DriverChannel C PWM / Direction Input 2
14PB0IN_D2TIM3_CH3 / GPIOMotor DriverChannel D PWM / Direction Input 2
15PB1VSENSADC1_INP1TelemetryMotor / Bus Supply Voltage sense divider
16VCAPVCAPInternal RegulatorPowerLDO core supply bypass (C6 4.7µF to GND)
17VDD3V3Power SupplyPowerDigital 3.3V supply (Decoupled by C1 4.7µF, C7 100nF)
18PB15IN_B2TIM1_CH3N / GPIOMotor DriverChannel B PWM / Direction Input 2
19PA8IN_B1TIM1_CH1 / GPIOMotor DriverChannel B PWM / Direction Input 1
20PA9IN_D1TIM1_CH2 / GPIOMotor DriverChannel D PWM / Direction Input 1
21PA11RXUSART1_RX (AF7)Serial CommsUART Receive (routed to test point TP2)
22PA12TXUSART1_TX (AF7)Serial CommsUART Transmit (routed to test point TP1)
23PA13SWDIOSWDIO (AF0)DebugSerial Wire Debug Data (SWD)
24PA14SWCLKSWCLK (AF0)DebugSerial Wire Debug Clock (SWD)
25PA15IN_A1TIM2_CH1 / GPIOMotor DriverChannel A PWM / Direction Input 1
26PB3SCLI2C2_SCL (AF8)Host CommsHost I2C Clock (BareBoot bootloader target)
27PB4SDAI2C2_SDA (AF8)Host CommsHost I2C Data (BareBoot bootloader target)
28PB5IN_A2TIM3_CH2 / GPIOMotor DriverChannel A PWM / Direction Input 2
29PB63D_SCLI2C1_SCL (AF4)Sensor Bus3D Magnetic Sensor I2C Clock
30PB73D_SDAI2C1_SDA (AF4)Sensor Bus3D Magnetic Sensor I2C Data
31BOOT0BOOTBoot SelectorControlBoot configuration (10 kΩ pull-down R30 to GND)
32PB8NCNoneUnusedUnconnected / Not bonded out
33EPGNDGround PadGroundCentral thermal & ground pad to system GND

Mechanical Assembly

Motor Form Factor & Mounting

  • Target Motor: Standard micro N20 DC brushed metal gearmotor (nominal cross-section 12 × 10 mm) or auxiliary DC loads.
  • Gear Reduction Compatibility: Compatible with standard N20 reduction gearboxes (e.g. 50:1, 100:1, 150:1, 298:1).
  • External FPC Sensor Tail: The TI TMAG5273 3D magnetic position sensor is mounted on a flexible printed circuit (FPC) that connects to the FishyServo board via an FPC connector. This isolates the sensor IC from board mechanical constraints and enables placement directly at the rear shaft or output horn of the gearmotor.
  • Magnetic Angle Tracking: In closed-loop servo mode, a diametrically magnetized cylindrical neodymium magnet (such as 4 × 2 mm or 6 × 2.5 mm) is aligned axially on the motor shaft or output horn directly over the TMAG5273 package to supply magnetic flux vectors for XY angle computation.

Firmware & Control System

The firmware for FishyServo is officially maintained upstream within the BareFOC repository under examples/fishyservo_stm32h5. See firmware/README.md for quick build and flashing instructions.

Control Pipeline Architecture

flowchart TD
    subgraph host_subsystem ["Host Interface (Interrupt Executor)"]
        direction TB
        HostReq["Host Controller / Katapult"] -->|"I2C2 @ 0x40"| RegServ["barefoc-multibus Register Server"]
        RegServ <-->|"Atomic Mutex"| SharedState["SharedServoState Register Cache"]
    end

    subgraph control_loop ["1 kHz Motor Control Task (Thread Executor)"]
        direction TB
        SharedState -->|"Target & PID Gains"| PosPID["Outer Position PID Controller"]
        SensorRead["TMAG5273 Sensor (I2C1 FPC)"] -->|"Absolute Angle"| PosPID
        PosPID -->|"Velocity Setpoint"| VelPID["Inner Velocity Sub-Loop & Ramp Limiter"]
        VelPID -->|"Voltage Command"| LPF["Voltage & Current Limiter"]
        LPF -->|"25 kHz Complementary PWM"| MotorDriver["DCDriver2PWM (TIM1/TIM3)"]
    end

    subgraph feedback_subsystem ["Telemetry & Current Protection"]
        direction TB
        AdcCurrent["ADC1 PA5 (ISENS_D)"] -->|"CurrentMirrorSense (500 Rounds Calibration)"| LpfCurrent["Low-Pass Filter (Tf = 15 ms)"]
        LpfCurrent -->|"Live Current Telemetry"| SharedState
    end

    MotorDriver -->|"Bridge D PWM"| HBridge["TI DRV8411A Output"]
    HBridge -.->|"Current Mirror"| AdcCurrent

MultiBus Register Map

The host interface exposes a register map through barefoc-multibus over I2C2 (Target Address 0x40):

Register IDNameTypeAccessUnits / Description
0x00Statusu8RController status flags (0: Uninit, 1: Ready, 2: Fault)
0x01Targetf32R/WMotion target: angle in radians (Mode 0), rad/s (Mode 1), or Volts (Mode 2)
0x02Enableu8R/WMotor driver bridge enable (0: Hi-Z Coast, 1: Active)
0x03ControlModeu8ROperating mode: 0 = Angle (Closed-loop), 1 = Velocity Open-loop, 2 = Voltage
0x04Anglef32RFiltered shaft angle in radians from TMAG5273
0x05Velocityf32RAngular velocity in radians per second
0x06CurrentDCf32RMotor current in milliamperes (mA)
0x10AngPidPf32R/WAngle loop proportional gain (PangleP_{angle})
0x11VelPidPf32R/WVelocity sub-loop proportional gain (KpK_p)
0x12VelPidIf32R/WVelocity sub-loop integral gain (KiK_i)
0x13VelPidDf32R/WVelocity sub-loop derivative gain (KdK_d)
0x20VoltageLimitf32R/WDriver output voltage limit in Volts
0x21VelocityLimitf32R/WVelocity limit in radians per second

Bootloader & Upload Guide

  • BareBoot / Katapult Bootloader:
    • Located in Sector 0 of flash memory.
    • Receives firmware updates over the external I2C2 interface (PB3 SCL / PB4 SDA) at target address 0x40 using Katapult host tooling:
      python3 katapult/scripts/flashtool.py --ch341 -a 0x40 -f firmware.bin
      
    • An I2C target write of byte [0xF0] initiates software reset into bootloader mode.
  • Direct SWD Flashing:
    • Connect a debug probe (e.g. Raspberry Pi Debugprobe or ST-Link) to PA13 (SWDIO), PA14 (SWCLK), and GND:
      cargo run --release
      

EasyEDA Spark

Spark PCB Boards

This project's PCB and PCBA service was sponsored by the OSHWLab Spark campaign.