ARES
/
Timer
Time is good
Fork of Robot2016_2-0 by
AX12/AX12.cpp@71:5590dbe8393a, 2016-05-05 (annotated)
- Committer:
- Jagang
- Date:
- Thu May 05 03:47:05 2016 +0000
- Revision:
- 71:5590dbe8393a
- Parent:
- 51:1056dd73a748
Uodate Controlleur, ajout des mouvement d et c, ajout des AX12
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 | |
Jagang | 71:5590dbe8393a | 101 | int AX12::setSpeed(int speed) |
Jagang | 71:5590dbe8393a | 102 | { |
Jagang | 71:5590dbe8393a | 103 | char data[2]; |
Jagang | 71:5590dbe8393a | 104 | |
Jagang | 71:5590dbe8393a | 105 | data[0] = speed & 0xFF; |
Jagang | 71:5590dbe8393a | 106 | data[1] = speed >> 8; |
Jagang | 71:5590dbe8393a | 107 | |
Jagang | 71:5590dbe8393a | 108 | // write the packet, return the error code |
Jagang | 71:5590dbe8393a | 109 | int rVal = write(_ID, 0x20, 2, data); |
Jagang | 71:5590dbe8393a | 110 | |
Jagang | 71:5590dbe8393a | 111 | return(rVal); |
Jagang | 71:5590dbe8393a | 112 | } |
Jagang | 71:5590dbe8393a | 113 | |
sype | 41:b5a2fbc20beb | 114 | |
sype | 41:b5a2fbc20beb | 115 | // set continuous rotation speed from -1 to 1 |
sype | 41:b5a2fbc20beb | 116 | int AX12::setCRSpeed(float speed) { |
sype | 41:b5a2fbc20beb | 117 | |
sype | 41:b5a2fbc20beb | 118 | // bit 10 = direction, 0 = CCW, 1=CW |
sype | 41:b5a2fbc20beb | 119 | // bits 9-0 = Speed |
sype | 41:b5a2fbc20beb | 120 | char data[2]; |
sype | 41:b5a2fbc20beb | 121 | |
sype | 41:b5a2fbc20beb | 122 | int goal = (0x3ff * abs(speed)); |
sype | 41:b5a2fbc20beb | 123 | |
sype | 41:b5a2fbc20beb | 124 | // set direction CW if we have a negative speed |
sype | 41:b5a2fbc20beb | 125 | if (speed < 0) { |
sype | 41:b5a2fbc20beb | 126 | goal |= (0x1 << 10); |
sype | 41:b5a2fbc20beb | 127 | } |
sype | 41:b5a2fbc20beb | 128 | |
sype | 41:b5a2fbc20beb | 129 | data[0] = goal & 0xff; // bottom 8 bits |
sype | 41:b5a2fbc20beb | 130 | data[1] = goal >> 8; // top 8 bits |
sype | 41:b5a2fbc20beb | 131 | |
sype | 41:b5a2fbc20beb | 132 | // write the packet, return the error code |
sype | 41:b5a2fbc20beb | 133 | int rVal = write(_ID, 0x20, 2, data); |
sype | 41:b5a2fbc20beb | 134 | |
sype | 41:b5a2fbc20beb | 135 | return(rVal); |
sype | 41:b5a2fbc20beb | 136 | } |
sype | 41:b5a2fbc20beb | 137 | |
sype | 41:b5a2fbc20beb | 138 | |
sype | 41:b5a2fbc20beb | 139 | int AX12::setCWLimit (int degrees) { |
sype | 41:b5a2fbc20beb | 140 | |
sype | 41:b5a2fbc20beb | 141 | char data[2]; |
sype | 41:b5a2fbc20beb | 142 | |
sype | 41:b5a2fbc20beb | 143 | // 1023 / 300 * degrees |
sype | 41:b5a2fbc20beb | 144 | short limit = (1023 * degrees) / 300; |
sype | 41:b5a2fbc20beb | 145 | |
sype | 41:b5a2fbc20beb | 146 | #ifdef AX12_DEBUG |
sype | 41:b5a2fbc20beb | 147 | logger.printf("setCWLimit to 0x%x\n",limit); |
sype | 41:b5a2fbc20beb | 148 | #endif |
sype | 41:b5a2fbc20beb | 149 | |
sype | 41:b5a2fbc20beb | 150 | data[0] = limit & 0xff; // bottom 8 bits |
sype | 41:b5a2fbc20beb | 151 | data[1] = limit >> 8; // top 8 bits |
sype | 41:b5a2fbc20beb | 152 | |
sype | 41:b5a2fbc20beb | 153 | // write the packet, return the error code |
sype | 41:b5a2fbc20beb | 154 | return (write(_ID, AX12_REG_CW_LIMIT, 2, data)); |
sype | 41:b5a2fbc20beb | 155 | |
sype | 41:b5a2fbc20beb | 156 | } |
sype | 41:b5a2fbc20beb | 157 | |
sype | 41:b5a2fbc20beb | 158 | int AX12::setCCWLimit (int degrees) { |
sype | 41:b5a2fbc20beb | 159 | |
sype | 41:b5a2fbc20beb | 160 | char data[2]; |
sype | 41:b5a2fbc20beb | 161 | |
sype | 41:b5a2fbc20beb | 162 | // 1023 / 300 * degrees |
sype | 41:b5a2fbc20beb | 163 | short limit = (1023 * degrees) / 300; |
sype | 41:b5a2fbc20beb | 164 | |
sype | 41:b5a2fbc20beb | 165 | #ifdef AX12_DEBUG |
sype | 41:b5a2fbc20beb | 166 | logger.printf("setCCWLimit to 0x%x\n",limit); |
sype | 41:b5a2fbc20beb | 167 | #endif |
sype | 41:b5a2fbc20beb | 168 | |
sype | 41:b5a2fbc20beb | 169 | data[0] = limit & 0xff; // bottom 8 bits |
sype | 41:b5a2fbc20beb | 170 | data[1] = limit >> 8; // top 8 bits |
sype | 41:b5a2fbc20beb | 171 | |
sype | 41:b5a2fbc20beb | 172 | // write the packet, return the error code |
sype | 41:b5a2fbc20beb | 173 | return (write(_ID, AX12_REG_CCW_LIMIT, 2, data)); |
sype | 41:b5a2fbc20beb | 174 | } |
sype | 41:b5a2fbc20beb | 175 | |
sype | 41:b5a2fbc20beb | 176 | |
sype | 41:b5a2fbc20beb | 177 | int AX12::setID (int CurrentID, int NewID) { |
sype | 41:b5a2fbc20beb | 178 | |
sype | 41:b5a2fbc20beb | 179 | char data[1]; |
sype | 41:b5a2fbc20beb | 180 | data[0] = NewID; |
sype | 41:b5a2fbc20beb | 181 | |
sype | 41:b5a2fbc20beb | 182 | #ifdef AX12_DEBUG |
sype | 41:b5a2fbc20beb | 183 | logger.printf("setting ID from 0x%x to 0x%x\n",CurrentID,NewID); |
sype | 41:b5a2fbc20beb | 184 | #endif |
sype | 41:b5a2fbc20beb | 185 | |
sype | 41:b5a2fbc20beb | 186 | return (write(CurrentID, AX12_REG_ID, 1, data)); |
sype | 41:b5a2fbc20beb | 187 | |
sype | 41:b5a2fbc20beb | 188 | } |
sype | 41:b5a2fbc20beb | 189 | |
sype | 41:b5a2fbc20beb | 190 | |
sype | 41:b5a2fbc20beb | 191 | int AX12::setBaud (int baud) { |
sype | 41:b5a2fbc20beb | 192 | |
sype | 41:b5a2fbc20beb | 193 | char data[1]; |
sype | 41:b5a2fbc20beb | 194 | data[0] = baud; |
sype | 41:b5a2fbc20beb | 195 | |
sype | 41:b5a2fbc20beb | 196 | #ifdef AX12_DEBUG |
sype | 41:b5a2fbc20beb | 197 | logger.printf("setting Baud rate to %d\n",baud); |
sype | 41:b5a2fbc20beb | 198 | #endif |
sype | 41:b5a2fbc20beb | 199 | |
sype | 41:b5a2fbc20beb | 200 | return (write(0xFE, AX12_REG_BAUD, 1, data)); |
sype | 41:b5a2fbc20beb | 201 | |
sype | 41:b5a2fbc20beb | 202 | } |
sype | 41:b5a2fbc20beb | 203 | |
sype | 41:b5a2fbc20beb | 204 | |
sype | 41:b5a2fbc20beb | 205 | |
sype | 41:b5a2fbc20beb | 206 | // return 1 is the servo is still in flight |
sype | 41:b5a2fbc20beb | 207 | int AX12::isMoving(void) { |
sype | 41:b5a2fbc20beb | 208 | |
sype | 41:b5a2fbc20beb | 209 | char data[1]; |
sype | 41:b5a2fbc20beb | 210 | read(_ID,AX12_REG_MOVING,1,data); |
sype | 41:b5a2fbc20beb | 211 | return(data[0]); |
sype | 41:b5a2fbc20beb | 212 | } |
sype | 41:b5a2fbc20beb | 213 | |
sype | 41:b5a2fbc20beb | 214 | |
sype | 41:b5a2fbc20beb | 215 | void AX12::trigger(void) { |
sype | 41:b5a2fbc20beb | 216 | |
sype | 41:b5a2fbc20beb | 217 | char TxBuf[16]; |
sype | 41:b5a2fbc20beb | 218 | char sum = 0; |
sype | 41:b5a2fbc20beb | 219 | |
sype | 41:b5a2fbc20beb | 220 | #ifdef AX12_TRIGGER_DEBUG |
sype | 41:b5a2fbc20beb | 221 | // Build the TxPacket first in RAM, then we'll send in one go |
sype | 41:b5a2fbc20beb | 222 | logger.printf("\nTriggered\n"); |
sype | 41:b5a2fbc20beb | 223 | logger.printf("\nTrigger Packet\n Header : 0xFF, 0xFF\n"); |
sype | 41:b5a2fbc20beb | 224 | #endif |
sype | 41:b5a2fbc20beb | 225 | |
sype | 41:b5a2fbc20beb | 226 | TxBuf[0] = 0xFF; |
sype | 41:b5a2fbc20beb | 227 | TxBuf[1] = 0xFF; |
sype | 41:b5a2fbc20beb | 228 | |
sype | 41:b5a2fbc20beb | 229 | // ID - Broadcast |
sype | 41:b5a2fbc20beb | 230 | TxBuf[2] = 0xFE; |
sype | 41:b5a2fbc20beb | 231 | sum += TxBuf[2]; |
sype | 41:b5a2fbc20beb | 232 | |
sype | 41:b5a2fbc20beb | 233 | #ifdef AX12_TRIGGER_DEBUG |
sype | 41:b5a2fbc20beb | 234 | logger.printf(" ID : %d\n",TxBuf[2]); |
sype | 41:b5a2fbc20beb | 235 | #endif |
sype | 41:b5a2fbc20beb | 236 | |
sype | 41:b5a2fbc20beb | 237 | // Length |
sype | 41:b5a2fbc20beb | 238 | TxBuf[3] = 0x02; |
sype | 41:b5a2fbc20beb | 239 | sum += TxBuf[3]; |
sype | 41:b5a2fbc20beb | 240 | |
sype | 41:b5a2fbc20beb | 241 | #ifdef AX12_TRIGGER_DEBUG |
sype | 41:b5a2fbc20beb | 242 | logger.printf(" Length %d\n",TxBuf[3]); |
sype | 41:b5a2fbc20beb | 243 | #endif |
sype | 41:b5a2fbc20beb | 244 | |
sype | 41:b5a2fbc20beb | 245 | // Instruction - ACTION |
sype | 41:b5a2fbc20beb | 246 | TxBuf[4] = 0x04; |
sype | 41:b5a2fbc20beb | 247 | sum += TxBuf[4]; |
sype | 41:b5a2fbc20beb | 248 | |
sype | 41:b5a2fbc20beb | 249 | #ifdef AX12_TRIGGER_DEBUG |
sype | 41:b5a2fbc20beb | 250 | logger.printf(" Instruction 0x%X\n",TxBuf[5]); |
sype | 41:b5a2fbc20beb | 251 | #endif |
sype | 41:b5a2fbc20beb | 252 | |
sype | 41:b5a2fbc20beb | 253 | // Checksum |
sype | 41:b5a2fbc20beb | 254 | TxBuf[5] = 0xFF - sum; |
sype | 41:b5a2fbc20beb | 255 | #ifdef AX12_TRIGGER_DEBUG |
sype | 41:b5a2fbc20beb | 256 | logger.printf(" Checksum 0x%X\n",TxBuf[5]); |
sype | 41:b5a2fbc20beb | 257 | #endif |
sype | 41:b5a2fbc20beb | 258 | |
sype | 41:b5a2fbc20beb | 259 | // Transmit the packet in one burst with no pausing |
sype | 41:b5a2fbc20beb | 260 | for (int i = 0; i < 6 ; i++) { |
sype | 41:b5a2fbc20beb | 261 | _ax12.putc(TxBuf[i]); |
sype | 41:b5a2fbc20beb | 262 | } |
sype | 41:b5a2fbc20beb | 263 | |
sype | 41:b5a2fbc20beb | 264 | // This is a broadcast packet, so there will be no reply |
sype | 41:b5a2fbc20beb | 265 | return; |
sype | 41:b5a2fbc20beb | 266 | } |
sype | 41:b5a2fbc20beb | 267 | |
sype | 41:b5a2fbc20beb | 268 | |
sype | 41:b5a2fbc20beb | 269 | float AX12::getPosition(void) { |
sype | 41:b5a2fbc20beb | 270 | |
sype | 41:b5a2fbc20beb | 271 | #ifdef AX12_DEBUG |
sype | 41:b5a2fbc20beb | 272 | logger.printf("\ngetPosition(%d)",_ID); |
sype | 41:b5a2fbc20beb | 273 | #endif |
sype | 41:b5a2fbc20beb | 274 | |
sype | 41:b5a2fbc20beb | 275 | char data[2]; |
sype | 41:b5a2fbc20beb | 276 | |
sype | 41:b5a2fbc20beb | 277 | int ErrorCode = read(_ID, AX12_REG_POSITION, 2, data); |
sype | 41:b5a2fbc20beb | 278 | short position = data[0] + (data[1] << 8); |
sype | 41:b5a2fbc20beb | 279 | float angle = (position * 300)/1024; |
sype | 41:b5a2fbc20beb | 280 | |
sype | 41:b5a2fbc20beb | 281 | return (angle); |
sype | 41:b5a2fbc20beb | 282 | } |
sype | 41:b5a2fbc20beb | 283 | |
sype | 41:b5a2fbc20beb | 284 | |
sype | 41:b5a2fbc20beb | 285 | float AX12::getTemp (void) { |
sype | 41:b5a2fbc20beb | 286 | |
sype | 41:b5a2fbc20beb | 287 | #ifdef AX12_DEBUG |
sype | 41:b5a2fbc20beb | 288 | logger.printf("\ngetTemp(%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_TEMP, 1, data); |
sype | 41:b5a2fbc20beb | 293 | float temp = data[0]; |
sype | 41:b5a2fbc20beb | 294 | return(temp); |
sype | 41:b5a2fbc20beb | 295 | } |
sype | 41:b5a2fbc20beb | 296 | |
sype | 41:b5a2fbc20beb | 297 | |
sype | 41:b5a2fbc20beb | 298 | float AX12::getVolts (void) { |
sype | 41:b5a2fbc20beb | 299 | |
sype | 41:b5a2fbc20beb | 300 | #ifdef AX12_DEBUG |
sype | 41:b5a2fbc20beb | 301 | logger.printf("\ngetVolts(%d)",_ID); |
sype | 41:b5a2fbc20beb | 302 | #endif |
sype | 41:b5a2fbc20beb | 303 | |
sype | 41:b5a2fbc20beb | 304 | char data[1]; |
sype | 41:b5a2fbc20beb | 305 | int ErrorCode = read(_ID, AX12_REG_VOLTS, 1, data); |
sype | 41:b5a2fbc20beb | 306 | float volts = data[0]/10.0; |
sype | 41:b5a2fbc20beb | 307 | return(volts); |
sype | 41:b5a2fbc20beb | 308 | } |
sype | 41:b5a2fbc20beb | 309 | |
sype | 50:2dea82393beb | 310 | float AX12::getLoad (void) { |
sype | 50:2dea82393beb | 311 | char data[2]; |
sype | 51:1056dd73a748 | 312 | int ErrorCode = read(_ID, AX12_REG_LOAD, 2, data); |
sype | 51:1056dd73a748 | 313 | short load1 = data[0] + (data[1] << 8); |
sype | 51:1056dd73a748 | 314 | float load = load1/1023; |
sype | 51:1056dd73a748 | 315 | return(load); //renvoie la charge en % |
sype | 50:2dea82393beb | 316 | } |
sype | 41:b5a2fbc20beb | 317 | |
sype | 41:b5a2fbc20beb | 318 | int AX12::read(int ID, int start, int bytes, char* data) { |
sype | 41:b5a2fbc20beb | 319 | |
sype | 41:b5a2fbc20beb | 320 | char PacketLength = 0x4; |
sype | 41:b5a2fbc20beb | 321 | char TxBuf[16]; |
sype | 41:b5a2fbc20beb | 322 | char sum = 0; |
sype | 41:b5a2fbc20beb | 323 | char Status[16]; |
sype | 41:b5a2fbc20beb | 324 | |
sype | 41:b5a2fbc20beb | 325 | Status[4] = 0xFE; // return code |
sype | 41:b5a2fbc20beb | 326 | |
sype | 41:b5a2fbc20beb | 327 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 328 | logger.printf("\nread(%d,0x%x,%d,data)\n",ID,start,bytes); |
sype | 41:b5a2fbc20beb | 329 | #endif |
sype | 41:b5a2fbc20beb | 330 | |
sype | 41:b5a2fbc20beb | 331 | // Build the TxPacket first in RAM, then we'll send in one go |
sype | 41:b5a2fbc20beb | 332 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 333 | logger.printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n"); |
sype | 41:b5a2fbc20beb | 334 | #endif |
sype | 41:b5a2fbc20beb | 335 | |
sype | 41:b5a2fbc20beb | 336 | TxBuf[0] = 0xff; |
sype | 41:b5a2fbc20beb | 337 | TxBuf[1] = 0xff; |
sype | 41:b5a2fbc20beb | 338 | |
sype | 41:b5a2fbc20beb | 339 | // ID |
sype | 41:b5a2fbc20beb | 340 | TxBuf[2] = ID; |
sype | 41:b5a2fbc20beb | 341 | sum += TxBuf[2]; |
sype | 41:b5a2fbc20beb | 342 | |
sype | 41:b5a2fbc20beb | 343 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 344 | logger.printf(" ID : %d\n",TxBuf[2]); |
sype | 41:b5a2fbc20beb | 345 | #endif |
sype | 41:b5a2fbc20beb | 346 | |
sype | 41:b5a2fbc20beb | 347 | // Packet Length |
sype | 41:b5a2fbc20beb | 348 | TxBuf[3] = PacketLength; // Length = 4 ; 2 + 1 (start) = 1 (bytes) |
sype | 41:b5a2fbc20beb | 349 | sum += TxBuf[3]; // Accululate the packet sum |
sype | 41:b5a2fbc20beb | 350 | |
sype | 41:b5a2fbc20beb | 351 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 352 | logger.printf(" Length : 0x%x\n",TxBuf[3]); |
sype | 41:b5a2fbc20beb | 353 | #endif |
sype | 41:b5a2fbc20beb | 354 | |
sype | 41:b5a2fbc20beb | 355 | // Instruction - Read |
sype | 41:b5a2fbc20beb | 356 | TxBuf[4] = 0x2; |
sype | 41:b5a2fbc20beb | 357 | sum += TxBuf[4]; |
sype | 41:b5a2fbc20beb | 358 | |
sype | 41:b5a2fbc20beb | 359 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 360 | logger.printf(" Instruction : 0x%x\n",TxBuf[4]); |
sype | 41:b5a2fbc20beb | 361 | #endif |
sype | 41:b5a2fbc20beb | 362 | |
sype | 41:b5a2fbc20beb | 363 | // Start Address |
sype | 41:b5a2fbc20beb | 364 | TxBuf[5] = start; |
sype | 41:b5a2fbc20beb | 365 | sum += TxBuf[5]; |
sype | 41:b5a2fbc20beb | 366 | |
sype | 41:b5a2fbc20beb | 367 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 368 | logger.printf(" Start Address : 0x%x\n",TxBuf[5]); |
sype | 41:b5a2fbc20beb | 369 | #endif |
sype | 41:b5a2fbc20beb | 370 | |
sype | 41:b5a2fbc20beb | 371 | // Bytes to read |
sype | 41:b5a2fbc20beb | 372 | TxBuf[6] = bytes; |
sype | 41:b5a2fbc20beb | 373 | sum += TxBuf[6]; |
sype | 41:b5a2fbc20beb | 374 | |
sype | 41:b5a2fbc20beb | 375 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 376 | logger.printf(" No bytes : 0x%x\n",TxBuf[6]); |
sype | 41:b5a2fbc20beb | 377 | #endif |
sype | 41:b5a2fbc20beb | 378 | |
sype | 41:b5a2fbc20beb | 379 | // Checksum |
sype | 41:b5a2fbc20beb | 380 | TxBuf[7] = 0xFF - sum; |
sype | 41:b5a2fbc20beb | 381 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 382 | logger.printf(" Checksum : 0x%x\n",TxBuf[7]); |
sype | 41:b5a2fbc20beb | 383 | #endif |
sype | 41:b5a2fbc20beb | 384 | |
sype | 41:b5a2fbc20beb | 385 | // Transmit the packet in one burst with no pausing |
sype | 41:b5a2fbc20beb | 386 | for (int i = 0; i<8 ; i++) { |
sype | 41:b5a2fbc20beb | 387 | _ax12.putc(TxBuf[i]); |
sype | 41:b5a2fbc20beb | 388 | } |
sype | 41:b5a2fbc20beb | 389 | |
sype | 41:b5a2fbc20beb | 390 | // Wait for the bytes to be transmitted |
sype | 41:b5a2fbc20beb | 391 | wait (0.00002); |
sype | 50:2dea82393beb | 392 | |
sype | 41:b5a2fbc20beb | 393 | // Skip if the read was to the broadcast address |
sype | 41:b5a2fbc20beb | 394 | if (_ID != 0xFE) { |
sype | 41:b5a2fbc20beb | 395 | |
sype | 41:b5a2fbc20beb | 396 | |
sype | 41:b5a2fbc20beb | 397 | |
sype | 41:b5a2fbc20beb | 398 | // response packet is always 6 + bytes |
sype | 41:b5a2fbc20beb | 399 | // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum |
sype | 41:b5a2fbc20beb | 400 | // timeout is a little more than the time to transmit |
sype | 41:b5a2fbc20beb | 401 | // the packet back, i.e. (6+bytes)*10 bit periods |
sype | 41:b5a2fbc20beb | 402 | |
sype | 41:b5a2fbc20beb | 403 | int timeout = 0; |
sype | 41:b5a2fbc20beb | 404 | int plen = 0; |
sype | 41:b5a2fbc20beb | 405 | while ((timeout < ((6+bytes)*10)) && (plen<(6+bytes))) { |
sype | 41:b5a2fbc20beb | 406 | |
sype | 41:b5a2fbc20beb | 407 | if (_ax12.readable()) { |
sype | 41:b5a2fbc20beb | 408 | Status[plen] = _ax12.getc(); |
sype | 41:b5a2fbc20beb | 409 | plen++; |
sype | 41:b5a2fbc20beb | 410 | timeout = 0; |
sype | 41:b5a2fbc20beb | 411 | } |
sype | 41:b5a2fbc20beb | 412 | |
sype | 41:b5a2fbc20beb | 413 | // wait for the bit period |
sype | 41:b5a2fbc20beb | 414 | wait (1.0/_baud); |
sype | 41:b5a2fbc20beb | 415 | timeout++; |
sype | 41:b5a2fbc20beb | 416 | } |
sype | 41:b5a2fbc20beb | 417 | |
sype | 41:b5a2fbc20beb | 418 | if (timeout == ((6+bytes)*10) ) { |
sype | 41:b5a2fbc20beb | 419 | return(-1); |
sype | 41:b5a2fbc20beb | 420 | } |
sype | 41:b5a2fbc20beb | 421 | |
sype | 41:b5a2fbc20beb | 422 | // Copy the data from Status into data for return |
sype | 41:b5a2fbc20beb | 423 | for (int i=0; i < Status[3]-2 ; i++) { |
sype | 41:b5a2fbc20beb | 424 | data[i] = Status[5+i]; |
sype | 41:b5a2fbc20beb | 425 | } |
sype | 41:b5a2fbc20beb | 426 | |
sype | 41:b5a2fbc20beb | 427 | #ifdef AX12_READ_DEBUG |
sype | 41:b5a2fbc20beb | 428 | logger.printf("\nStatus Packet\n"); |
sype | 41:b5a2fbc20beb | 429 | logger.printf(" Header : 0x%x\n",Status[0]); |
sype | 41:b5a2fbc20beb | 430 | logger.printf(" Header : 0x%x\n",Status[1]); |
sype | 41:b5a2fbc20beb | 431 | logger.printf(" ID : 0x%x\n",Status[2]); |
sype | 41:b5a2fbc20beb | 432 | logger.printf(" Length : 0x%x\n",Status[3]); |
sype | 41:b5a2fbc20beb | 433 | logger.printf(" Error Code : 0x%x\n",Status[4]); |
sype | 41:b5a2fbc20beb | 434 | |
sype | 41:b5a2fbc20beb | 435 | for (int i=0; i < Status[3]-2 ; i++) { |
sype | 41:b5a2fbc20beb | 436 | logger.printf(" Data : 0x%x\n",Status[5+i]); |
sype | 41:b5a2fbc20beb | 437 | } |
sype | 41:b5a2fbc20beb | 438 | |
sype | 41:b5a2fbc20beb | 439 | logger.printf(" Checksum : 0x%x\n",Status[5+(Status[3]-2)]); |
sype | 41:b5a2fbc20beb | 440 | #endif |
sype | 41:b5a2fbc20beb | 441 | |
sype | 41:b5a2fbc20beb | 442 | } // if (ID!=0xFE) |
sype | 41:b5a2fbc20beb | 443 | |
sype | 41:b5a2fbc20beb | 444 | return(Status[4]); |
sype | 41:b5a2fbc20beb | 445 | } |
sype | 41:b5a2fbc20beb | 446 | |
sype | 41:b5a2fbc20beb | 447 | |
sype | 41:b5a2fbc20beb | 448 | int AX12::write(int ID, int start, int bytes, char* data, int flag) { |
sype | 41:b5a2fbc20beb | 449 | // 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum |
sype | 41:b5a2fbc20beb | 450 | |
sype | 41:b5a2fbc20beb | 451 | char TxBuf[16]; |
sype | 41:b5a2fbc20beb | 452 | char sum = 0; |
sype | 41:b5a2fbc20beb | 453 | char Status[6]; |
sype | 41:b5a2fbc20beb | 454 | |
sype | 41:b5a2fbc20beb | 455 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 456 | logger.printf("\nwrite(%d,0x%x,%d,data,%d)\n",ID,start,bytes,flag); |
sype | 41:b5a2fbc20beb | 457 | #endif |
sype | 41:b5a2fbc20beb | 458 | |
sype | 41:b5a2fbc20beb | 459 | // Build the TxPacket first in RAM, then we'll send in one go |
sype | 41:b5a2fbc20beb | 460 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 461 | logger.printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n"); |
sype | 41:b5a2fbc20beb | 462 | #endif |
sype | 41:b5a2fbc20beb | 463 | |
sype | 41:b5a2fbc20beb | 464 | TxBuf[0] = 0xff; |
sype | 41:b5a2fbc20beb | 465 | TxBuf[1] = 0xff; |
sype | 41:b5a2fbc20beb | 466 | |
sype | 41:b5a2fbc20beb | 467 | // ID |
sype | 41:b5a2fbc20beb | 468 | TxBuf[2] = ID; |
sype | 41:b5a2fbc20beb | 469 | sum += TxBuf[2]; |
sype | 41:b5a2fbc20beb | 470 | |
sype | 41:b5a2fbc20beb | 471 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 472 | logger.printf(" ID : %d\n",TxBuf[2]); |
sype | 41:b5a2fbc20beb | 473 | #endif |
sype | 41:b5a2fbc20beb | 474 | |
sype | 41:b5a2fbc20beb | 475 | // packet Length |
sype | 41:b5a2fbc20beb | 476 | TxBuf[3] = 3+bytes; |
sype | 41:b5a2fbc20beb | 477 | sum += TxBuf[3]; |
sype | 41:b5a2fbc20beb | 478 | |
sype | 41:b5a2fbc20beb | 479 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 480 | logger.printf(" Length : %d\n",TxBuf[3]); |
sype | 41:b5a2fbc20beb | 481 | #endif |
sype | 41:b5a2fbc20beb | 482 | |
sype | 41:b5a2fbc20beb | 483 | // Instruction |
sype | 41:b5a2fbc20beb | 484 | if (flag == 1) { |
sype | 41:b5a2fbc20beb | 485 | TxBuf[4]=0x04; |
sype | 41:b5a2fbc20beb | 486 | sum += TxBuf[4]; |
sype | 41:b5a2fbc20beb | 487 | } else { |
sype | 41:b5a2fbc20beb | 488 | TxBuf[4]=0x03; |
sype | 41:b5a2fbc20beb | 489 | sum += TxBuf[4]; |
sype | 41:b5a2fbc20beb | 490 | } |
sype | 41:b5a2fbc20beb | 491 | |
sype | 41:b5a2fbc20beb | 492 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 493 | logger.printf(" Instruction : 0x%x\n",TxBuf[4]); |
sype | 41:b5a2fbc20beb | 494 | #endif |
sype | 41:b5a2fbc20beb | 495 | |
sype | 41:b5a2fbc20beb | 496 | // Start Address |
sype | 41:b5a2fbc20beb | 497 | TxBuf[5] = start; |
sype | 41:b5a2fbc20beb | 498 | sum += TxBuf[5]; |
sype | 41:b5a2fbc20beb | 499 | |
sype | 41:b5a2fbc20beb | 500 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 501 | logger.printf(" Start : 0x%x\n",TxBuf[5]); |
sype | 41:b5a2fbc20beb | 502 | #endif |
sype | 41:b5a2fbc20beb | 503 | |
sype | 41:b5a2fbc20beb | 504 | // data |
sype | 41:b5a2fbc20beb | 505 | for (char i=0; i<bytes ; i++) { |
sype | 41:b5a2fbc20beb | 506 | TxBuf[6+i] = data[i]; |
sype | 41:b5a2fbc20beb | 507 | sum += TxBuf[6+i]; |
sype | 41:b5a2fbc20beb | 508 | |
sype | 41:b5a2fbc20beb | 509 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 510 | logger.printf(" Data : 0x%x\n",TxBuf[6+i]); |
sype | 41:b5a2fbc20beb | 511 | #endif |
sype | 41:b5a2fbc20beb | 512 | |
sype | 41:b5a2fbc20beb | 513 | } |
sype | 41:b5a2fbc20beb | 514 | |
sype | 41:b5a2fbc20beb | 515 | // checksum |
sype | 41:b5a2fbc20beb | 516 | TxBuf[6+bytes] = 0xFF - sum; |
sype | 41:b5a2fbc20beb | 517 | |
sype | 41:b5a2fbc20beb | 518 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 519 | logger.printf(" Checksum : 0x%x\n",TxBuf[6+bytes]); |
sype | 41:b5a2fbc20beb | 520 | #endif |
sype | 41:b5a2fbc20beb | 521 | |
sype | 41:b5a2fbc20beb | 522 | // Transmit the packet in one burst with no pausing |
sype | 41:b5a2fbc20beb | 523 | for (int i = 0; i < (7 + bytes) ; i++) { |
sype | 41:b5a2fbc20beb | 524 | _ax12.putc(TxBuf[i]); |
sype | 41:b5a2fbc20beb | 525 | } |
sype | 41:b5a2fbc20beb | 526 | |
sype | 41:b5a2fbc20beb | 527 | // Wait for data to transmit |
sype | 41:b5a2fbc20beb | 528 | wait (0.00002); |
sype | 41:b5a2fbc20beb | 529 | |
sype | 41:b5a2fbc20beb | 530 | // make sure we have a valid return |
sype | 41:b5a2fbc20beb | 531 | Status[4]=0x00; |
sype | 41:b5a2fbc20beb | 532 | |
sype | 41:b5a2fbc20beb | 533 | // we'll only get a reply if it was not broadcast |
sype | 41:b5a2fbc20beb | 534 | if (_ID!=0xFE) { |
sype | 41:b5a2fbc20beb | 535 | |
sype | 41:b5a2fbc20beb | 536 | |
sype | 41:b5a2fbc20beb | 537 | // response packet is always 6 bytes |
sype | 41:b5a2fbc20beb | 538 | // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum |
sype | 41:b5a2fbc20beb | 539 | // timeout is a little more than the time to transmit |
sype | 41:b5a2fbc20beb | 540 | // the packet back, i.e. 60 bit periods, round up to 100 |
sype | 41:b5a2fbc20beb | 541 | int timeout = 0; |
sype | 41:b5a2fbc20beb | 542 | int plen = 0; |
sype | 41:b5a2fbc20beb | 543 | while ((timeout < 100) && (plen<6)) { |
sype | 41:b5a2fbc20beb | 544 | |
sype | 41:b5a2fbc20beb | 545 | if (_ax12.readable()) { |
sype | 41:b5a2fbc20beb | 546 | Status[plen] = _ax12.getc(); |
sype | 41:b5a2fbc20beb | 547 | plen++; |
sype | 41:b5a2fbc20beb | 548 | timeout = 0; |
sype | 41:b5a2fbc20beb | 549 | } |
sype | 41:b5a2fbc20beb | 550 | |
sype | 41:b5a2fbc20beb | 551 | // wait for the bit period |
sype | 41:b5a2fbc20beb | 552 | wait (1.0/_baud); |
sype | 41:b5a2fbc20beb | 553 | timeout++; |
sype | 41:b5a2fbc20beb | 554 | } |
sype | 41:b5a2fbc20beb | 555 | |
sype | 41:b5a2fbc20beb | 556 | |
sype | 41:b5a2fbc20beb | 557 | // Build the TxPacket first in RAM, then we'll send in one go |
sype | 41:b5a2fbc20beb | 558 | #ifdef AX12_WRITE_DEBUG |
sype | 41:b5a2fbc20beb | 559 | logger.printf("\nStatus Packet\n Header : 0x%X, 0x%X\n",Status[0],Status[1]); |
sype | 41:b5a2fbc20beb | 560 | logger.printf(" ID : %d\n",Status[2]); |
sype | 41:b5a2fbc20beb | 561 | logger.printf(" Length : %d\n",Status[3]); |
sype | 41:b5a2fbc20beb | 562 | logger.printf(" Error : 0x%x\n",Status[4]); |
sype | 41:b5a2fbc20beb | 563 | logger.printf(" Checksum : 0x%x\n",Status[5]); |
sype | 41:b5a2fbc20beb | 564 | #endif |
sype | 41:b5a2fbc20beb | 565 | |
sype | 41:b5a2fbc20beb | 566 | |
sype | 41:b5a2fbc20beb | 567 | } |
sype | 41:b5a2fbc20beb | 568 | |
sype | 41:b5a2fbc20beb | 569 | return(Status[4]); // return error code |
sype | 41:b5a2fbc20beb | 570 | } |