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 )
79#define TMRANGLE_RETVAL uint8_t
80
81#define TMRANGLE_OK 0x00
82#define TMRANGLE_INIT_ERROR 0xFF
89#define TMRANGLE_COS_N 0x00
90#define TMRANGLE_COS_P 0x01
91#define TMRANGLE_SIN_P 0x02
92#define TMRANGLE_SIN_N 0x03
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
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
121 // End group macro
122// --------------------------------------------------------------- PUBLIC TYPES
131typedef 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
148typedef 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
164typedef 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
183typedef struct
184{
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
206
208
210
212
214
216
217 float offset_x;
218
219 float offset_y;
220
222
224
226
228
238
239typedef struct
240{
245 float sin_45;
246 float cos_45;
247 float sin_135;
248 float cos_135;
249
251
252 // End types group
253// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
254
259#ifdef __cplusplus
260extern "C"{
261#endif
262
272
281
295uint16_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
444 // End public_function group
446
447// ------------------------------------------------------------------------- END
#define TMRANGLE_RETVAL
Definition tmrangle.h:79
void tmrangle_calculate_diff(tmrangle_t *ctx)
Calculate the differential values of sin,cos signal and angle function.
void tmrangle_calibration_find_param(tmrangle_t *ctx, tmrangle_calib_data_t *calib_param)
Get new calibration rotation parameters function.
float tmrangle_get_calib_deff_angle(tmrangle_t *ctx, tmrangle_calib_data_t *calib_param)
Calculate the calibrated angle ( radians ) function.
void tmrangle_cfg_setup(tmrangle_cfg_t *cfg)
Config Object Initialization function.
void tmrangle_init_sensor_data(tmrangle_t *ctx)
Read and stores parameters data function.
void tmrangle_init_calib_data(tmrangle_t *ctx, tmrangle_calib_data_t *calib_param, trigonometry_t *dev)
Initialization and stores calibration parameters function.
float tmrangle_get_calib_angle(tmrangle_t *ctx, tmrangle_calib_data_t *calib_param)
Calculate the calibrated angle ( degree ) function.
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.
uint16_t tmrangle_read_channel_adc(tmrangle_t *ctx, uint8_t channel)
Read the ADC value of the selected channel function.
TMRANGLE_RETVAL tmrangle_init(tmrangle_t *ctx, tmrangle_cfg_t *cfg)
Initialization function.
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.
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.
void tmrangle_calib_find_min_max(tmrangle_t *ctx, tmrangle_rotation_data_store_t *rotation_data_store)
Compare and stores rotation parameters function.
Click ctx object definition.
Definition tmrangle.h:132
int16_t diff_x
Definition tmrangle.h:141
uint16_t cos_p
Definition tmrangle.h:135
float uncalibrated_angle
Definition tmrangle.h:145
int16_t diff_y
Definition tmrangle.h:143
uint16_t sin_n
Definition tmrangle.h:137
uint16_t cos_n
Definition tmrangle.h:139
uint16_t sin_p
Definition tmrangle.h:133
Definition tmrangle.h:230
float cos_ortho
Definition tmrangle.h:235
tmrangle_rotation_data_store_t rotation_data
Definition tmrangle.h:231
float sin_ortho
Definition tmrangle.h:233
Click configuration structure definition.
Definition tmrangle.h:165
uint8_t spi_mode
Definition tmrangle.h:177
spi_master_chip_select_polarity_t cs_polarity
Definition tmrangle.h:178
pin_name_t sck
Definition tmrangle.h:170
pin_name_t mosi
Definition tmrangle.h:169
uint32_t spi_speed
Definition tmrangle.h:176
pin_name_t miso
Definition tmrangle.h:168
pin_name_t cs
Definition tmrangle.h:171
Definition tmrangle.h:184
int32_t max_cos
Definition tmrangle.h:189
float magnitude_135
Definition tmrangle.h:223
float amplitude_y
Definition tmrangle.h:215
float y_135
Definition tmrangle.h:203
float x_135_corr
Definition tmrangle.h:207
int32_t min_cos
Definition tmrangle.h:191
float amplitude_x
Definition tmrangle.h:213
uint8_t angle_45_found
Definition tmrangle.h:185
float offset_x
Definition tmrangle.h:217
float ortho_one_dir
Definition tmrangle.h:225
float x_45
Definition tmrangle.h:197
float y_45_corr
Definition tmrangle.h:209
float y_45
Definition tmrangle.h:201
float magnitude_45
Definition tmrangle.h:221
uint32_t nr_valid_rotations
Definition tmrangle.h:187
float x_45_corr
Definition tmrangle.h:205
uint8_t angle_135_found
Definition tmrangle.h:186
float y_135_corr
Definition tmrangle.h:211
int32_t max_sin
Definition tmrangle.h:193
float x_135
Definition tmrangle.h:199
int32_t min_sin
Definition tmrangle.h:195
float offset_y
Definition tmrangle.h:219
Definition tmrangle.h:149
digital_out_t cs
Definition tmrangle.h:150
spi_master_t spi
Definition tmrangle.h:154
tmrangle_analog_sensor_data_t sensor_data
Definition tmrangle.h:157
pin_name_t chip_select
Definition tmrangle.h:155
Definition tmrangle.h:240
float sin_45
Definition tmrangle.h:245
int32_t max_diff_sin
Definition tmrangle.h:241
int32_t min_diff_cos
Definition tmrangle.h:244
float cos_45
Definition tmrangle.h:246
float sin_135
Definition tmrangle.h:247
int32_t max_diff_cos
Definition tmrangle.h:242
int32_t min_diff_sin
Definition tmrangle.h:243
float cos_135
Definition tmrangle.h:248