Created wacky_bully project + a bit of work towards implementing motor drivers

This commit is contained in:
2026-05-08 16:38:10 +12:00
parent 52aac06b2c
commit baf2f0be9e
10 changed files with 7963 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.22)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(wacky_bully)

View File

@@ -0,0 +1,3 @@
idf_component_register(SRCS "drv8701.c"
INCLUDE_DIRS "include"
REQUIRES esp_driver_mcpwm esp_driver_gpio)

View File

@@ -0,0 +1,105 @@
#include <math.h>
#include <stdio.h>
#include "drv8701.h"
#include "driver/mcpwm_cmpr.h"
#include "driver/mcpwm_gen.h"
#include "driver/mcpwm_oper.h"
#include "driver/mcpwm_timer.h"
#include "driver/mcpwm_types.h"
#include "driver/gpio.h"
#include "esp_err.h"
#include "freertos/idf_additions.h"
#include "hal/gpio_types.h"
#include "portmacro.h"
#define PWM_RESOLUTION_HZ 2000000
#define PWM_TIMEBASE_PERIOD PWM_RESOLUTION_HZ/20000
void drv8701_task(void *args) {
drv8701_task_config_t *motor_cfg = ((drv8701_task_config_t *)args);
motor_cfg->in_ctrl_queue = xQueueCreate(3, sizeof(float));
motor_cfg->current_mutex = xSemaphoreCreateMutex();
// --------------------- Configure PWM -------------------------
mcpwm_timer_handle_t timer = NULL;
mcpwm_timer_config_t timer_cfg = {
.group_id = motor_cfg->pwm_group_num,
.clk_src = MCPWM_TIMER_CLK_SRC_DEFAULT,
.resolution_hz = PWM_RESOLUTION_HZ,
.count_mode = MCPWM_TIMER_COUNT_MODE_UP,
.period_ticks = PWM_TIMEBASE_PERIOD,
};
ESP_ERROR_CHECK(mcpwm_new_timer(&timer_cfg, &timer));
mcpwm_oper_handle_t oper = NULL;
mcpwm_operator_config_t oper_cfg = {
.group_id = motor_cfg->pwm_group_num,
};
ESP_ERROR_CHECK(mcpwm_new_operator(&oper_cfg, &oper));
ESP_ERROR_CHECK(mcpwm_operator_connect_timer(oper, timer));
mcpwm_cmpr_handle_t cmpr = NULL;
mcpwm_comparator_config_t cmpr_cfg = {
.flags.update_cmp_on_tep = true,
};
ESP_ERROR_CHECK(mcpwm_new_comparator(oper, &cmpr_cfg, &cmpr));
mcpwm_gen_handle_t gen = NULL;
mcpwm_generator_config_t gen_cfg = {
.gen_gpio_num = motor_cfg->en_pin,
};
ESP_ERROR_CHECK(mcpwm_new_generator(oper, &gen_cfg, &gen));
ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(cmpr, 0));
ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(gen,
MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, MCPWM_TIMER_EVENT_EMPTY, MCPWM_GEN_ACTION_HIGH)));
ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(gen,
MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP, cmpr, MCPWM_GEN_ACTION_LOW)));
ESP_ERROR_CHECK(mcpwm_timer_enable(timer));
ESP_ERROR_CHECK(mcpwm_timer_start_stop(timer, MCPWM_TIMER_START_NO_STOP));
// --------------------- Configure Direction -------------------------
printf("Configuring Direction\n");
gpio_config_t phase_cfg = {
.pin_bit_mask = 1ULL << motor_cfg->ph_pin,
.intr_type = GPIO_INTR_DISABLE,
.mode = GPIO_MODE_OUTPUT,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.pull_up_en = GPIO_PULLUP_DISABLE
};
gpio_config(&phase_cfg);
// Main Loop
float target_velocity = 0.0;
while(1) {
// Get new velocity if it has changed
if(xQueueReceive(motor_cfg->in_ctrl_queue, &target_velocity, 0)){
target_velocity = fmaxf(-1.0, fminf(1.0, target_velocity));
}
uint32_t abs_scaled_vel = (uint32_t) (fabsf(target_velocity) * ((float)PWM_TIMEBASE_PERIOD));
mcpwm_comparator_set_compare_value(cmpr, abs_scaled_vel);
if (target_velocity > 0.0) {
gpio_set_level(motor_cfg->ph_pin, 1);
}
else {
gpio_set_level(motor_cfg->ph_pin, 0);
}
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}
void set_motor_vel(drv8701_task_config_t *motor_cfg, float velocity) {
xQueueSend(motor_cfg->in_ctrl_queue, &velocity, 0);
}

View File

@@ -0,0 +1,38 @@
#ifndef DRV8701_H
#define DRV8701_H
#include "freertos/idf_additions.h"
// Enable PWM for speed control
// Phase chooses direction
typedef struct {
uint8_t en_pin;
uint8_t ph_pin;
uint8_t pwm_group_num;
// Do not populate, done as part of task creation
QueueHandle_t in_ctrl_queue;
SemaphoreHandle_t current_mutex;
} drv8701_task_config_t;
/**
* @brief Launch the motor driver task
*
* @param motor_cfg The configuration for the motor driver
*/
void drv8701_task(void *args);
/**
* @brief Set the velocity of a specific motor driver
*
* @param motor_cfg The configuration for the specific motor driver
* @param velocity -1.0 to 1.0
*/
void set_motor_vel(drv8701_task_config_t *motor_cfg, float velocity);
#endif

View File

@@ -0,0 +1,10 @@
dependencies:
idf:
source:
type: idf
version: 6.1.0
direct_dependencies:
- idf
manifest_hash: e44bf68eca6b7b264ddae08cd014cd3294c0473230381b6d6f88ed18ec879038
target: esp32s3
version: 3.0.0

View File

@@ -0,0 +1,3 @@
idf_component_register(SRCS "wacky_bully.c"
REQUIRES "drv8701" freertos
INCLUDE_DIRS ".")

View File

@@ -0,0 +1,16 @@
## IDF Component Manager Manifest File
dependencies:
## Required IDF version
idf:
version: ">=4.1.0"
# # Put list of dependencies here
# # For components maintained by Espressif:
# component: "~1.0.0"
# # For 3rd party components:
# username/component: ">=1.0.0,<2.0.0"
# username2/component2:
# version: "~1.0.0"
# # For transient dependencies `public` flag can be set.
# # `public` flag doesn't have an effect dependencies of the `main` component.
# # All dependencies of `main` are public by default.
# public: true

View File

@@ -0,0 +1,51 @@
#include <stdio.h>
#include "drv8701.h"
#include "freertos/idf_additions.h"
#include "portmacro.h"
#include "soc/gpio_num.h"
drv8701_task_config_t motor1 = {
.ph_pin = GPIO_NUM_13,
.en_pin = GPIO_NUM_14,
.pwm_group_num = 0,
};
drv8701_task_config_t motor2 = {
.ph_pin = GPIO_NUM_21,
.en_pin = GPIO_NUM_45,
.pwm_group_num = 0,
};
drv8701_task_config_t motor3 = {
.ph_pin = GPIO_NUM_9,
.en_pin = GPIO_NUM_10,
.pwm_group_num = 1,
};
drv8701_task_config_t motor4 = {
.ph_pin = GPIO_NUM_11,
.en_pin = GPIO_NUM_12,
.pwm_group_num = 1,
};
void app_main(void)
{
printf("Starting\n");
xTaskCreate(drv8701_task, "Motor1 Task", 2048, (void *)&motor1, 4, NULL);
xTaskCreate(drv8701_task, "Motor2 Task", 2048, (void *)&motor2, 4, NULL);
xTaskCreate(drv8701_task, "Motor3 Task", 2048, (void *)&motor3, 4, NULL);
xTaskCreate(drv8701_task, "Motor4 Task", 2048, (void *)&motor4, 4, NULL);
vTaskDelay(500 / portTICK_PERIOD_MS);
float motor1_vel = 0.0;
while(true) {
motor1_vel = motor1_vel + 0.1;
if (motor1_vel > 1.0)
motor1_vel = -1.0;
printf("Motor1 velocity: %0.2f\n", motor1_vel);
set_motor_vel(&motor1, motor1_vel);
set_motor_vel(&motor2, motor1_vel);
set_motor_vel(&motor3, motor1_vel);
set_motor_vel(&motor4, motor1_vel);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff