Skip to content

Commit

Permalink
arch: refactor espFoC to be a motor control solution instead
Browse files Browse the repository at this point in the history
of a library

Signed-off-by: Felipe Neves <[email protected]>
  • Loading branch information
uLipe committed Aug 31, 2024
1 parent b602c07 commit 9dd619a
Show file tree
Hide file tree
Showing 27 changed files with 612 additions and 187 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
![Build](https://github.com/uLipe/espFoC/workflows/Build/badge.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

espFoC is a simple implementation of voltage mode, vector controller intended to be used with permanent-magnet synchronous motors (PMSM), and general brushless motors. This component was developed to be used with Zephyr RTOS for example and is aimed to transform
your esp32 chip into a dual axis PMSM motor controller chip
espFoC is a simple implementation of voltage mode, vector controller intended to be used with permanent-magnet synchronous motors (PMSM), and general brushless motors for ESP32S3 Espressif SoC, other SoC from espressif might be supported but the primary requirement is to be a dual
core variant. espFoC started as a library for motor control, but it suffered a goal change, and now it is intended to create a programmable
BLDC (FoC based) motor control-IC, the dual core capabillity splits the controller into application side and motor control core side
the former is intended for user to write it own application or exchange packets with external controller via CAN-Bus, the later is
100% focused on perform motor control algorithm achieving better control bandwidth. Up two axis are supported in this new variant, refer the
simplified architecture below:
![espFoC Simplified Architecture](/doc/images/arch.png)

## Features:

* Voltage mode control, control a PMSM like a DC motor!;
* Position and Speed closed-loop control;
* Single-precision Floating point implementation;
* Sample inverter driver based on esp32 LEDC PWM (easy to wire!);
* Sample rotor position driver based on as5600 encoder (very popular!);
* Easy to wire motor using common drivers and I2C encoders out there!
* Uses openAMP to offload motor control tasks to one of the core, and leave the other for communication;
* support UART and CAN communication;

## Limitations:

* Support for esp32 and esp32s3 only;
* Support for esp32 and esp32s3(primary) only;
* Requires and rotor position sensor, for example, incremental encoder.

## Getting started:
Expand Down
40 changes: 40 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

set(REMOTE_ZEPHYR_DIR ${CMAKE_CURRENT_BINARY_DIR}/ipm_esp32_appcpu-prefix/src/ipm_esp32_appcpu-build/zephyr)

if("${BOARD}" STREQUAL "esp32s3_devkitm/esp32s3/procpu")
set(BOARD_REMOTE "esp32s3_devkitm/esp32s3/appcpu")
else()
message(FATAL_ERROR "${BOARD} was not supported for this project yet!")
endif()

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(espfoc)

set(MOTOR_CONTROL_SRC ${APPLICATION_SOURCE_DIR}/../common)
set(MOTOR_CONTROL_INC ${APPLICATION_SOURCE_DIR}/../common/include/espFoC)

FILE(GLOB ipc ${MOTOR_CONTROL_SRC}/ipc/*.c)

set_source_files_properties(${REMOTE_ZEPHYR_DIR}/esp32_appcpu_firmware.c PROPERTIES GENERATED TRUE)
target_sources(app PRIVATE esp_foc_app_core/esp_foc_app.c
esp_foc_app_core/esp_foc_app_shell.c
${ipc}
${REMOTE_ZEPHYR_DIR}/esp32_appcpu_firmware.c)

target_include_directories(app PRIVATE ${MOTOR_CONTROL_INC})

include(ExternalProject)

ExternalProject_Add(
espfoc_remote
SOURCE_DIR ${APPLICATION_SOURCE_DIR}/espfoc_motor_core
INSTALL_COMMAND ""
CMAKE_CACHE_ARGS -DBOARD:STRING=${BOARD_REMOTE}
BUILD_BYPRODUCTS "${REMOTE_ZEPHYR_DIR}/${KERNEL_BIN_NAME}"
BUILD_ALWAYS True
)

add_dependencies(app espfoc_remote)
1 change: 1 addition & 0 deletions app/boards/esp32s3_devkitm.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_SOC_ESP32S3_PROCPU=y
19 changes: 19 additions & 0 deletions app/boards/esp32s3_devkitm.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) 2023 Felipe Neves <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

/ {
chosen {
/*
* shared memory reserved for the inter-processor communication
*/
zephyr,ipc_shm = &shm0;
zephyr,ipc = &ipm0;
};
};

&ipm0 {
status = "okay";
};
File renamed without changes.
Empty file.
19 changes: 19 additions & 0 deletions app/espfoc_motor_core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(espfoc_remote)

set(MOTOR_CONTROL_SRC ${APPLICATION_SOURCE_DIR}/../../common)
set(MOTOR_CONTROL_INC ${APPLICATION_SOURCE_DIR}/../../common/include/espFoC)

FILE(GLOB ipc ${MOTOR_CONTROL_SRC}/ipc/*.c)
FILE(GLOB motor_control ${MOTOR_CONTROL_SRC}/motor_control/*.c)

target_sources(app PRIVATE esp_foc_motor.c
esp_foc_hardware_if.c
${ipc}
${motor_control})

target_include_directories(app PRIVATE ${MOTOR_CONTROL_INC})
168 changes: 168 additions & 0 deletions app/espfoc_motor_core/boards/esp32s3_devkitm_appcpu.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2023 Felipe Neves <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/dt-bindings/i2c/i2c.h>
#include <zephyr/dt-bindings/pwm/pwm.h>
#include <dt-bindings/pinctrl/esp32s3-pinctrl.h>

/ {
chosen {
/*
* shared memory reserved for the inter-processor communication
*/
zephyr,ipc_shm = &shm0;
zephyr,ipc = &ipm0;
};

aliases {
motor-pwm-0 = &ledc0;
motor-pwm-1 = &ledc1;
angle-0 = &i2c0;
angle-0 = &i2c1;
};

gpio_enable {
compatible = "gpio-keys";
inverter_enable_0: button {
label = "inverter_enable";
gpios = <&gpio0 9 GPIO_ACTIVE_HIGH>;
};

inverter_enable_1: button {
label = "inverter_enable";
gpios = <&gpio0 10 GPIO_ACTIVE_HIGH>;
};

};
};

&pinctrl {
ledc0_default: ledc0_default {
group1 {
pinmux = <LEDC_CH0_GPIO2>;
output-enable;
};
group2 {
pinmux = <LEDC_CH1_GPIO3>;
output-enable;
};
group3 {
pinmux = <LEDC_CH2_GPIO4>;
output-enable;
};

};

i2c0_default: i2c0_default {
group1 {
pinmux = <I2C0_SDA_GPIO17>,
<I2C0_SCL_GPIO18>;
bias-pull-up;
drive-open-drain;
output-high;
};
};

ledc1_default: ledc1_default {
group1 {
pinmux = <LEDC_CH0_GPIO5>;
output-enable;
};
group2 {
pinmux = <LEDC_CH1_GPIO6>;
output-enable;
};
group3 {
pinmux = <LEDC_CH2_GPIO7>;
output-enable;
};

};

i2c1_default: i2c1_default {
group1 {
pinmux = <I2C1_SDA_GPIO19>,
<I2C1_SCL_GPIO20>;
bias-pull-up;
drive-open-drain;
output-high;
};
};

};

&ipm0 {
status = "okay";
};

&ledc0 {
pinctrl-0 = <&ledc0_default>;
pinctrl-names = "default";
status = "okay";
#address-cells = <1>;
#size-cells = <0>;
channel0@0 {
reg = <0x0>;
timer = <0>;
};

channel1@0 {
reg = <0x0>;
timer = <0>;
};

channel2@0 {
reg = <0x0>;
timer = <0>;
};
};

&i2c0 {
pinctrl-0 = <&i2c0_default>;
pinctrl-names = "default";
status = "okay";
angle_sensor_0: as5600@36 {
compatible = "ams,as5600";
status = "okay";
reg = <0x36>
};
};

&ledc1 {
pinctrl-0 = <&ledc1_default>;
pinctrl-names = "default";
status = "okay";
#address-cells = <1>;
#size-cells = <0>;
channel0@0 {
reg = <0x0>;
timer = <0>;
};

channel1@0 {
reg = <0x0>;
timer = <0>;
};

channel2@0 {
reg = <0x0>;
timer = <0>;
};
};

&i2c1 {
pinctrl-0 = <&i2c1_default>;
pinctrl-names = "default";
status = "okay";
angle_sensor_0: as5600@36 {
compatible = "ams,as5600";
status = "okay";
reg = <0x36>
};
};

&timer3 {
status = "okay";
};
Loading

0 comments on commit 9dd619a

Please sign in to comment.