Robot's source code

Dependencies:   mbed

Committer:
Jagang
Date:
Mon May 11 20:32:11 2015 +0000
Revision:
123:55e5e9acc541
Parent:
109:53918ba98306
Maj AI; Repositionnement du d?part (1000,177); Ajout de l'action sur les claps

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jagang 39:09c04fd42c94 1 /* mbed AX-12+ Servo Library
Jagang 39:09c04fd42c94 2 *
Jagang 39:09c04fd42c94 3 * Copyright (c) 2010, cstyles (http://mbed.org)
Jagang 39:09c04fd42c94 4 *
Jagang 39:09c04fd42c94 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
Jagang 39:09c04fd42c94 6 * of this software and associated documentation files (the "Software"), to deal
Jagang 39:09c04fd42c94 7 * in the Software without restriction, including without limitation the rights
Jagang 39:09c04fd42c94 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Jagang 39:09c04fd42c94 9 * copies of the Software, and to permit persons to whom the Software is
Jagang 39:09c04fd42c94 10 * furnished to do so, subject to the following conditions:
Jagang 39:09c04fd42c94 11 *
Jagang 39:09c04fd42c94 12 * The above copyright notice and this permission notice shall be included in
Jagang 39:09c04fd42c94 13 * all copies or substantial portions of the Software.
Jagang 39:09c04fd42c94 14 *
Jagang 39:09c04fd42c94 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Jagang 39:09c04fd42c94 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Jagang 39:09c04fd42c94 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Jagang 39:09c04fd42c94 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Jagang 39:09c04fd42c94 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Jagang 39:09c04fd42c94 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Jagang 39:09c04fd42c94 21 * THE SOFTWARE.
Jagang 39:09c04fd42c94 22 */
Jagang 39:09c04fd42c94 23
Jagang 39:09c04fd42c94 24 #include "AX12.h"
Jagang 39:09c04fd42c94 25 #include "mbed.h"
Jagang 39:09c04fd42c94 26
Jagang 39:09c04fd42c94 27 extern Serial logger;
Jagang 39:09c04fd42c94 28
Jagang 39:09c04fd42c94 29 AX12::AX12(PinName tx, PinName rx, int ID, int baud)
Jagang 39:09c04fd42c94 30 : _ax12(tx,rx) {
Jagang 39:09c04fd42c94 31 _baud = baud;
Jagang 39:09c04fd42c94 32 _ID = ID;
Jagang 39:09c04fd42c94 33 _ax12.baud(_baud);
Jagang 39:09c04fd42c94 34
Jagang 39:09c04fd42c94 35 }
Jagang 39:09c04fd42c94 36
Jagang 74:88be86f83d17 37 // set the mode of the servo
Jagang 39:09c04fd42c94 38 // 0 = Positional (0-300 degrees)
Jagang 39:09c04fd42c94 39 // 1 = Rotational -1 to 1 speed
Jagang 74:88be86f83d17 40 int AX12::setMode(int mode) {
Jagang 39:09c04fd42c94 41
Jagang 39:09c04fd42c94 42 if (mode == 1) { // set CR
Jagang 74:88be86f83d17 43 setCWLimit(0);
Jagang 74:88be86f83d17 44 setCCWLimit(0);
Jagang 74:88be86f83d17 45 setCRSpeed(0.0);
Jagang 39:09c04fd42c94 46 } else {
Jagang 74:88be86f83d17 47 setCWLimit(0);
Jagang 74:88be86f83d17 48 setCCWLimit(300);
Jagang 74:88be86f83d17 49 setCRSpeed(0.0);
Jagang 39:09c04fd42c94 50 }
Jagang 39:09c04fd42c94 51 return(0);
Jagang 39:09c04fd42c94 52 }
Jagang 39:09c04fd42c94 53
Jagang 39:09c04fd42c94 54
Jagang 39:09c04fd42c94 55 // if flag[0] is set, were blocking
Jagang 39:09c04fd42c94 56 // if flag[1] is set, we're registering
Jagang 39:09c04fd42c94 57 // they are mutually exclusive operations
Jagang 74:88be86f83d17 58 int AX12::setGoal(int degrees, int flags) {
Jagang 39:09c04fd42c94 59
Jagang 39:09c04fd42c94 60 char reg_flag = 0;
Jagang 39:09c04fd42c94 61 char data[2];
Jagang 109:53918ba98306 62 _goal = degrees;
Jagang 39:09c04fd42c94 63
Jagang 39:09c04fd42c94 64 // set the flag is only the register bit is set in the flag
Jagang 39:09c04fd42c94 65 if (flags == 0x2) {
Jagang 39:09c04fd42c94 66 reg_flag = 1;
Jagang 39:09c04fd42c94 67 }
Jagang 39:09c04fd42c94 68
Jagang 39:09c04fd42c94 69 // 1023 / 300 * degrees
Jagang 39:09c04fd42c94 70 short goal = (1023 * degrees) / 300;
Jagang 39:09c04fd42c94 71 #ifdef AX12_DEBUG
Jagang 74:88be86f83d17 72 logger.printf("setGoal to 0x%x\n",goal);
Jagang 39:09c04fd42c94 73 #endif
Jagang 39:09c04fd42c94 74
Jagang 39:09c04fd42c94 75 data[0] = goal & 0xff; // bottom 8 bits
Jagang 39:09c04fd42c94 76 data[1] = goal >> 8; // top 8 bits
Jagang 39:09c04fd42c94 77
Jagang 39:09c04fd42c94 78 // write the packet, return the error code
Jagang 39:09c04fd42c94 79 int rVal = write(_ID, AX12_REG_GOAL_POSITION, 2, data, reg_flag);
Jagang 39:09c04fd42c94 80
Jagang 39:09c04fd42c94 81 if (flags == 1) {
Jagang 39:09c04fd42c94 82 // block until it comes to a halt
Jagang 39:09c04fd42c94 83 while (isMoving()) {}
Jagang 39:09c04fd42c94 84 }
Jagang 39:09c04fd42c94 85 return(rVal);
Jagang 39:09c04fd42c94 86 }
Jagang 39:09c04fd42c94 87
Jagang 74:88be86f83d17 88 int AX12::setMaxTorque(int maxTorque)
Jagang 74:88be86f83d17 89 {
Jagang 74:88be86f83d17 90 char data[2];
Jagang 74:88be86f83d17 91
Jagang 74:88be86f83d17 92 data[0] = maxTorque & 0xFF;
Jagang 74:88be86f83d17 93 data[1] = maxTorque >> 8;
Jagang 74:88be86f83d17 94
Jagang 74:88be86f83d17 95 // write the packet, return the error code
Jagang 74:88be86f83d17 96 int rVal = write(_ID, 0x22, 2, data);
Jagang 39:09c04fd42c94 97
Jagang 74:88be86f83d17 98 return(rVal);
Jagang 74:88be86f83d17 99 }
Jagang 74:88be86f83d17 100
Jagang 74:88be86f83d17 101
Jagang 74:88be86f83d17 102 // set continuous rotation speed from -1 to 1
Jagang 74:88be86f83d17 103 int AX12::setCRSpeed(float speed) {
Jagang 39:09c04fd42c94 104
Jagang 39:09c04fd42c94 105 // bit 10 = direction, 0 = CCW, 1=CW
Jagang 39:09c04fd42c94 106 // bits 9-0 = Speed
Jagang 39:09c04fd42c94 107 char data[2];
Jagang 39:09c04fd42c94 108
Jagang 39:09c04fd42c94 109 int goal = (0x3ff * abs(speed));
Jagang 39:09c04fd42c94 110
Jagang 74:88be86f83d17 111 // set direction CW if we have a negative speed
Jagang 39:09c04fd42c94 112 if (speed < 0) {
Jagang 39:09c04fd42c94 113 goal |= (0x1 << 10);
Jagang 39:09c04fd42c94 114 }
Jagang 39:09c04fd42c94 115
Jagang 39:09c04fd42c94 116 data[0] = goal & 0xff; // bottom 8 bits
Jagang 39:09c04fd42c94 117 data[1] = goal >> 8; // top 8 bits
Jagang 39:09c04fd42c94 118
Jagang 39:09c04fd42c94 119 // write the packet, return the error code
Jagang 39:09c04fd42c94 120 int rVal = write(_ID, 0x20, 2, data);
Jagang 39:09c04fd42c94 121
Jagang 39:09c04fd42c94 122 return(rVal);
Jagang 39:09c04fd42c94 123 }
Jagang 39:09c04fd42c94 124
Jagang 39:09c04fd42c94 125
Jagang 74:88be86f83d17 126 int AX12::setCWLimit (int degrees) {
Jagang 39:09c04fd42c94 127
Jagang 39:09c04fd42c94 128 char data[2];
Jagang 39:09c04fd42c94 129
Jagang 39:09c04fd42c94 130 // 1023 / 300 * degrees
Jagang 39:09c04fd42c94 131 short limit = (1023 * degrees) / 300;
Jagang 39:09c04fd42c94 132
Jagang 39:09c04fd42c94 133 #ifdef AX12_DEBUG
Jagang 74:88be86f83d17 134 logger.printf("setCWLimit to 0x%x\n",limit);
Jagang 39:09c04fd42c94 135 #endif
Jagang 39:09c04fd42c94 136
Jagang 39:09c04fd42c94 137 data[0] = limit & 0xff; // bottom 8 bits
Jagang 39:09c04fd42c94 138 data[1] = limit >> 8; // top 8 bits
Jagang 39:09c04fd42c94 139
Jagang 39:09c04fd42c94 140 // write the packet, return the error code
Jagang 39:09c04fd42c94 141 return (write(_ID, AX12_REG_CW_LIMIT, 2, data));
Jagang 39:09c04fd42c94 142
Jagang 39:09c04fd42c94 143 }
Jagang 39:09c04fd42c94 144
Jagang 74:88be86f83d17 145 int AX12::setCCWLimit (int degrees) {
Jagang 39:09c04fd42c94 146
Jagang 39:09c04fd42c94 147 char data[2];
Jagang 39:09c04fd42c94 148
Jagang 39:09c04fd42c94 149 // 1023 / 300 * degrees
Jagang 39:09c04fd42c94 150 short limit = (1023 * degrees) / 300;
Jagang 39:09c04fd42c94 151
Jagang 39:09c04fd42c94 152 #ifdef AX12_DEBUG
Jagang 74:88be86f83d17 153 logger.printf("setCCWLimit to 0x%x\n",limit);
Jagang 39:09c04fd42c94 154 #endif
Jagang 39:09c04fd42c94 155
Jagang 39:09c04fd42c94 156 data[0] = limit & 0xff; // bottom 8 bits
Jagang 39:09c04fd42c94 157 data[1] = limit >> 8; // top 8 bits
Jagang 39:09c04fd42c94 158
Jagang 39:09c04fd42c94 159 // write the packet, return the error code
Jagang 39:09c04fd42c94 160 return (write(_ID, AX12_REG_CCW_LIMIT, 2, data));
Jagang 39:09c04fd42c94 161 }
Jagang 39:09c04fd42c94 162
Jagang 39:09c04fd42c94 163
Jagang 74:88be86f83d17 164 int AX12::setID (int CurrentID, int NewID) {
Jagang 39:09c04fd42c94 165
Jagang 39:09c04fd42c94 166 char data[1];
Jagang 39:09c04fd42c94 167 data[0] = NewID;
Jagang 39:09c04fd42c94 168
Jagang 39:09c04fd42c94 169 #ifdef AX12_DEBUG
Jagang 74:88be86f83d17 170 logger.printf("setting ID from 0x%x to 0x%x\n",CurrentID,NewID);
Jagang 39:09c04fd42c94 171 #endif
Jagang 39:09c04fd42c94 172
Jagang 39:09c04fd42c94 173 return (write(CurrentID, AX12_REG_ID, 1, data));
Jagang 39:09c04fd42c94 174
Jagang 39:09c04fd42c94 175 }
Jagang 39:09c04fd42c94 176
Jagang 39:09c04fd42c94 177
Jagang 74:88be86f83d17 178 int AX12::setBaud (int baud) {
Jagang 39:09c04fd42c94 179
Jagang 39:09c04fd42c94 180 char data[1];
Jagang 39:09c04fd42c94 181 data[0] = baud;
Jagang 39:09c04fd42c94 182
Jagang 39:09c04fd42c94 183 #ifdef AX12_DEBUG
Jagang 74:88be86f83d17 184 logger.printf("setting Baud rate to %d\n",baud);
Jagang 39:09c04fd42c94 185 #endif
Jagang 39:09c04fd42c94 186
Jagang 39:09c04fd42c94 187 return (write(0xFE, AX12_REG_BAUD, 1, data));
Jagang 39:09c04fd42c94 188
Jagang 39:09c04fd42c94 189 }
Jagang 39:09c04fd42c94 190
Jagang 39:09c04fd42c94 191
Jagang 39:09c04fd42c94 192
Jagang 39:09c04fd42c94 193 // return 1 is the servo is still in flight
Jagang 39:09c04fd42c94 194 int AX12::isMoving(void) {
Jagang 39:09c04fd42c94 195
Jagang 39:09c04fd42c94 196 char data[1];
Jagang 39:09c04fd42c94 197 read(_ID,AX12_REG_MOVING,1,data);
Jagang 39:09c04fd42c94 198 return(data[0]);
Jagang 39:09c04fd42c94 199 }
Jagang 39:09c04fd42c94 200
Jagang 39:09c04fd42c94 201
Jagang 39:09c04fd42c94 202 void AX12::trigger(void) {
Jagang 39:09c04fd42c94 203
Jagang 39:09c04fd42c94 204 char TxBuf[16];
Jagang 39:09c04fd42c94 205 char sum = 0;
Jagang 39:09c04fd42c94 206
Jagang 39:09c04fd42c94 207 #ifdef AX12_TRIGGER_DEBUG
Jagang 39:09c04fd42c94 208 // Build the TxPacket first in RAM, then we'll send in one go
Jagang 39:09c04fd42c94 209 logger.printf("\nTriggered\n");
Jagang 39:09c04fd42c94 210 logger.printf("\nTrigger Packet\n Header : 0xFF, 0xFF\n");
Jagang 39:09c04fd42c94 211 #endif
Jagang 39:09c04fd42c94 212
Jagang 39:09c04fd42c94 213 TxBuf[0] = 0xFF;
Jagang 39:09c04fd42c94 214 TxBuf[1] = 0xFF;
Jagang 39:09c04fd42c94 215
Jagang 39:09c04fd42c94 216 // ID - Broadcast
Jagang 39:09c04fd42c94 217 TxBuf[2] = 0xFE;
Jagang 39:09c04fd42c94 218 sum += TxBuf[2];
Jagang 39:09c04fd42c94 219
Jagang 39:09c04fd42c94 220 #ifdef AX12_TRIGGER_DEBUG
Jagang 39:09c04fd42c94 221 logger.printf(" ID : %d\n",TxBuf[2]);
Jagang 39:09c04fd42c94 222 #endif
Jagang 39:09c04fd42c94 223
Jagang 39:09c04fd42c94 224 // Length
Jagang 39:09c04fd42c94 225 TxBuf[3] = 0x02;
Jagang 39:09c04fd42c94 226 sum += TxBuf[3];
Jagang 39:09c04fd42c94 227
Jagang 39:09c04fd42c94 228 #ifdef AX12_TRIGGER_DEBUG
Jagang 39:09c04fd42c94 229 logger.printf(" Length %d\n",TxBuf[3]);
Jagang 39:09c04fd42c94 230 #endif
Jagang 39:09c04fd42c94 231
Jagang 39:09c04fd42c94 232 // Instruction - ACTION
Jagang 39:09c04fd42c94 233 TxBuf[4] = 0x04;
Jagang 39:09c04fd42c94 234 sum += TxBuf[4];
Jagang 39:09c04fd42c94 235
Jagang 39:09c04fd42c94 236 #ifdef AX12_TRIGGER_DEBUG
Jagang 39:09c04fd42c94 237 logger.printf(" Instruction 0x%X\n",TxBuf[5]);
Jagang 39:09c04fd42c94 238 #endif
Jagang 39:09c04fd42c94 239
Jagang 39:09c04fd42c94 240 // Checksum
Jagang 39:09c04fd42c94 241 TxBuf[5] = 0xFF - sum;
Jagang 39:09c04fd42c94 242 #ifdef AX12_TRIGGER_DEBUG
Jagang 39:09c04fd42c94 243 logger.printf(" Checksum 0x%X\n",TxBuf[5]);
Jagang 39:09c04fd42c94 244 #endif
Jagang 39:09c04fd42c94 245
Jagang 39:09c04fd42c94 246 // Transmit the packet in one burst with no pausing
Jagang 39:09c04fd42c94 247 for (int i = 0; i < 6 ; i++) {
Jagang 39:09c04fd42c94 248 _ax12.putc(TxBuf[i]);
Jagang 39:09c04fd42c94 249 }
Jagang 39:09c04fd42c94 250
Jagang 39:09c04fd42c94 251 // This is a broadcast packet, so there will be no reply
Jagang 39:09c04fd42c94 252 return;
Jagang 39:09c04fd42c94 253 }
Jagang 39:09c04fd42c94 254
Jagang 39:09c04fd42c94 255
Jagang 74:88be86f83d17 256 float AX12::getPosition(void) {
Jagang 39:09c04fd42c94 257
Jagang 39:09c04fd42c94 258 #ifdef AX12_DEBUG
Jagang 74:88be86f83d17 259 logger.printf("\ngetPosition(%d)",_ID);
Jagang 39:09c04fd42c94 260 #endif
Jagang 39:09c04fd42c94 261
Jagang 39:09c04fd42c94 262 char data[2];
Jagang 39:09c04fd42c94 263
Jagang 39:09c04fd42c94 264 int ErrorCode = read(_ID, AX12_REG_POSITION, 2, data);
Jagang 39:09c04fd42c94 265 short position = data[0] + (data[1] << 8);
Jagang 39:09c04fd42c94 266 float angle = (position * 300)/1024;
Jagang 39:09c04fd42c94 267
Jagang 39:09c04fd42c94 268 return (angle);
Jagang 39:09c04fd42c94 269 }
Jagang 39:09c04fd42c94 270
Jagang 39:09c04fd42c94 271
Jagang 74:88be86f83d17 272 float AX12::getTemp (void) {
Jagang 39:09c04fd42c94 273
Jagang 39:09c04fd42c94 274 #ifdef AX12_DEBUG
Jagang 74:88be86f83d17 275 logger.printf("\ngetTemp(%d)",_ID);
Jagang 39:09c04fd42c94 276 #endif
Jagang 39:09c04fd42c94 277
Jagang 39:09c04fd42c94 278 char data[1];
Jagang 39:09c04fd42c94 279 int ErrorCode = read(_ID, AX12_REG_TEMP, 1, data);
Jagang 39:09c04fd42c94 280 float temp = data[0];
Jagang 39:09c04fd42c94 281 return(temp);
Jagang 39:09c04fd42c94 282 }
Jagang 39:09c04fd42c94 283
Jagang 39:09c04fd42c94 284
Jagang 74:88be86f83d17 285 float AX12::getVolts (void) {
Jagang 39:09c04fd42c94 286
Jagang 39:09c04fd42c94 287 #ifdef AX12_DEBUG
Jagang 74:88be86f83d17 288 logger.printf("\ngetVolts(%d)",_ID);
Jagang 39:09c04fd42c94 289 #endif
Jagang 39:09c04fd42c94 290
Jagang 39:09c04fd42c94 291 char data[1];
Jagang 39:09c04fd42c94 292 int ErrorCode = read(_ID, AX12_REG_VOLTS, 1, data);
Jagang 39:09c04fd42c94 293 float volts = data[0]/10.0;
Jagang 39:09c04fd42c94 294 return(volts);
Jagang 39:09c04fd42c94 295 }
Jagang 39:09c04fd42c94 296
Jagang 39:09c04fd42c94 297
Jagang 39:09c04fd42c94 298 int AX12::read(int ID, int start, int bytes, char* data) {
Jagang 39:09c04fd42c94 299
Jagang 39:09c04fd42c94 300 char PacketLength = 0x4;
Jagang 39:09c04fd42c94 301 char TxBuf[16];
Jagang 39:09c04fd42c94 302 char sum = 0;
Jagang 39:09c04fd42c94 303 char Status[16];
Jagang 39:09c04fd42c94 304
Jagang 39:09c04fd42c94 305 Status[4] = 0xFE; // return code
Jagang 39:09c04fd42c94 306
Jagang 39:09c04fd42c94 307 #ifdef AX12_READ_DEBUG
Jagang 39:09c04fd42c94 308 logger.printf("\nread(%d,0x%x,%d,data)\n",ID,start,bytes);
Jagang 39:09c04fd42c94 309 #endif
Jagang 39:09c04fd42c94 310
Jagang 39:09c04fd42c94 311 // Build the TxPacket first in RAM, then we'll send in one go
Jagang 39:09c04fd42c94 312 #ifdef AX12_READ_DEBUG
Jagang 39:09c04fd42c94 313 logger.printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n");
Jagang 39:09c04fd42c94 314 #endif
Jagang 39:09c04fd42c94 315
Jagang 39:09c04fd42c94 316 TxBuf[0] = 0xff;
Jagang 39:09c04fd42c94 317 TxBuf[1] = 0xff;
Jagang 39:09c04fd42c94 318
Jagang 39:09c04fd42c94 319 // ID
Jagang 39:09c04fd42c94 320 TxBuf[2] = ID;
Jagang 39:09c04fd42c94 321 sum += TxBuf[2];
Jagang 39:09c04fd42c94 322
Jagang 39:09c04fd42c94 323 #ifdef AX12_READ_DEBUG
Jagang 39:09c04fd42c94 324 logger.printf(" ID : %d\n",TxBuf[2]);
Jagang 39:09c04fd42c94 325 #endif
Jagang 39:09c04fd42c94 326
Jagang 39:09c04fd42c94 327 // Packet Length
Jagang 39:09c04fd42c94 328 TxBuf[3] = PacketLength; // Length = 4 ; 2 + 1 (start) = 1 (bytes)
Jagang 39:09c04fd42c94 329 sum += TxBuf[3]; // Accululate the packet sum
Jagang 39:09c04fd42c94 330
Jagang 39:09c04fd42c94 331 #ifdef AX12_READ_DEBUG
Jagang 39:09c04fd42c94 332 logger.printf(" Length : 0x%x\n",TxBuf[3]);
Jagang 39:09c04fd42c94 333 #endif
Jagang 39:09c04fd42c94 334
Jagang 39:09c04fd42c94 335 // Instruction - Read
Jagang 39:09c04fd42c94 336 TxBuf[4] = 0x2;
Jagang 39:09c04fd42c94 337 sum += TxBuf[4];
Jagang 39:09c04fd42c94 338
Jagang 39:09c04fd42c94 339 #ifdef AX12_READ_DEBUG
Jagang 39:09c04fd42c94 340 logger.printf(" Instruction : 0x%x\n",TxBuf[4]);
Jagang 39:09c04fd42c94 341 #endif
Jagang 39:09c04fd42c94 342
Jagang 39:09c04fd42c94 343 // Start Address
Jagang 39:09c04fd42c94 344 TxBuf[5] = start;
Jagang 39:09c04fd42c94 345 sum += TxBuf[5];
Jagang 39:09c04fd42c94 346
Jagang 39:09c04fd42c94 347 #ifdef AX12_READ_DEBUG
Jagang 39:09c04fd42c94 348 logger.printf(" Start Address : 0x%x\n",TxBuf[5]);
Jagang 39:09c04fd42c94 349 #endif
Jagang 39:09c04fd42c94 350
Jagang 39:09c04fd42c94 351 // Bytes to read
Jagang 39:09c04fd42c94 352 TxBuf[6] = bytes;
Jagang 39:09c04fd42c94 353 sum += TxBuf[6];
Jagang 39:09c04fd42c94 354
Jagang 39:09c04fd42c94 355 #ifdef AX12_READ_DEBUG
Jagang 39:09c04fd42c94 356 logger.printf(" No bytes : 0x%x\n",TxBuf[6]);
Jagang 39:09c04fd42c94 357 #endif
Jagang 39:09c04fd42c94 358
Jagang 39:09c04fd42c94 359 // Checksum
Jagang 39:09c04fd42c94 360 TxBuf[7] = 0xFF - sum;
Jagang 39:09c04fd42c94 361 #ifdef AX12_READ_DEBUG
Jagang 39:09c04fd42c94 362 logger.printf(" Checksum : 0x%x\n",TxBuf[7]);
Jagang 39:09c04fd42c94 363 #endif
Jagang 39:09c04fd42c94 364
Jagang 39:09c04fd42c94 365 // Transmit the packet in one burst with no pausing
Jagang 39:09c04fd42c94 366 for (int i = 0; i<8 ; i++) {
Jagang 39:09c04fd42c94 367 _ax12.putc(TxBuf[i]);
Jagang 39:09c04fd42c94 368 }
Jagang 39:09c04fd42c94 369
Jagang 39:09c04fd42c94 370 // Wait for the bytes to be transmitted
Jagang 39:09c04fd42c94 371 wait (0.00002);
Jagang 39:09c04fd42c94 372
Jagang 39:09c04fd42c94 373 // Skip if the read was to the broadcast address
Jagang 39:09c04fd42c94 374 if (_ID != 0xFE) {
Jagang 39:09c04fd42c94 375
Jagang 39:09c04fd42c94 376
Jagang 39:09c04fd42c94 377
Jagang 39:09c04fd42c94 378 // response packet is always 6 + bytes
Jagang 39:09c04fd42c94 379 // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum
Jagang 39:09c04fd42c94 380 // timeout is a little more than the time to transmit
Jagang 39:09c04fd42c94 381 // the packet back, i.e. (6+bytes)*10 bit periods
Jagang 39:09c04fd42c94 382
Jagang 39:09c04fd42c94 383 int timeout = 0;
Jagang 39:09c04fd42c94 384 int plen = 0;
Jagang 39:09c04fd42c94 385 while ((timeout < ((6+bytes)*10)) && (plen<(6+bytes))) {
Jagang 39:09c04fd42c94 386
Jagang 39:09c04fd42c94 387 if (_ax12.readable()) {
Jagang 39:09c04fd42c94 388 Status[plen] = _ax12.getc();
Jagang 39:09c04fd42c94 389 plen++;
Jagang 39:09c04fd42c94 390 timeout = 0;
Jagang 39:09c04fd42c94 391 }
Jagang 39:09c04fd42c94 392
Jagang 39:09c04fd42c94 393 // wait for the bit period
Jagang 39:09c04fd42c94 394 wait (1.0/_baud);
Jagang 39:09c04fd42c94 395 timeout++;
Jagang 39:09c04fd42c94 396 }
Jagang 39:09c04fd42c94 397
Jagang 39:09c04fd42c94 398 if (timeout == ((6+bytes)*10) ) {
Jagang 39:09c04fd42c94 399 return(-1);
Jagang 39:09c04fd42c94 400 }
Jagang 39:09c04fd42c94 401
Jagang 39:09c04fd42c94 402 // Copy the data from Status into data for return
Jagang 39:09c04fd42c94 403 for (int i=0; i < Status[3]-2 ; i++) {
Jagang 39:09c04fd42c94 404 data[i] = Status[5+i];
Jagang 39:09c04fd42c94 405 }
Jagang 39:09c04fd42c94 406
Jagang 39:09c04fd42c94 407 #ifdef AX12_READ_DEBUG
Jagang 39:09c04fd42c94 408 logger.printf("\nStatus Packet\n");
Jagang 39:09c04fd42c94 409 logger.printf(" Header : 0x%x\n",Status[0]);
Jagang 39:09c04fd42c94 410 logger.printf(" Header : 0x%x\n",Status[1]);
Jagang 39:09c04fd42c94 411 logger.printf(" ID : 0x%x\n",Status[2]);
Jagang 39:09c04fd42c94 412 logger.printf(" Length : 0x%x\n",Status[3]);
Jagang 39:09c04fd42c94 413 logger.printf(" Error Code : 0x%x\n",Status[4]);
Jagang 39:09c04fd42c94 414
Jagang 39:09c04fd42c94 415 for (int i=0; i < Status[3]-2 ; i++) {
Jagang 39:09c04fd42c94 416 logger.printf(" Data : 0x%x\n",Status[5+i]);
Jagang 39:09c04fd42c94 417 }
Jagang 39:09c04fd42c94 418
Jagang 39:09c04fd42c94 419 logger.printf(" Checksum : 0x%x\n",Status[5+(Status[3]-2)]);
Jagang 39:09c04fd42c94 420 #endif
Jagang 39:09c04fd42c94 421
Jagang 39:09c04fd42c94 422 } // if (ID!=0xFE)
Jagang 39:09c04fd42c94 423
Jagang 39:09c04fd42c94 424 return(Status[4]);
Jagang 39:09c04fd42c94 425 }
Jagang 39:09c04fd42c94 426
Jagang 39:09c04fd42c94 427
Jagang 39:09c04fd42c94 428 int AX12::write(int ID, int start, int bytes, char* data, int flag) {
Jagang 39:09c04fd42c94 429 // 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum
Jagang 39:09c04fd42c94 430
Jagang 39:09c04fd42c94 431 char TxBuf[16];
Jagang 39:09c04fd42c94 432 char sum = 0;
Jagang 39:09c04fd42c94 433 char Status[6];
Jagang 39:09c04fd42c94 434
Jagang 39:09c04fd42c94 435 #ifdef AX12_WRITE_DEBUG
Jagang 39:09c04fd42c94 436 logger.printf("\nwrite(%d,0x%x,%d,data,%d)\n",ID,start,bytes,flag);
Jagang 39:09c04fd42c94 437 #endif
Jagang 39:09c04fd42c94 438
Jagang 39:09c04fd42c94 439 // Build the TxPacket first in RAM, then we'll send in one go
Jagang 39:09c04fd42c94 440 #ifdef AX12_WRITE_DEBUG
Jagang 39:09c04fd42c94 441 logger.printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n");
Jagang 39:09c04fd42c94 442 #endif
Jagang 39:09c04fd42c94 443
Jagang 39:09c04fd42c94 444 TxBuf[0] = 0xff;
Jagang 39:09c04fd42c94 445 TxBuf[1] = 0xff;
Jagang 39:09c04fd42c94 446
Jagang 39:09c04fd42c94 447 // ID
Jagang 39:09c04fd42c94 448 TxBuf[2] = ID;
Jagang 39:09c04fd42c94 449 sum += TxBuf[2];
Jagang 39:09c04fd42c94 450
Jagang 39:09c04fd42c94 451 #ifdef AX12_WRITE_DEBUG
Jagang 39:09c04fd42c94 452 logger.printf(" ID : %d\n",TxBuf[2]);
Jagang 39:09c04fd42c94 453 #endif
Jagang 39:09c04fd42c94 454
Jagang 39:09c04fd42c94 455 // packet Length
Jagang 39:09c04fd42c94 456 TxBuf[3] = 3+bytes;
Jagang 39:09c04fd42c94 457 sum += TxBuf[3];
Jagang 39:09c04fd42c94 458
Jagang 39:09c04fd42c94 459 #ifdef AX12_WRITE_DEBUG
Jagang 39:09c04fd42c94 460 logger.printf(" Length : %d\n",TxBuf[3]);
Jagang 39:09c04fd42c94 461 #endif
Jagang 39:09c04fd42c94 462
Jagang 39:09c04fd42c94 463 // Instruction
Jagang 39:09c04fd42c94 464 if (flag == 1) {
Jagang 39:09c04fd42c94 465 TxBuf[4]=0x04;
Jagang 39:09c04fd42c94 466 sum += TxBuf[4];
Jagang 39:09c04fd42c94 467 } else {
Jagang 39:09c04fd42c94 468 TxBuf[4]=0x03;
Jagang 39:09c04fd42c94 469 sum += TxBuf[4];
Jagang 39:09c04fd42c94 470 }
Jagang 39:09c04fd42c94 471
Jagang 39:09c04fd42c94 472 #ifdef AX12_WRITE_DEBUG
Jagang 39:09c04fd42c94 473 logger.printf(" Instruction : 0x%x\n",TxBuf[4]);
Jagang 39:09c04fd42c94 474 #endif
Jagang 39:09c04fd42c94 475
Jagang 39:09c04fd42c94 476 // Start Address
Jagang 39:09c04fd42c94 477 TxBuf[5] = start;
Jagang 39:09c04fd42c94 478 sum += TxBuf[5];
Jagang 39:09c04fd42c94 479
Jagang 39:09c04fd42c94 480 #ifdef AX12_WRITE_DEBUG
Jagang 39:09c04fd42c94 481 logger.printf(" Start : 0x%x\n",TxBuf[5]);
Jagang 39:09c04fd42c94 482 #endif
Jagang 39:09c04fd42c94 483
Jagang 39:09c04fd42c94 484 // data
Jagang 39:09c04fd42c94 485 for (char i=0; i<bytes ; i++) {
Jagang 39:09c04fd42c94 486 TxBuf[6+i] = data[i];
Jagang 39:09c04fd42c94 487 sum += TxBuf[6+i];
Jagang 39:09c04fd42c94 488
Jagang 39:09c04fd42c94 489 #ifdef AX12_WRITE_DEBUG
Jagang 39:09c04fd42c94 490 logger.printf(" Data : 0x%x\n",TxBuf[6+i]);
Jagang 39:09c04fd42c94 491 #endif
Jagang 39:09c04fd42c94 492
Jagang 39:09c04fd42c94 493 }
Jagang 39:09c04fd42c94 494
Jagang 39:09c04fd42c94 495 // checksum
Jagang 39:09c04fd42c94 496 TxBuf[6+bytes] = 0xFF - sum;
Jagang 39:09c04fd42c94 497
Jagang 39:09c04fd42c94 498 #ifdef AX12_WRITE_DEBUG
Jagang 39:09c04fd42c94 499 logger.printf(" Checksum : 0x%x\n",TxBuf[6+bytes]);
Jagang 39:09c04fd42c94 500 #endif
Jagang 39:09c04fd42c94 501
Jagang 39:09c04fd42c94 502 // Transmit the packet in one burst with no pausing
Jagang 39:09c04fd42c94 503 for (int i = 0; i < (7 + bytes) ; i++) {
Jagang 39:09c04fd42c94 504 _ax12.putc(TxBuf[i]);
Jagang 39:09c04fd42c94 505 }
Jagang 39:09c04fd42c94 506
Jagang 39:09c04fd42c94 507 // Wait for data to transmit
Jagang 39:09c04fd42c94 508 wait (0.00002);
Jagang 39:09c04fd42c94 509
Jagang 39:09c04fd42c94 510 // make sure we have a valid return
Jagang 39:09c04fd42c94 511 Status[4]=0x00;
Jagang 39:09c04fd42c94 512
Jagang 39:09c04fd42c94 513 // we'll only get a reply if it was not broadcast
Jagang 39:09c04fd42c94 514 if (_ID!=0xFE) {
Jagang 39:09c04fd42c94 515
Jagang 39:09c04fd42c94 516
Jagang 39:09c04fd42c94 517 // response packet is always 6 bytes
Jagang 39:09c04fd42c94 518 // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum
Jagang 39:09c04fd42c94 519 // timeout is a little more than the time to transmit
Jagang 39:09c04fd42c94 520 // the packet back, i.e. 60 bit periods, round up to 100
Jagang 39:09c04fd42c94 521 int timeout = 0;
Jagang 39:09c04fd42c94 522 int plen = 0;
Jagang 39:09c04fd42c94 523 while ((timeout < 100) && (plen<6)) {
Jagang 39:09c04fd42c94 524
Jagang 39:09c04fd42c94 525 if (_ax12.readable()) {
Jagang 39:09c04fd42c94 526 Status[plen] = _ax12.getc();
Jagang 39:09c04fd42c94 527 plen++;
Jagang 39:09c04fd42c94 528 timeout = 0;
Jagang 39:09c04fd42c94 529 }
Jagang 39:09c04fd42c94 530
Jagang 39:09c04fd42c94 531 // wait for the bit period
Jagang 39:09c04fd42c94 532 wait (1.0/_baud);
Jagang 39:09c04fd42c94 533 timeout++;
Jagang 39:09c04fd42c94 534 }
Jagang 39:09c04fd42c94 535
Jagang 39:09c04fd42c94 536
Jagang 39:09c04fd42c94 537 // Build the TxPacket first in RAM, then we'll send in one go
Jagang 39:09c04fd42c94 538 #ifdef AX12_WRITE_DEBUG
Jagang 39:09c04fd42c94 539 logger.printf("\nStatus Packet\n Header : 0x%X, 0x%X\n",Status[0],Status[1]);
Jagang 39:09c04fd42c94 540 logger.printf(" ID : %d\n",Status[2]);
Jagang 39:09c04fd42c94 541 logger.printf(" Length : %d\n",Status[3]);
Jagang 39:09c04fd42c94 542 logger.printf(" Error : 0x%x\n",Status[4]);
Jagang 39:09c04fd42c94 543 logger.printf(" Checksum : 0x%x\n",Status[5]);
Jagang 39:09c04fd42c94 544 #endif
Jagang 39:09c04fd42c94 545
Jagang 39:09c04fd42c94 546
Jagang 39:09c04fd42c94 547 }
Jagang 39:09c04fd42c94 548
Jagang 39:09c04fd42c94 549 return(Status[4]); // return error code
Jagang 39:09c04fd42c94 550 }