Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
GyverLibs committed Feb 18, 2022
1 parent 85119b1 commit 0de37bf
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 63 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Foo](https://img.shields.io/badge/Version-2.4-brightgreen.svg?style=flat-square)](#versions)
[![Foo](https://img.shields.io/badge/Version-2.5-brightgreen.svg?style=flat-square)](#versions)
[![Foo](https://img.shields.io/badge/Website-AlexGyver.ru-blue.svg?style=flat-square)](https://alexgyver.ru/)
[![Foo](https://img.shields.io/badge/%E2%82%BD$%E2%82%AC%20%D0%9D%D0%B0%20%D0%BF%D0%B8%D0%B2%D0%BE-%D1%81%20%D1%80%D1%8B%D0%B1%D0%BA%D0%BE%D0%B9-orange.svg?style=flat-square)](https://alexgyver.ru/support_alex/)

Expand Down Expand Up @@ -780,6 +780,7 @@ void loop() {
- v2.2.1 - небольшая оптимизация SRAM
- v2.3 - fix compiler warnings, поддержка esp32
- v2.4 - повышена плавность движения шаговиков в Planner и Planner2. Исправлена бага в Stepper2
- v2.5 - исправлено плавное изменение скорости для KEEP_SPEED

<a id="feedback"></a>
## Баги и обратная связь
Expand Down
4 changes: 2 additions & 2 deletions examples/Planner/PlannerArray/PlannerArray.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ int path[][2] = {
int nodeAmount = sizeof(path) / 4;

#include "GyverPlanner.h"
Stepper<STEPPER2WIRE> stepper1(2, 3);
Stepper<STEPPER2WIRE> stepper2(4, 5);
Stepper<STEPPER2WIRE> stepper1(2, 5);
Stepper<STEPPER2WIRE> stepper2(3, 6);
GPlanner<STEPPER2WIRE, 2> planner;

void setup() {
Expand Down
4 changes: 2 additions & 2 deletions examples/Planner/PlannerCircle/PlannerCircle.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ int radius = 100; // радиус круга
int32_t path[pointAm + 2][2]; // буфер круга

#include "GyverPlanner.h"
Stepper<STEPPER2WIRE> stepper1(2, 3);
Stepper<STEPPER2WIRE> stepper2(4, 5);
Stepper<STEPPER2WIRE> stepper1(2, 5);
Stepper<STEPPER2WIRE> stepper2(3, 6);
GPlanner<STEPPER2WIRE, 2> planner;

void setup() {
Expand Down
4 changes: 2 additions & 2 deletions examples/Planner/PlannerControl/PlannerControl.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// r - продолжить

#include "GyverPlanner.h"
Stepper<STEPPER2WIRE> stepper1(2, 3);
Stepper<STEPPER2WIRE> stepper2(4, 5);
Stepper<STEPPER2WIRE> stepper1(2, 5);
Stepper<STEPPER2WIRE> stepper2(3, 6);
GPlanner<STEPPER2WIRE, 2> planner;

void setup() {
Expand Down
4 changes: 2 additions & 2 deletions examples/Planner/PlannerDemo/PlannerDemo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

// МОТОРЫ ДОЛЖНЫ БЫТЬ С ОДИНАКОВЫМ ТИПОМ ДРАЙВЕРА
// вот они красавцы
Stepper<STEPPER2WIRE> stepper1(2, 3);
Stepper<STEPPER2WIRE> stepper2(4, 5);
Stepper<STEPPER2WIRE> stepper1(2, 5);
Stepper<STEPPER2WIRE> stepper2(3, 6);

// создаём планировщик, указываем в <> тип драйвера КАК У МОТОРОВ
// и количество осей, равное количеству моторов (любое больше 1)
Expand Down
4 changes: 2 additions & 2 deletions examples/Planner/PlannerHoming/PlannerHoming.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#define LIMSW_Y 7

#include "GyverPlanner.h"
Stepper<STEPPER2WIRE> stepper1(2, 3);
Stepper<STEPPER2WIRE> stepper2(4, 5);
Stepper<STEPPER2WIRE> stepper1(2, 5);
Stepper<STEPPER2WIRE> stepper2(3, 6);
GPlanner<STEPPER2WIRE, 2> planner;

void setup() {
Expand Down
4 changes: 2 additions & 2 deletions examples/Planner/PlannerSpeedControl/PlannerSpeedControl.ino
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// крутим мотор. Отправляй в сериал целое число, шаг/сек

#include "GyverPlanner.h"
Stepper<STEPPER2WIRE> stepper1(2, 3);
Stepper<STEPPER2WIRE> stepper2(4, 5);
Stepper<STEPPER2WIRE> stepper1(2, 5);
Stepper<STEPPER2WIRE> stepper2(3, 6);
GPlanner<STEPPER2WIRE, 2> planner;

void setup() {
Expand Down
4 changes: 2 additions & 2 deletions examples/Planner/PlannerTimerISR/PlannerTimerISR.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// используется Timer1 на atmega328

#include "GyverPlanner.h"
Stepper<STEPPER2WIRE> stepper1(2, 3);
Stepper<STEPPER2WIRE> stepper2(4, 5);
Stepper<STEPPER2WIRE> stepper1(2, 5);
Stepper<STEPPER2WIRE> stepper2(3, 6);
GPlanner<STEPPER2WIRE, 2> planner;

void setup() {
Expand Down
4 changes: 2 additions & 2 deletions examples/Planner2/PlannerArray/PlannerArray.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ int path[][2] = {
int nodeAmount = sizeof(path) / 4;

#include "GyverPlanner2.h"
Stepper<STEPPER2WIRE> stepper1(2, 3);
Stepper<STEPPER2WIRE> stepper2(4, 5);
Stepper<STEPPER2WIRE> stepper1(2, 5);
Stepper<STEPPER2WIRE> stepper2(3, 6);
GPlanner2<STEPPER2WIRE, 2> planner;

void setup() {
Expand Down
4 changes: 2 additions & 2 deletions examples/Planner2/PlannerCircle/PlannerCircle.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ int32_t path[pointAm + 1 + 1][2]; // буфер круга
// +1 на стартовую точку +1 на замыкание круга

#include "GyverPlanner2.h"
Stepper<STEPPER2WIRE> stepper1(2, 3);
Stepper<STEPPER2WIRE> stepper2(4, 5);
Stepper<STEPPER2WIRE> stepper1(2, 5);
Stepper<STEPPER2WIRE> stepper2(3, 6);
GPlanner2<STEPPER2WIRE, 2> planner;

void setup() {
Expand Down
4 changes: 2 additions & 2 deletions examples/Planner2/PlannerCircleISR/PlannerCircleISR.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ int32_t path[pointAm + 1 + 1][2]; // буфер круга
// +1 на стартовую точку +1 на замыкание круга

#include "GyverPlanner2.h"
Stepper<STEPPER2WIRE> stepper1(2, 3);
Stepper<STEPPER2WIRE> stepper2(4, 5);
Stepper<STEPPER2WIRE> stepper1(2, 5);
Stepper<STEPPER2WIRE> stepper2(3, 6);
GPlanner2<STEPPER2WIRE, 2> planner;

void setup() {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=GyverStepper
version=2.4
version=2.5
author=AlexGyver <alex@alexgyver.ru>
maintainer=AlexGyver <alex@alexgyver.ru>
sentence=Fast library for stepmotor control and multi-axis planning
Expand Down
65 changes: 26 additions & 39 deletions src/GyverStepper.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
v2.2.1 - небольшая оптимизация SRAM
v2.3 - fix compiler warnings, поддержка esp32
v2.4 - повышена плавность движения шаговиков в Planner и Planner2. Исправлена бага в Stepper2
v2.5 - исправлено плавное изменение скорости для KEEP_SPEED
*/

/*
Expand Down Expand Up @@ -179,8 +180,7 @@ void attachPower(handler)
// =========== МАКРОСЫ ===========
#define degPerMinute(x) ((x)/60.0f)
#define degPerHour(x) ((x)/3600.0f)
#define _sign(x) ((x) >= 0 ? 1 : -1) // знак числа
#define maxMacro(a,b) ((a)>(b)?(a):(b)) // привет esp
#define _sign(x) ((x) >= 0 ? 1 : -1) // знак числа

// =========== КОНСТАНТЫ ===========
#define _MIN_SPEED_FP 5 // мин. скорость для движения в FOLLOW_POS с ускорением
Expand All @@ -203,10 +203,11 @@ class GStepper : public Stepper<_DRV, _TYPE> {
public:
// конструктор
GStepper(int stepsPerRev, uint8_t pin1 = 255, uint8_t pin2 = 255, uint8_t pin3 = 255, uint8_t pin4 = 255, uint8_t pin5 = 255) :
_stepsPerDeg(stepsPerRev / 360.0), Stepper<_DRV, _TYPE> (pin1, pin2, pin3, pin4, pin5) {
Stepper<_DRV, _TYPE> (pin1, pin2, pin3, pin4, pin5) {
// умолчания
setMaxSpeed(300);
setAcceleration(300);
_stepsPerDeg = (stepsPerRev / 360.0);
}

// ============================== TICK ==============================
Expand All @@ -218,7 +219,7 @@ class GStepper : public Stepper<_DRV, _TYPE> {
if (!_curMode && _accel != 0 && _maxSpeed >= _MIN_SPEED_FP) planner(); // планировщик скорости FOLLOW_POS быстрый
#endif

if (_smoothStart && _curMode) smoothSpeedPlanner(); // планировщик скорости KEEP_SPEED
if (_curMode && _accel != 0) smoothSpeedPlanner(); // планировщик скорости KEEP_SPEED

if (tickUs - _prevTime >= stepTime) { // основной таймер степпера
_prevTime = tickUs;
Expand Down Expand Up @@ -295,7 +296,7 @@ class GStepper : public Stepper<_DRV, _TYPE> {
// установка максимальной скорости в шагах/секунду
void setMaxSpeed(float speed) {
speed = abs(speed);
_maxSpeed = maxMacro(speed, _MIN_STEP_SPEED); // 1 шаг в час минимум
_maxSpeed = max(speed, _MIN_STEP_SPEED); // 1 шаг в час минимум
// считаем stepTime для низких скоростей или отключенного ускорения
if (_accel == 0 || _maxSpeed < _MIN_SPEED_FP) stepTime = 1000000.0 / _maxSpeed;

Expand Down Expand Up @@ -367,31 +368,28 @@ class GStepper : public Stepper<_DRV, _TYPE> {
void setSpeed(float speed, bool smooth = false) { // smooth убран!
// 1 шаг в час минимум
_speed = speed;
if (abs(_speed) < _MIN_STEP_SPEED) _speed = _MIN_STEP_SPEED * _sign(_speed);
enable();
_stopF = (_speed == 0);
if (_speed == 0 && _accelSpeed == 0) return;
dir = (_speed > 0) ? 1 : -1;
if (abs(_speed) < _MIN_STEP_SPEED) _speed = _MIN_STEP_SPEED * dir;

if (_accel != 0) { // плавный старт
if (_accelSpeed != _speed) {
_smoothStart = true;
#ifdef __AVR__
_smoothPlannerPrd = map(max(abs((int)_speed), abs((int)_accelSpeed)), 1000, 20000, 15000, 1000);
#else
// горячий привет авторам ядра esp8266
int speed1 = abs(_speed);
int speed2 = abs((int)_accelSpeed);
int maxSpeed = maxMacro(speed1, speed2);
_smoothPlannerPrd = map(maxSpeed, 1000, 20000, 15000, 1000);
#endif
_smoothPlannerPrd = constrain(_smoothPlannerPrd, 15000, 1000);
int speed1 = (int)abs(_speed);
int speed2 = (int)abs(_accelSpeed);
_speedPlannerPrd = map(max(speed1, speed2), 1000, 20000, 15000, 2000);
_speedPlannerPrd = constrain(_speedPlannerPrd, 15000, 2000);
stepTime = abs(1000000.0 / _accelSpeed);
}
} else { // резкий старт
if (speed == 0) { // скорость 0? Отключаемся и выходим
brake();
return;
}
_accelSpeed = _speed;
stepTime = round(1000000.0 / abs(_speed));
stepTime = abs(1000000.0 / _speed);
}
enable();
}

// установка целевой скорости в градусах/секунду
Expand All @@ -412,7 +410,6 @@ class GStepper : public Stepper<_DRV, _TYPE> {
// установка режима работы
void setRunMode(GS_runMode mode){
_curMode = mode;
if (mode == FOLLOW_POS) _smoothStart = false;
resetTimers();
}

Expand Down Expand Up @@ -468,7 +465,7 @@ class GStepper : public Stepper<_DRV, _TYPE> {
}
// аккуратно сбросить все таймеры
void resetTimers() {
_smoothPlannerTime = _plannerTime = _prevTime = micros();
_speedPlannerTime = _plannerTime = _prevTime = micros();
}


Expand Down Expand Up @@ -541,40 +538,30 @@ class GStepper : public Stepper<_DRV, _TYPE> {

// ======================= SPEED PLANNER =======================
float _accelTime = 0;
uint16_t _smoothPlannerPrd = 15000;
uint32_t _smoothPlannerTime = 0;
uint16_t _speedPlannerPrd = 15000;
uint32_t _speedPlannerTime = 0;
uint32_t _plannerTime = 0;

// планировщик разгона для KEEP_SPEED
void smoothSpeedPlanner() {
if (tickUs - _smoothPlannerTime >= _smoothPlannerPrd) {
_smoothPlannerTime = tickUs;
int8_t dir = _sign(_speed - _accelSpeed); // 1 - разгон, -1 - торможение
_accelSpeed += (_accelTime * _smoothPlannerPrd * dir);
if (tickUs - _speedPlannerTime >= _speedPlannerPrd) {
_speedPlannerTime = tickUs;
_accelSpeed += (_accelTime * _speedPlannerPrd * _sign(_speed - _accelSpeed));
dir = _sign(_accelSpeed);

// прекращение работы планировщика
if ((dir == 1 && _accelSpeed >= _speed) || (dir == -1 && _accelSpeed <= _speed)) {
_accelSpeed = _speed;
_smoothStart = false;
if (abs(_speed) <= _MIN_STEP_SPEED) { // если нужно остановиться
brake();
return; // выходим
}
}
stepTime = abs(1000000.0 / _accelSpeed);
if (_stopF && abs(_accelSpeed) <= _MIN_STEP_SPEED) brake();
}
}

// ========================= VARIABLES =========================
const float _stepsPerDeg;
bool _stopF = 0;
float _stepsPerDeg;
uint32_t _prevTime = 0;
float _accelSpeed = 0;
int32_t _target = 0;
volatile uint32_t tickUs = 0;
bool _workState = false;
bool _autoPower = false;
bool _smoothStart = false;
float _stopSpeed = 0;
float _maxSpeed = 300;
float _speed = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/StepperCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ class Stepper {
// шажочек степдир
void stepDir() {
if (_TYPE == STEPPER_PINS) {
setPin(1, (dir > 0 ? _globDir : !_globDir) ); // DIR
setPin(1, (dir > 0) ^ _globDir); // DIR
setPin(0, 1); // step HIGH
if (DRIVER_STEP_TIME > 0) delayMicroseconds(DRIVER_STEP_TIME);
setPin(0, 0); // step LOW
} else if (*_step) {
_step(dir > 0 ? _globDir : !_globDir);
_step((dir > 0) ^ _globDir);
}
}

Expand Down

0 comments on commit 0de37bf

Please sign in to comment.