Time is good

Dependencies:   RoboClaw mbed

Fork of Robot2016_2-0 by ARES

Committer:
sype
Date:
Wed May 04 16:15:13 2016 +0000
Revision:
51:1056dd73a748
Parent:
50:2dea82393beb
Child:
71:5590dbe8393a
Impl?mentation de l'homologation

Who changed what in which revision?

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