Skip to content

Commit

Permalink
arch: refactor espFoC on top of the Zephyr RTOS
Browse files Browse the repository at this point in the history
This refactor also changes the espFoC to be a motor
control application for esp32 SoCs instead of a
library.

Signed-off-by: Felipe Neves <[email protected]>
  • Loading branch information
uLipe committed Nov 14, 2024
1 parent b602c07 commit 100ca74
Show file tree
Hide file tree
Showing 32 changed files with 1,323 additions and 237 deletions.
37 changes: 26 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,43 @@
![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!);
* Uses openAMP to offload motor control tasks to one of the core, and leave the other for communication;
* support UART and CAN communication;
* Easy to wire motor using common drivers and I2C encoders out there!
* Uses ESP32 AMP solution for Zephyr RTOS to split execution;
* App CPU runs the motor control firmware;
* Pro CPU runs communication and exposes a thread for user application;
* Interaction between the firmwares are doing by a custom IPC protocol;
* Planned support for UART and CAN communication;

## Limitations:

* Support for esp32 and esp32s3 only;
* Requires and rotor position sensor, for example, incremental encoder.
* Support only for espressif SoC that are dual-core and have FPU;
* Once sensored support, requires an I2C magnetic encoder sensor.

## Getting started:

* Just clone this project on most convenient folder;
* Just clone this project on most convenient folder then:

```
$ west build -palways -besp32s3_devkitm/esp32s3/procpu --sysbuild /path/to/espFoC/app
```

* To flash:
```
$ west flash
```

## Typical wiring:

Expand All @@ -35,5 +51,4 @@ your esp32 chip into a dual axis PMSM motor controller chip
## Support:

If you find some trouble, open an issue, and if you are enjoying the project
give it a star or submir a PR. Also, you can try reaching me at:
[email protected]
give it a star or submir a PR. Also, you can try reaching me at:
20 changes: 20 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

set(REMOTE_ZEPHYR_DIR ${CMAKE_CURRENT_BINARY_DIR}/../espfoc_motor_remote/zephyr)

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

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

FILE(GLOB ipc ${MOTOR_CONTROL_SRC}/ipc/*.c)
target_sources(app PRIVATE espfoc_app/esp_foc_app_shell.c
espfoc_app/esp_foc_main.c
espfoc_app/esp_foc_user.c
${ipc})

target_include_directories(app PRIVATE ${MOTOR_CONTROL_INC})

7 changes: 7 additions & 0 deletions app/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source "share/sysbuild/Kconfig"

config ESP_FOC_REMOTE_BOARD
string
default "esp32_devkitc_wrover/esp32/appcpu" if $(BOARD) = "esp32_devkitc_wroom"
default "esp32_devkitc_wroom/esp32/appcpu" if $(BOARD) = "esp32_devkitc_wroom"
default "esp32s3_devkitm/esp32s3/appcpu" if $(BOARD) = "esp32s3_devkitm"
37 changes: 37 additions & 0 deletions app/boards/esp32s3_devkitm_procpu.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 Felipe Neves <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

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

&ipm0 {
status = "okay";
};

&timer0 {
status = "okay";
};

&timer1 {
status = "okay";
};

&timer2 {
status = "okay";
};

&timer3 {
status = "okay";
};
147 changes: 147 additions & 0 deletions app/espfoc_app/esp_foc_app_shell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* MIT License
*
* Copyright (c) 2021 Felipe Neves
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/shell/shell.h>
#include <espFoC/ipc/esp_foc_ipc.h>

static int cmd_set_position(const struct shell *shell, size_t argc, char **argv)
{
struct motor_command command;

if (argc != 2) {
return -EINVAL;
}

command.position_mdeg = (int32_t)(strtof(argv[1], NULL) * 1000.0f);
command.command_mask = MOTOR_CMD_POSITION_MASK;

return esp_foc_ipc_send_command(&command);
}

static int cmd_set_speed(const struct shell *shell, size_t argc, char **argv)
{
struct motor_command command;

if (argc != 2) {
return -EINVAL;
}

command.speed_mdps = (int32_t)(strtof(argv[1], NULL) * 1000.0f);
command.command_mask = MOTOR_CMD_SPEED_MASK;

return esp_foc_ipc_send_command(&command);
}

static int cmd_enable(const struct shell *shell, size_t argc, char **argv)
{
struct motor_command command;

if (argc != 1) {
return -EINVAL;
}

command.command_mask = MOTOR_CMD_SHUTDOWN_MASK | MOTOR_CMD_GLOBAL_ENABLE;
command.power_state = 1;

return esp_foc_ipc_send_command(&command);
}

static int cmd_disable(const struct shell *shell, size_t argc, char **argv)
{
struct motor_command command;

if (argc != 1) {
return -EINVAL;
}

command.command_mask = MOTOR_CMD_SHUTDOWN_MASK | MOTOR_CMD_GLOBAL_DISABLE;
command.power_state = 0;

return esp_foc_ipc_send_command(&command);
}

static int cmd_set_gains_pid_vel(const struct shell *shell, size_t argc, char **argv)
{
struct motor_command command;

if (argc != 4) {
return -EINVAL;
}

command.command_mask = MOTOR_CMD_SPD_PID_MASK;
command.pid_gains[0] = (int32_t)(strtof(argv[1], NULL) * 1e+6f);
command.pid_gains[1] = (int32_t)(strtof(argv[2], NULL) * 1e+6f);
command.pid_gains[2] = (int32_t)(strtof(argv[3], NULL) * 1e+6f);

return esp_foc_ipc_send_command(&command);

}

static int cmd_set_gains_pid_pos(const struct shell *shell, size_t argc, char **argv)
{
struct motor_command command;

if (argc != 4) {
return -EINVAL;
}

command.command_mask = MOTOR_CMD_POS_PID_MASK;
command.pid_gains[0] = (int32_t)(strtof(argv[1], NULL) * 1e+6f);
command.pid_gains[1] = (int32_t)(strtof(argv[2], NULL) * 1e+6f);
command.pid_gains[2] = (int32_t)(strtof(argv[3], NULL) * 1e+6f);

return esp_foc_ipc_send_command(&command);
}

static int cmd_set_maintenance_pwm(const struct shell *shell, size_t argc, char **argv)
{
struct motor_command command;

if (argc != 4) {
return -EINVAL;
}

command.command_mask = (MOTOR_CMD_MAITENANCE_MODE | MOTOR_CMD_MAITENANCE_PWMS);
command.pwms[0] = (int32_t)(strtof(argv[1], NULL) * 1e+6f);
command.pwms[1] = (int32_t)(strtof(argv[2], NULL) * 1e+6f);
command.pwms[2] = (int32_t)(strtof(argv[3], NULL) * 1e+6f);

return esp_foc_ipc_send_command(&command);
}

SHELL_STATIC_SUBCMD_SET_CREATE(
esp_foc,
SHELL_CMD(set_position, NULL, "sets position in degrees", cmd_set_position),
SHELL_CMD(set_speed, NULL, "sets speed in degrees per second", cmd_set_speed),
SHELL_CMD(enable, NULL, "enable motor driver", cmd_enable),
SHELL_CMD(disable, NULL, "disable motor driver", cmd_disable),
SHELL_CMD(set_gains_pid_vel, NULL, "set pids velocity", cmd_set_gains_pid_vel),
SHELL_CMD(set_gains_pid_pos, NULL, "set pids speed", cmd_set_gains_pid_pos),
SHELL_CMD(set_maintenance_pwm, NULL, "set pwms maitenance mode", cmd_set_maintenance_pwm),
SHELL_SUBCMD_SET_END
);

SHELL_CMD_REGISTER(esp_foc, &esp_foc, "espFoC Basic shell commands", NULL);
101 changes: 101 additions & 0 deletions app/espfoc_app/esp_foc_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* MIT License
*
* Copyright (c) 2021 Felipe Neves
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <stdio.h>
#include <string.h>
#include <zephyr/kernel.h>
#include <espFoC/motor_control/esp_foc.h>
#include <espFoC/ipc/esp_foc_ipc.h>
#include <zephyr/drivers/counter.h>

static const struct device *timer_dev = DEVICE_DT_GET( DT_NODELABEL(timer3));
static struct counter_alarm_cfg alarm_cfg;
static struct motor_state motor_fw_state;
K_SEM_DEFINE(sync,0,1);

static void esp_foc_motor_on_state (const struct motor_state *state);
ESP_FOC_DEFINE_IPC_CALLBACK(state_callback, NULL, esp_foc_motor_on_state);

static void esp_foc_motor_on_state (const struct motor_state *state)
{
memcpy(&motor_fw_state, state, sizeof(motor_fw_state));
k_sem_give(&sync);
}

static void espfoc_pilot_motor_firmware(struct k_work *w)
{
counter_set_channel_alarm(timer_dev, 0, &alarm_cfg);
struct motor_command command;
command.command_mask = MOTOR_CMD_SET_FEEDBACK;
command.encoder_reading = 1.0f;
command.motor_number = 0;
//esp_foc_ipc_send_command(&command);

}
K_WORK_DEFINE(motor_cfw_work, espfoc_pilot_motor_firmware);

static void espfoc_timer_isr(const struct device *counter_dev,
uint8_t chan_id, uint32_t ticks,
void *user_data)
{
k_work_submit(&motor_cfw_work);
}

int esp_foc_early_init(void)
{
esp_foc_ipc_init();
esp_foc_ipc_register_callback(&state_callback);
k_msleep(50);

return 0;
}
SYS_INIT(esp_foc_early_init, POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY);

int main (void)
{
/**TODO use the main application to implement espfoc commander
* Through UART, SPI, I2C and CAN, last one is the priority
*/
printk("espFoC is running in the %s\n", CONFIG_BOARD);

counter_start(timer_dev);
alarm_cfg.flags = 0;
alarm_cfg.ticks = counter_us_to_ticks(timer_dev, ESP_FOC_INNER_CONTROL_US_PERIOD);
alarm_cfg.callback = espfoc_timer_isr;
alarm_cfg.user_data = &alarm_cfg;
counter_set_channel_alarm(timer_dev, 0, &alarm_cfg);

while(1) {
k_sem_take(&sync, K_FOREVER);
printk("espFoc Motor firmware uptime: %u [ms] \n", motor_fw_state.motor_system_uptime);
printk("espFoc Motor firmware timestep: %u [us] \n", motor_fw_state.timestamp_us);
printk("espFoC Motor system status: %d \n", motor_fw_state.system_enabled);
}
}

__weak void esp_foc_user_entry(void)
{
printk("Weak user entry of the espFoC \n");
}
K_THREAD_DEFINE(foc_tid, 4096, esp_foc_user_entry, NULL, NULL, NULL, 1, 0, 0);
Loading

0 comments on commit 100ca74

Please sign in to comment.