00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __MSMMUX_H__
00013 #define __MSMMUX_H__
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #pragma systemFile
00037
00038 #ifndef __COMMON_H__
00039 #include "common.h"
00040 #endif
00041
00042 #ifndef __MMUX_H__
00043 #include "common-MMUX.h"
00044 #endif
00045
00046 #define MSMMUX_I2C_ADDR 0x06
00047
00048
00049 #define MSMMUX_REG_CMD 0x41
00050
00051 #define MSMMUX_MOT_OFFSET 0x42
00052 #define MSMMUX_TARG_ENC 0x00
00053 #define MSMMUX_POWER 0x04
00054 #define MSMMUX_TARG_TIME 0x05
00055 #define MSMMUX_CMD_B 0x06
00056 #define MSMMUX_CMD_A 0x07
00057 #define MSMMUX_ENTRY_SIZE 0x08
00058 #define MSMMUX_TACHO_MOT1 0x62
00059 #define MSMMUX_TACHO_MOT2 0x66
00060 #define MSMMUX_STATUS_MOT1 0x72
00061 #define MSMMUX_STATUS_MOT2 0x73
00062
00063
00064 #define MSMMUX_KP_TACHO 0x7A
00065 #define MSMMUX_KI_TACHO 0x7C
00066 #define MSMMUX_KD_TACHO 0x7E
00067 #define MSMMUX_KP_SPEED 0x80
00068 #define MSMMUX_KI_SPEED 0x82
00069 #define MSMMUX_KD_SPEED 0x84
00070 #define MSMMUX_PASSCOUNT 0x86
00071 #define MSMMUX_TOLERANCE 0x87
00072
00073
00074 #define MSMMUX_CMD_RESET_ALL 0x52
00075 #define MSMMUX_CMD_START_BOTH 0x53
00076 #define MSMMUX_CMD_FLOAT_MOT1 0x61
00077 #define MSMMUX_CMD_FLOAT_MOT2 0x62
00078 #define MSMMUX_CMD_FLOAT_BOTH 0x63
00079 #define MSMMUX_CMD_BRAKE_MOT1 0x41
00080 #define MSMMUX_CMD_BRAKE_MOT2 0x42
00081 #define MSMMUX_CMD_BRAKE_BOTH 0x43
00082 #define MSMMUX_CMD_RESET_MOT1 0x72
00083 #define MSMMUX_CMD_RESET_MOT2 0x73
00084
00085
00086 #define MSMMUX_STAT_SPEED_CTRL (0x01 << 0)
00087 #define MSMMUX_STAT_RAMPING (0x01 << 1)
00088 #define MSMMUX_STAT_POWERED (0x01 << 2)
00089 #define MSMMUX_STAT_POS_CTRL (0x01 << 3)
00090 #define MSMMUX_STAT_BRAKED (0x01 << 4)
00091 #define MSMMUX_STAT_OVERLOADED (0x01 << 5)
00092 #define MSMMUX_STAT_TIMED (0x01 << 6)
00093 #define MSMMUX_STAT_STALLED (0x01 << 7)
00094
00095
00096 #define MSMMUX_CMD_SPEED 0x01
00097 #define MSMMUX_CMD_RAMP 0x02
00098 #define MSMMUX_CMD_RELATIVE 0x04
00099 #define MSMMUX_CMD_TACHO 0x08
00100 #define MSMMUX_CMD_BRK 0x10
00101 #define MSMMUX_CMD_HOLDPOS 0x20
00102 #define MSMMUX_CMD_TIME 0x40
00103 #define MSMMUX_CMD_GO 0x80
00104
00105 #define MSMMUX_RAMP_NONE 0x00
00106 #define MSMMUX_RAMP_UP_DOWN 0x03
00107
00108 #define MSMMUX_ROT_UNLIMITED 0x00
00109 #define MSMMUX_ROT_DEGREES 0x01
00110
00111 #define MSMMUX_ROT_SECONDS 0x03
00112
00113
00114 tByteArray MSMMUX_I2CRequest;
00115 tByteArray MSMMUX_I2CReply;
00116
00117
00118 void MSMMUXinit();
00119 bool MSMMUXreadStatus(tMUXmotor muxmotor, ubyte &motorStatus);
00120 bool MSMMUXsendCommand(tSensors link, ubyte channel, long setpoint, byte speed, ubyte seconds, ubyte commandA, ubyte address = MSMMUX_I2C_ADDR);
00121 bool MSMMUXsendCommand(tSensors link, ubyte command, ubyte address = MSMMUX_I2C_ADDR);
00122 bool MSMMUXsetPID(tSensors link, unsigned int kpTacho, unsigned int kiTacho, unsigned int kdTacho, unsigned int kpSpeed, unsigned int kiSpeed, unsigned int kdSpeed, ubyte passCount, ubyte tolerance, ubyte address = MSMMUX_I2C_ADDR);
00123
00124 bool MSMMotor(tMUXmotor muxmotor, byte power, ubyte address = MSMMUX_I2C_ADDR);
00125 bool MSMotorStop(tMUXmotor muxmotor, ubyte address = MSMMUX_I2C_ADDR);
00126 bool MSMotorStop(tMUXmotor muxmotor, bool brake, ubyte address = MSMMUX_I2C_ADDR);
00127 void MSMMotorSetRotationTarget(tMUXmotor muxmotor, long target);
00128 void MSMMotorSetTimeTarget(tMUXmotor muxmotor, int target);
00129 void MSMMotorSetEncoderTarget(tMUXmotor muxmotor, long target);
00130 void MSMMotorSetEncoderTarget(tMUXmotor muxmotor, long target, bool relative);
00131 long MSMMotorEncoder(tMUXmotor muxmotor, ubyte address = MSMMUX_I2C_ADDR);
00132 bool MSMMotorEncoderReset(tMUXmotor muxmotor, ubyte address = MSMMUX_I2C_ADDR);
00133 bool MSMMotorEncoderResetAll(tSensors link, ubyte address = MSMMUX_I2C_ADDR);
00134 bool MSMMotorBusy(tMUXmotor muxmotor, ubyte address = MSMMUX_I2C_ADDR);
00135 bool MSMMotorStalled(tMUXmotor muxmotor, ubyte address = MSMMUX_I2C_ADDR);
00136 void MSMMotorSetBrake(tMUXmotor muxmotor);
00137 void MSMMotorSetFloat(tMUXmotor muxmotor);
00138 void MSMMotorSetSpeedCtrl(tMUXmotor muxmotor, bool constspeed);
00139 void MSMMotorSetRamping(tMUXmotor muxmotor, bool ramping);
00140
00141
00142
00143
00144
00145 void MSMMUXinit(){
00146 for (int i = 0; i < 4; i++) {
00147 memset(mmuxData[i].runToTarget[0], false, 4);
00148 memset(mmuxData[i].brake[0], true, 4);
00149 memset(mmuxData[i].pidcontrol[0], true, 4);
00150 memset(mmuxData[i].target[0], 0, 4*4);
00151 memset(mmuxData[i].ramping[0], MSMMUX_RAMP_NONE, 4);
00152 memset(mmuxData[i].targetUnit[0], MSMMUX_ROT_UNLIMITED, 4);
00153 mmuxData[i].initialised = true;
00154 }
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 bool MSMMUXreadStatus(tMUXmotor muxmotor, ubyte &motorStatus, ubyte address) {
00167
00168 memset(MSMMUX_I2CRequest, 0, sizeof(tByteArray));
00169
00170 MSMMUX_I2CRequest[0] = 2;
00171 MSMMUX_I2CRequest[1] = MSMMUX_I2C_ADDR;
00172
00173 switch ((byte)MPORT(muxmotor)) {
00174 case 0: MSMMUX_I2CRequest[2] = MSMMUX_STATUS_MOT1; break;
00175 case 1: MSMMUX_I2CRequest[2] = MSMMUX_STATUS_MOT2; break;
00176 }
00177
00178 if (!writeI2C((tSensors)SPORT(muxmotor), MSMMUX_I2CRequest, MSMMUX_I2CReply, 1))
00179 return false;
00180
00181 motorStatus = MSMMUX_I2CReply[0];
00182
00183 return true;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 bool MSMMUXsendCommand(tSensors link, ubyte channel, long setpoint, byte speed, ubyte seconds, ubyte commandA, ubyte address) {
00201 memset(MSMMUX_I2CRequest, 0, sizeof(tByteArray));
00202
00203 MSMMUX_I2CRequest[0] = 10;
00204 MSMMUX_I2CRequest[1] = address;
00205 MSMMUX_I2CRequest[2] = MSMMUX_MOT_OFFSET + (channel * MSMMUX_ENTRY_SIZE);
00206 MSMMUX_I2CRequest[3] = (setpoint >> 0) & 0xFF;
00207 MSMMUX_I2CRequest[4] = (setpoint >> 8) & 0xFF;
00208 MSMMUX_I2CRequest[5] = (setpoint >> 16) & 0xFF;
00209 MSMMUX_I2CRequest[6] = (setpoint >> 24) & 0xFF;
00210 MSMMUX_I2CRequest[7] = (speed & 0xFF);
00211 MSMMUX_I2CRequest[8] = seconds;
00212 MSMMUX_I2CRequest[9] = 0;
00213 MSMMUX_I2CRequest[10] = commandA;
00214
00215
00216 mmuxData[link].targetUnit[channel] = MSMMUX_ROT_UNLIMITED;
00217
00218
00219 return writeI2C(link, MSMMUX_I2CRequest);
00220
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233 bool MSMMUXsendCommand(tSensors link, ubyte command, ubyte address) {
00234 memset(MSMMUX_I2CRequest, 0, sizeof(tByteArray));
00235
00236 MSMMUX_I2CRequest[0] = 3;
00237 MSMMUX_I2CRequest[1] = address;
00238 MSMMUX_I2CRequest[2] = MSMMUX_REG_CMD;
00239 MSMMUX_I2CRequest[3] = command;
00240
00241 return writeI2C(link, MSMMUX_I2CRequest);
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266 bool MSMMUXsetPID(tSensors link, unsigned int kpTacho, unsigned int kiTacho, unsigned int kdTacho, unsigned int kpSpeed, unsigned int kiSpeed, unsigned int kdSpeed, ubyte passCount, ubyte tolerance, ubyte address) {
00267 memset(MSMMUX_I2CRequest, 0, sizeof(tByteArray));
00268
00269 MSMMUX_I2CRequest[0] = 16;
00270 MSMMUX_I2CRequest[1] = address;
00271 MSMMUX_I2CRequest[2] = MSMMUX_KP_TACHO;
00272 MSMMUX_I2CRequest[3] = kpTacho & 0xFF;
00273 MSMMUX_I2CRequest[4] = (kpTacho >> 8) & 0xFF;
00274 MSMMUX_I2CRequest[5] = kiTacho & 0xFF;
00275 MSMMUX_I2CRequest[6] = (kiTacho >> 8) & 0xFF;
00276 MSMMUX_I2CRequest[7] = kdTacho & 0xFF;
00277 MSMMUX_I2CRequest[8] = (kdTacho >> 8) & 0xFF;
00278 MSMMUX_I2CRequest[9] = kpSpeed & 0xFF;
00279 MSMMUX_I2CRequest[10] = (kpSpeed >> 8) & 0xFF;
00280 MSMMUX_I2CRequest[11] = kiSpeed & 0xFF;
00281 MSMMUX_I2CRequest[12] = (kiSpeed >> 8) & 0xFF;
00282 MSMMUX_I2CRequest[13] = kdSpeed & 0xFF;
00283 MSMMUX_I2CRequest[14] = (kdSpeed >> 8) & 0xFF;
00284 MSMMUX_I2CRequest[15] = passCount;
00285 MSMMUX_I2CRequest[16] = tolerance;
00286
00287 return writeI2C(link, MSMMUX_I2CRequest);
00288 }
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299 bool MSMMotor(tMUXmotor muxmotor, byte power, ubyte address) {
00300 ubyte commandA = 0;
00301 commandA += (mmuxData[SPORT(muxmotor)].pidcontrol[MPORT(muxmotor)]) ? MSMMUX_CMD_SPEED : 0;
00302 commandA += (mmuxData[SPORT(muxmotor)].ramping[MPORT(muxmotor)] != MSMMUX_RAMP_NONE) ? MSMMUX_CMD_RAMP : 0;
00303 commandA += (mmuxData[SPORT(muxmotor)].brake[MPORT(muxmotor)]) ? MSMMUX_CMD_BRK : 0;
00304 commandA += (mmuxData[SPORT(muxmotor)].targetUnit[MPORT(muxmotor)] == MSMMUX_ROT_DEGREES) ? MSMMUX_CMD_TACHO : 0;
00305 commandA += (mmuxData[SPORT(muxmotor)].targetUnit[MPORT(muxmotor)] == MSMMUX_ROT_SECONDS) ? MSMMUX_CMD_TIME : 0;
00306 commandA += (mmuxData[SPORT(muxmotor)].relTarget[MPORT(muxmotor)]) ? MSMMUX_CMD_RELATIVE : 0;
00307 commandA += MSMMUX_CMD_GO;
00308
00309 switch (mmuxData[SPORT(muxmotor)].targetUnit[MPORT(muxmotor)]) {
00310 case MSMMUX_ROT_UNLIMITED: return MSMMUXsendCommand((tSensors)SPORT(muxmotor), (ubyte)MPORT(muxmotor), 0, power, 0, commandA, address);
00311 case MSMMUX_ROT_DEGREES: return MSMMUXsendCommand((tSensors)SPORT(muxmotor), (ubyte)MPORT(muxmotor), mmuxData[SPORT(muxmotor)].target[MPORT(muxmotor)], power, 0, commandA, address);
00312 case MSMMUX_ROT_SECONDS: return MSMMUXsendCommand((tSensors)SPORT(muxmotor), (ubyte)MPORT(muxmotor), 0, power, mmuxData[SPORT(muxmotor)].target[MPORT(muxmotor)], commandA, address);
00313 }
00314 return true;
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326 bool MSMotorStop(tMUXmotor muxmotor, ubyte address) {
00327 if (MPORT(muxmotor) == 0)
00328 return MSMotorStop(muxmotor, mmuxData[SPORT(muxmotor)].brake[MPORT(muxmotor)], address);
00329 else if (MPORT(muxmotor) == 1)
00330 return MSMotorStop(muxmotor, mmuxData[SPORT(muxmotor)].brake[MPORT(muxmotor)], address);
00331 return true;
00332 }
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 bool MSMotorStop(tMUXmotor muxmotor, bool brake, ubyte address) {
00344 if (MPORT(muxmotor) == 0)
00345 return MSMMUXsendCommand((tSensors)SPORT(muxmotor), brake ? MSMMUX_CMD_BRAKE_MOT1 : MSMMUX_CMD_FLOAT_MOT1, address);
00346 else if (MPORT(muxmotor) == 1)
00347 return MSMMUXsendCommand((tSensors)SPORT(muxmotor), brake ? MSMMUX_CMD_BRAKE_MOT2 : MSMMUX_CMD_FLOAT_MOT2, address);
00348 return true;
00349 }
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 void MSMMotorSetRotationTarget(tMUXmotor muxmotor, long target) {
00360 mmuxData[SPORT(muxmotor)].target[MPORT(muxmotor)] = target * 360;
00361 mmuxData[SPORT(muxmotor)].targetUnit[MPORT(muxmotor)] = MSMMUX_ROT_DEGREES;
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 void MSMMotorSetTimeTarget(tMUXmotor muxmotor, int target) {
00374 mmuxData[SPORT(muxmotor)].target[MPORT(muxmotor)] = target;
00375 mmuxData[SPORT(muxmotor)].targetUnit[MPORT(muxmotor)] = MSMMUX_ROT_SECONDS;
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 void MSMMotorSetEncoderTarget(tMUXmotor muxmotor, long target) {
00387 mmuxData[SPORT(muxmotor)].target[MPORT(muxmotor)] = target;
00388 mmuxData[SPORT(muxmotor)].targetUnit[MPORT(muxmotor)] = MSMMUX_ROT_DEGREES;
00389 mmuxData[SPORT(muxmotor)].relTarget[MPORT(muxmotor)] = true;
00390 }
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 void MSMMotorSetEncoderTarget(tMUXmotor muxmotor, long target, bool relative) {
00402 mmuxData[SPORT(muxmotor)].target[MPORT(muxmotor)] = target;
00403 mmuxData[SPORT(muxmotor)].targetUnit[MPORT(muxmotor)] = MSMMUX_ROT_DEGREES;
00404 mmuxData[SPORT(muxmotor)].relTarget[MPORT(muxmotor)] = relative;
00405 }
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 long MSMMotorEncoder(tMUXmotor muxmotor, ubyte address) {
00416 long result;
00417
00418 memset(MSMMUX_I2CRequest, 0, sizeof(tByteArray));
00419
00420 MSMMUX_I2CRequest[0] = 2;
00421 MSMMUX_I2CRequest[1] = address;
00422
00423 switch ((byte)MPORT(muxmotor)) {
00424 case 0: MSMMUX_I2CRequest[2] = MSMMUX_TACHO_MOT1; break;
00425 case 1: MSMMUX_I2CRequest[2] = MSMMUX_TACHO_MOT2; break;
00426 }
00427
00428 if (!writeI2C((tSensors)SPORT(muxmotor), MSMMUX_I2CRequest, MSMMUX_I2CReply, 4))
00429 return 0;
00430
00431 result = MSMMUX_I2CReply[0] + (MSMMUX_I2CReply[1]<<8) + (MSMMUX_I2CReply[2]<<16) + (MSMMUX_I2CReply[3]<<24);
00432
00433 return result;
00434 }
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448 bool MSMMotorEncoderReset(tMUXmotor muxmotor, ubyte address) {
00449 switch((byte)MPORT(muxmotor)) {
00450 case 0: return MSMMUXsendCommand((tSensors)SPORT(muxmotor), MSMMUX_CMD_RESET_MOT1, address); break;
00451 case 1: return MSMMUXsendCommand((tSensors)SPORT(muxmotor), MSMMUX_CMD_RESET_MOT2, address); break;
00452 }
00453 return false;
00454 }
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467 bool MSMMotorEncoderResetAll(tSensors link, ubyte address) {
00468 return MSMMUXsendCommand(link, MSMMUX_CMD_RESET_ALL, address);
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 bool MSMMotorBusy(tMUXmotor muxmotor, ubyte address) {
00480 ubyte status = 0;
00481 ubyte commandA = 0;
00482
00483
00484 memset(MSMMUX_I2CRequest, 0, sizeof(tByteArray));
00485
00486 MSMMUX_I2CRequest[0] = 2;
00487 MSMMUX_I2CRequest[1] = address;
00488 MSMMUX_I2CRequest[2] = MSMMUX_MOT_OFFSET + (MPORT(muxmotor) * MSMMUX_ENTRY_SIZE) + MSMMUX_CMD_A;
00489
00490 if (!writeI2C((tSensors)SPORT(muxmotor), MSMMUX_I2CRequest, MSMMUX_I2CReply, 1))
00491 return false;
00492
00493 commandA = MSMMUX_I2CReply[0];
00494
00495
00496 if (commandA == 0)
00497 return false;
00498
00499 if (!MSMMUXreadStatus(muxmotor, status, address))
00500 return false;
00501
00502 if ((commandA & MSMMUX_ROT_UNLIMITED) == MSMMUX_ROT_UNLIMITED)
00503 return ((status & MSMMUX_STAT_POWERED) != 0);
00504 else if ((commandA & MSMMUX_ROT_DEGREES) == MSMMUX_ROT_DEGREES)
00505 return ((status & MSMMUX_STAT_POS_CTRL) != 0);
00506 else if ((commandA & MSMMUX_ROT_SECONDS) == MSMMUX_ROT_SECONDS)
00507 return ((status & MSMMUX_STAT_TIMED) != 0);
00508
00509 return false;
00510 }
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520 bool MSMMotorStalled(tMUXmotor muxmotor, ubyte address) {
00521 ubyte status = 0;
00522 if (!MSMMUXreadStatus(muxmotor, status, address))
00523 return false;
00524
00525 return ((status & MSMMUX_STAT_STALLED) != 0);
00526 }
00527
00528
00529
00530
00531
00532
00533
00534 void MSMMotorSetBrake(tMUXmotor muxmotor) {
00535 mmuxData[SPORT(muxmotor)].brake[MPORT(muxmotor)] = true;
00536 }
00537
00538
00539
00540
00541
00542
00543
00544 void MSMMotorSetFloat(tMUXmotor muxmotor) {
00545 mmuxData[SPORT(muxmotor)].brake[MPORT(muxmotor)] = false;
00546 }
00547
00548
00549
00550
00551
00552
00553
00554
00555 void MSMMotorSetSpeedCtrl(tMUXmotor muxmotor, bool constspeed) {
00556 mmuxData[SPORT(muxmotor)].pidcontrol[MPORT(muxmotor)] = true;
00557 }
00558
00559
00560
00561
00562
00563
00564
00565
00566 void MSMMotorSetRamping(tMUXmotor muxmotor, bool ramping) {
00567 mmuxData[SPORT(muxmotor)].ramping[MPORT(muxmotor)] = (ramping) ? MSMMUX_RAMP_UP_DOWN : MSMMUX_RAMP_NONE;
00568 }
00569
00570 #endif // __MSMMUX_H__
00571
00572
00573
00574
00575
00576