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