-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathEVs_NXTMMX.cpp
418 lines (362 loc) · 13.4 KB
/
EVs_NXTMMX.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// EVs_NXTMMX.cpp
//
// Initial version: 2010-06-07 by Clinton Blackmore
// Modified for EVShield: 2015-02-16 by Michael Giles
// Corrected encoder cast: 2017-02-08 by Seth Tenembaum
// Large parts of the code is ported from the NXC library for the device,
// written by Deepak Patil.
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "EVs_NXTMMX.h"
EVs_NXTMMX::EVs_NXTMMX(uint8_t i2c_address)
: EVShieldI2C(i2c_address)
{
// for some reason the reset command doesn't work here
//reset(); // start all encoder positions off at 0
}
uint8_t EVs_NXTMMX::getBatteryVoltage()
{
return readByte(MMX_VOLTAGE)*39;
}
uint8_t EVs_NXTMMX::issueCommand(char command)
{
return writeByte(MMX_COMMAND, (uint8_t)command);
}
// Set/get the encoder target for the motor (ie. a position to go
// to and stop at)
bool EVs_NXTMMX::setEncoderTarget(uint8_t which_motor, long target)
{
uint8_t reg = (which_motor == MMX_Motor_1) ? MMX_SETPT_M1 : MMX_SETPT_M2;
return writeLong(reg, target);
}
long EVs_NXTMMX::getEncoderTarget(uint8_t which_motor)
{
uint8_t reg = (which_motor == MMX_Motor_1) ? MMX_SETPT_M1 : MMX_SETPT_M2;
return (long)readLong(reg);
}
// Set/get the speed of the motor
// (I believe this is in the range [-100, +100])
bool EVs_NXTMMX::setSpeed(uint8_t which_motor, int speed)
{
uint8_t reg = (which_motor == MMX_Motor_1) ? MMX_SPEED_M1 : MMX_SPEED_M2;
return writeByte(reg, (uint8_t)(int8_t)speed);
}
int8_t EVs_NXTMMX::getSpeed(uint8_t which_motor)
{
uint8_t reg = (which_motor == MMX_Motor_1) ? MMX_SPEED_M1 : MMX_SPEED_M2;
return (int8_t)readByte(reg);
}
// This is the time, in seconds, for the motor to run
bool EVs_NXTMMX::getTimeToRun(uint8_t which_motor, int seconds)
{
uint8_t reg = (which_motor == MMX_Motor_1) ? MMX_TIME_M1 : MMX_TIME_M2;
return writeByte(reg, seconds);
}
uint8_t EVs_NXTMMX::getTimeToRun(uint8_t which_motor)
{
uint8_t reg = (which_motor == MMX_Motor_1) ? MMX_TIME_M1 : MMX_TIME_M2;
return readByte(reg);
}
// Command Register 'B' is currently unused, but reserved for future expansion
// If you set it, you must set it to zero.
bool EVs_NXTMMX::setCommandRegB(uint8_t which_motor, uint8_t value)
{
uint8_t reg = (which_motor == MMX_Motor_1) ? MMX_CMD_B_M1 : MMX_CMD_B_M2;
return writeByte(reg, value);
}
uint8_t EVs_NXTMMX::getCommandRegB(uint8_t which_motor)
{
uint8_t reg = (which_motor == MMX_Motor_1) ? MMX_CMD_B_M1 : MMX_CMD_B_M2;
return readByte(reg);
}
// See User's Guide for what command register A does
bool EVs_NXTMMX::setCommandRegA(uint8_t which_motor, uint8_t value)
{
uint8_t reg = (which_motor == MMX_Motor_1) ? MMX_CMD_A_M1 : MMX_CMD_A_M2;
return writeByte(reg, value);
}
uint8_t EVs_NXTMMX::getCommandRegA(uint8_t which_motor)
{
uint8_t reg = (which_motor == MMX_Motor_1) ? MMX_CMD_A_M1 : MMX_CMD_A_M2;
return readByte(reg);
}
// Get the current encoder position
int32_t EVs_NXTMMX::getEncoderPosition(uint8_t which_motor)
{
uint8_t location = (which_motor == MMX_Motor_1) ? MMX_POSITION_M1 : MMX_POSITION_M2;
return (int32_t)readLong(location);
}
// See User's Guide for documentation on the status byte
uint8_t EVs_NXTMMX::getMotorStatusByte(uint8_t which_motor)
{
uint8_t location = (which_motor == MMX_Motor_1) ? MMX_STATUS_M1 : MMX_STATUS_M2;
return readByte(location);
}
// (I couldn't find an explanation for this in the User's Guide)
uint8_t EVs_NXTMMX::getMotorTasksRunningByte(uint8_t which_motor)
{
uint8_t location = (which_motor == MMX_Motor_1) ? MMX_TASKS_M1 : MMX_TASKS_M2;
return readByte(location);
}
// Set the PID that controls how we stop as we approach the
// angle we're set to stop at
bool EVs_NXTMMX::setEncoderPID(uint16_t Kp, uint16_t Ki, uint16_t Kd)
{
writeIntToBuffer(_buffer + 0, Kp);
writeIntToBuffer(_buffer + 2, Ki);
writeIntToBuffer(_buffer + 4, Kd);
return writeRegisters(MMX_ENCODER_PID, 6, _buffer);
}
// Sets the PID that controls how well that motor maintains its speed
bool EVs_NXTMMX::setSpeedPID(uint16_t Kp, uint16_t Ki, uint16_t Kd)
{
writeIntToBuffer(_buffer + 0, Kp);
writeIntToBuffer(_buffer + 2, Ki);
writeIntToBuffer(_buffer + 4, Kd);
return writeRegisters(MMX_SPEED_PID, 6, _buffer);
}
// See user's guide for details.
bool EVs_NXTMMX::setPassCount(uint8_t pass_count)
{
return writeByte(MMX_PASS_COUNT, pass_count);
}
// Sets tolerance which adjust accuracy while positioning.
// See user's guide for more details.
bool EVs_NXTMMX::setTolerance(uint8_t tolerance)
{
return writeByte(MMX_TOLERANCE, tolerance);
}
// Special I2C commands
// Resets all encoder values and motor parameters. Leaves PIDs untouched.
bool EVs_NXTMMX::reset()
{
return issueCommand('R');
}
// Tells the motors to start at the same time.
bool EVs_NXTMMX::startMotorsInSync()
{
return issueCommand('S');
}
// Reset the encoder for motor 1 or motor 2
bool EVs_NXTMMX::resetEncoder(uint8_t which_motor)
{
char code = (which_motor == MMX_Motor_1) ? 'r' : 's';
return issueCommand(code);
}
// This function sets the speed, the number of seconds, and
// the control (a.k.a. command register A)
bool EVs_NXTMMX::setSpeedTimeAndControl(
uint8_t which_motors, // MMX_Motor_ 1, 2, or Both
int speed, // in range [-100, +100]
uint8_t duration, // in seconds
uint8_t control) // bit flags for control purposes
{
if (which_motors == MMX_Motor_Both)
{
control &= ~MMX_CONTROL_GO; // Clear the 'go right now' flag
bool m1 = setSpeedTimeAndControl(MMX_Motor_1, speed, duration, control);
bool m2 = setSpeedTimeAndControl(MMX_Motor_2, speed, duration, control);
startMotorsInSync();
return m1 && m2;
}
_buffer[0] = (uint8_t)(int8_t)speed;
_buffer[1] = duration;
_buffer[2] = 0; // command register B
_buffer[3] = control; // command register A
uint8_t reg = (which_motors == MMX_Motor_1) ? MMX_SPEED_M1 : MMX_SPEED_M2;
return writeRegisters(reg, 4);
}
void setEncoderSpeedTimeAndControlInBuffer(
uint8_t* buffer, // pointer to the buffer
long encoder, // encoder value
int speed, // speed, in range [-100, +100]
uint8_t duration, // in seconds
uint8_t control) // control flags
{
writeLongToBuffer(buffer + 0, (uint32_t)(int32_t)encoder);
buffer[4] = (uint8_t)(int8_t)speed;
buffer[5] = duration;
buffer[6] = 0; // command register B
buffer[7] = control; // command register A
}
// This function sets the speed, the number of seconds, and
// the control (a.k.a. command register A)
bool EVs_NXTMMX::setEncoderSpeedTimeAndControl(
uint8_t which_motors, // MMX_Motor_ 1, 2, or Both
long encoder, // encoder/tachometer position
int speed, // speed, in range [-100, +100]
uint8_t duration, // in seconds
uint8_t control) // control flags
{
if (which_motors == MMX_Motor_Both)
{
// The motor control registers are back to back, and both can be written in one command
control &= ~MMX_CONTROL_GO; // Clear the 'go right now' flag
setEncoderSpeedTimeAndControlInBuffer(_buffer + 0, encoder, speed, duration, control);
setEncoderSpeedTimeAndControlInBuffer(_buffer + 8, encoder, speed, duration, control);
bool success = writeRegisters(MMX_SETPT_M1, 16);
startMotorsInSync();
return success;
}
// Or, just issue the command for one motor
setEncoderSpeedTimeAndControlInBuffer(_buffer, encoder, speed, duration, control);
uint8_t reg = (which_motors == MMX_Motor_1) ? MMX_SETPT_M1 : MMX_SETPT_M2;
return writeRegisters(reg, 8);
}
// True when a motor has completed a timed move
bool EVs_NXTMMX::isTimeDone(uint8_t which_motors)
{
if (which_motors == MMX_Motor_Both)
{
return isTimeDone(MMX_Motor_1) && isTimeDone(MMX_Motor_2);
}
uint8_t status = getMotorStatusByte(which_motors);
return (status & MMX_CONTROL_TIME) == 0;
}
// waited until a timed command finishes
void EVs_NXTMMX::waitUntilTimeDone(uint8_t which_motors)
{
delay(50); // this delay is required for the status byte to be available for reading.
while (!isTimeDone(which_motors)) delay(50);
}
// True when a command based on using the motor encoder completes
bool EVs_NXTMMX::isTachoDone(uint8_t which_motors)
{
if (which_motors == MMX_Motor_Both)
{
return isTachoDone(MMX_Motor_1) && isTachoDone(MMX_Motor_2);
}
uint8_t status = getMotorStatusByte(which_motors);
return (status & MMX_CONTROL_TACHO) == 0;
}
// waited until a turn-by-degrees command ends
void EVs_NXTMMX::waitUntilTachoDone(uint8_t which_motors)
{
delay(50); // this delay is required for the status byte to be available for reading.
while (!isTachoDone(which_motors)) delay(50);
}
// Utility functions for motor control
// Take a speed and direction and give just a speed
inline int calcFinalSpeed(int initialSpeed, uint8_t direction)
{
if (direction == MMX_Direction_Forward)
return initialSpeed;
return -initialSpeed;
}
// Calculate the bits that control what happens when this action finishes
inline uint8_t calcNextActionBits(uint8_t next_action)
{
if (next_action == MMX_Next_Action_Brake)
return MMX_CONTROL_BRK;
else if (next_action == MMX_Next_Action_BrakeHold)
return MMX_CONTROL_BRK | MMX_CONTROL_ON;
}
void EVs_NXTMMX::runUnlimited(
uint8_t which_motors, // MMX_Motor_ 1, 2, or Both
uint8_t direction, // MMX_Direction_ Forward or Reverse
int speed) // in range [-100, +100]
{
uint8_t ctrl = MMX_CONTROL_SPEED | MMX_CONTROL_GO;
int sp = calcFinalSpeed(speed, direction);
setSpeedTimeAndControl(which_motors, sp, 0, ctrl);
}
// runs the motors for a given number of seconds
void EVs_NXTMMX::runSeconds(
uint8_t which_motors, // MMX_Motor_ 1, 2, or Both
uint8_t direction, // MMX_Direction_ Forward or Reverse
int speed, // [-100, +100]
uint8_t duration, // in seconds
uint8_t wait_for_completion, // MMX_Completion_ Wait_For or Dont_Wait
uint8_t next_action) // MMX_Next_Action_ Brake, BrakeHold or Float
{
uint8_t ctrl = MMX_CONTROL_SPEED | MMX_CONTROL_TIME | MMX_CONTROL_GO;
ctrl |= calcNextActionBits(next_action);
int sp = calcFinalSpeed(speed, direction);
setSpeedTimeAndControl(which_motors, sp, duration, ctrl);
if (wait_for_completion == MMX_Completion_Wait_For)
{
waitUntilTimeDone(which_motors);
}
}
// runs the motors until the tachometer reaches a certain position
void EVs_NXTMMX::runTachometer(
uint8_t which_motors, // MMX_Motor_ 1, 2, or Both
uint8_t direction, // MMX_Direction_ Forward or Reverse
int speed, // [-100, +100]
long tachometer, // in degrees
uint8_t relative, // MMX_Move_ Relative or Absolute
uint8_t wait_for_completion, // MMX_Completion_ Wait_For or Dont_Wait
uint8_t next_action) // MMX_Next_Action_ Brake, BrakeHold or Float
{
uint8_t ctrl = MMX_CONTROL_SPEED | MMX_CONTROL_TACHO | MMX_CONTROL_GO;
ctrl |= calcNextActionBits(next_action);
int final_speed = calcFinalSpeed(speed, direction);
// The tachometer can be absolute or relative.
// If it is absolute, we ignore the direction parameter.
long final_tach = tachometer;
if (relative == MMX_Move_Relative)
{
ctrl |= MMX_CONTROL_RELATIVE;
// a (relative) forward command is always a positive tachometer reading
final_tach = abs(tachometer);
if (final_speed < 0)
{
// and a (relative) reverse command is always negative
final_tach = -final_tach;
}
}
setEncoderSpeedTimeAndControl(which_motors, final_tach, final_speed, 0, ctrl);
if (wait_for_completion == MMX_Completion_Wait_For)
{
waitUntilTachoDone(which_motors);
}
}
// Turns the motors the specified number of degrees
void EVs_NXTMMX::runDegrees(
uint8_t which_motors, // MMX_Motor_ 1, 2, or Both
uint8_t direction, // MMX_Direction_ Forward or Reverse
int speed, // [-100, +100]
long degrees, // in degrees
uint8_t wait_for_completion, // MMX_Completion_ Wait_For or Dont_Wait
uint8_t next_action) // MMX_Next_Action_ Brake, BrakeHold or Float
{
runTachometer(which_motors, direction, speed, degrees,
MMX_Move_Relative, wait_for_completion, next_action);
}
// runs the motor(s) the specified number of rotations
void EVs_NXTMMX::runRotations(
uint8_t which_motors, // MMX_Motor_ 1, 2, or Both
uint8_t direction, // MMX_Direction_ Forward or Reverse
int speed, // [-100, +100]
long rotations, // number of full rotations of the motor
uint8_t wait_for_completion, // MMX_Completion_ Wait_For or Dont_Wait
uint8_t next_action) // MMX_Next_Action_ Brake, BrakeHold or Float
{
runTachometer(which_motors, direction, speed, 360 * rotations,
MMX_Move_Relative, wait_for_completion, next_action);
}
// The stop command will only stop the motor(s) by making them float/coast
// or brake. Even if you specify MMX_Next_Action_BrakeHold, the motor
// will only brake, not hold.
bool EVs_NXTMMX::stop(uint8_t which_motors, uint8_t next_action)
{
if (which_motors >= MMX_Motor_1 && which_motors <= MMX_Motor_Both)
{
// The magic variables become clear in the user's guide
uint8_t base_code = (next_action != MMX_Next_Action_Float) ? 'A' - 1 : 'a' - 1;
return issueCommand(base_code + which_motors);
}
setWriteErrorCode(5); // bad parameters
return false;
}