StepperMotorControlLibrary  1.0
stepper.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!****************************************************************************
33  * @file stepper.h
34  * @brief STEPPER_MODULE Module
35  *
36  *
37  * @anchor stepper_h
38  * # Overview
39  *
40  * Does stuff
41  *
42  * <hr>
43  ******************************************************************************/
47 #ifndef STEPPER_H
48 #define STEPPER_H
49 
50 #include <stdint.h>
51 
52 #include <ti/devices/msp/msp.h>
53 
54 /* Include the IQmath Library */
55 #include <ti/iqmath/include/IQmathLib.h>
56 
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
64 #define STEPPER_INDEXER_MAX_COUNT (1024)
65 
69 #define STEPPER_INDEXER_0_BY_8 (0)
70 
74 #define STEPPER_INDEXER_1_BY_8 (STEPPER_INDEXER_MAX_COUNT >> 3)
75 
79 #define STEPPER_INDEXER_2_BY_8 (2 * STEPPER_INDEXER_1_BY_8)
80 
84 #define STEPPER_INDEXER_3_BY_8 (3 * STEPPER_INDEXER_1_BY_8)
85 
89 #define STEPPER_INDEXER_4_BY_8 (4 * STEPPER_INDEXER_1_BY_8)
90 
94 #define STEPPER_INDEXER_5_BY_8 (5 * STEPPER_INDEXER_1_BY_8)
95 
99 #define STEPPER_INDEXER_6_BY_8 (6 * STEPPER_INDEXER_1_BY_8)
100 
104 #define STEPPER_INDEXER_7_BY_8 (7 * STEPPER_INDEXER_1_BY_8)
105 
109 #define STEPPER_2PI (6.2831853071f)
110 
112 typedef enum {
132 
134 typedef enum {
144 
146 typedef enum {
152 
154 typedef enum {
159 }STEPPER_DIR;
160 
162 typedef struct {
164  int16_t counter;
166  STEPPER_DIR dir;
170  STEPPER_STEP_TYPE stepType;
172  STEPPER_DRIVE IaDrive;
174  STEPPER_DRIVE IbDrive;
175 #ifdef __HAS_2_DAC
176 
177  _iq15 IaCurrRef;
179  _iq15 IbCurrRef;
180 #endif
182 
186 extern _iq18 angleSf;
187 
191 void STEPPER_init(void);
192 
198 __STATIC_INLINE void STEPPER_setIndexerDir(
199  INDEXER_Instance *handle, STEPPER_DIR dir)
200 {
201  handle->dir = dir;
202 }
203 
210 __STATIC_INLINE void STEPPER_setIndexerIncVal(
211  INDEXER_Instance *handle, STEPPER_STEP_INC_DEC incDecVal)
212 {
213  handle->indexIncDec = incDecVal;
214 }
215 
221 __STATIC_INLINE void STEPPER_setStepType(
222  INDEXER_Instance *handle, STEPPER_STEP_TYPE stepType)
223 {
224  handle->stepType = stepType;
225 }
226 
233 __STATIC_INLINE void STEPPER_setMotorDrives(INDEXER_Instance *handle,
234  STEPPER_DRIVE IaDrive,
235  STEPPER_DRIVE IbDrive)
236 {
237  handle->IaDrive = IaDrive;
238  handle->IbDrive = IbDrive;
239 }
240 
245 __STATIC_INLINE void STEPPER_cirStepPhaseGen(INDEXER_Instance *handle)
246 {
247  int16_t counter = handle->counter;
248  if(counter >= STEPPER_INDEXER_0_BY_8 && counter < STEPPER_INDEXER_2_BY_8)
249  {
252  }
253  else if(counter >= STEPPER_INDEXER_2_BY_8 && counter < STEPPER_INDEXER_4_BY_8)
254  {
257  }
258  else if(counter >= STEPPER_INDEXER_4_BY_8 && counter < STEPPER_INDEXER_6_BY_8)
259  {
262  }
263  else if(counter >= STEPPER_INDEXER_6_BY_8)
264  {
267  }
268  else
269  {
270  /* This is expected to be empty*/
271  }
272 }
273 
278 __STATIC_INLINE void STEPPER_nonCirPhaseGen(INDEXER_Instance *handle)
279 {
280  int16_t counter = handle->counter;
281  if(counter >= STEPPER_INDEXER_0_BY_8 && counter < STEPPER_INDEXER_1_BY_8)
282  {
285  }
286  if(counter >= STEPPER_INDEXER_1_BY_8 && counter < STEPPER_INDEXER_2_BY_8)
287  {
290  }
291  else if(counter >= STEPPER_INDEXER_2_BY_8 && counter < STEPPER_INDEXER_3_BY_8)
292  {
295  }
296  else if(counter >= STEPPER_INDEXER_3_BY_8 && counter < STEPPER_INDEXER_4_BY_8)
297  {
300  }
301  else if(counter >= STEPPER_INDEXER_4_BY_8 && counter < STEPPER_INDEXER_5_BY_8)
302  {
305  }
306  else if(counter >= STEPPER_INDEXER_5_BY_8 && counter < STEPPER_INDEXER_6_BY_8)
307  {
310  }
311  else if(counter >= STEPPER_INDEXER_6_BY_8 && counter < STEPPER_INDEXER_7_BY_8)
312  {
315  }
316  else if(counter >= STEPPER_INDEXER_7_BY_8)
317  {
320  }
321  else
322  {
323  /* This is expected to be empty*/
324  }
325 }
326 
327 /*
328 * The current reference is used in case of drivers which has independent vref
329 * for each motor phase. This is disabled when not using such drivers
330 */
331 #ifdef __HAS_2_DAC
332 
336 __STATIC_INLINE void STEPPER_currRefGen(INDEXER_Instance *handle)
337 {
338  uint32_t countValue = handle->counter;
339  _iq15 angle = _IQ15mpy(_IQ15(countValue), angleSf);
340  handle->IaCurrRef = _IQ15abs(_IQ15cos(angle));
341  handle->IbCurrRef = _IQ15abs(_IQ15sin(angle));
342 }
343 #endif
344 
350 __STATIC_INLINE void STEPPER_indexer(INDEXER_Instance *handle)
351 {
352  if(handle->dir == STEPPER_DIR_FORWARD)
353  {
354  handle->counter += handle->indexIncDec;
355  }
356  else
357  {
358  handle->counter -= handle->indexIncDec;
359  }
360 
361  if(handle->counter < 0)
362  {
363  handle->counter = (STEPPER_INDEXER_MAX_COUNT + handle->counter);
364  }
365 
366  if(handle->counter >= STEPPER_INDEXER_MAX_COUNT - 1)
367  {
368  handle->counter = 0;
369  }
370 
371  if(handle->stepType == STEPPER_STEP_TYPE_CIR)
372  {
373  STEPPER_cirStepPhaseGen(handle);
374  }
375  else if(handle->stepType == STEPPER_STEP_TYPE_NONCIR)
376  {
377  STEPPER_nonCirPhaseGen(handle);
378  }
379  else
380  {
381  /* This is expected to be empty*/
382  }
383 }
384 
385 #ifdef __cplusplus
386 }
387 #endif
388 #endif /* STEPPER_H */
Definition: stepper.h:116
Definition: stepper.h:150
#define STEPPER_INDEXER_0_BY_8
Value corresponding to 0/8 of indexer max count.
Definition: stepper.h:69
Definition: stepper.h:148
void STEPPER_init(void)
Calculates the angle scale factor.
Definition: stepper.h:138
#define STEPPER_INDEXER_7_BY_8
Value corresponding to 7/8 of indexer max count.
Definition: stepper.h:104
Definition: stepper.h:140
_iq18 angleSf
Extern angleSf.
__STATIC_INLINE void STEPPER_setStepType(INDEXER_Instance *handle, STEPPER_STEP_TYPE stepType)
Sets the step type.
Definition: stepper.h:221
#define STEPPER_INDEXER_MAX_COUNT
Maximum count for the indexer.
Definition: stepper.h:64
#define STEPPER_INDEXER_1_BY_8
Value corresponding to 1/8 of indexer max count.
Definition: stepper.h:74
#define STEPPER_INDEXER_4_BY_8
Value corresponding to 4/8 of indexer max count.
Definition: stepper.h:89
Define indexer structure.
Definition: stepper.h:162
#define STEPPER_INDEXER_5_BY_8
Value corresponding to 5/8 of indexer max count.
Definition: stepper.h:94
Definition: stepper.h:156
Definition: stepper.h:120
__STATIC_INLINE void STEPPER_nonCirPhaseGen(INDEXER_Instance *handle)
Updates phase drive state in non circular mode.
Definition: stepper.h:278
STEPPER_STEP_TYPE stepType
Definition: stepper.h:170
Definition: stepper.h:126
STEPPER_DRIVE
Definition: stepper.h:134
STEPPER_DRIVE IaDrive
Definition: stepper.h:172
Definition: stepper.h:118
#define STEPPER_INDEXER_6_BY_8
Value corresponding to 6/8 of indexer max count.
Definition: stepper.h:99
__STATIC_INLINE void STEPPER_setIndexerIncVal(INDEXER_Instance *handle, STEPPER_STEP_INC_DEC incDecVal)
Sets the direction for the indexer.
Definition: stepper.h:210
Definition: stepper.h:122
STEPPER_STEP_INC_DEC
Definition: stepper.h:112
int16_t counter
Definition: stepper.h:164
__STATIC_INLINE void STEPPER_setIndexerDir(INDEXER_Instance *handle, STEPPER_DIR dir)
Sets the direction for the indexer.
Definition: stepper.h:198
Definition: stepper.h:142
Definition: stepper.h:128
#define STEPPER_INDEXER_2_BY_8
Value corresponding to 2/8 of indexer max count.
Definition: stepper.h:79
STEPPER_DIR dir
Definition: stepper.h:166
STEPPER_DIR
Definition: stepper.h:154
Definition: stepper.h:158
#define STEPPER_INDEXER_3_BY_8
Value corresponding to 3/8 of indexer max count.
Definition: stepper.h:84
STEPPER_DRIVE IbDrive
Definition: stepper.h:174
Definition: stepper.h:130
Definition: stepper.h:124
STEPPER_STEP_INC_DEC indexIncDec
Definition: stepper.h:168
__STATIC_INLINE void STEPPER_setMotorDrives(INDEXER_Instance *handle, STEPPER_DRIVE IaDrive, STEPPER_DRIVE IbDrive)
Sets the motor drive states.
Definition: stepper.h:233
Definition: stepper.h:136
__STATIC_INLINE void STEPPER_cirStepPhaseGen(INDEXER_Instance *handle)
Updates phase drive state in circular mode.
Definition: stepper.h:245
Definition: stepper.h:114
STEPPER_STEP_TYPE
Definition: stepper.h:146
__STATIC_INLINE void STEPPER_indexer(INDEXER_Instance *handle)
Increments the indexer counter and updates the phase reference calculation.
Definition: stepper.h:350
© Copyright 1995-2024, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale