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