Robot's source code

Dependencies:   mbed

Committer:
Jagang
Date:
Mon Apr 13 16:53:19 2015 +0000
Revision:
74:88be86f83d17
Parent:
39:09c04fd42c94
Child:
109:53918ba98306
bla

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