-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallbackStepper.h
63 lines (52 loc) · 3.04 KB
/
CallbackStepper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Author : HH-1 (https://github.com/HH-1)
// A library to control stepper motor with 3 pins controller.
// Tested and develloped for TB-6600 controller and Arduino Uno Rev3
#pragma once
#include <Arduino.h>
class CallbackStepper{
public:
// Constructor. It is possible to run multiple motors from the same arduino. They must be controled throught 3 pins : enable(EN+), direction(DIR+) and step(STP+).
// You have to provide the step number (characteristic of the motor), the minimum and maximum speed in rotations per minute and the acceleration (increase in speed at each revolution)
// The last argument define if you want the motor to hold at start.
CallbackStepper(int Pin_EN_5v, int Pin_DIR_5v, int Pin_STP_5v, int steps_per_Revolution = 200, double min_speed = 100.0, double max_speed = 250.0, double acc = 200.0, bool hold_on_start = false);
// Those two functions allow you to run the motor with algebraic number of steps.
// the callback is a function that will be executed every periodicity steps and at the end if steps is not an even number of periodicity. It's argument is the algebraic number of steps performed since the last call.
// periodicity is the number of steps performed between each call of callback.
//This function will run accelerating from the minimum speed to the maximum speed at the beginning. Then at max speed and finally between max speed and minimum speed.
void runWithAcceleration(long steps, void callback(int), int periodicity=200);
// This function will run the motor at maximum speed.
void runWithoutAcceleration(long steps, void callback(int), int periodicity=200);
void CallbackStepper::runAccelerationDeceleration(long steps, void callback(int), int periodicity=200);
void CallbackStepper::runAccelerationNoDeceleration(long steps, void callback(int), int periodicity=200);
double getMinSpeed();
double getMaxSpeed();
double getAcc();
int getStepsPerRevolution();
double setMinSpeed(double newSpeed);
double setMaxSpeed(double newSpeed);
double setAcc(double newAcc);
int setStepsPerRevolution(int new_steps_per_revolution);
// allow the motor to hold it's position.
void startHolding();
// disable holding position.
void stopHolding();
private:
int _Pin_EN_5v;
int _Pin_DIR_5v;
int _Pin_STP_5v;
double _min_speed;
double _max_speed;
double _acc;
bool _holding;
int _steps_per_revolution;
double _StepsToAccelerate;
// Compute the number of steps needed to go from min to max speed according to acceleration.
long setStepsToAccelerate();
void run(long steps, void callback(int), int periodicity, bool accelerate, bool decelerate=true);
// compute the speed for the next move.
double computeActualSpeed(double actualSpeed, long absStepsToPerform, int i, int &accelerationPhase, double maxSpeedPossible, bool decelerate);
void setDirection(int direction);
int getDirection(long nbStepsToPerform);
void performOneStep(int stepDelay);
long computeStepDelay(double speed_value);
};