kk

Dependents:   2-FisherMan 2-FisherMan

Fork of AX12 by Chris Styles

Committer:
julientiron
Date:
Fri May 06 07:21:41 2016 +0000
Revision:
4:c1835649c36f
Parent:
3:ced71d1b2558
d

Who changed what in which revision?

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