├── .gitignore ├── .vscode └── extensions.json ├── LICENSE ├── README.md ├── gifs ├── b-g431b-esc1-discovery-kit.png ├── b-g431b-esc1-drone_x4.png ├── b-g431b-esc1-hall-sensor.png ├── b-g431b-esc1-pwm.png ├── b-g431b-esc1-shunt.png └── skate-static.jpg ├── include └── README ├── lib └── README ├── platformio.ini ├── src └── main.cpp └── test └── README /.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .vscode/.browse.c_cpp.db* 3 | .vscode/c_cpp_properties.json 4 | .vscode/launch.json 5 | .vscode/ipch 6 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 3 | // for the documentation about the extensions.json format 4 | "recommendations": [ 5 | "platformio.platformio-ide" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Owen Williams 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 3hr Electric Skate Code/Build with SimpleFOC using b-g431b-esc1 stm32 discovery kit for drones 2 | 3 | I'm using this [b-g431b-esc1](https://www.st.com/en/evaluation-tools/b-g431b-esc1.html) discovery kit to drive and control an electric BLDC motor with hallsensors. It uses SimpleFOC in closed loop voltage mode. 4 | 5 | This is the code that accompanies my 3hr code/build video. It's not identical, it has more debug code and some motor calibration to help with different motors: 6 | 7 | [![3hr Electric Skate Code/Build with SimpleFOC](http://img.youtube.com/vi/BfrIjmR5qrM/0.jpg)](https://youtu.be/BfrIjmR5qrM) 8 | 9 | Please note that this board at time of writing needs a couple of Pull Requests to be approved to be 'easy' to use from platformio and Arduino IDE: 10 | 11 | https://github.com/stm32duino/Arduino_Core_STM32/pull/1236 - this will add the board to the stm32duino board manager list. If you have added stm32duino as a board manager and this PR has been approved you should see it in the list of boards within Arduino 12 | 13 | https://github.com/platformio/platform-ststm32/pull/447 - this adds extra info required by platformio to find use the board. 14 | 15 | Of lesser importance - I managed to get breakpoint debugging to work for stm32g4 boards like this one, again this needs another PR to be approved before it becomes easy from platformio: 16 | https://sourceforge.net/p/openocd/code/merge-requests/16/ 17 | 18 | ## STM32 datasheet 19 | The stm32 user manual for this board is excellent: 20 | https://www.st.com/resource/en/user_manual/dm00564746-electronic-speed-controller-discovery-kit-for-drones-with-stm32g431cb-stmicroelectronics.pdf 21 | 22 | Some pics from the manual 23 | ![b-g431b-esc1 discovery-kit](./gifs/b-g431b-esc1-discovery-kit.png) ![b-g431b-esc1 drone x4](./gifs/b-g431b-esc1-drone_x4.png) ![b-g431b-esc1 hall sensors](./gifs/b-g431b-esc1-hall-sensor.png) ![b-g431b-esc1 pwm](./gifs/b-g431b-esc1-pwm.png) ![b-g431b-esc1 shunt and opamp](./gifs/b-g431b-esc1-shunt.png) 24 | 25 | ## Board with Duct tape 26 | ![skate](./gifs/skate-static.jpg) 27 | -------------------------------------------------------------------------------- /gifs/b-g431b-esc1-discovery-kit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owennewo/simplefoc_skateboard_g431b/823ff7bf400cab6de0e17f19a6c24b6190eeb21d/gifs/b-g431b-esc1-discovery-kit.png -------------------------------------------------------------------------------- /gifs/b-g431b-esc1-drone_x4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owennewo/simplefoc_skateboard_g431b/823ff7bf400cab6de0e17f19a6c24b6190eeb21d/gifs/b-g431b-esc1-drone_x4.png -------------------------------------------------------------------------------- /gifs/b-g431b-esc1-hall-sensor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owennewo/simplefoc_skateboard_g431b/823ff7bf400cab6de0e17f19a6c24b6190eeb21d/gifs/b-g431b-esc1-hall-sensor.png -------------------------------------------------------------------------------- /gifs/b-g431b-esc1-pwm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owennewo/simplefoc_skateboard_g431b/823ff7bf400cab6de0e17f19a6c24b6190eeb21d/gifs/b-g431b-esc1-pwm.png -------------------------------------------------------------------------------- /gifs/b-g431b-esc1-shunt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owennewo/simplefoc_skateboard_g431b/823ff7bf400cab6de0e17f19a6c24b6190eeb21d/gifs/b-g431b-esc1-shunt.png -------------------------------------------------------------------------------- /gifs/skate-static.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/owennewo/simplefoc_skateboard_g431b/823ff7bf400cab6de0e17f19a6c24b6190eeb21d/gifs/skate-static.jpg -------------------------------------------------------------------------------- /include/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project header files. 3 | 4 | A header file is a file containing C declarations and macro definitions 5 | to be shared between several project source files. You request the use of a 6 | header file in your project source file (C, C++, etc) located in `src` folder 7 | by including it, with the C preprocessing directive `#include'. 8 | 9 | ```src/main.c 10 | 11 | #include "header.h" 12 | 13 | int main (void) 14 | { 15 | ... 16 | } 17 | ``` 18 | 19 | Including a header file produces the same results as copying the header file 20 | into each source file that needs it. Such copying would be time-consuming 21 | and error-prone. With a header file, the related declarations appear 22 | in only one place. If they need to be changed, they can be changed in one 23 | place, and programs that include the header file will automatically use the 24 | new version when next recompiled. The header file eliminates the labor of 25 | finding and changing all the copies as well as the risk that a failure to 26 | find one copy will result in inconsistencies within a program. 27 | 28 | In C, the usual convention is to give header files names that end with `.h'. 29 | It is most portable to use only letters, digits, dashes, and underscores in 30 | header file names, and at most one dot. 31 | 32 | Read more about using header files in official GCC documentation: 33 | 34 | * Include Syntax 35 | * Include Operation 36 | * Once-Only Headers 37 | * Computed Includes 38 | 39 | https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html 40 | -------------------------------------------------------------------------------- /lib/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project specific (private) libraries. 3 | PlatformIO will compile them to static libraries and link into executable file. 4 | 5 | The source code of each library should be placed in a an own separate directory 6 | ("lib/your_library_name/[here are source files]"). 7 | 8 | For example, see a structure of the following two libraries `Foo` and `Bar`: 9 | 10 | |--lib 11 | | | 12 | | |--Bar 13 | | | |--docs 14 | | | |--examples 15 | | | |--src 16 | | | |- Bar.c 17 | | | |- Bar.h 18 | | | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html 19 | | | 20 | | |--Foo 21 | | | |- Foo.c 22 | | | |- Foo.h 23 | | | 24 | | |- README --> THIS FILE 25 | | 26 | |- platformio.ini 27 | |--src 28 | |- main.c 29 | 30 | and a contents of `src/main.c`: 31 | ``` 32 | #include 33 | #include 34 | 35 | int main (void) 36 | { 37 | ... 38 | } 39 | 40 | ``` 41 | 42 | PlatformIO Library Dependency Finder will find automatically dependent 43 | libraries scanning project source files. 44 | 45 | More information about PlatformIO Library Dependency Finder 46 | - https://docs.platformio.org/page/librarymanager/ldf.html 47 | -------------------------------------------------------------------------------- /platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:disco_b_g431b_esc1] 12 | platform = ststm32 13 | board = disco_b_g431b_esc1 14 | framework = arduino 15 | monitor_speed = 115200 16 | monitor_port = /dev/ttyUSB0 17 | debug_init_break = 18 | lib_archive = no 19 | lib_deps = 20 | https://github.com/simplefoc/Arduino-FOC#dev 21 | SPI 22 | Wire 23 | aau-eit5/iBUS @ ^0.1.7 -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | HardwareSerial Serial1(USART2_RX, USART2_TX); 6 | BLDCDriver6PWM driver(PHASE_UH, PHASE_UL, PHASE_VH, PHASE_VL, PHASE_WH, PHASE_WL); 7 | BLDCMotor motor(6); 8 | HallSensor sensor(HALL1, HALL2, HALL3, 6); 9 | 10 | void hallA() { 11 | sensor.handleA(); 12 | } 13 | 14 | void hallB() { 15 | sensor.handleB(); 16 | } 17 | 18 | void hallC() { 19 | sensor.handleC(); 20 | } 21 | 22 | volatile long pwm_on_micros; // from 1000 to 2000 23 | 24 | void pwm1() { 25 | static bool last_state; 26 | static long last_start_time; 27 | unsigned long now = micros(); 28 | bool state_now = digitalRead(PWM); 29 | if (state_now != last_state) { 30 | if (state_now) { 31 | last_start_time = now; 32 | } else { 33 | pwm_on_micros = now - last_start_time; 34 | } 35 | last_state = state_now; 36 | } 37 | } 38 | 39 | void setup() { 40 | 41 | Serial1.begin(115200); 42 | delay(1000); 43 | 44 | attachInterrupt(digitalPinToInterrupt(PWM), pwm1, CHANGE); 45 | sensor.enableInterrupts(hallA, hallB, hallC); 46 | sensor.init(); 47 | 48 | motor.linkDriver(&driver); 49 | motor.linkSensor(&sensor); 50 | motor.controller = ControlType::voltage; 51 | 52 | driver.voltage_power_supply = 24; 53 | motor.voltage_limit = 5; 54 | motor.voltage_sensor_align = 1; 55 | motor.useMonitoring(Serial1); 56 | 57 | driver.init(); 58 | motor.init(); 59 | // motor.initFOC(2.1, Direction::CCW); 60 | motor.initFOC(); 61 | } 62 | 63 | void loop() { 64 | float voltage_target = map(pwm_on_micros,1000,2000,-500,500)/100.0; // map from -5v to +5v 65 | motor.loopFOC(); 66 | motor.move(voltage_target); 67 | motor.monitor(); 68 | } -------------------------------------------------------------------------------- /test/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for PlatformIO Unit Testing and project tests. 3 | 4 | Unit Testing is a software testing method by which individual units of 5 | source code, sets of one or more MCU program modules together with associated 6 | control data, usage procedures, and operating procedures, are tested to 7 | determine whether they are fit for use. Unit testing finds problems early 8 | in the development cycle. 9 | 10 | More information about PlatformIO Unit Testing: 11 | - https://docs.platformio.org/page/plus/unit-testing.html 12 | --------------------------------------------------------------------------------