new ax12 lib

Dependents:   2014-ax12-test

Fork of AX12 by Chris Styles

Committer:
ppr2013G2
Date:
Thu Jun 26 15:26:50 2014 +0000
Revision:
6:99c6ddabe9e6
Parent:
4:8350c0155571
fix podcast id

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