uStepper S
uStepperDriver.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: uStepperDriver.h *
3 * Version: 2.0.0 *
4 * Date: March 30th, 2020 *
5 * Author: Thomas Hørring Olsen *
6 * *
7 *********************************************************************************************
8 * (C) 2020 *
9 * *
10 * uStepper ApS *
11 * www.ustepper.com *
12 * administration@ustepper.com *
13 * *
14 * The code contained in this file is released under the following open source license: *
15 * *
16 * Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International *
17 * *
18 * The code in this file is provided without warranty of any kind - use at own risk! *
19 * neither uStepper ApS nor the author, can be held responsible for any damage *
20 * caused by the use of the code contained in this file ! *
21 * *
22 ********************************************************************************************/
33 #include <Arduino.h>
34 #include <uStepperS.h>
35 
36 /* TMC5130 Register Address Defines */
37 
38 #define GCONF 0x00
40 #define DIRECTION(n) (((n)&0x1)<<4)
41 #define EN_PWM_MODE(n) (((n)&0x1)<<2)
42 #define I_SCALE_ANALOG(n) (((n)&0x1)<<0)
44 #define GSTAT 0x01
45 #define X_COMPARE 0x05
46 #define IHOLD_IRUN 0x10
47 #define TPOWERDOWN 0x11
48 #define TSTEP 0x12
49 #define TPWMTHRS 0x13
50 #define TCOOLTHRS 0x14
51 #define THIGH 0x15
52 #define RAMPMODE 0x20
53 #define XACTUAL 0x21
54 #define VACTUAL 0x22
57 #define VSTART_REG 0x23
58 #define A1_REG 0x24
59 #define V1_REG 0x25
60 #define AMAX_REG 0x26
61 #define VMAX_REG 0x27
62 #define DMAX_REG 0x28
63 #define D1_REG 0x2A
64 #define VSTOP_REG 0x2B
65 #define TZEROWAIT 0x2C
66 #define XTARGET 0x2D
67 #define VDCMIN 0x33
68 #define SW_MODE 0x34
69 #define SG_STOP(n) (((n)&0x1)<<10)
70 #define RAMP_STAT 0x35
71 #define XLATCH 0x36
74 #define PWMCONF 0x70
76 #define FREEWHEEL(n) (((n)&0x3UL)<<20)
77 #define PWM_AUTOSCALE(n) (((n)&0x1UL)<<18)
78 #define PWM_FREQ(n) (((n)&0x3UL)<<16)
79 #define PWM_GRAD(n) (((n)&0xFF)<<8)
80 #define PWM_AMPL(n) (((n)&0xFF)<<0)
85 #define CHOPCONF 0x6C
87 #define DISS2G(n) (((n)&0x1UL)<<30)
88 #define DEDGE(n) (((n)&0x1UL)<<29)
89 #define INTPOL(n) (((n)&0x1UL)<<28)
90 #define MRES(n) (((n)&0xFUL)<<24)
91 #define SYNC(n) (((n)&0xFUL)<<20)
92 #define VHIGHCHM(n) (((n)&0x1UL)<<19)
93 #define VHIGHFS(n) (((n)&0x1UL)<<18)
94 #define VSENSE(n) (((n)&0x1UL)<<17)
95 #define TBL(n) (((n)&0x3UL)<<15)
96 #define CHM(n) (((n)&0x1UL)<<14)
97 #define RNDTF(n) (((n)&0x1)<<13)
98 #define DISFDCC(n) (((n)&0x1)<<12)
99 #define TFD3(n) (((n)&0x1)<<11)
100 #define HEND(n) (((n)&0xF)<<7)
101 #define HSTRT_TFD(n) (((n)&0x7)<<4)
102 #define TOFF(n) (((n)&0xF)<<0)
105 /* CoolStep smart current control register and stallGuard2 configuration **/
106 
107 #define COOLCONF 0x6D
108 #define SFILT(n) (((n)&0x1UL)<<24)
109 #define SGT(n) (((n)&0x7FUL)<<16)
110 #define SEIMIN(n) (((n)&0x1UL)<<15)
111 #define SEDN(n) (((n)&0x3)<<13)
112 #define SEMAX(n) (((n)&0xF)<<8)
113 #define SEUP(n) (((n)&0x3)<<5)
114 #define SEMIN(n) (((n)&0xF)<<0)
117 #define DCCTRL 0x6E
118 #define DC_SG(n) (((n)&0xFFUL)<<16)
119 #define DC_TIME(n) (((n)&0x3FF)<<0)
121 #define DRV_STATUS 0x6F
123 #define IHOLDDELAY(n) (((n)&0xFUL)<<16)
124 #define IRUN(n) (((n)&0x1F)<<8)
125 #define IHOLD(n) (((n)&0x1F)<<0)
128 #define WRITE_ACCESS 0x80
132 #define POSITIONING_MODE 0x00
133 #define VELOCITY_MODE_POS 0x01
134 #define VELOCITY_MODE_NEG 0x02
135 #define HOLD_MODE 0x03
137 #define DRIVER_STOP 0
138 #define DRIVER_VELOCITY 1
139 #define DRIVER_POSITION 2
141 #define ACCELERATIONCONVERSION 1.0/116.415321827
142 #define VELOCITYCONVERSION 1.0/0.953674316
150 class uStepperDriver{
151 
152 friend class uStepperS;
153  public:
159  uStepperDriver( void );
160 
168  void init( uStepperS * _pointer );
169 
177  void setPosition( int32_t position );
178 
187  void setVelocity( uint32_t velocity );
196  void setAcceleration( uint32_t acceleration );
205  void setDeceleration( uint32_t deceleration );
206 
212  void setCurrent( uint8_t current );
213 
219  void setHoldCurrent( uint8_t current );
220 
231  void setShaftDirection( bool direction );
232 
236  void stop( void );
237 
248  int32_t getVelocity( void );
249 
259  int32_t getPosition( void );
260 
266  void setHome(int32_t initialSteps = 0);
267 
283  int32_t writeRegister( uint8_t address, uint32_t datagram );
284 
296  int32_t readRegister( uint8_t address );
297 
301  uint16_t getStallValue( void );
302 
304  volatile int32_t xTarget = 0;
305 
307  volatile int32_t xActual = 0;
308 
309 
310  protected:
312  uint8_t status;
313 
315  uint8_t mode = DRIVER_STOP;
316 
317  uStepperS * pointer;
318 
319  uint8_t current = 16;
320  uint8_t holdCurrent = 0;
321  uint8_t holdDelay = 0;
322 
324  uint32_t VSTART = 0;
325  uint32_t V1 = 0;
326  uint32_t VMAX = 200000;
327  uint32_t VSTOP = 10;
328  uint16_t A1 = 600;
329  uint16_t AMAX = 100;
330  uint16_t DMAX = 600;
331  uint16_t D1 = 600;
332 
333 
334  void chipSelect(bool state);
335 
339  void updateCurrent( void );
340 
346  void setRampMode( uint8_t mode );
347 
348  void setDirection( bool direction );
349 
350  void reset( void );
351 
352  void enableStealth( void );
353 
354  void enableStallguard( int8_t threshold, bool stopOnStall );
355 
356  void disableStallguard( void );
357 
358  void clearStall( void );
359 
360  void readMotorStatus(void);
361 
362 
363 
364 };
uStepperS
Prototype of class for accessing all features of the uStepper S in a single object.
Definition: uStepperS.h:272
uStepperS::enableStallguard
void enableStallguard(int8_t threshold=4, bool stopOnStall=false)
Enable TMC5130 StallGuard.
Definition: uStepperS.cpp:301
uStepperS.h
uStepperS::chipSelect
void chipSelect(uint8_t pin, bool state)
uStepperS::disableStallguard
void disableStallguard(void)
Disables the builtin stallguard offered from TMC5130, and reenables StealthChop.
Definition: uStepperS.cpp:312
uStepperS::stop
void stop(bool mode=HARD)
Stop the motor.
Definition: uStepperS.cpp:486
uStepperS::clearStall
void clearStall(void)
Clear the stallguard, reenabling the motor to return to its previous operation.
Definition: uStepperS.cpp:319
uStepperS::init
void init(void)
Internal function to prepare the uStepperS in the constructor.
Definition: uStepperS.cpp:57
uStepperS::mode
volatile uint8_t mode
Definition: uStepperS.h:729
pointer
uStepperS * pointer
Definition: uStepperS.cpp:32
uStepperS::setHoldCurrent
void setHoldCurrent(double current)
Set motor hold current.
Definition: uStepperS.cpp:454
uStepperS::uStepperDriver
friend class uStepperDriver
Definition: uStepperS.h:275
DRIVER_STOP
#define DRIVER_STOP
Definition: uStepperDriver.h:136
uStepperS::setCurrent
void setCurrent(double current)
Set motor output current.
Definition: uStepperS.cpp:441