Corentin Potron / AX12

Dependencies:   SerialHalfDuplex

Dependents:   Programme_3Cubes_Trappes AX13 AX12_RobotDemineur Robot_demineur ... more

Committer:
cpotron
Date:
Mon Mar 26 10:42:09 2018 +0000
Revision:
1:19cd7097b3e4
Parent:
0:6062259b2143
Child:
2:4b23ea069b7e
Add Speed control with servo register

Who changed what in which revision?

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