Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stepper motor driver firmware to sra-board-component #77

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion .github/workflows/test-build-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
image: espressif/idf:release-v4.1
strategy:
matrix:
test-apps: [bar_graph, lsa, motor_driver_normal, motor_driver_parallel, mpu6050, servos, switches, oled]
test-apps: [bar_graph, lsa, motor_driver_normal, motor_driver_parallel, mpu6050, servos, switches, oled, stepper_motor]
steps:
- name: Force Install GIT latest
run: |
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(srcs "src/i2cdev.c" "src/adc.c" "src/bar_graph.c" "src/switches.c" "src/lsa.c" "src/motor_driver.c" "src/mpu6050.c" "src/servo.c" "src/utils.c")
set(srcs "src/i2cdev.c" "src/adc.c" "src/bar_graph.c" "src/switches.c" "src/lsa.c" "src/motor_driver.c" "src/mpu6050.c" "src/servo.c" "src/utils.c" "src/stepper.c")
set(include_dirs include)

if(CONFIG_ENABLE_OLED)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ _For more examples, please refer to the [Documentation](https://sravjti.tech/sra
| [Servos](https://github.com/SRA-VJTI/sra-board-component/tree/main/examples/servos) | Servos are used in the locomotion of bots like [Wall-E](https://github.com/SRA-VJTI/Wall-E_v2.1) . This example is for using the servo port on SRA Board to run servos |
| [Switches](https://github.com/SRA-VJTI/sra-board-component/tree/main/examples/switches) | Manual Switches provide the traditional comfort to toggle the input variables. This example is for using four user switches present on SRA Board |
| [OLED](https://github.com/SRA-VJTI/sra-board-component/tree/main/examples/oled) | Example for initialising the OLED and diplaying the "Hello World" on the screen |
| [Stepper Motor](https://github.com/SRA-VJTI/sra-board-component/tree/main/examples/stepper_motor) | Example for testing the stepper motor driver A4988 along with a bipolar stepper motor using ESP32 |

<!-- ROADMAP -->
## Roadmap
Expand Down
8 changes: 7 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ Example for using two motor drivers present on SRA Board in both parallel and no

- Manual Switches provide the traditional comfort to toggle the input variables

- [Example](https://github.com/SRA-VJTI/sra-board-component/tree/main/examples/switches) for using four user switches present on SRA Board
- [Example](https://github.com/SRA-VJTI/sra-board-component/tree/main/examples/switches) for using four user switches present on SRA Board

### Stepper Motor

- Stepper motors are used in application involving powerful and precise rotary movements

- [Example](https://github.com/SRA-VJTI/sra-board-component/tree/main/examples/stepper_motor)
8 changes: 8 additions & 0 deletions examples/stepper_motor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# The following 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.5)

set(EXTRA_COMPONENT_DIRS ../../.)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(stepper_motor)
21 changes: 21 additions & 0 deletions examples/stepper_motor/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Society of Robotics and Automation
ChinmayLonkar marked this conversation as resolved.
Show resolved Hide resolved

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.
9 changes: 9 additions & 0 deletions examples/stepper_motor/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := stepper_motor

include $(IDF_PATH)/make/project.mk

8 changes: 8 additions & 0 deletions examples/stepper_motor/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Edit following two lines to set component requirements (see docs)
set(COMPONENT_REQUIRES )
set(COMPONENT_PRIV_REQUIRES )

set(COMPONENT_SRCS "main.c")
set(COMPONENT_ADD_INCLUDEDIRS "")

register_component()
8 changes: 8 additions & 0 deletions examples/stepper_motor/main/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#
35 changes: 35 additions & 0 deletions examples/stepper_motor/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "sra_board.h"

static const char* TAG = "STEPPER_DEBUG";

stepper_config_t stepper_1 = {
ChinmayLonkar marked this conversation as resolved.
Show resolved Hide resolved
.step_pin = STEP,
.dir_pin = DIR,
.accl_decel = 0.001,
.min_step_interval_us = MIN_STEP_INTERVAL,
.steps_per_revolution = 200,
.current_step_count = 0,
};

void run_motor(void *arg)
{
while (1)
{
ESP_LOGI(TAG, "Inside task");
move_to_step(&stepper_1, 10000);
ESP_LOGI(TAG, "Done 1");
vTaskDelay(1000 / portTICK_PERIOD_MS);
move_to_step(&stepper_1, 0);
ESP_LOGI(TAG, "Done 2");
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

void app_main(void)
{
init_stepper(&stepper_1);
xTaskCreatePinnedToCore(&run_motor, "Homing Task", 4096, NULL, 1, NULL, 0);
}
1 change: 1 addition & 0 deletions examples/stepper_motor/sdkconfigs.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n
3 changes: 3 additions & 0 deletions include/pin_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,7 @@
#define SERVO_D 18
/////////////////////////////

#define STEP 14
#define DIR 27
Comment on lines +108 to +109
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STEP_DIR would be better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These macros define the pins to which the STEP pin and DIRECTION pins are connected to ESP32.
we cannot name it STEP_DIR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is DIR okay then?

Copy link
Member

@SAtacker SAtacker Jan 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also is STEP okay? it needs to mean something atleast

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make it STEP_SIGNAL_PIN and DIR_SIGNAL_PIN

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DIR_SIGNAL_PIN should be related to the stepper.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CC @gautam-dev-maker suggest a better naming scheme


#endif
1 change: 1 addition & 0 deletions include/sra_board.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "servo.h"
#include "pin_defs.h"
#include "utils.h"
#include "stepper.h"

#ifdef CONFIG_ENABLE_OLED
#include "oled.h"
Expand Down
123 changes: 123 additions & 0 deletions include/stepper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* MIT License
*
* Copyright (c) 2021 Society of Robotics and Automation
*
* 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.
*/

#ifndef STEPPER_H
#define STEPPER_H

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "driver/gpio.h"
#include "esp_timer.h"
#include "esp32/rom/ets_sys.h"
#include "esp_log.h"
#include "esp_err.h"
#include "esp_sleep.h"
#include "pin_defs.h"

#define MIN_STEP_INTERVAL 1000

typedef struct
{
int step_pin; // Stepping Signal Pin of ESP32.
int dir_pin; // Direction signal Pin of ESP32.
int min_step_interval_us; // minimum step interval in microseconds
int steps_per_revolution; // Number of Step pulses per revolution.
int prev_delay; // Internal varaible to store previous pulse interval.
int dir; // Internal variable to store the direction of stepper.
float accl_decel; // Accelaration/Decelaration value.
bool movement_done; // To check if movement is done.
long current_step_count; // Internal variable to store current position of stepper motor.
} stepper_config_t;

/** @struct stepper_config_t
* @brief This structure contains the configuration of stepper.
* @var stepper_config_t::step_pin
* Member 'step_pin' contains the gpio pin number to which step signal pin of stepper motor driver is connected
* @var stepper_config_t::dir_pin
* Member 'dir_pin' contains the gpio pin number to which direction signal pin of stepper motor driver is connected
* @var stepper_config_t::min_step_interval_us
* Member 'min_step_interval_us' contains the minimum pulse interval for the step signal of stepper motor
* @var stepper_config_t::steps_per_revolution
* Member 'steps_per_revolution' contains number of step pulses per revolution.
* @var stepper_config_t::prev_delay
* Member 'prev_delay' contains internal varaible to store previous pulse interval.
* @var stepper_config_t::dir
* Member 'dir' contains internal variable to store the direction of stepper.
* @var stepper_config_t::accl_decel
* Member 'accl_decel' contains accelaration/decelaration value.
* @var stepper_config_t::movement_done
* Member 'movement_done' contains variable to check if movement is done.
* @var stepper_config_t::current_step_count
* Member 'current_step_count' contains internal variable to store current position of stepper motor.
*/

/**
* @brief Initiates and configures ESP32 for stepper control

* @param stepper pointer to stepper_config_t struct

* @return esp_err_t i.e it shows if pins are configured successfully or not.
**/
esp_err_t init_stepper(stepper_config_t *stepper);

/**
* @brief calculates first pulse interval delay as per the accelaration value.

* @param stepper pointer to stepper_config_t struct

* @return int containing first pulse interval delay as per the accelaration value.
**/
int calc_first_delay_in_millis(stepper_config_t *stepper);

/**
* @brief calculates delay for next step interval for accelaration.

* @param stepper pointer to stepper_config_t struct

* @return int containing delay for next step interval for accelaration.
**/
int cal_delay_accl(stepper_config_t *stepper, int n);
ChinmayLonkar marked this conversation as resolved.
Show resolved Hide resolved
ChinmayLonkar marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief calculates delay for next step interval for decelaration.

* @param stepper pointer to stepper_config_t struct

* @return int containing delay for next step interval for decelaration.
**/
int cal_delay_decel(stepper_config_t *stepper, int n);
ChinmayLonkar marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief moves stepper to a specified position as per number of steps.

* @param stepper pointer to stepper_config_t struct.
* @param pos_step step position to which stepper should be moved.

* @return esp_err_t i.e it shows if stepper has moved successfully or not.
**/
esp_err_t move_to_step(stepper_config_t *stepper, float pos_step);

#endif
Loading