MotorWare f2806x Module API Documentation
enc.h
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2012, 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  * --/COPYRIGHT--*/
32 #ifndef _ENC_H_
33 #define _ENC_H_
34 
40 
41 
42 // **************************************************************************
43 // the includes
46 
51 
52 
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 
58 // **************************************************************************
59 // the defines
60 #define ENC_SPEED_SCALING_FACTOR 16777216.0
61 #define ENC_ZERO_OFFSET 3813071.0
62 #define ENC_SPEED_CUTOFF_FREQ 100.0
63 #define ENC_SPEED_COEFF_Q 6
64 #define ENC_SPEED_COEFF_SCALING (1<<ENC_SPEED_COEFF_Q)
65 #define ENC_2PI (2.0*3.14159)
66 #define ENC_LOG_LEN 256
67 #define ENC_RPM_Q1 8
68 #define ENC_RPM_Q2 16
69 #define ENC_LOG_DELTA_TRIGGER_THRES 400
70 
71 
72 // **************************************************************************
73 // the typedefs
74 
77 typedef enum {
82 
83 
86 typedef struct _ENC_Obj_
87 {
88  int16_t sample_count;
89  int16_t sample_period;
90  uint16_t num_pole_pairs;
91  uint16_t num_enc_slots;
93  uint32_t enc_zero_offset;
94  int32_t enc_elec_angle;
95  int32_t incremental_slip;
96  int32_t enc_slip_angle;
98  int32_t prev_enc;
99  int32_t delta_enc;
101  int32_t speed_gain;
102  int32_t rpm_gain;
104  int32_t speed_lpf_cx;
105  int32_t speed_lpf_cy;
106  int32_t speed_lpf_out;
108  int16_t run_flag;
111  int16_t trigger_idx;
112  int16_t trigger_delta;
113  int32_t log_idx;
114  int16_t log[ENC_LOG_LEN];
115 } ENC_Obj;
116 
117 
120 typedef struct _ENC_Obj_ *ENC_Handle;
121 
122 
123 // **************************************************************************
124 // the function prototypes
125 
130 void ENC_calcElecAngle(ENC_Handle encHandle, uint32_t posnCounts);
131 
132 
136 inline _iq ENC_getElecAngle(ENC_Handle encHandle) {
137  ENC_Obj *enc = (ENC_Obj *) encHandle;
138 
139  return enc->enc_elec_angle;
140 }
141 
142 
146 inline _iq ENC_getMagneticAngle(ENC_Handle encHandle) {
147  ENC_Obj *enc = (ENC_Obj *) encHandle;
148 
149  return enc->enc_magnetic_angle;
150 }
151 
152 
156 inline _iq ENC_getFilteredSpeed(ENC_Handle encHandle) {
157  ENC_Obj *enc = (ENC_Obj *) encHandle;
158 
159  return enc->speed_lpf_out;
160 }
161 
162 
166 extern int16_t ENC_getSpeedRPM(ENC_Handle encHandle);
167 
168 
173 extern ENC_Handle ENC_init(void *pMemory,const size_t numBytes);
174 
175 
183 void ENC_run(ENC_Handle encHandle, uint32_t posnCounts, uint16_t indextFlag, uint16_t dirFlag, int16_t log_flag);
184 
185 
189 inline void ENC_setIncrementalSlip(ENC_Handle encHandle, _iq incrementalSlip) {
190  ENC_Obj *enc = (ENC_Obj *) encHandle;
191 
192  // set the amount of incremental slip
193  enc->incremental_slip = incrementalSlip;
194 
195  return;
196 }
197 
198 
201 inline void ENC_setRunFlag(ENC_Handle encHandle) {
202  ENC_Obj *enc = (ENC_Obj *) encHandle;
203 
204  // set the run flag
205  enc->run_flag = 1;
206 
207  return;
208 }
209 
210 
214 inline void ENC_setZeroOffset(ENC_Handle encHandle, uint32_t zeroOffset) {
215  ENC_Obj *enc = (ENC_Obj *)encHandle;
216 
217  enc->enc_zero_offset = zeroOffset*enc->mech_angle_gain;
218 
219  return;
220 }
221 
222 
232 extern void ENC_setup(ENC_Handle encHandle, const int16_t sample_period,
233  const uint16_t num_pole_pairs, const uint16_t num_enc_slots,
234  const uint32_t enc_zero_offset, const float_t full_scale_freq,
235  const float_t speed_update_freq, const float_t speed_cutoff);
236 
237 #ifdef __cplusplus
238 }
239 #endif // extern "C"
240 
242 #endif // end of _ENC_H_ definition
243 
int16_t post_trigger_len
encoder log post trigger length
Definition: enc.h:109
int16_t trigger_idx
index where trigger event happened
Definition: enc.h:111
struct _ENC_Obj_ ENC_Obj
Defines the encoder object.
int16_t run_flag
encoder log free run flag
Definition: enc.h:108
void ENC_run(ENC_Handle encHandle, uint32_t posnCounts, uint16_t indextFlag, uint16_t dirFlag, int16_t log_flag)
Based on the encoder reading, computes the electrical angle and the electrical "speed".
Definition: enc.c:163
Contains the public interface to the types definitions.
float_t full_scale_freq
full scale frequency
Definition: enc.h:100
void ENC_setZeroOffset(ENC_Handle encHandle, uint32_t zeroOffset)
Sets the value for the encoder object zero offset.
Definition: enc.h:214
acquire state
Definition: enc.h:80
int32_t speed_lpf_cy
speed output coefficient
Definition: enc.h:105
int32_t delta_enc
encoder count delta
Definition: enc.h:99
int16_t sample_count
when it reaches the sample period, collect & process encoder data
Definition: enc.h:88
#define ENC_LOG_LEN
Definition: enc.h:66
int32_t speed_lpf_cx
speed input coefficient
Definition: enc.h:104
int32_t rpm_gain
gain which converts the Q24 normalized electrical freq to RPM
Definition: enc.h:102
long _iq
uint16_t num_pole_pairs
number of pole pairs in motor
Definition: enc.h:90
int32_t prev_enc
previous encoder reading
Definition: enc.h:98
freerun state
Definition: enc.h:79
uint32_t enc_zero_offset
encoder zero offset in counts
Definition: enc.h:93
Defines the encoder object.
Definition: enc.h:86
_iq ENC_getFilteredSpeed(ENC_Handle encHandle)
Returns the low-pass filtered speed output.
Definition: enc.h:156
int16_t sample_period
sample period of encoder processing
Definition: enc.h:89
void ENC_setIncrementalSlip(ENC_Handle encHandle, _iq incrementalSlip)
Set the amount of incremental slip.
Definition: enc.h:189
int16_t trigger_delta
calculated delta when trigger happened
Definition: enc.h:112
int32_t incremental_slip
incremental amount of slip
Definition: enc.h:95
_iq ENC_getMagneticAngle(ENC_Handle encHandle)
Returns the magnetic angle.
Definition: enc.h:146
ENC_Handle ENC_init(void *pMemory, const size_t numBytes)
Initializes the encoder object.
Definition: enc.c:55
void ENC_calcElecAngle(ENC_Handle encHandle, uint32_t posnCounts)
Reads encoder and returns the electrical degrees in Q24 format.
Definition: enc.c:129
ENC_LOG_State_e
Enumeration for the encoder log states.
Definition: enc.h:77
void ENC_setup(ENC_Handle encHandle, const int16_t sample_period, const uint16_t num_pole_pairs, const uint16_t num_enc_slots, const uint32_t enc_zero_offset, const float_t full_scale_freq, const float_t speed_update_freq, const float_t speed_cutoff)
Initializes encoder object parameters.
Definition: enc.c:68
int32_t enc_elec_angle
encoder current electrical angle
Definition: enc.h:94
int32_t log_idx
encoder log index
Definition: enc.h:113
int16_t post_trigger_cnt
encoder log post trigger counter
Definition: enc.h:110
uint16_t num_enc_slots
number of encoder slots
Definition: enc.h:91
int32_t enc_magnetic_angle
encoder current magnetic angle (compensated for slip)
Definition: enc.h:97
int32_t enc_slip_angle
amount of total slip
Definition: enc.h:96
ENC_LOG_State_e log_state
encoder log state
Definition: enc.h:107
int32_t speed_lpf_out
speed lpf output
Definition: enc.h:106
void ENC_setRunFlag(ENC_Handle encHandle)
Start logging encoder data.
Definition: enc.h:201
int16_t ENC_getSpeedRPM(ENC_Handle encHandle)
Returns the filtered speed in RPM.
Definition: enc.c:300
int32_t speed_gain
gain which converts a difference in encoder counts to Q24 normalized electrical freq ...
Definition: enc.h:101
float_t speed_cutoff
speed cutoff frequency in Hz
Definition: enc.h:103
idle state
Definition: enc.h:78
struct _ENC_Obj_ * ENC_Handle
Defines the encoder handle.
Definition: enc.h:120
_iq mech_angle_gain
gain which converts the encoder counts to Q24 mechanical degrees
Definition: enc.h:92
_iq ENC_getElecAngle(ENC_Handle encHandle)
Returns the electrical angle.
Definition: enc.h:136
int16_t log[ENC_LOG_LEN]
encoder log length
Definition: enc.h:114
float float_t
Defines the portable data type for 32 bit, signed floating-point data.
Definition: types.h:121