Corentin Potron / AX12

Dependencies:   SerialHalfDuplex

Dependents:   Programme_3Cubes_Trappes AX13 AX12_RobotDemineur Robot_demineur ... more

Committer:
cpotron
Date:
Sun Mar 25 20:38:22 2018 +0000
Revision:
0:6062259b2143
Child:
1:19cd7097b3e4
Initial Commit

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