arduino 步进电机驱动库_分享好人经验:自己写个库,控制TB6560驱动步进电机

本文介绍了如何使用EasyStepper库来驱动步进电机,包括初始化、设置方向、旋转速度、停止及运行等功能。通过实例代码展示了如何控制TB6560步进电机的正反转,并提供了调试模式以输出相关信息。
摘要由CSDN通过智能技术生成

#include "EasyStepper.h"

EasyStepper::EasyStepper(byte stepPin, byte directionPin, byte enablePin, bool directionPinsInverted, bool enablePinsInverted)

{

// save the parameters

this->stepPin = stepPin;

this->directionPin = directionPin;

this->enablePin = enablePin;

this->directionPinsInverted = directionPinsInverted;

this->enablePinsInverted = enablePinsInverted;

}

void EasyStepper::startup()

{

// set the pin mode

pinMode(this->stepPin, OUTPUT);

pinMode(this->directionPin, OUTPUT);

pinMode(this->enablePin, OUTPUT);

// enable the stepper

digitalWrite(enablePin, HIGH ^ this->enablePinsInverted);

// initialize the done to true

this->done = true;

}

void EasyStepper::shutdown()

{

// disable the stepper

digitalWrite(enablePin, LOW ^ this->enablePinsInverted);

}

void EasyStepper::debugMode(boolean enabled)

{

this->debugModeFlag = enabled;

}

void EasyStepper::rotate(float speed, int steps)

{

// ignore the zero value

if (speed != 0 && steps != 0)

{

if (steps > 0)

{

// CW

digitalWrite(directionPin, HIGH ^ this->directionPinsInverted);

if (this->debugModeFlag)

{

Serial.println("CW");

}

}

else if (steps < 0)

{

// CCW

digitalWrite(directionPin, LOW ^ this->directionPinsInverted);

if (this->debugModeFlag)

{

Serial.println("CCW");

}

}

this->done = false;

// the steps to go

this->stepsToGo = abs(steps);

// the steps gone

this->stepsGone = 0;

// change the speed to stepTime, micro seconds per step

this->stepTime = 1000.0 * 1000.0 / abs(speed);

// current timestamp

unsigned long time = micros();

this->nextTimestamp = time + this->stepTime;

if (this->debugModeFlag)

{

Serial.print("stepsToGo=");

Serial.print(this->stepsToGo);

Serial.print(", stepTime=");

Serial.print(this->stepTime);

Serial.print(", currentTimestamp=");

Serial.print(time);

Serial.print(", nextTimestamp=");

Serial.println(this->nextTimestamp);

}

// call the step method to rotate the motor

this->step();

}

}

void EasyStepper::stop()

{

this->stepsToGo = 0;

this->done = true;

}

void EasyStepper::run()

{

// the current timestamp

unsigned long time = micros();

if (time >= this->nextTimestamp && !this->done)

{

this->step();

}

if (this->debugModeFlag)

{

Serial.print("currentTimestamp=");

Serial.println(time);

}

}

void EasyStepper::step()

{

// are there some steps to rotate?

if (this->stepsToGo > this->stepsGone)

{

// HIGH value

digitalWrite(stepPin, HIGH);

// delay

delayMicroseconds(2);

// LOW value

digitalWrite(stepPin, LOW);

// increase the stepsGone

this->stepsGone++;

// current timestamp

unsigned long time = micros();

this->nextTimestamp = time + this->stepTime;

if (this->debugModeFlag)

{

Serial.print("stepsGone=");

Serial.print(stepsGone);

Serial.print(", currentTimestamp=");

Serial.print(time);

Serial.print(", nextTimestamp=");

Serial.println(this->nextTimestamp);

}

}

else

{

this->done = true;

}

}

boolean EasyStepper::isDone()

{

return this->done;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值