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