Carbon225 / esp32-dshot

DSHOT600 driver with command support for ESP-IDF
MIT License
18 stars 1 forks source link

Examples #2

Closed VTeselkin closed 1 year ago

VTeselkin commented 1 year ago

Could you please add some examples of using your library. Unable to start the engine. I hear a maximum of two signals during initialization.

Carbon225 commented 1 year ago

Try this:

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "DShotRMT.h"

#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
#include "esp_log.h"

static const char *TAG = "dshot-example";

#define DSHOT_RMT_CHANNEL RMT_CHANNEL_0
#define DSHOT_GPIO GPIO_NUM_25

#define MIN_THROTTLE 48
#define FULL_THROTTLE 2047

DShotRMT esc;

static void rampThrottle(int start, int stop, int step)
{
    if (step == 0)
        return;

    for (int i = start; step > 0 ? i < stop : i > stop; i += step)
    {
        esc.sendThrottle(i);
        vTaskDelay(1);
    }
    esc.sendThrottle(stop);
}

extern "C" void app_main(void)
{
    ESP_LOGI(TAG, "Initializing DShot RMT");
    ESP_ERROR_CHECK(esc.install(DSHOT_GPIO, DSHOT_RMT_CHANNEL));
    ESP_ERROR_CHECK(esc.init());

    ESP_LOGW(TAG, "Reversing");
    ESP_ERROR_CHECK(esc.setReversed(true));

    {
        ESP_LOGI(TAG, "Ramping throttle");
        rampThrottle(MIN_THROTTLE, FULL_THROTTLE, 20);

        ESP_LOGI(TAG, "Full throttle");
        const TickType_t holdStop = xTaskGetTickCount() + (3000 / portTICK_PERIOD_MS);
        while (xTaskGetTickCount() < holdStop)
        {
            esc.sendThrottle(FULL_THROTTLE);
            vTaskDelay(1);
        }

        ESP_LOGI(TAG, "Ramping down");
        rampThrottle(FULL_THROTTLE, MIN_THROTTLE, -20);
    }

    ESP_LOGW(TAG, "Normal direction");
    ESP_ERROR_CHECK(esc.setReversed(false));

    {
        ESP_LOGI(TAG, "Ramping throttle");
        rampThrottle(MIN_THROTTLE, FULL_THROTTLE, 20);

        ESP_LOGI(TAG, "Full throttle");
        const TickType_t holdStop = xTaskGetTickCount() + (3000 / portTICK_PERIOD_MS);
        while (xTaskGetTickCount() < holdStop)
        {
            esc.sendThrottle(FULL_THROTTLE);
            vTaskDelay(1);
        }

        ESP_LOGI(TAG, "Ramping down");
        rampThrottle(FULL_THROTTLE, MIN_THROTTLE, -20);
    }

    ESP_LOGW(TAG, "Exiting");
}

I had this on my computer and if you confirm it works I will add it as an official example.

Carbon225 commented 1 year ago

The trick is constantly feeding the motor commands or it will disarm.

VTeselkin commented 1 year ago

it`s example for espidf platform?

Carbon225 commented 1 year ago

Yes

Carbon225 commented 1 year ago

You can clone this repo into the components directory of your project.

VTeselkin commented 1 year ago

I have an Arduino project...

Carbon225 commented 1 year ago

It should still work if you "transplant" the example code into an arduino project with a setup() and loop(). I don't know how to install IDF projects into arduino but maybe just placing the library .cpp and .h next to your main file will work. I mainly wanted to support IDF since it's not very popular and doesn't have many libraries like arduino.

VTeselkin commented 1 year ago

All worked! But I use this example with arduino sdk...