SimpleFOC examples for the Spectral Micro BLDC controller. A PlatformIO project that runs the SimpleFOC library on the driver's STM32F103CB in place of the Spectral firmware, with the board's pins, its MT6816 encoder, and two examples: open loop and closed-loop FOC.
Documentation · SimpleFOC docs · Examples · Discord
SimpleFOC is an alternative to the Spectral firmware, the driver's main firmware. The manual's SimpleFOC guide explains the setup.
git clone https://github.com/Source-Robotics/Spectral-Micro-Simple-FOC.git
Open the folder in VS Code with the PlatformIO extension
(SimpleFOC's own installation guide has the other ways). PlatformIO
fetches SimpleFOC (askuric/Simple FOC@^2.3.2) on the first build; Upload writes src/main.cpp to the driver
through ST-LINK.
You need the programming adapter (an ST-LINK with the 1.27 mm pogo-pin adapter for the JTAG pads), or any ST-LINK wired to 3.3 V, GND, SWDIO and SWCLK. The examples print on the driver's UART connector at 115200 baud: read it with the USB-to-serial adapter as in The serial terminal (3.3 V only).
Warning
This replaces the bootloader. This project has no upload offset, so an upload with the programming adapter
writes SimpleFOC at 0x08000000, over the bootloader. Firmware updates over UART or CAN stop working. To go back
to the Spectral firmware, install the bootloader again as in
Installing the bootloader on an older board,
then flash a release.
The heart of examples/open_loop_example.cpp, which spins the motor in open loop (// ... marks the lines left
out here):
#include "Arduino.h"
#include "SimpleFOC.h"
// Driver PWM pins
#define PWM_CH1 PA_2
#define PWM_CH2 PA_1
#define PWM_CH3 PA_0
// ...
// BLDC motor & driver instance
// BLDCMotor motor = BLDCMotor(pole pair number, phase resistance (optional) );
BLDCMotor motor = BLDCMotor(11);
// BLDCDriver3PWM( pin_pwmA, pin_pwmB, pin_pwmC )
BLDCDriver3PWM driver = BLDCDriver3PWM(PWM_CH1, PWM_CH2, PWM_CH3);
//target variable
float target_velocity = 0.1;
void Init_pins();
void setup() {
// Init driver enable pins and LED
Init_pins();
// pwm frequency to be used [Hz]
driver.pwm_frequency = 20000;
// power supply voltage [V]
driver.voltage_power_supply = 24;
// Max DC voltage allowed - default voltage_power_supply
driver.voltage_limit = 5;
// driver init
driver.init();
// link the motor and the driver
motor.linkDriver(&driver);
motor.controller = MotionControlType::velocity_openloop;
// init motor hardware
motor.init();
// ...
}
void loop() {
// open loop velocity movement
// using motor.voltage_limit and motor.velocity_limit
motor.move(target_velocity);
// monitoring function outputting motor variables to the serial terminal
motor.monitor();
}
// ...The pole pairs (BLDCMotor(11)) and the supply voltage (driver.voltage_power_supply = 24) are set in each
example: change them for your motor and supply.
How the examples set up the Spectral Micro:
| Signal | Pins | In the examples |
|---|---|---|
| PWM, phases A, B, C | PA2, PA1, PA0 | BLDCDriver3PWM, 20 kHz |
| Encoder, MT6816 on SPI | MOSI PB5, MISO PB4, CLK PB3, CS PA15 | MagneticSensorMT6816 on SPIClass SPI_2(MOSI, MISO, CLK) |
| Inline current sense, two phases | PA4, PA3; 0.025 Ω shunts, amplifier gain 20 | defined, not used yet |
| Serial, the UART connector | TX PB6, RX PB7 | HardwareSerial Serialx(RX_COM, TX_COM), used as Serial, 115200 baud |
| Driver enable pins and LED | PA9, PA8, PA10, PB15, PB14, PB13 | outputs, set HIGH by Init_pins() |
| Version | |
|---|---|
| Spectral Micro BLDC controller | PCB v2.0 (Hardware revisions) |
| SimpleFOC | 2.3.2 (askuric/Simple FOC@^2.3.2), the release the manual's guide was tested with |
| SimpleFOC drivers library | 1.0.8, in lib/Arduino-FOC-drivers-master (for the MT6816 encoder) |
| PlatformIO | platform ststm32, board genericSTM32F103CB, framework arduino, upload with stlink |
Back to the Spectral firmware and its releases: Firmware in the manual.
In examples. PlatformIO builds
and uploads src/main.cpp: to run an example, copy it over that file.
| Example | What it does |
|---|---|
open_loop_example.cpp |
spins the motor in open-loop velocity mode, without the encoder, at 0.1 rad/s with the driver's voltage limit at 5 V, and prints SimpleFOC's monitor output on the serial port |
voltage_torque_example.cpp |
closed-loop FOC with the MT6816 encoder: aligns the sensor at 3 V, then runs velocity control on voltage torque control (space-vector PWM, voltage limit 8 V) toward 60 rad/s and prints the target, velocity, angle, Uq and Iq; the lines for torque and angle control are there, commented out |
src/main.cpp |
the voltage-torque example with a target of 6 rad/s |
The SimpleFOC forum and Discord are the best place to look for help when you develop with the SimpleFOC library. To go further: Getting started, Writing the code and the SimpleFOC Driver and Support Library.
Ask in Discussions, chat on Discord, report a bug in Issues. Follow Source Robotics:
The examples are under the MIT License;
the SimpleFOC drivers library in lib/ keeps its own MIT License.