Robot's source code

Dependencies:   mbed

Committer:
Jagang
Date:
Mon Mar 16 21:46:12 2015 +0000
Revision:
39:09c04fd42c94
Child:
74:88be86f83d17
Update to AX12 from ProtocarteV1

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