-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeEncoderNew.h
562 lines (526 loc) · 11.6 KB
/
MeEncoderNew.h
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/**
* \par Copyright (C), 2012-2016, MakeBlock
* \class MeEncoderNew
* \brief Driver for Me Encoder New module.
* @file MeEncoderNew.h
* @author MakeBlock
* @version V1.0.0
* @date 2016/03/18
* @brief Header for MeEncoderNew.cpp module
*
* \par Copyright
* This software is Copyright (C), 2012-2016, MakeBlock. Use is subject to license \n
* conditions. The main licensing options available are GPL V2 or Commercial: \n
*
* \par Open Source Licensing GPL V2
* This is the appropriate option if you want to share the source code of your \n
* application with everyone you distribute it to, and you also want to give them \n
* the right to share who uses it. If you wish to use this software under Open \n
* Source Licensing, you must contribute all your source code to the open source \n
* community in accordance with the GPL Version 2 when your application is \n
* distributed. See http://www.gnu.org/copyleft/gpl.html
*
* \par Description
* This file is a drive for Me EncoderNew device, The Me EncoderNew inherited the
* MeSerial class from SoftwareSerial.
*
* \par Method List:
*
* 1. void MeEncoderNew::begin(void);
* 2. void MeEncoderNew::setAddr(uint8_t i2cAddr,uint8_t slot);
* 3. void MeEncoderNew::move(long angle, float speed, float lock_state);
* 4. void MeEncoderNew::moveTo(long angle, float speed,float lock_state);
* 5. void MeEncoderNew::runSpeed(int speed);
* 6. void MeEncoderNew::runTurns(long turns, float speed,float lock_state);
* 7. void MeEncoderNew::reset(void);
* 8. void MeEncoderNew::setSpeedPID(float p,float i,float d);
* 9. void MeEncoderNew::setPosPID(float p,float i,float d);
* 10. void MeEncoderNew::setMode(uint8_t mode);
* 11. void MeEncoderNew::setPWM(int pwm);
* 12. void MeEncoderNew::setCurrentPosition(long pulse_counter)
* 13. long MeEncoderNew::getCurrentPosition();
* 14. void MeEncoderNew::getSpeedPID(float * p,float * i,float * d);
* 15. void MeEncoderNew::getPosPID(float * p,float * i,float * d);
* 16. float MeEncoderNew::getCurrentSpeed(void);
* 17. void MeEncoderNew::sendCmd(void);
* 18. float MeEncoderNew::getRatio(void);
* 19. void MeEncoderNew::setRatio(float r);
* 20. int MeEncoderNew::getPulse(void);
* 21. void MeEncoderNew::setPulse(int p);
* 22. void MeEncoderNew::setDevid(int devid);
* 23. void MeEncoderNew::runSpeedAndTime(float speed, float time, float lock_state);
* 24. boolean MeEncoderNew::isTarPosReached(void);
*
* \par History:
* <pre>
* `<Author>` `<Time>` `<Version>` `<Descr>`
* Mark Yan 2016/03/18 1.0.0 build the new
* Mark Yan 2017/06/09 1.0.1 add function setAddr
* </pre>
*
*/
#ifndef ME_LEGOENCODER_H
#define ME_LEGOENCODER_H
#include <Arduino.h>
#include <stdbool.h>
#include "MeConfig.h"
#include "MePort.h"
#define LEONARDO_PORT1
#if defined(UNO_PORT1) //10:PB3 11:PB2
#define SDA_PORT PORTB
#define SDA_PIN 3
#define SCL_PORT PORTB
#define SCL_PIN 2
#define I2C_SLOWMODE 1
#elif defined(UNO_PORT2) //3:PD3 9:PB1
#define SDA_PORT PORTD
#define SDA_PIN 3
#define SCL_PORT PORTB
#define SCL_PIN 1
#define I2C_SLOWMODE 1
#elif defined(LEONARDO_PORT1) //11:PB7 8:PB4
#define SDA_PORT PORTB
#define SDA_PIN 7
#define SCL_PORT PORTB
#define SCL_PIN 4
#define I2C_SLOWMODE 1
#elif defined(LEONARDO_PORT2) //13:PC7 12:PD6
#define SDA_PORT PORTC
#define SDA_PIN 7
#define SCL_PORT PORTD
#define SCL_PIN 6
#define I2C_SLOWMODE 1
#else
// change sda scl if you use a different pin mapping
#endif
#define PULSE_PER_C 8
/**
* Class: MeEncoderNew
* \par Description
* Declaration of Class MeEncoderNew
*/
class MeEncoderNew{
public:
/**
* Alternate Constructor which can call your own function to map the Encoder Motor New to arduino port,
* you can set any slot for the Encoder Motor New device.
* \param[in]
* port - RJ25 port from PORT_1 to PORT_10
* \param[in]
* slot - SLOT1 or SLOT2
*/
MeEncoderNew(uint8_t addr,uint8_t slot);
/**
* Alternate Constructor which can call your own function to map the Encoder Motor New to arduino port,
* you can set any slot for the Encoder Motor New device.
* \param[in]
* slot - SLOT1 or SLOT2
*/
MeEncoderNew(uint8_t slot);
/**
* Alternate Constructor which can call your own function to map the Encoder Motor New to arduino port,
* you should initialized slot and slaveAddress here for the Encoder Motor New device.
* \param[in]
* None
*/
MeEncoderNew(void);
/**
* \par Function
* begin
* \par Description
* Initialize Encoder Motor New.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void begin(void);
/**
* \par Function
* setAddr
* \par Description
* Reset the i2c address of encoder motor .
* \param[in]
* i2cAddr - i2c address of encoder motor
* \param[in]
* slot - slot number of encoder motor
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void setAddr(uint8_t i2cAddr,uint8_t slot);
/**
* \par Function
* reset
* \par Description
* Reset the parameter of Motor.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void reset(void);
/**
* \par Function
* move
* \par Description
* Motor move.
* \param[in]
* angle - The angle move of Motor New.
* \param[in]
* speed - The speed move of Motor New.
* \param[in]
* lock_state - The lock state of Motor.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void move(long angle, float speed = 220, float lock_state = 1);
/**
* \par Function
* moveTo
* \par Description
* Motor New move to the aim.
* \param[in]
* angle - The angle move of Motor New.
* \param[in]
* speed - The speed move of Motor New.
* \param[in]
* lock_state - The lock state of Motor.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void moveTo(long angle, float speed = 220, float lock_state = 1);
/**
* \par Function
* runTurns
* \par Description
* Motor move turns.
* \param[in]
* turns - The turns move of Motor.
* \param[in]
* speed - The speed move of Motor.
* \param[in]
* lock_state - The lock state of Motor.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void runTurns(long turns, float speed = 220,float lock_state = 1);
/**
* \par Function
* runSpeed
* \par Description
* The speed of Motor's movement.
* \param[in]
* speed - The speed move of Motor.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void runSpeed(float speed,float lock_state = 1);
/**
* \par Function
* getCurrentSpeed
* \par Description
* The current speed of Motor's movement.
* \param[in]
* None
* \par Output
* None
* \par Return
* The speed of encoder motor(The unit is rpm)
* \par Others
* None
*/
float getCurrentSpeed(void);
/**
* \par Function
* setCurrentPosition
* \par Description
* Set current position of Motor.
* \param[in]
* pulse_counter - The count value of current encoder
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void setCurrentPosition(long pulse_counter);
/**
* \par Function
* getCurrentPosition
* \par Description
* The current position of Motor.
* \param[in]
* None
* \par Output
* None
* \par Return
* Motor encoder pulse count value.
* \par Others
* None
*/
long getCurrentPosition(void);
/**
* \par Function
* setSpeedPID
* \par Description
* Set speed PID for Motor.
* \param[in]
* p - P means Proportion.
* \param[in]
* i - I means Integration.
* \param[in]
* d - D means Differentiation.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void setSpeedPID(float p,float i,float d);
/**
* \par Function
* setPosPID
* \par Description
* Set pos PID for Motor.
* \param[in]
* p - P means Proportion.
* \param[in]
* i - I means Integration.
* \param[in]
* d - D means Differentiation.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void setPosPID(float p,float i,float d);
/**
* \par Function
* getSpeedPID
* \par Description
* Get Speed PID from Motor.
* \param[in]
* p - P means Proportion.
* \param[in]
* i - I means Integration.
* \param[in]
* d - D means Differentiation.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void getSpeedPID(float * p,float * i,float * d);
/**
* \par Function
* getPosPID
* \par Description
* Get PID from Motor.
* \param[in]
* p - P means Proportion.
* \param[in]
* i - I means Integration.
* \param[in]
* d - D means Differentiation.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void getPosPID(float * p,float * i,float * d);
/**
* \par Function
* getRatio
* \par Description
* Get the ratio of Motor.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return the ratio of Motor.
* \par Others
* None
*/
float getRatio(void);
/**
* \par Function
* setRatio
* \par Description
* Set the ratio to Motor.
* \param[in]
* ratio - the ratio of Motor
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void setRatio(float r);
/**
* \par Function
* getPulse
* \par Description
* Get the pulse of Motor.
* \param[in]
* None
* \par Output
* None
* \par Return
* Return the pulse of Motor.
* \par Others
* None
*/
int getPulse(void);
/**
* \par Function
* setPulse
* \par Description
* Set the pulse to Motor.
* \param[in]
* pulse - the line number of Motor
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void setPulse(int p);
/**
* \par Function
* setDevid
* \par Description
* Set the devid to Motor.
* \param[in]
* devid - the I2C adress
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void setDevid(uint8_t devid);
/**
* \par Function
* setMode
* \par Description
* Set the work mode to Motor.
* \param[in]
* mode - The work mode of Motor.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void setMode(uint8_t mode);
/**
* \par Function
* setPWM
* \par Description
* Set the PWM to Motor.
* \param[in]
* pwm - Pulse-Width Modulation.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void setPWM(int pwm);
/**
* \par Function
* runSpeedAndTime
* \par Description
* The speed and time of Motor's movement.
* \param[in]
* speed - The speed move of Motor.
* \param[in]
* time - The time move of Motor.
* \param[in]
* lock_state - The lock state of Motor.
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void runSpeedAndTime(float speed, float time, float lock_state = 1);
/**
* \par Function
* isTarPosReached
* \par Description
* Check whether the target position has been reached
* \param[in]
* None
* \par Output
* None
* \par Return
* true - The target position reaches
* false - Does not reach the target position
* \par Others
* None
*/
boolean isTarPosReached(void);
private:
/**
* \par Function
* sendCmd
* \par Description
* Send command to Motor.
* \param[in]
* None
* \par Output
* None
* \par Return
* None
* \par Others
* None
*/
void sendCmd(void);
uint8_t _slot;
uint8_t address;
unsigned long _lastTime;
char cmdBuf[18];
};
#endif