-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnalogDevice_AD5161.cpp
395 lines (333 loc) · 10.5 KB
/
AnalogDevice_AD5161.cpp
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**************************************************************************/
/*!
@file AnalogDevice_AD5161.cpp
@author SOSAndroid.fr (E. Ha.)
@section HISTORY
v1.0 - First release
This library aims to interact with the AD5161 from Analog Digital. This works through I2C or SPI
This should work on other 1 channel devices from the manufacturer
*/
/**************************************************************************/
#include <stdlib.h>
#include <Wire.h>
#include <SPI.h>
#include "AnalogDevice_AD5161.h"
/*========================================================================*/
/* CONSTRUCTORS */
/*========================================================================*/
/**************************************************************************/
/*!
Constructors
*/
/**************************************************************************/
ANALOGDEVICE_AD5161::ANALOGDEVICE_AD5161(void)
{
//default : SPI mode
_I2C_mode = false;
_chipAddress = 0;
_CS_pin = AD5161_CS_PIN;
_stepper_maxposition = AD5161_STEPS;
_chip_maxvalue = AD5161_RVALUE;
}
ANALOGDEVICE_AD5161::ANALOGDEVICE_AD5161(boolean SpiI2c_mode, uint8_t cs_pin, uint8_t ad0_value)
{
//SpiI2c_mode : False = SPI, True = I2C
//ad0_value within 0-1 range
_I2C_mode = SpiI2c_mode;
_chipAddress = (AD5161_I2C_ADDR_BASE << 1);
if (ad0_value < 2) _chipAddress += ad0_value;
_CS_pin = cs_pin;
_stepper_maxposition = AD5161_STEPS;
_chip_maxvalue = AD5161_RVALUE;
}
ANALOGDEVICE_AD5161::ANALOGDEVICE_AD5161(boolean SpiI2c_mode, uint8_t cs_pin, uint8_t ad0_value, uint8_t nb_step, uint16_t R_value)
{
//SpiI2c_mode : False = SPI, True = I2C
//ad0_value within 0-1 range
_I2C_mode = SpiI2c_mode;
_chipAddress = (AD5161_I2C_ADDR_BASE << 1);
if (ad0_value < 2) _chipAddress += ad0_value;
_CS_pin = cs_pin;
_stepper_maxposition = nb_step;
_chip_maxvalue = R_value;
}
/*========================================================================*/
/* PUBLIC FUNCTIONS */
/*========================================================================*/
void ANALOGDEVICE_AD5161::init(void) {
#ifdef SERIAL_DEBUG
if(!Serial) {
Serial.begin(DEBUG_SERIALBAUD);
}
#endif
#ifdef BUSES_INIT
(_I2C_mode) ? Wire.begin() : SPI.begin();
#endif
_step_Rvalue = _chip_maxvalue / (_stepper_maxposition + 1);
_step0_Rvalue = AD5161_0_VALUE;
_stepper_currentposition = 0;
_OP_mode = OP_STOP;
_stepper_stepsize = OP_DEFAULT_STEP_SIZE;
_stepper_updateTimeframe = OP_DEFAULT_TIME_FRAME;
if(!_I2C_mode) {
pinMode(_CS_pin, OUTPUT);
digitalWrite(_CS_pin, HIGH);
}
ANALOGDEVICE_AD5161::debug_serial(DEBUG_ALL);
_debug_enabled = false;
ANALOGDEVICE_AD5161::setMidscale();
return;
}
void ANALOGDEVICE_AD5161::update(void) {
switch(_OP_mode) {
case OP_STEP_MODE:
ANALOGDEVICE_AD5161::update_byStep();
break;
case OP_TIME_MODE:
ANALOGDEVICE_AD5161::update_byTime();
break;
default:
break;
}
return;
}
void ANALOGDEVICE_AD5161::setMidscale(void) {
_stepper_currentposition = (_stepper_maxposition >> 1);
if (_I2C_mode) {
ANALOGDEVICE_AD5161::writeI2C(_stepper_currentposition);
}
else {
ANALOGDEVICE_AD5161::writeSPI(_stepper_currentposition);
}
return;
}
uint8_t ANALOGDEVICE_AD5161::getPosition(void) {
if (_I2C_mode) {
_stepper_currentposition = ANALOGDEVICE_AD5161::readI2C();
}
else {
//means SPI mode, non readable
}
return _stepper_currentposition;
}
int ANALOGDEVICE_AD5161::getRvalue(void) {
//provides a rounded value of the current resistor stating from B pin of the device
return (int)((_stepper_currentposition * _step_Rvalue) + _step0_Rvalue);
}
void ANALOGDEVICE_AD5161::moveTo_byStep(int8_t position, int8_t step_size) {
if (_OP_mode == OP_TIME_MODE) return; //We are not changing opration mode during ongoing operations
//a null step size means moving straight to the desired position
_stepper_target = position;
if (step_size == 0) {
if (position >= _stepper_currentposition) {
_stepper_stepsize = position - _stepper_currentposition;
}
else {
_stepper_stepsize = _stepper_currentposition - position;
}
}
else {
_stepper_stepsize = step_size;
}
_OP_mode = OP_STEP_MODE;
ANALOGDEVICE_AD5161::debug_serial(DEBUG_STEPPERPOSITION);
ANALOGDEVICE_AD5161::debug_serial(DEBUG_OPMODE);
ANALOGDEVICE_AD5161::debug_serial(DEBUG_STEPSIZE);
ANALOGDEVICE_AD5161::update();
return;
}
void ANALOGDEVICE_AD5161::moveTo_byTime(int8_t position, int8_t step_size, unsigned long min_time) {
if (step_size == 0) return; //we cannot move with a step size null and a minimum delay per step. Please use ANALOGDEVICE_AD5161::moveTo_bystep instead
if (_OP_mode == OP_STEP_MODE) return; //We are not changing opration mode during ongoing operations
_stepper_target = position;
_stepper_stepsize = step_size;
_stepper_updateTimeframe = min_time;
_OP_mode = OP_TIME_MODE;
ANALOGDEVICE_AD5161::debug_serial(DEBUG_STEPPERPOSITION);
ANALOGDEVICE_AD5161::debug_serial(DEBUG_OPMODE);
ANALOGDEVICE_AD5161::debug_serial(DEBUG_STEPSIZE);
ANALOGDEVICE_AD5161::debug_serial(DEBUG_TIMEFRAME);
ANALOGDEVICE_AD5161::update();
return;
}
void ANALOGDEVICE_AD5161::moveBy(int decalage, uint8_t mode) {
//uses the last known values for _stepper_stepsize & _stepper_updateTimeframe
uint8_t target;
int var = (int) _stepper_currentposition + decalage;
if (var < 0 ) {
target = 0;
}
else if (var >= 0 && var <= (int) _stepper_maxposition) {
target = (uint8_t) var;
}
else {
target = _stepper_maxposition;
}
switch (_OP_mode) {
case OP_STOP:
if (mode == OP_STEP_MODE) ANALOGDEVICE_AD5161::moveTo_byStep(target, _stepper_stepsize);
if (mode == OP_TIME_MODE) ANALOGDEVICE_AD5161::moveTo_byTime(target, _stepper_stepsize, _stepper_updateTimeframe);
//in other cases do nothing as this not a known mode
break;
case OP_STEP_MODE: case OP_TIME_MODE:
_stepper_target = target;
break;
default:
break;
}
return;
}
uint8_t ANALOGDEVICE_AD5161::getOpMode(void) {
return _OP_mode;
}
void ANALOGDEVICE_AD5161::enable_debug (boolean status) {
#ifdef SERIAL_DEBUG
//to avoid debug enabling if Serial not available
if (status) {
_debug_enabled = true;
}
else {
_debug_enabled = false;
}
ANALOGDEVICE_AD5161::debug_serial(DEBUG_DEBUGSTATUS);
#endif
return;
}
/*========================================================================*/
/* PRIVATE FUNCTIONS */
/*========================================================================*/
void ANALOGDEVICE_AD5161::writeI2C(uint8_t position) {
if(_I2C_mode) {
Wire.beginTransmission(_chipAddress);
Wire.write(AD5161_I2C_NORMALWRITE);
Wire.write(position);
Wire.endTransmission();
}
ANALOGDEVICE_AD5161::debug_serial(DEBUG_STEPPERPOSITION);
return;
}
uint8_t ANALOGDEVICE_AD5161::readI2C(void) {
uint8_t nb_byte = 1;
if(_I2C_mode) {
Wire.requestFrom(_chipAddress, nb_byte);
return Wire.read();
}
ANALOGDEVICE_AD5161::debug_serial(DEBUG_STEPPERPOSITION);
return 0;
}
void ANALOGDEVICE_AD5161::writeSPI(uint8_t position) {
if(!_I2C_mode) {
digitalWrite(_CS_pin, LOW);
SPI.transfer(position);
digitalWrite(_CS_pin, HIGH);
}
ANALOGDEVICE_AD5161::debug_serial(DEBUG_STEPPERPOSITION);
return;
}
void ANALOGDEVICE_AD5161::update_byStep(void) {
uint8_t nextstep;
//calculating next step to reach
if (_stepper_currentposition < _stepper_target) {
//we are increasing stepper
if ((_stepper_currentposition + _stepper_stepsize) >= _stepper_maxposition) {
//We are over the top limit
_OP_mode = OP_STOP;
nextstep = _stepper_maxposition;
}
else if ((_stepper_currentposition + _stepper_stepsize) >= _stepper_target) {
//we are over the required position
_OP_mode = OP_STOP;
nextstep = _stepper_target;
}
else {
//Still some work to do
nextstep = _stepper_currentposition + _stepper_stepsize;
}
}
else {
//we are decreasing stepper
if (_stepper_stepsize >= _stepper_currentposition) {
//We are trying to go below 0
_OP_mode = OP_STOP;
nextstep = 0x00;
}
else if ((_stepper_currentposition - _stepper_stepsize) <= _stepper_target) {
//we are going below the required position
_OP_mode = OP_STOP;
nextstep = _stepper_target;
}
else {
//Still some work to do
nextstep = _stepper_currentposition - _stepper_stepsize;
}
}
//moving to next step
if(_I2C_mode) {
ANALOGDEVICE_AD5161::writeI2C(nextstep);
}
else {
ANALOGDEVICE_AD5161::writeSPI(nextstep);
}
return;
}
void ANALOGDEVICE_AD5161::update_byTime(void) {
if (millis() >= (_lastmillis + _stepper_updateTimeframe)) {
ANALOGDEVICE_AD5161::update_byStep();
_lastmillis = millis();
}
return;
}
void ANALOGDEVICE_AD5161::debug_serial(uint8_t dataCode) {
#ifdef SERIAL_DEBUG
if (_debug_enabled) {
switch (dataCode) {
case DEBUG_COMMODE:
Serial.print("communication mode : ");
(_I2C_mode) ? Serial.println("I2C") : Serial.println("SPI");
break;
case DEBUG_CHIPADDR:
Serial.print("I2C chip address : ");
(_I2C_mode) ? Serial.println(_chipAddress, HEX) : Serial.println("Error, SPI mode");
break;
case DEBUG_CSPIN:
Serial.print("SPI CS PIN : ");
(_I2C_mode) ? Serial.println("Error, I2C mode") : Serial.println(_CS_pin, DEC);
break;
case DEBUG_OPMODE:
Serial.print("Operation mode : ");
if (_OP_mode == OP_STOP) Serial.println("stepper not moving");
if (_OP_mode == OP_STEP_MODE) Serial.println("stepper moving by steps");
if (_OP_mode == OP_STOP) Serial.println("stepper moving by steps and by time");
break;
case DEBUG_STEPSIZE:
Serial.print("Step size : ");
Serial.println(_stepper_stepsize, DEC);
break;
case DEBUG_TIMEFRAME:
Serial.print("Timeframe size : ");
Serial.print(_stepper_updateTimeframe, DEC);
Serial.println(" ms");
break;
case DEBUG_STEPPERPOSITION:
Serial.print("Stepper current position : ");
Serial.print(_stepper_currentposition, DEC);
break;
case DEBUG_DEBUGSTATUS:
Serial.print("Debug is ");
(_debug_enabled) ? Serial.println("on") : Serial.println("off");
default:
//means all debug data to serial
ANALOGDEVICE_AD5161::debug_serial(DEBUG_COMMODE);
ANALOGDEVICE_AD5161::debug_serial(DEBUG_CHIPADDR);
ANALOGDEVICE_AD5161::debug_serial(DEBUG_CSPIN);
ANALOGDEVICE_AD5161::debug_serial(DEBUG_STEPSIZE);
ANALOGDEVICE_AD5161::debug_serial(DEBUG_TIMEFRAME);
ANALOGDEVICE_AD5161::debug_serial(DEBUG_STEPPERPOSITION);
ANALOGDEVICE_AD5161::debug_serial(DEBUG_DEBUGSTATUS);
Serial.println("--");
break;
}
}
#endif
return;
}