-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathads.c
More file actions
373 lines (303 loc) · 8.33 KB
/
Copy pathads.c
File metadata and controls
373 lines (303 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "ads.h"
#include "esp_log.h"
#define ADS_BASE_ADDR 0
static ads_callback ads_data_callback;
static bool stretch_en = false;
static char * TAG = {"ADS"};
static void ads_Callback(float * sample, uint8_t sample_type)
{
}
//######################## NEW code start ######################//
int ads(){
ads_init_t init = {0};
init.addr = ADS_BASE_ADDR;
init.ads_sample_callback = &ads_Callback;
init.datardy_pin = 17;
init.reset_pin = 16;
init.sps = ADS_100_HZ;
int ret = ads_init(&init);
if( ret == ADS_ERR ){
ESP_LOGE(TAG, "ads_init failed");
}
ads_hal_delay(10);
ads_polled(true);
ads_hal_delay(10);
return ret;
}
int ads_read()
{
float sample[2];
uint8_t data_type;
int ret = ads_read_polled(sample,&data_type);
if(ret != ADS_OK)
{
ESP_LOGE(TAG, "ADS read in polled mode failed");
return 0;
}
return (int32_t)sample[0];
}
//######################## NEW code End ######################//
/**
* @brief Parses sample buffer from one axis ADS. Scales to degrees and
* executes callback registered in ads_init.
* This function is called from ads_hal.
* Application should never call this function.
*/
static void ads_parse_read_buffer(uint8_t * buffer)
{
static float sample[2];
if(!stretch_en)
{
sample[1] = 0.0f;
}
if(buffer[0] == ADS_SAMPLE)
{
int16_t temp = ads_int16_decode(&buffer[1]);
sample[0] = (float)temp/64.0f;
ads_data_callback(sample, buffer[0]);
}
else if(buffer[0] == ADS_STRETCH_SAMPLE)
{
int16_t temp = ads_int16_decode(&buffer[1]);
sample[1] = (float)temp/64.0f;
ads_data_callback(sample, buffer[0]);
}
}
/**
* @brief Reads ADS sample data when ADS is in polled mode
*
* @param sample[out] floating point array returns new sample
* @param data_type[out] returns if the data read is bend or stretch data
* @return ADS_OK if successful ADS_ERR_IO if failed
*/
int ads_read_polled(float * sample, uint8_t * data_type)
{
uint8_t buffer[ADS_TRANSFER_SIZE];
int16_t temp;
// Read data from sensor
int ret_val = ads_hal_read_buffer(buffer, ADS_TRANSFER_SIZE);
// Parse data if successful read
if(ret_val == ADS_OK)
{
// Check that read packet is a data packet
if(buffer[0] == ADS_SAMPLE)
{
data_type[0] = buffer[0];
temp = ads_int16_decode(&buffer[1]);
sample[0] = (float)temp/64.0f;
}
else if(buffer[0] == ADS_STRETCH_SAMPLE)
{
data_type[0] = buffer[0];
temp = ads_int16_decode(&buffer[1]);
sample[1] = (float)temp/64.0f;
}
else
{
ret_val = ADS_ERR; // Set to general error, data packet not found
}
}
return ret_val;
}
/**
* @brief Places ADS in free run or sleep mode
*
* @param run true if activating ADS, false is putting in suspend mode
* @return ADS_OK if successful ADS_ERR_IO if failed
*/
int ads_run(bool run)
{
uint8_t buffer[ADS_TRANSFER_SIZE];
buffer[0] = ADS_RUN;
buffer[1] = run;
return ads_hal_write_buffer(buffer, ADS_TRANSFER_SIZE);
}
/**
* @brief Places ADS in poll mode. Each time sensor data is read a new sample is taken
*
* @param poll true if activating ADS, false is putting in suspend mode
* @return ADS_OK if successful ADS_ERR_IO if failed
*/
int ads_polled(bool poll)
{
uint8_t buffer[ADS_TRANSFER_SIZE];
buffer[0] = ADS_POLLED_MODE;
buffer[1] = poll;
return ads_hal_write_buffer(buffer, ADS_TRANSFER_SIZE);
}
/**
* @brief Enables and Disables the reading of linear displacment data
*
* @param enable true if enabling ADS to read stretch, false is disabling
* @return ADS_OK if successful ADS_ERR_IO if failed
*/
int ads_stretch_en(bool enable)
{
uint8_t buffer[ADS_TRANSFER_SIZE];
buffer[0] = ADS_READ_STRETCH;
buffer[1] = enable;
stretch_en = enable;
return ads_hal_write_buffer(buffer, ADS_TRANSFER_SIZE);
}
/**
* @brief Sets the sample rate of the ADS in free run mode
*
* @param sps ADS_SPS_T sample rate
* @return ADS_OK if successful ADS_ERR_IO if failed
*/
int ads_set_sample_rate(ADS_SPS_T sps)
{
uint8_t buffer[ADS_TRANSFER_SIZE];
buffer[0] = ADS_SPS;
ads_uint16_encode(sps, &buffer[1]);
return ads_hal_write_buffer(buffer, ADS_TRANSFER_SIZE);
}
/**
* @brief Updates the I2C address of the selected ADS. The default address
* is 0x12. Use this function to program an ADS to allow multiple
* devices on the same I2C bus.
*
* @param address new address of the ADS
* @return ADS_OK if successful ADS_ERR_IO if failed
*/
int ads_update_device_address(uint8_t address)
{
uint8_t buffer[ADS_TRANSFER_SIZE];
buffer[0] = ADS_SET_ADDRESS;
buffer[1] = address;
if(ads_hal_write_buffer(buffer, ADS_TRANSFER_SIZE) != ADS_OK)
return ADS_ERR_IO;
ads_hal_set_address(address);
return ADS_OK;
}
/**
* @brief Initializes the hardware abstraction layer and sample rate of the ADS
*
* @param ads_init_t initialization structure of the ADS
* @return ADS_OK if successful ADS_ERR if failed
*/
int ads_init(ads_init_t * ads_init)
{
// If addr variable updated update address in HAL
if(ads_init->addr != 0)
{
ads_hal_set_address(ads_init->addr);
}
// Initialize the hardware abstraction layer
ads_hal_init(&ads_parse_read_buffer, ads_init->reset_pin, ads_init->datardy_pin);
// Copy local pointer of callback to user application code
ads_data_callback = ads_init->ads_sample_callback;
// Check that the device type is a one axis
ADS_DEV_TYPE_T ads_dev_type;
if (ads_get_dev_type(&ads_dev_type) != ADS_OK)
return ADS_ERR_DEV_ID;
switch (ads_dev_type)
{
case ADS_DEV_ONE_AXIS_V1:
case ADS_DEV_ONE_AXIS_V2:
break;
default:
return ADS_ERR_DEV_ID;
}
ads_hal_delay(2);
// Set the sample rate for interrupt mode
if(ads_set_sample_rate(ads_init->sps))
return ADS_ERR;
ads_hal_delay(2);
return ADS_OK;
}
/**
* @brief Calibrates one axis ADS. ADS_CALIBRATE_FIRST should be at 0 degrees on
* ADS_CALIBRATE_SECOND can be at 45 - 255 degrees, recommended 90 degrees.
*
* @param ads_calibration_step ADS_CALIBRATE_STEP_T to perform
* @param degrees uint8_t angle at which sensor is bent when performing
* ADS_CALIBRATE_FIRST, and ADS_CALIBRATE_SECOND
* @return ADS_OK if successful ADS_ERR_IO or ADS_BAD_PARAM if failed
*/
int ads_calibrate(ADS_CALIBRATION_STEP_T ads_calibration_step, uint8_t degrees)
{
uint8_t buffer[ADS_TRANSFER_SIZE];
buffer[0] = ADS_CALIBRATE;
buffer[1] = ads_calibration_step;
buffer[2] = degrees;
return ads_hal_write_buffer(buffer, ADS_TRANSFER_SIZE);
}
/**
* @brief Shutdown ADS. Requires reset to wake up from Shutdown. ~50nA in shutdwon
*
* @return ADS_OK if successful ADS_ERR_IO if failed
*/
int ads_shutdown(void)
{
uint8_t buffer[ADS_TRANSFER_SIZE];
buffer[0] = ADS_SHUTDOWN;
return ads_hal_write_buffer(buffer, ADS_TRANSFER_SIZE);
}
/**
* @brief Wakes up ADS from shutdown. Delay is necessary for ADS to reinitialize
* all settings on ADS will be reset to default. Reinitilaztion necessary
*
* @return ADS_OK if successful ADS_ERR_IO if failed
*/
int ads_wake(void)
{
// Reset ADS to wake from shutdown
ads_hal_reset();
// Allow time for ADS to reinitialize
ads_hal_delay(100);
return ADS_OK;
}
/**
* @brief Checks that the device id is ADS_ONE_AXIS. ADS should not be in free run
* when this function is called.
*
* @return ADS_OK if dev_id is ADS_ONE_AXIS, ADS_ERR_DEV_ID if not
*/
int ads_get_dev_id(void)
{
ADS_DEV_TYPE_T device_type;
if (ads_get_dev_type(&device_type) == ADS_OK)
{
switch (device_type)
{
case ADS_DEV_ONE_AXIS_V1:
case ADS_DEV_ONE_AXIS_V2:
return ADS_OK;
default:
return ADS_ERR_DEV_ID;
}
}
return ADS_ERR_DEV_ID;
}
/**
* @brief Returns the device type in device_type. ADS should not be in free run
* when this function is called.
*
* @param device_type recipient of the device type
* @return ADS_OK if dev_id is one of ADS_DEV_TYPE_T, ADS_ERR_DEV_ID if not
*/
int ads_get_dev_type(ADS_DEV_TYPE_T * ads_dev_type)
{
uint8_t buffer[ADS_TRANSFER_SIZE];
buffer[0] = ADS_GET_DEV_ID;
// Disable interrupt to prevent callback from reading out device id
ads_hal_pin_int_enable(false);
ads_hal_write_buffer(buffer, ADS_TRANSFER_SIZE);
ads_hal_delay(2);
ads_hal_read_buffer(buffer, ADS_TRANSFER_SIZE);
ads_hal_pin_int_enable(true);
if (buffer[0] == ADS_DEV_ID)
{
switch (buffer[1])
{
case ADS_DEV_ONE_AXIS_V1:
case ADS_DEV_ONE_AXIS_V2:
case ADS_DEV_TWO_AXIS_V1:
*ads_dev_type = (ADS_DEV_TYPE_T)buffer[1];
return ADS_OK;
}
}
*ads_dev_type = ADS_DEV_UNKNOWN;
return ADS_ERR_DEV_ID;
}