Romain Ame
/
Timer71pt
Fork de Timer après le match à 61 points
Fork of Timer by
AX12/AX12.cpp@41:b5a2fbc20beb, 2016-04-13 (annotated)
- Committer:
- sype
- Date:
- Wed Apr 13 11:27:34 2016 +0000
- Revision:
- 41:b5a2fbc20beb
- Child:
- 50:2dea82393beb
Impl?mentation des dispositifs du robot
Who changed what in which revision?
User | Revision | Line number | New 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 | 41:b5a2fbc20beb | 297 | |
sype | 41:b5a2fbc20beb | 298 | int AX12::read(int ID, int start, int bytes, char* data) { |
sype | 41:b5a2fbc20beb | 299 | |
sype | 41:b5a2fbc20beb | 300 | char PacketLength = 0x4; |
sype | 41:b5a2fbc20beb | 301 | char TxBuf[16]; |
sype | 41:b5a2fbc20beb | 302 | char sum = 0; |
sype | 41:b5a2fbc20beb | 303 | char Status[16]; |
sype | 41:b5a2fbc20beb | 304 | |
sype | 41:b5a2fbc20beb | 305 | Status[4] = 0xFE; // return code |
sype | 41:b5a2fbc20beb | 306 | |
sype | 41:b5a2fbc20beb | 307 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 308 | logger.printf("\nread(%d,0x%x,%d,data)\n",ID,start,bytes); |
sype | 41:b5a2fbc20beb | 309 | #endif |
sype | 41:b5a2fbc20beb | 310 | |
sype | 41:b5a2fbc20beb | 311 | // Build the TxPacket first in RAM, then we'll send in one go |
sype | 41:b5a2fbc20beb | 312 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 313 | logger.printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n"); |
sype | 41:b5a2fbc20beb | 314 | #endif |
sype | 41:b5a2fbc20beb | 315 | |
sype | 41:b5a2fbc20beb | 316 | TxBuf[0] = 0xff; |
sype | 41:b5a2fbc20beb | 317 | TxBuf[1] = 0xff; |
sype | 41:b5a2fbc20beb | 318 | |
sype | 41:b5a2fbc20beb | 319 | // ID |
sype | 41:b5a2fbc20beb | 320 | TxBuf[2] = ID; |
sype | 41:b5a2fbc20beb | 321 | sum += TxBuf[2]; |
sype | 41:b5a2fbc20beb | 322 | |
sype | 41:b5a2fbc20beb | 323 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 324 | logger.printf(" ID : %d\n",TxBuf[2]); |
sype | 41:b5a2fbc20beb | 325 | #endif |
sype | 41:b5a2fbc20beb | 326 | |
sype | 41:b5a2fbc20beb | 327 | // Packet Length |
sype | 41:b5a2fbc20beb | 328 | TxBuf[3] = PacketLength; // Length = 4 ; 2 + 1 (start) = 1 (bytes) |
sype | 41:b5a2fbc20beb | 329 | sum += TxBuf[3]; // Accululate the packet sum |
sype | 41:b5a2fbc20beb | 330 | |
sype | 41:b5a2fbc20beb | 331 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 332 | logger.printf(" Length : 0x%x\n",TxBuf[3]); |
sype | 41:b5a2fbc20beb | 333 | #endif |
sype | 41:b5a2fbc20beb | 334 | |
sype | 41:b5a2fbc20beb | 335 | // Instruction - Read |
sype | 41:b5a2fbc20beb | 336 | TxBuf[4] = 0x2; |
sype | 41:b5a2fbc20beb | 337 | sum += TxBuf[4]; |
sype | 41:b5a2fbc20beb | 338 | |
sype | 41:b5a2fbc20beb | 339 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 340 | logger.printf(" Instruction : 0x%x\n",TxBuf[4]); |
sype | 41:b5a2fbc20beb | 341 | #endif |
sype | 41:b5a2fbc20beb | 342 | |
sype | 41:b5a2fbc20beb | 343 | // Start Address |
sype | 41:b5a2fbc20beb | 344 | TxBuf[5] = start; |
sype | 41:b5a2fbc20beb | 345 | sum += TxBuf[5]; |
sype | 41:b5a2fbc20beb | 346 | |
sype | 41:b5a2fbc20beb | 347 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 348 | logger.printf(" Start Address : 0x%x\n",TxBuf[5]); |
sype | 41:b5a2fbc20beb | 349 | #endif |
sype | 41:b5a2fbc20beb | 350 | |
sype | 41:b5a2fbc20beb | 351 | // Bytes to read |
sype | 41:b5a2fbc20beb | 352 | TxBuf[6] = bytes; |
sype | 41:b5a2fbc20beb | 353 | sum += TxBuf[6]; |
sype | 41:b5a2fbc20beb | 354 | |
sype | 41:b5a2fbc20beb | 355 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 356 | logger.printf(" No bytes : 0x%x\n",TxBuf[6]); |
sype | 41:b5a2fbc20beb | 357 | #endif |
sype | 41:b5a2fbc20beb | 358 | |
sype | 41:b5a2fbc20beb | 359 | // Checksum |
sype | 41:b5a2fbc20beb | 360 | TxBuf[7] = 0xFF - sum; |
sype | 41:b5a2fbc20beb | 361 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 362 | logger.printf(" Checksum : 0x%x\n",TxBuf[7]); |
sype | 41:b5a2fbc20beb | 363 | #endif |
sype | 41:b5a2fbc20beb | 364 | |
sype | 41:b5a2fbc20beb | 365 | // Transmit the packet in one burst with no pausing |
sype | 41:b5a2fbc20beb | 366 | for (int i = 0; i<8 ; i++) { |
sype | 41:b5a2fbc20beb | 367 | _ax12.putc(TxBuf[i]); |
sype | 41:b5a2fbc20beb | 368 | } |
sype | 41:b5a2fbc20beb | 369 | |
sype | 41:b5a2fbc20beb | 370 | // Wait for the bytes to be transmitted |
sype | 41:b5a2fbc20beb | 371 | wait (0.00002); |
sype | 41:b5a2fbc20beb | 372 | |
sype | 41:b5a2fbc20beb | 373 | // Skip if the read was to the broadcast address |
sype | 41:b5a2fbc20beb | 374 | if (_ID != 0xFE) { |
sype | 41:b5a2fbc20beb | 375 | |
sype | 41:b5a2fbc20beb | 376 | |
sype | 41:b5a2fbc20beb | 377 | |
sype | 41:b5a2fbc20beb | 378 | // response packet is always 6 + bytes |
sype | 41:b5a2fbc20beb | 379 | // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum |
sype | 41:b5a2fbc20beb | 380 | // timeout is a little more than the time to transmit |
sype | 41:b5a2fbc20beb | 381 | // the packet back, i.e. (6+bytes)*10 bit periods |
sype | 41:b5a2fbc20beb | 382 | |
sype | 41:b5a2fbc20beb | 383 | int timeout = 0; |
sype | 41:b5a2fbc20beb | 384 | int plen = 0; |
sype | 41:b5a2fbc20beb | 385 | while ((timeout < ((6+bytes)*10)) && (plen<(6+bytes))) { |
sype | 41:b5a2fbc20beb | 386 | |
sype | 41:b5a2fbc20beb | 387 | if (_ax12.readable()) { |
sype | 41:b5a2fbc20beb | 388 | Status[plen] = _ax12.getc(); |
sype | 41:b5a2fbc20beb | 389 | plen++; |
sype | 41:b5a2fbc20beb | 390 | timeout = 0; |
sype | 41:b5a2fbc20beb | 391 | } |
sype | 41:b5a2fbc20beb | 392 | |
sype | 41:b5a2fbc20beb | 393 | // wait for the bit period |
sype | 41:b5a2fbc20beb | 394 | wait (1.0/_baud); |
sype | 41:b5a2fbc20beb | 395 | timeout++; |
sype | 41:b5a2fbc20beb | 396 | } |
sype | 41:b5a2fbc20beb | 397 | |
sype | 41:b5a2fbc20beb | 398 | if (timeout == ((6+bytes)*10) ) { |
sype | 41:b5a2fbc20beb | 399 | return(-1); |
sype | 41:b5a2fbc20beb | 400 | } |
sype | 41:b5a2fbc20beb | 401 | |
sype | 41:b5a2fbc20beb | 402 | // Copy the data from Status into data for return |
sype | 41:b5a2fbc20beb | 403 | for (int i=0; i < Status[3]-2 ; i++) { |
sype | 41:b5a2fbc20beb | 404 | data[i] = Status[5+i]; |
sype | 41:b5a2fbc20beb | 405 | } |
sype | 41:b5a2fbc20beb | 406 | |
sype | 41:b5a2fbc20beb | 407 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 408 | logger.printf("\nStatus Packet\n"); |
sype | 41:b5a2fbc20beb | 409 | logger.printf(" Header : 0x%x\n",Status[0]); |
sype | 41:b5a2fbc20beb | 410 | logger.printf(" Header : 0x%x\n",Status[1]); |
sype | 41:b5a2fbc20beb | 411 | logger.printf(" ID : 0x%x\n",Status[2]); |
sype | 41:b5a2fbc20beb | 412 | logger.printf(" Length : 0x%x\n",Status[3]); |
sype | 41:b5a2fbc20beb | 413 | logger.printf(" Error Code : 0x%x\n",Status[4]); |
sype | 41:b5a2fbc20beb | 414 | |
sype | 41:b5a2fbc20beb | 415 | for (int i=0; i < Status[3]-2 ; i++) { |
sype | 41:b5a2fbc20beb | 416 | logger.printf(" Data : 0x%x\n",Status[5+i]); |
sype | 41:b5a2fbc20beb | 417 | } |
sype | 41:b5a2fbc20beb | 418 | |
sype | 41:b5a2fbc20beb | 419 | logger.printf(" Checksum : 0x%x\n",Status[5+(Status[3]-2)]); |
sype | 41:b5a2fbc20beb | 420 | #endif |
sype | 41:b5a2fbc20beb | 421 | |
sype | 41:b5a2fbc20beb | 422 | } // if (ID!=0xFE) |
sype | 41:b5a2fbc20beb | 423 | |
sype | 41:b5a2fbc20beb | 424 | return(Status[4]); |
sype | 41:b5a2fbc20beb | 425 | } |
sype | 41:b5a2fbc20beb | 426 | |
sype | 41:b5a2fbc20beb | 427 | |
sype | 41:b5a2fbc20beb | 428 | int AX12::write(int ID, int start, int bytes, char* data, int flag) { |
sype | 41:b5a2fbc20beb | 429 | // 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum |
sype | 41:b5a2fbc20beb | 430 | |
sype | 41:b5a2fbc20beb | 431 | char TxBuf[16]; |
sype | 41:b5a2fbc20beb | 432 | char sum = 0; |
sype | 41:b5a2fbc20beb | 433 | char Status[6]; |
sype | 41:b5a2fbc20beb | 434 | |
sype | 41:b5a2fbc20beb | 435 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 436 | logger.printf("\nwrite(%d,0x%x,%d,data,%d)\n",ID,start,bytes,flag); |
sype | 41:b5a2fbc20beb | 437 | #endif |
sype | 41:b5a2fbc20beb | 438 | |
sype | 41:b5a2fbc20beb | 439 | // Build the TxPacket first in RAM, then we'll send in one go |
sype | 41:b5a2fbc20beb | 440 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 441 | logger.printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n"); |
sype | 41:b5a2fbc20beb | 442 | #endif |
sype | 41:b5a2fbc20beb | 443 | |
sype | 41:b5a2fbc20beb | 444 | TxBuf[0] = 0xff; |
sype | 41:b5a2fbc20beb | 445 | TxBuf[1] = 0xff; |
sype | 41:b5a2fbc20beb | 446 | |
sype | 41:b5a2fbc20beb | 447 | // ID |
sype | 41:b5a2fbc20beb | 448 | TxBuf[2] = ID; |
sype | 41:b5a2fbc20beb | 449 | sum += TxBuf[2]; |
sype | 41:b5a2fbc20beb | 450 | |
sype | 41:b5a2fbc20beb | 451 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 452 | logger.printf(" ID : %d\n",TxBuf[2]); |
sype | 41:b5a2fbc20beb | 453 | #endif |
sype | 41:b5a2fbc20beb | 454 | |
sype | 41:b5a2fbc20beb | 455 | // packet Length |
sype | 41:b5a2fbc20beb | 456 | TxBuf[3] = 3+bytes; |
sype | 41:b5a2fbc20beb | 457 | sum += TxBuf[3]; |
sype | 41:b5a2fbc20beb | 458 | |
sype | 41:b5a2fbc20beb | 459 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 460 | logger.printf(" Length : %d\n",TxBuf[3]); |
sype | 41:b5a2fbc20beb | 461 | #endif |
sype | 41:b5a2fbc20beb | 462 | |
sype | 41:b5a2fbc20beb | 463 | // Instruction |
sype | 41:b5a2fbc20beb | 464 | if (flag == 1) { |
sype | 41:b5a2fbc20beb | 465 | TxBuf[4]=0x04; |
sype | 41:b5a2fbc20beb | 466 | sum += TxBuf[4]; |
sype | 41:b5a2fbc20beb | 467 | } else { |
sype | 41:b5a2fbc20beb | 468 | TxBuf[4]=0x03; |
sype | 41:b5a2fbc20beb | 469 | sum += TxBuf[4]; |
sype | 41:b5a2fbc20beb | 470 | } |
sype | 41:b5a2fbc20beb | 471 | |
sype | 41:b5a2fbc20beb | 472 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 473 | logger.printf(" Instruction : 0x%x\n",TxBuf[4]); |
sype | 41:b5a2fbc20beb | 474 | #endif |
sype | 41:b5a2fbc20beb | 475 | |
sype | 41:b5a2fbc20beb | 476 | // Start Address |
sype | 41:b5a2fbc20beb | 477 | TxBuf[5] = start; |
sype | 41:b5a2fbc20beb | 478 | sum += TxBuf[5]; |
sype | 41:b5a2fbc20beb | 479 | |
sype | 41:b5a2fbc20beb | 480 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 481 | logger.printf(" Start : 0x%x\n",TxBuf[5]); |
sype | 41:b5a2fbc20beb | 482 | #endif |
sype | 41:b5a2fbc20beb | 483 | |
sype | 41:b5a2fbc20beb | 484 | // data |
sype | 41:b5a2fbc20beb | 485 | for (char i=0; i<bytes ; i++) { |
sype | 41:b5a2fbc20beb | 486 | TxBuf[6+i] = data[i]; |
sype | 41:b5a2fbc20beb | 487 | sum += TxBuf[6+i]; |
sype | 41:b5a2fbc20beb | 488 | |
sype | 41:b5a2fbc20beb | 489 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 490 | logger.printf(" Data : 0x%x\n",TxBuf[6+i]); |
sype | 41:b5a2fbc20beb | 491 | #endif |
sype | 41:b5a2fbc20beb | 492 | |
sype | 41:b5a2fbc20beb | 493 | } |
sype | 41:b5a2fbc20beb | 494 | |
sype | 41:b5a2fbc20beb | 495 | // checksum |
sype | 41:b5a2fbc20beb | 496 | TxBuf[6+bytes] = 0xFF - sum; |
sype | 41:b5a2fbc20beb | 497 | |
sype | 41:b5a2fbc20beb | 498 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 499 | logger.printf(" Checksum : 0x%x\n",TxBuf[6+bytes]); |
sype | 41:b5a2fbc20beb | 500 | #endif |
sype | 41:b5a2fbc20beb | 501 | |
sype | 41:b5a2fbc20beb | 502 | // Transmit the packet in one burst with no pausing |
sype | 41:b5a2fbc20beb | 503 | for (int i = 0; i < (7 + bytes) ; i++) { |
sype | 41:b5a2fbc20beb | 504 | _ax12.putc(TxBuf[i]); |
sype | 41:b5a2fbc20beb | 505 | } |
sype | 41:b5a2fbc20beb | 506 | |
sype | 41:b5a2fbc20beb | 507 | // Wait for data to transmit |
sype | 41:b5a2fbc20beb | 508 | wait (0.00002); |
sype | 41:b5a2fbc20beb | 509 | |
sype | 41:b5a2fbc20beb | 510 | // make sure we have a valid return |
sype | 41:b5a2fbc20beb | 511 | Status[4]=0x00; |
sype | 41:b5a2fbc20beb | 512 | |
sype | 41:b5a2fbc20beb | 513 | // we'll only get a reply if it was not broadcast |
sype | 41:b5a2fbc20beb | 514 | if (_ID!=0xFE) { |
sype | 41:b5a2fbc20beb | 515 | |
sype | 41:b5a2fbc20beb | 516 | |
sype | 41:b5a2fbc20beb | 517 | // response packet is always 6 bytes |
sype | 41:b5a2fbc20beb | 518 | // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum |
sype | 41:b5a2fbc20beb | 519 | // timeout is a little more than the time to transmit |
sype | 41:b5a2fbc20beb | 520 | // the packet back, i.e. 60 bit periods, round up to 100 |
sype | 41:b5a2fbc20beb | 521 | int timeout = 0; |
sype | 41:b5a2fbc20beb | 522 | int plen = 0; |
sype | 41:b5a2fbc20beb | 523 | while ((timeout < 100) && (plen<6)) { |
sype | 41:b5a2fbc20beb | 524 | |
sype | 41:b5a2fbc20beb | 525 | if (_ax12.readable()) { |
sype | 41:b5a2fbc20beb | 526 | Status[plen] = _ax12.getc(); |
sype | 41:b5a2fbc20beb | 527 | plen++; |
sype | 41:b5a2fbc20beb | 528 | timeout = 0; |
sype | 41:b5a2fbc20beb | 529 | } |
sype | 41:b5a2fbc20beb | 530 | |
sype | 41:b5a2fbc20beb | 531 | // wait for the bit period |
sype | 41:b5a2fbc20beb | 532 | wait (1.0/_baud); |
sype | 41:b5a2fbc20beb | 533 | timeout++; |
sype | 41:b5a2fbc20beb | 534 | } |
sype | 41:b5a2fbc20beb | 535 | |
sype | 41:b5a2fbc20beb | 536 | |
sype | 41:b5a2fbc20beb | 537 | // Build the TxPacket first in RAM, then we'll send in one go |
sype | 41:b5a2fbc20beb | 538 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 539 | logger.printf("\nStatus Packet\n Header : 0x%X, 0x%X\n",Status[0],Status[1]); |
sype | 41:b5a2fbc20beb | 540 | logger.printf(" ID : %d\n",Status[2]); |
sype | 41:b5a2fbc20beb | 541 | logger.printf(" Length : %d\n",Status[3]); |
sype | 41:b5a2fbc20beb | 542 | logger.printf(" Error : 0x%x\n",Status[4]); |
sype | 41:b5a2fbc20beb | 543 | logger.printf(" Checksum : 0x%x\n",Status[5]); |
sype | 41:b5a2fbc20beb | 544 | #endif |
sype | 41:b5a2fbc20beb | 545 | |
sype | 41:b5a2fbc20beb | 546 | |
sype | 41:b5a2fbc20beb | 547 | } |
sype | 41:b5a2fbc20beb | 548 | |
sype | 41:b5a2fbc20beb | 549 | return(Status[4]); // return error code |
sype | 41:b5a2fbc20beb | 550 | } |