tmrangle  2.0.0.0
tmrangle.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef TMRANGLE_H
36 #define TMRANGLE_H
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_spi_master.h"
55 
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
68 #define TMRANGLE_MAP_MIKROBUS( cfg, mikrobus ) \
69  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
70  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
71  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
72  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
73 
79 #define TMRANGLE_RETVAL uint8_t
80 
81 #define TMRANGLE_OK 0x00
82 #define TMRANGLE_INIT_ERROR 0xFF
83 
89 #define TMRANGLE_COS_N 0x00
90 #define TMRANGLE_COS_P 0x01
91 #define TMRANGLE_SIN_P 0x02
92 #define TMRANGLE_SIN_N 0x03
93 
99 #define TMRANGLE_RAD2DEGFACTOR 57.3248
100 #define TMRANGLE_ROTATION_VALID 1
101 #define TMRANGLE_ANGLE45_H 0.7894
102 #define TMRANGLE_ANGLE45_L 0.7806
103 #define TMRANGLE_ANGLE135_H 2.3594
104 #define TMRANGLE_ANGLE135_L 2.3506
105 
111 #define TMRANGLE_MAX_DIFF_SIN 1251
112 #define TMRANGLE_MAX_DIFF_COS 1217
113 #define TMRANGLE_MIN_DIFF_SIN -1200
114 #define TMRANGLE_MIN_DIFF_COS -1206
115 #define TMRANGLE_SIN_45 854.0
116 #define TMRANGLE_COS_45 864.0
117 #define TMRANGLE_SIN_135 873.0
118 #define TMRANGLE_COS_135 -865.0
119  // End group macro
122 // --------------------------------------------------------------- PUBLIC TYPES
131 typedef struct
132 {
133  uint16_t sin_p;
134 
135  uint16_t cos_p;
136 
137  uint16_t sin_n;
138 
139  uint16_t cos_n;
140 
141  int16_t diff_x;
142 
143  int16_t diff_y;
144 
147 
148 typedef struct
149 {
150  digital_out_t cs;
151 
152  // Modules
153 
154  spi_master_t spi;
155  pin_name_t chip_select;
156 
158 
159 } tmrangle_t;
160 
164 typedef struct
165 {
166  // Communication gpio pins
167 
168  pin_name_t miso;
169  pin_name_t mosi;
170  pin_name_t sck;
171  pin_name_t cs;
172 
173 
174  // static variable
175 
176  uint32_t spi_speed;
177  uint8_t spi_mode;
178  spi_master_chip_select_polarity_t cs_polarity;
179 
181 
182 
183 typedef struct
184 {
185  uint8_t angle_45_found;
188 
189  int32_t max_cos;
190 
191  int32_t min_cos;
192 
193  int32_t max_sin;
194 
195  int32_t min_sin;
196 
197  float x_45;
198 
199  float x_135;
200 
201  float y_45;
202 
203  float y_135;
204 
205  float x_45_corr;
206 
207  float x_135_corr;
208 
209  float y_45_corr;
210 
211  float y_135_corr;
212 
213  float amplitude_x;
214 
215  float amplitude_y;
216 
217  float offset_x;
218 
219  float offset_y;
220 
222 
224 
226 
228 
229 typedef struct
230 {
232 
233  float sin_ortho;
234 
235  float cos_ortho;
236 
238 
239 typedef struct
240 {
241  int32_t max_diff_sin;
242  int32_t max_diff_cos;
243  int32_t min_diff_sin;
244  int32_t min_diff_cos;
245  float sin_45;
246  float cos_45;
247  float sin_135;
248  float cos_135;
249 
251  // End types group
253 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
254 
259 #ifdef __cplusplus
260 extern "C"{
261 #endif
262 
272 
281 
295 uint16_t tmrangle_read_channel_adc ( tmrangle_t* ctx, uint8_t channel );
296 
308 
319 
331  tmrangle_rotation_data_store_t* rotation_data_store );
332 
343 
359  trigonometry_t* dev );
360 
375 
376 
392 
405 
418 
435  tmrangle_calib_data_t* calib_param );
436 
437 
438 
439 #ifdef __cplusplus
440 }
441 #endif
442 #endif // _TMRANGLE_H_
443  // End public_function group
446 
447 // ------------------------------------------------------------------------- END
tmrangle_cfg_t::mosi
pin_name_t mosi
Definition: tmrangle.h:169
tmrangle_rotation_data_store_t::x_45_corr
float x_45_corr
Definition: tmrangle.h:205
tmrangle_calib_data_t::cos_ortho
float cos_ortho
Definition: tmrangle.h:235
tmrangle_init
TMRANGLE_RETVAL tmrangle_init(tmrangle_t *ctx, tmrangle_cfg_t *cfg)
Initialization function.
tmrangle_cfg_t::cs
pin_name_t cs
Definition: tmrangle.h:171
tmrangle_t::sensor_data
tmrangle_analog_sensor_data_t sensor_data
Definition: tmrangle.h:157
tmrangle_rotation_data_store_t::amplitude_y
float amplitude_y
Definition: tmrangle.h:215
tmrangle_rotation_data_store_t::min_sin
int32_t min_sin
Definition: tmrangle.h:195
trigonometry_t::sin_135
float sin_135
Definition: tmrangle.h:247
trigonometry_t::max_diff_sin
int32_t max_diff_sin
Definition: tmrangle.h:241
tmrangle_init_calib_data
void tmrangle_init_calib_data(tmrangle_t *ctx, tmrangle_calib_data_t *calib_param, trigonometry_t *dev)
Initialization and stores calibration parameters function.
tmrangle_calib_one_direction_min_max
void tmrangle_calib_one_direction_min_max(tmrangle_t *ctx, tmrangle_rotation_data_store_t *rotation_data_store)
Find and stores rotation parameters ( min, max ) function.
tmrangle_rotation_data_store_t::angle_135_found
uint8_t angle_135_found
Definition: tmrangle.h:186
tmrangle_get_calib_deff_angle
float tmrangle_get_calib_deff_angle(tmrangle_t *ctx, tmrangle_calib_data_t *calib_param)
Calculate the calibrated angle ( radians ) function.
TMRANGLE_RETVAL
#define TMRANGLE_RETVAL
Definition: tmrangle.h:79
tmrangle_init_sensor_data
void tmrangle_init_sensor_data(tmrangle_t *ctx)
Read and stores parameters data function.
tmrangle_analog_sensor_data_t
Click ctx object definition.
Definition: tmrangle.h:132
tmrangle_t::cs
digital_out_t cs
Definition: tmrangle.h:150
tmrangle_analog_sensor_data_t::cos_p
uint16_t cos_p
Definition: tmrangle.h:135
tmrangle_calib_data_t::sin_ortho
float sin_ortho
Definition: tmrangle.h:233
tmrangle_calibration_find_param
void tmrangle_calibration_find_param(tmrangle_t *ctx, tmrangle_calib_data_t *calib_param)
Get new calibration rotation parameters function.
tmrangle_analog_sensor_data_t::diff_y
int16_t diff_y
Definition: tmrangle.h:143
tmrangle_rotation_data_store_t::magnitude_135
float magnitude_135
Definition: tmrangle.h:223
tmrangle_rotation_data_store_t::amplitude_x
float amplitude_x
Definition: tmrangle.h:213
tmrangle_rotation_data_store_t::y_135_corr
float y_135_corr
Definition: tmrangle.h:211
tmrangle_analog_sensor_data_t::sin_p
uint16_t sin_p
Definition: tmrangle.h:133
tmrangle_t
Definition: tmrangle.h:149
tmrangle_rotation_data_store_t::nr_valid_rotations
uint32_t nr_valid_rotations
Definition: tmrangle.h:187
trigonometry_t::min_diff_sin
int32_t min_diff_sin
Definition: tmrangle.h:243
tmrangle_rotation_data_store_t::y_135
float y_135
Definition: tmrangle.h:203
tmrangle_calculate_diff
void tmrangle_calculate_diff(tmrangle_t *ctx)
Calculate the differential values of sin,cos signal and angle function.
tmrangle_calib_calculate_ortho
void tmrangle_calib_calculate_ortho(tmrangle_calib_data_t *calib_param)
Calculates and stores parameters, based on found parameters from both CW and CCW function.
tmrangle_analog_sensor_data_t::cos_n
uint16_t cos_n
Definition: tmrangle.h:139
tmrangle_analog_sensor_data_t::sin_n
uint16_t sin_n
Definition: tmrangle.h:137
tmrangle_rotation_data_store_t::angle_45_found
uint8_t angle_45_found
Definition: tmrangle.h:185
tmrangle_rotation_data_store_t::x_135
float x_135
Definition: tmrangle.h:199
tmrangle_cfg_setup
void tmrangle_cfg_setup(tmrangle_cfg_t *cfg)
Config Object Initialization function.
tmrangle_read_channel_adc
uint16_t tmrangle_read_channel_adc(tmrangle_t *ctx, uint8_t channel)
Read the ADC value of the selected channel function.
tmrangle_cfg_t::sck
pin_name_t sck
Definition: tmrangle.h:170
tmrangle_get_calib_angle
float tmrangle_get_calib_angle(tmrangle_t *ctx, tmrangle_calib_data_t *calib_param)
Calculate the calibrated angle ( degree ) function.
tmrangle_cfg_t::spi_speed
uint32_t spi_speed
Definition: tmrangle.h:176
tmrangle_rotation_data_store_t::max_cos
int32_t max_cos
Definition: tmrangle.h:189
tmrangle_cfg_t
Click configuration structure definition.
Definition: tmrangle.h:165
tmrangle_calib_data_t::rotation_data
tmrangle_rotation_data_store_t rotation_data
Definition: tmrangle.h:231
tmrangle_rotation_data_store_t
Definition: tmrangle.h:184
tmrangle_cfg_t::spi_mode
uint8_t spi_mode
Definition: tmrangle.h:177
tmrangle_rotation_data_store_t::ortho_one_dir
float ortho_one_dir
Definition: tmrangle.h:225
trigonometry_t
Definition: tmrangle.h:240
tmrangle_analog_sensor_data_t::diff_x
int16_t diff_x
Definition: tmrangle.h:141
tmrangle_t::chip_select
pin_name_t chip_select
Definition: tmrangle.h:155
tmrangle_calib_data_t
Definition: tmrangle.h:230
tmrangle_calib_find_min_max
void tmrangle_calib_find_min_max(tmrangle_t *ctx, tmrangle_rotation_data_store_t *rotation_data_store)
Compare and stores rotation parameters function.
tmrangle_rotation_data_store_t::offset_x
float offset_x
Definition: tmrangle.h:217
tmrangle_rotation_data_store_t::x_45
float x_45
Definition: tmrangle.h:197
tmrangle_rotation_data_store_t::max_sin
int32_t max_sin
Definition: tmrangle.h:193
tmrangle_rotation_data_store_t::y_45_corr
float y_45_corr
Definition: tmrangle.h:209
tmrangle_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: tmrangle.h:178
trigonometry_t::cos_135
float cos_135
Definition: tmrangle.h:248
tmrangle_rotation_data_store_t::x_135_corr
float x_135_corr
Definition: tmrangle.h:207
trigonometry_t::sin_45
float sin_45
Definition: tmrangle.h:245
tmrangle_rotation_data_store_t::offset_y
float offset_y
Definition: tmrangle.h:219
tmrangle_calib_calculate_one_direction
void tmrangle_calib_calculate_one_direction(tmrangle_t *ctx, tmrangle_rotation_data_store_t *rotation_data_store)
Calculates and stores parameters, based on found parameters function.
trigonometry_t::cos_45
float cos_45
Definition: tmrangle.h:246
tmrangle_rotation_data_store_t::min_cos
int32_t min_cos
Definition: tmrangle.h:191
tmrangle_rotation_data_store_t::y_45
float y_45
Definition: tmrangle.h:201
trigonometry_t::max_diff_cos
int32_t max_diff_cos
Definition: tmrangle.h:242
tmrangle_t::spi
spi_master_t spi
Definition: tmrangle.h:154
tmrangle_cfg_t::miso
pin_name_t miso
Definition: tmrangle.h:168
tmrangle_rotation_data_store_t::magnitude_45
float magnitude_45
Definition: tmrangle.h:221
tmrangle_analog_sensor_data_t::uncalibrated_angle
float uncalibrated_angle
Definition: tmrangle.h:145
trigonometry_t::min_diff_cos
int32_t min_diff_cos
Definition: tmrangle.h:244