Marco Newman / CartesianRobot

Dependents:   CartesianRobot_Demo

Committer:
marcoanewman
Date:
Thu Dec 16 01:19:17 2021 +0000
Revision:
0:5af6295f4ec9
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcoanewman 0:5af6295f4ec9 1 #ifndef STEPPER_H
marcoanewman 0:5af6295f4ec9 2 #define STEPPER_H
marcoanewman 0:5af6295f4ec9 3
marcoanewman 0:5af6295f4ec9 4 #include "mbed.h"
marcoanewman 0:5af6295f4ec9 5
marcoanewman 0:5af6295f4ec9 6 class Stepper : public TimerEvent
marcoanewman 0:5af6295f4ec9 7 {
marcoanewman 0:5af6295f4ec9 8 public:
marcoanewman 0:5af6295f4ec9 9 // Create Stepper instance connected to pins step, direction, and enable
marcoanewman 0:5af6295f4ec9 10 Stepper(PinName step, PinName dir, PinName en);
marcoanewman 0:5af6295f4ec9 11
marcoanewman 0:5af6295f4ec9 12 // Set origin for motor absolute movement
marcoanewman 0:5af6295f4ec9 13 void setPositionZero(void);
marcoanewman 0:5af6295f4ec9 14 // Set limit for motor absolute movement
marcoanewman 0:5af6295f4ec9 15 void setPositionLimit(void);
marcoanewman 0:5af6295f4ec9 16 // Get limit of motor absolute movement
marcoanewman 0:5af6295f4ec9 17 int getPositionLimit(void);
marcoanewman 0:5af6295f4ec9 18
marcoanewman 0:5af6295f4ec9 19 // Stop motor
marcoanewman 0:5af6295f4ec9 20 void stop(void);
marcoanewman 0:5af6295f4ec9 21 // Move motor to absolute position
marcoanewman 0:5af6295f4ec9 22 void goTo(int position);
marcoanewman 0:5af6295f4ec9 23 // Move motor a set number of steps
marcoanewman 0:5af6295f4ec9 24 void move(int steps);
marcoanewman 0:5af6295f4ec9 25
marcoanewman 0:5af6295f4ec9 26 // Set rotation speed [steps/sec]
marcoanewman 0:5af6295f4ec9 27 void setSpeed(float speed);
marcoanewman 0:5af6295f4ec9 28 // Set acceleration ramp [steps/sec²] (0 = infinite acceleration)
marcoanewman 0:5af6295f4ec9 29 void setAcceleration(float acc);
marcoanewman 0:5af6295f4ec9 30 // Set deceleration ramp [steps/sec²] (0 = infinite deceleration)
marcoanewman 0:5af6295f4ec9 31 void setDeceleration(float dec);
marcoanewman 0:5af6295f4ec9 32
marcoanewman 0:5af6295f4ec9 33 // Enable motor driver
marcoanewman 0:5af6295f4ec9 34 void enable(void);
marcoanewman 0:5af6295f4ec9 35 // Disable motor driver
marcoanewman 0:5af6295f4ec9 36 void disable(void);
marcoanewman 0:5af6295f4ec9 37 // Check if motor driver is enabled
marcoanewman 0:5af6295f4ec9 38 bool enabled(void);
marcoanewman 0:5af6295f4ec9 39 // Check if motor is stopped
marcoanewman 0:5af6295f4ec9 40 bool stopped(void);
marcoanewman 0:5af6295f4ec9 41 // Get absolute position from origin
marcoanewman 0:5af6295f4ec9 42 int getPosition(void);
marcoanewman 0:5af6295f4ec9 43 // Get speed [steps/sec]
marcoanewman 0:5af6295f4ec9 44 float getSpeed(void);
marcoanewman 0:5af6295f4ec9 45 // Get acceperation [steps/sec²]
marcoanewman 0:5af6295f4ec9 46 float getAcceleration(void);
marcoanewman 0:5af6295f4ec9 47 // Get deceleration [steps/sec²]
marcoanewman 0:5af6295f4ec9 48 float getDeceleration(void);
marcoanewman 0:5af6295f4ec9 49
marcoanewman 0:5af6295f4ec9 50 // Enum for direction
marcoanewman 0:5af6295f4ec9 51 typedef enum {CW=1,CCW=0} direction;
marcoanewman 0:5af6295f4ec9 52
marcoanewman 0:5af6295f4ec9 53 protected:
marcoanewman 0:5af6295f4ec9 54 unsigned int nTo(float speed,float acc);
marcoanewman 0:5af6295f4ec9 55
marcoanewman 0:5af6295f4ec9 56 private:
marcoanewman 0:5af6295f4ec9 57 DigitalOut _step; // output step pin
marcoanewman 0:5af6295f4ec9 58 DigitalOut _dir; // output direction pin
marcoanewman 0:5af6295f4ec9 59 DigitalOut _en; // output enable pin
marcoanewman 0:5af6295f4ec9 60
marcoanewman 0:5af6295f4ec9 61 virtual void handler();
marcoanewman 0:5af6295f4ec9 62 enum {STOP,ACCEL,CRUISE,DECEL}_state; // motor state
marcoanewman 0:5af6295f4ec9 63
marcoanewman 0:5af6295f4ec9 64 int _pos; // motor position
marcoanewman 0:5af6295f4ec9 65 int _lim; // motor limit
marcoanewman 0:5af6295f4ec9 66 float _spd; // speed [step/s]
marcoanewman 0:5af6295f4ec9 67 float _acc; // acceleration [step/s²]
marcoanewman 0:5af6295f4ec9 68 float _dec; // decceleration [step/s²]
marcoanewman 0:5af6295f4ec9 69
marcoanewman 0:5af6295f4ec9 70 unsigned int _steps; // nbr total of steps per mov
marcoanewman 0:5af6295f4ec9 71
marcoanewman 0:5af6295f4ec9 72 unsigned int _dt0; // initial delay [µs]
marcoanewman 0:5af6295f4ec9 73 unsigned int _dtmin; // delay minimum [µs]
marcoanewman 0:5af6295f4ec9 74 unsigned int _dtn; // current delay
marcoanewman 0:5af6295f4ec9 75
marcoanewman 0:5af6295f4ec9 76 unsigned int _n; // steps counters
marcoanewman 0:5af6295f4ec9 77 unsigned int _nStartDec; // steps to decelerate
marcoanewman 0:5af6295f4ec9 78
marcoanewman 0:5af6295f4ec9 79 };
marcoanewman 0:5af6295f4ec9 80
marcoanewman 0:5af6295f4ec9 81 #endif