uStepper S
uStepperDriver.h
Go to the documentation of this file.
1 /********************************************************************************************
2 * File: uStepperDriver.h *
3 * Version: 1.0.1 *
4 * Date: May 14th, 2019 *
5 * Author: Thomas Hørring Olsen *
6 * *
7 *********************************************************************************************
8 * (C) 2019 *
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 /* TMC5130 Register Address Defines */
36 
37 #define GCONF 0x00
39 #define DIRECTION(n) (((n)&0x1)<<4)
40 #define EN_PWM_MODE(n) (((n)&0x1)<<2)
41 #define I_SCALE_ANALOG(n) (((n)&0x1)<<0)
43 #define GSTAT 0x01
44 #define X_COMPARE 0x05
45 #define IHOLD_IRUN 0x10
46 #define TPOWERDOWN 0x11
47 #define TPWMTHRS 0x13
48 #define TCOOLTHRS 0x14
49 #define THIGH 0x15
50 #define RAMPMODE 0x20
51 #define XACTUAL 0x21
52 #define VACTUAL 0x22
55 #define VSTART_REG 0x23
56 #define A1_REG 0x24
57 #define V1_REG 0x25
58 #define AMAX_REG 0x26
59 #define VMAX_REG 0x27
60 #define DMAX_REG 0x28
61 #define D1_REG 0x2A
62 #define VSTOP_REG 0x2B
63 #define TZEROWAIT 0x2C
64 #define XTARGET 0x2D
65 #define VDCMIN 0x33
66 #define SW_MODE 0x34
67 #define RAMP_STAT 0x35
68 #define XLATCH 0x36
71 #define PWMCONF 0x70
73 #define FREEWHEEL(n) (((n)&0x3UL)<<20)
74 #define PWM_AUTOSCALE(n) (((n)&0x1UL)<<18)
75 #define PWM_FREQ(n) (((n)&0x3UL)<<16)
76 #define PWM_GRAD(n) (((n)&0xFF)<<8)
77 #define PWM_AMPL(n) (((n)&0xFF)<<0)
82 #define CHOPCONF 0x6C
84 #define DISS2G(n) (((n)&0x1UL)<<30)
85 #define DEDGE(n) (((n)&0x1UL)<<29)
86 #define INTPOL(n) (((n)&0x1UL)<<28)
87 #define MRES(n) (((n)&0xFUL)<<24)
88 #define SYNC(n) (((n)&0xFUL)<<20)
89 #define VHIGHCHM(n) (((n)&0x1UL)<<19)
90 #define VHIGHFS(n) (((n)&0x1UL)<<18)
91 #define VSENSE(n) (((n)&0x1UL)<<17)
92 #define TBL(n) (((n)&0x3UL)<<15)
93 #define CHM(n) (((n)&0x1UL)<<14)
94 #define RNDTF(n) (((n)&0x1)<<13)
95 #define DISFDCC(n) (((n)&0x1)<<12)
96 #define TFD3(n) (((n)&0x1)<<11)
97 #define HEND(n) (((n)&0xF)<<7)
98 #define HSTRT_TFD(n) (((n)&0x7)<<4)
99 #define TOFF(n) (((n)&0xF)<<0)
102 /* CoolStep smart current control register and stallGuard2 configuration **/
103 
104 #define COOLCONF 0x6D
105 #define SFILT(n) (((n)&0x1UL)<<24)
106 #define SGT(n) (((n)&0x7FUL)<<16)
107 #define SEIMIN(n) (((n)&0x1UL)<<15)
108 #define SEDN(n) (((n)&0x3)<<13)
109 #define SEMAX(n) (((n)&0xF)<<8)
110 #define SEUP(n) (((n)&0x3)<<5)
111 #define SEMIN(n) (((n)&0xF)<<0)
114 #define DCCTRL 0x6E
115 #define DC_SG(n) (((n)&0xFFUL)<<16)
116 #define DC_TIME(n) (((n)&0x3FF)<<0)
118 #define DRV_STATUS 0x6F
120 #define IHOLDDELAY(n) (((n)&0xFUL)<<16)
121 #define IRUN(n) (((n)&0x1F)<<8)
122 #define IHOLD(n) (((n)&0x1F)<<0)
125 #define WRITE_ACCESS 0x80
129 #define POSITIONING_MODE 0x00
130 #define VELOCITY_MODE_POS 0x01
131 #define VELOCITY_MODE_NEG 0x02
132 #define HOLD_MODE 0x03
134 #define DRIVER_STOP 0
135 #define DRIVER_VELOCITY 1
136 #define DRIVER_POSITION 2
138 #define ACCELERATIONCONVERSION 1.0/116.415321827
139 #define VELOCITYCONVERSION 1.0/0.953674316
147 class uStepperDriver{
148 
149 friend class uStepperS;
150  public:
156  uStepperDriver( void );
157 
165  void init( uStepperS * _pointer );
166 
174  void setPosition( int32_t position );
175 
184  void setVelocity( uint32_t velocity );
193  void setAcceleration( uint32_t acceleration );
202  void setDeceleration( uint32_t deceleration );
203 
209  void setCurrent( uint8_t current );
210 
216  void setHoldCurrent( uint8_t current );
217 
228  void setShaftDirection( bool direction );
229 
233  void stop( void );
234 
245  int32_t getVelocity( void );
246 
256  int32_t getPosition( void );
257 
261  void setHome(void);
262 
276  int32_t writeRegister( uint8_t address, uint32_t datagram );
277 
289  int32_t readRegister( uint8_t address );
290 
292  volatile int32_t xTarget = 0;
293 
294 
295  protected:
297  uint8_t status;
298 
300  uint8_t mode = DRIVER_STOP;
301 
302  uStepperS * pointer;
303 
304  uint8_t current = 16;
305  uint8_t holdCurrent = 10;
306  uint8_t holdDelay = 0;
307 
309  uint32_t VSTART = 0;
310  uint32_t V1 = 0;
311  uint32_t VMAX = 200000;
312  uint32_t VSTOP = 10;
313  uint16_t A1 = 600;
314  uint16_t AMAX = 600;
315  uint16_t DMAX = 600;
316  uint16_t D1 = 600;
317 
318  void chipSelect(bool state);
319 
323  void updateCurrent( void );
324 
330  void setRampMode( uint8_t mode );
331 
332  void setDirection( bool direction );
333 
334  void reset( void );
335 
336  void enableStealth( void);
337 
338  void readMotorStatus(void);
339 };
Prototype of class for accessing all features of the uStepper S in a single object.
Definition: uStepperS.h:252
void setHoldCurrent(double current)
Set motor hold current.
Definition: uStepperS.cpp:405
#define DRIVER_STOP
Prototype of class for the TMC5130 Driver.
uint8_t mode
Definition: uStepperS.h:635
void stop(bool mode=HARD)
Stop the motor.
Definition: uStepperS.cpp:437
void init(void)
Internal function to prepare the uStepperS in the constructor.
Definition: uStepperS.cpp:58
Function prototypes and definitions for the uStepper S library.
void setCurrent(double current)
Set motor output current.
Definition: uStepperS.cpp:392