Ngetes

Dependencies:   mbed

Committer:
hisyamfs
Date:
Fri Aug 31 10:45:30 2018 +0000
Revision:
1:2759fc78aa04
Parent:
0:23b97841e8dc
Child:
2:f07401343541
Bisa;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hisyamfs 0:23b97841e8dc 1 /* mbed AX-12+ Servo Library
hisyamfs 0:23b97841e8dc 2 *
hisyamfs 0:23b97841e8dc 3 * Copyright (c) 2010, cstyles (http://mbed.org)
hisyamfs 0:23b97841e8dc 4 *
hisyamfs 0:23b97841e8dc 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
hisyamfs 0:23b97841e8dc 6 * of this software and associated documentation files (the "Software"), to deal
hisyamfs 0:23b97841e8dc 7 * in the Software without restriction, including without limitation the rights
hisyamfs 0:23b97841e8dc 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hisyamfs 0:23b97841e8dc 9 * copies of the Software, and to permit persons to whom the Software is
hisyamfs 0:23b97841e8dc 10 * furnished to do so, subject to the following conditions:
hisyamfs 0:23b97841e8dc 11 *
hisyamfs 0:23b97841e8dc 12 * The above copyright notice and this permission notice shall be included in
hisyamfs 0:23b97841e8dc 13 * all copies or substantial portions of the Software.
hisyamfs 0:23b97841e8dc 14 *
hisyamfs 0:23b97841e8dc 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hisyamfs 0:23b97841e8dc 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hisyamfs 0:23b97841e8dc 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hisyamfs 0:23b97841e8dc 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hisyamfs 0:23b97841e8dc 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hisyamfs 0:23b97841e8dc 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hisyamfs 0:23b97841e8dc 21 * THE SOFTWARE.
hisyamfs 0:23b97841e8dc 22 */
hisyamfs 0:23b97841e8dc 23
hisyamfs 0:23b97841e8dc 24 #include "AX12.h"
hisyamfs 0:23b97841e8dc 25 #include "mbed.h"
hisyamfs 0:23b97841e8dc 26
hisyamfs 0:23b97841e8dc 27 AX12::AX12 (PinName tx, PinName rx, PinName tx_ena, int ID, int baud) : _ax12(tx,rx), _ena_tx(tx_ena, 1)
hisyamfs 0:23b97841e8dc 28 {
hisyamfs 0:23b97841e8dc 29 _ax12.baud(baud);
hisyamfs 0:23b97841e8dc 30 _baud_bit = 1000000/baud;
hisyamfs 0:23b97841e8dc 31 _wait_sent = 20 * _baud_bit;
hisyamfs 0:23b97841e8dc 32 _timeout_tick = 500 + 12 * _baud_bit;
hisyamfs 0:23b97841e8dc 33 _ID = ID;
hisyamfs 0:23b97841e8dc 34 _error_code = 0;
hisyamfs 0:23b97841e8dc 35 _data[0] = 0;
hisyamfs 0:23b97841e8dc 36 _reg_flag = 0;
hisyamfs 0:23b97841e8dc 37 }
hisyamfs 0:23b97841e8dc 38
hisyamfs 0:23b97841e8dc 39 // Set the mode of the servo
hisyamfs 0:23b97841e8dc 40 // 0 = Positional (0-300 degrees)
hisyamfs 0:23b97841e8dc 41 // 1 = Rotational -1 to 1 speed
hisyamfs 0:23b97841e8dc 42 int AX12::SetMode(int mode)
hisyamfs 0:23b97841e8dc 43 {
hisyamfs 0:23b97841e8dc 44 if (mode == 1)
hisyamfs 0:23b97841e8dc 45 { // set CR
hisyamfs 0:23b97841e8dc 46 SetCWLimit(0);
hisyamfs 0:23b97841e8dc 47 SetCCWLimit(0);
hisyamfs 0:23b97841e8dc 48 SetCRSpeed(0.0);
hisyamfs 0:23b97841e8dc 49 }
hisyamfs 0:23b97841e8dc 50 else
hisyamfs 0:23b97841e8dc 51 {
hisyamfs 0:23b97841e8dc 52 SetCWLimit(0);
hisyamfs 0:23b97841e8dc 53 SetCCWLimit(300);
hisyamfs 0:23b97841e8dc 54 SetCRSpeed(0.0);
hisyamfs 0:23b97841e8dc 55 }
hisyamfs 0:23b97841e8dc 56 return(0);
hisyamfs 0:23b97841e8dc 57 }
hisyamfs 0:23b97841e8dc 58
hisyamfs 0:23b97841e8dc 59 // if flag[0] is set, were blocking
hisyamfs 0:23b97841e8dc 60 // if flag[1] is set, we're registering
hisyamfs 0:23b97841e8dc 61 // they are mutually exclusive operations
hisyamfs 0:23b97841e8dc 62 int AX12::SetGoal(int degrees, int flags)
hisyamfs 0:23b97841e8dc 63 {
hisyamfs 0:23b97841e8dc 64 _reg_flag = 0;
hisyamfs 0:23b97841e8dc 65
hisyamfs 0:23b97841e8dc 66 // set the flag is only the register bit is set in the flag
hisyamfs 0:23b97841e8dc 67 if (flags == 0x2)
hisyamfs 0:23b97841e8dc 68 {
hisyamfs 0:23b97841e8dc 69 _reg_flag = 1;
hisyamfs 0:23b97841e8dc 70 }
hisyamfs 0:23b97841e8dc 71
hisyamfs 0:23b97841e8dc 72 // 1023 / 300 * degrees
hisyamfs 0:23b97841e8dc 73 short goal = (1023 * degrees) / 300;
hisyamfs 0:23b97841e8dc 74
hisyamfs 0:23b97841e8dc 75 _data[0] = goal & 0xff; // bottom 8 bits
hisyamfs 0:23b97841e8dc 76 _data[1] = goal >> 8; // top 8 bits
hisyamfs 0:23b97841e8dc 77
hisyamfs 0:23b97841e8dc 78 // write the packet, return the error code
hisyamfs 0:23b97841e8dc 79 int rVal = write(_ID, AX12_REG_GOAL_POSITION, 2, _data, _reg_flag);
hisyamfs 0:23b97841e8dc 80
hisyamfs 0:23b97841e8dc 81 if (flags == 1)
hisyamfs 0:23b97841e8dc 82 {
hisyamfs 0:23b97841e8dc 83 // block until it comes to a halt
hisyamfs 0:23b97841e8dc 84 while (isMoving())
hisyamfs 0:23b97841e8dc 85 {
hisyamfs 0:23b97841e8dc 86
hisyamfs 0:23b97841e8dc 87 }
hisyamfs 0:23b97841e8dc 88 }
hisyamfs 0:23b97841e8dc 89 return(rVal);
hisyamfs 0:23b97841e8dc 90 }
hisyamfs 0:23b97841e8dc 91
hisyamfs 0:23b97841e8dc 92 void AX12::MultSetGoal(int degrees1, float speed1,
hisyamfs 0:23b97841e8dc 93 int degrees2, float speed2,
hisyamfs 0:23b97841e8dc 94 int degrees3, float speed3,
hisyamfs 0:23b97841e8dc 95 int degrees4, float speed4,
hisyamfs 0:23b97841e8dc 96 int degrees5, float speed5,
hisyamfs 0:23b97841e8dc 97 int degrees6, float speed6,
hisyamfs 0:23b97841e8dc 98 int degrees7, float speed7,
hisyamfs 0:23b97841e8dc 99 int degrees8, float speed8,
hisyamfs 0:23b97841e8dc 100 int degrees9, float speed9,
hisyamfs 0:23b97841e8dc 101 int degrees10, float speed10,
hisyamfs 0:23b97841e8dc 102 int degrees11, float speed11,
hisyamfs 0:23b97841e8dc 103 int degrees12, float speed12,
hisyamfs 0:23b97841e8dc 104 int degrees13, float speed13,
hisyamfs 0:23b97841e8dc 105 int degrees14, float speed14,
hisyamfs 0:23b97841e8dc 106 int degrees15, float speed15,
hisyamfs 0:23b97841e8dc 107 int degrees16, float speed16,
hisyamfs 0:23b97841e8dc 108 int degrees17, float speed17,
hisyamfs 0:23b97841e8dc 109 int degrees18, float speed18)
hisyamfs 0:23b97841e8dc 110 {
hisyamfs 0:23b97841e8dc 111 char data[(total_ID*length_data) + 1];
hisyamfs 0:23b97841e8dc 112
hisyamfs 0:23b97841e8dc 113 int goal;
hisyamfs 0:23b97841e8dc 114 int goal_speed;
hisyamfs 0:23b97841e8dc 115
hisyamfs 0:23b97841e8dc 116 int alamat;
hisyamfs 0:23b97841e8dc 117
hisyamfs 0:23b97841e8dc 118 int degrees[19];
hisyamfs 0:23b97841e8dc 119 float speed[19];
hisyamfs 0:23b97841e8dc 120
hisyamfs 0:23b97841e8dc 121 // assignment value from parameter to array
hisyamfs 0:23b97841e8dc 122
hisyamfs 0:23b97841e8dc 123 degrees[1] = degrees1;
hisyamfs 0:23b97841e8dc 124 degrees[2] = degrees2;
hisyamfs 0:23b97841e8dc 125 degrees[3] = degrees3;
hisyamfs 0:23b97841e8dc 126 degrees[4] = degrees4;
hisyamfs 0:23b97841e8dc 127 degrees[5] = degrees5;
hisyamfs 0:23b97841e8dc 128 degrees[6] = degrees6;
hisyamfs 0:23b97841e8dc 129 degrees[7] = degrees7;
hisyamfs 0:23b97841e8dc 130 degrees[8] = degrees8;
hisyamfs 0:23b97841e8dc 131 degrees[9] = degrees9;
hisyamfs 0:23b97841e8dc 132 degrees[10] = degrees10;
hisyamfs 0:23b97841e8dc 133 degrees[11] = degrees11;
hisyamfs 0:23b97841e8dc 134 degrees[12] = degrees12;
hisyamfs 0:23b97841e8dc 135 degrees[13] = degrees13;
hisyamfs 0:23b97841e8dc 136 degrees[14] = degrees14;
hisyamfs 0:23b97841e8dc 137 degrees[15] = degrees15;
hisyamfs 0:23b97841e8dc 138 degrees[16] = degrees16;
hisyamfs 0:23b97841e8dc 139 degrees[17] = degrees17;
hisyamfs 0:23b97841e8dc 140 degrees[18] = degrees18;
hisyamfs 0:23b97841e8dc 141
hisyamfs 0:23b97841e8dc 142 speed[1] = speed1;
hisyamfs 0:23b97841e8dc 143 speed[2] = speed2;
hisyamfs 0:23b97841e8dc 144 speed[3] = speed3;
hisyamfs 0:23b97841e8dc 145 speed[4] = speed4;
hisyamfs 0:23b97841e8dc 146 speed[5] = speed5;
hisyamfs 0:23b97841e8dc 147 speed[6] = speed6;
hisyamfs 0:23b97841e8dc 148 speed[7] = speed7;
hisyamfs 0:23b97841e8dc 149 speed[8] = speed8;
hisyamfs 0:23b97841e8dc 150 speed[9] = speed9;
hisyamfs 0:23b97841e8dc 151 speed[10] = speed10;
hisyamfs 0:23b97841e8dc 152 speed[11] = speed11;
hisyamfs 0:23b97841e8dc 153 speed[12] = speed12;
hisyamfs 0:23b97841e8dc 154 speed[13] = speed13;
hisyamfs 0:23b97841e8dc 155 speed[14] = speed14;
hisyamfs 0:23b97841e8dc 156 speed[15] = speed15;
hisyamfs 0:23b97841e8dc 157 speed[16] = speed16;
hisyamfs 0:23b97841e8dc 158 speed[17] = speed17;
hisyamfs 0:23b97841e8dc 159 speed[18] = speed18;
hisyamfs 0:23b97841e8dc 160
hisyamfs 0:23b97841e8dc 161 /*
hisyamfs 0:23b97841e8dc 162 for (char k=1; k<=total_ID; k++)
hisyamfs 0:23b97841e8dc 163 {
hisyamfs 0:23b97841e8dc 164 degrees[k] = degrees1;
hisyamfs 0:23b97841e8dc 165 speed[k] = speed1;
hisyamfs 0:23b97841e8dc 166 }
hisyamfs 0:23b97841e8dc 167 */
hisyamfs 0:23b97841e8dc 168
hisyamfs 0:23b97841e8dc 169 for (int i=1; i<=total_ID; i++)
hisyamfs 0:23b97841e8dc 170 {
hisyamfs 0:23b97841e8dc 171 alamat = length_data*(i-1);
hisyamfs 0:23b97841e8dc 172
hisyamfs 0:23b97841e8dc 173 goal = (1023 * degrees[i]) / 300;
hisyamfs 0:23b97841e8dc 174
hisyamfs 0:23b97841e8dc 175 data[alamat+1] = goal & 0xff; // bottom 8 bits
hisyamfs 0:23b97841e8dc 176 data[alamat+2] = goal >> 8; // top 8 bits
hisyamfs 0:23b97841e8dc 177
hisyamfs 0:23b97841e8dc 178 float temp = (speed[i]<0)? (-1)*speed[i]:speed[i];
hisyamfs 0:23b97841e8dc 179 goal_speed = (0x3ff * temp);
hisyamfs 0:23b97841e8dc 180
hisyamfs 0:23b97841e8dc 181 // Set direction CW if we have a negative speed
hisyamfs 0:23b97841e8dc 182 if (speed[i] < 0)
hisyamfs 0:23b97841e8dc 183 {
hisyamfs 0:23b97841e8dc 184 goal_speed |= (0x1 << 10);
hisyamfs 0:23b97841e8dc 185 }
hisyamfs 0:23b97841e8dc 186
hisyamfs 0:23b97841e8dc 187 data[alamat+3] = goal_speed & 0xff; // bottom 8 bits
hisyamfs 0:23b97841e8dc 188 data[alamat+4] = goal_speed >> 8; // top 8 bits
hisyamfs 0:23b97841e8dc 189 }
hisyamfs 0:23b97841e8dc 190
hisyamfs 0:23b97841e8dc 191 // Write
hisyamfs 0:23b97841e8dc 192 sync_write(data);
hisyamfs 0:23b97841e8dc 193
hisyamfs 0:23b97841e8dc 194 return;
hisyamfs 0:23b97841e8dc 195 }
hisyamfs 0:23b97841e8dc 196
hisyamfs 0:23b97841e8dc 197 // Set continuous rotation speed from -1 to 1
hisyamfs 0:23b97841e8dc 198 int AX12::SetCRSpeed(float speed)
hisyamfs 0:23b97841e8dc 199 {
hisyamfs 0:23b97841e8dc 200 // bit 10 = direction, 0 = CCW, 1=CW
hisyamfs 0:23b97841e8dc 201 // bits 9-0 = Speed
hisyamfs 0:23b97841e8dc 202 float temp = (speed<0)? speed * (-1): speed;
hisyamfs 0:23b97841e8dc 203 int goal = (0x3ff * temp);
hisyamfs 0:23b97841e8dc 204
hisyamfs 0:23b97841e8dc 205 // Set direction CW if we have a negative speed
hisyamfs 0:23b97841e8dc 206 if (speed < 0)
hisyamfs 0:23b97841e8dc 207 {
hisyamfs 0:23b97841e8dc 208 goal |= (0x1 << 10);
hisyamfs 0:23b97841e8dc 209 }
hisyamfs 0:23b97841e8dc 210
hisyamfs 0:23b97841e8dc 211 _data[0] = goal & 0xff; // bottom 8 bits
hisyamfs 0:23b97841e8dc 212 _data[1] = goal >> 8; // top 8 bits
hisyamfs 0:23b97841e8dc 213
hisyamfs 0:23b97841e8dc 214 // write the packet, return the error code
hisyamfs 0:23b97841e8dc 215 int rVal = write(_ID, AX12_REG_MOVING_SPEED, 2, _data);
hisyamfs 0:23b97841e8dc 216
hisyamfs 0:23b97841e8dc 217 return(rVal);
hisyamfs 0:23b97841e8dc 218 }
hisyamfs 0:23b97841e8dc 219
hisyamfs 0:23b97841e8dc 220 int AX12::SetCWLimit (int degrees)
hisyamfs 0:23b97841e8dc 221 {
hisyamfs 0:23b97841e8dc 222 // 1023 / 300 * degrees
hisyamfs 0:23b97841e8dc 223 short limit = (1023 * degrees) / 300;
hisyamfs 0:23b97841e8dc 224
hisyamfs 0:23b97841e8dc 225 _data[0] = limit & 0xff; // bottom 8 bits
hisyamfs 0:23b97841e8dc 226 _data[1] = limit >> 8; // top 8 bits
hisyamfs 0:23b97841e8dc 227
hisyamfs 0:23b97841e8dc 228 // write the packet, return the error code
hisyamfs 0:23b97841e8dc 229 return (write(_ID, AX12_REG_CW_LIMIT, 2, _data));
hisyamfs 0:23b97841e8dc 230 }
hisyamfs 0:23b97841e8dc 231
hisyamfs 0:23b97841e8dc 232 int AX12::SetCCWLimit (int degrees)
hisyamfs 0:23b97841e8dc 233 {
hisyamfs 0:23b97841e8dc 234 // 1023 / 300 * degrees
hisyamfs 0:23b97841e8dc 235 short limit = (1023 * degrees) / 300;
hisyamfs 0:23b97841e8dc 236
hisyamfs 0:23b97841e8dc 237 _data[0] = limit & 0xff; // bottom 8 bits
hisyamfs 0:23b97841e8dc 238 _data[1] = limit >> 8; // top 8 bits
hisyamfs 0:23b97841e8dc 239
hisyamfs 0:23b97841e8dc 240 // write the packet, return the error code
hisyamfs 0:23b97841e8dc 241 return (write(_ID, AX12_REG_CCW_LIMIT, 2, _data));
hisyamfs 0:23b97841e8dc 242 }
hisyamfs 0:23b97841e8dc 243
hisyamfs 0:23b97841e8dc 244 void AX12::ControlID (int ID)
hisyamfs 0:23b97841e8dc 245 {
hisyamfs 0:23b97841e8dc 246 _ID = ID;
hisyamfs 0:23b97841e8dc 247
hisyamfs 0:23b97841e8dc 248 return;
hisyamfs 0:23b97841e8dc 249 }
hisyamfs 0:23b97841e8dc 250
hisyamfs 0:23b97841e8dc 251 int AX12::SetID (int CurrentID, int NewID)
hisyamfs 0:23b97841e8dc 252 {
hisyamfs 0:23b97841e8dc 253 _data[0] = NewID;
hisyamfs 0:23b97841e8dc 254 return (write(CurrentID, AX12_REG_ID, 1, _data));
hisyamfs 0:23b97841e8dc 255 }
hisyamfs 0:23b97841e8dc 256
hisyamfs 0:23b97841e8dc 257 int AX12::SetBaud (int baud)
hisyamfs 0:23b97841e8dc 258 {
hisyamfs 0:23b97841e8dc 259 _data[0] = baud;
hisyamfs 0:23b97841e8dc 260 return (write(0xFE, AX12_REG_BAUD, 1, _data));
hisyamfs 0:23b97841e8dc 261 }
hisyamfs 0:23b97841e8dc 262
hisyamfs 0:23b97841e8dc 263 // return 1 is the servo is still in flight
hisyamfs 0:23b97841e8dc 264 int AX12::isMoving(void)
hisyamfs 0:23b97841e8dc 265 {
hisyamfs 0:23b97841e8dc 266 read(_ID,AX12_REG_MOVING,1,_data);
hisyamfs 0:23b97841e8dc 267 return(_data[0]);
hisyamfs 0:23b97841e8dc 268 }
hisyamfs 0:23b97841e8dc 269
hisyamfs 0:23b97841e8dc 270 void AX12::trigger(void)
hisyamfs 0:23b97841e8dc 271 {
hisyamfs 0:23b97841e8dc 272 char TxBuf[16];
hisyamfs 0:23b97841e8dc 273 char sum = 0;
hisyamfs 0:23b97841e8dc 274
hisyamfs 0:23b97841e8dc 275 TxBuf[0] = 0xFF;
hisyamfs 0:23b97841e8dc 276 TxBuf[1] = 0xFF;
hisyamfs 0:23b97841e8dc 277
hisyamfs 0:23b97841e8dc 278 // ID - Broadcast
hisyamfs 0:23b97841e8dc 279 TxBuf[2] = 0xFE;
hisyamfs 0:23b97841e8dc 280 sum += TxBuf[2];
hisyamfs 0:23b97841e8dc 281
hisyamfs 0:23b97841e8dc 282 // Length
hisyamfs 0:23b97841e8dc 283 TxBuf[3] = 0x02;
hisyamfs 0:23b97841e8dc 284 sum += TxBuf[3];
hisyamfs 0:23b97841e8dc 285
hisyamfs 0:23b97841e8dc 286 // Instruction - ACTION
hisyamfs 0:23b97841e8dc 287 TxBuf[4] = 0x04;
hisyamfs 0:23b97841e8dc 288 sum += TxBuf[4];
hisyamfs 0:23b97841e8dc 289
hisyamfs 0:23b97841e8dc 290 // Checksum
hisyamfs 0:23b97841e8dc 291 TxBuf[5] = 0xFF - sum;
hisyamfs 0:23b97841e8dc 292
hisyamfs 0:23b97841e8dc 293 // Transmit the packet in one burst with no pausing
hisyamfs 0:23b97841e8dc 294 for (int i = 0; i < 6 ; i++) {
hisyamfs 0:23b97841e8dc 295 _ax12.putc(TxBuf[i]);
hisyamfs 0:23b97841e8dc 296 }
hisyamfs 0:23b97841e8dc 297
hisyamfs 0:23b97841e8dc 298 // This is a broadcast packet, so there will be no reply
hisyamfs 0:23b97841e8dc 299 return;
hisyamfs 0:23b97841e8dc 300 }
hisyamfs 0:23b97841e8dc 301
hisyamfs 0:23b97841e8dc 302 float AX12::GetPosition(void)
hisyamfs 0:23b97841e8dc 303 {
hisyamfs 0:23b97841e8dc 304 _error_code = read(_ID, AX12_REG_POSITION, 2, _data);
hisyamfs 0:23b97841e8dc 305 return ((short)(_data[0] + (_data[1] << 8)) * 300.0f / 1024.0f);
hisyamfs 0:23b97841e8dc 306 }
hisyamfs 0:23b97841e8dc 307
hisyamfs 0:23b97841e8dc 308 float AX12::GetTemp (void)
hisyamfs 0:23b97841e8dc 309 {
hisyamfs 0:23b97841e8dc 310 _error_code = read(_ID, AX12_REG_TEMP, 1, _data);
hisyamfs 0:23b97841e8dc 311 return(_data[0]);
hisyamfs 0:23b97841e8dc 312 }
hisyamfs 0:23b97841e8dc 313
hisyamfs 0:23b97841e8dc 314 float AX12::GetVolts (void)
hisyamfs 0:23b97841e8dc 315 {
hisyamfs 0:23b97841e8dc 316 _error_code = read(_ID, AX12_REG_VOLTS, 1, _data);
hisyamfs 0:23b97841e8dc 317 return(_data[0]/10.0f);
hisyamfs 0:23b97841e8dc 318 }
hisyamfs 0:23b97841e8dc 319
hisyamfs 0:23b97841e8dc 320 int AX12::read(int ID, int start, int bytes, char* data)
hisyamfs 0:23b97841e8dc 321 {
hisyamfs 0:23b97841e8dc 322 char PacketLength = 0x4;
hisyamfs 0:23b97841e8dc 323 char TxBuf[8];
hisyamfs 0:23b97841e8dc 324 char sum = 0;
hisyamfs 0:23b97841e8dc 325 // char Status[6 + bytes];
hisyamfs 0:23b97841e8dc 326
hisyamfs 0:23b97841e8dc 327 Status[4] = 0xFE; // return code
hisyamfs 0:23b97841e8dc 328
hisyamfs 0:23b97841e8dc 329 // Build the TxPacket first in RAM, then we'll send in one go
hisyamfs 0:23b97841e8dc 330
hisyamfs 0:23b97841e8dc 331 TxBuf[0] = 0xff;
hisyamfs 0:23b97841e8dc 332 TxBuf[1] = 0xff;
hisyamfs 0:23b97841e8dc 333
hisyamfs 0:23b97841e8dc 334 // ID
hisyamfs 0:23b97841e8dc 335 TxBuf[2] = ID;
hisyamfs 0:23b97841e8dc 336 sum += TxBuf[2];
hisyamfs 0:23b97841e8dc 337
hisyamfs 0:23b97841e8dc 338 // Packet Length
hisyamfs 0:23b97841e8dc 339 TxBuf[3] = PacketLength; // Length = 4 ; 2 + 1 (start) = 1 (bytes)
hisyamfs 0:23b97841e8dc 340 sum += TxBuf[3]; // Accululate the packet sum
hisyamfs 0:23b97841e8dc 341
hisyamfs 0:23b97841e8dc 342 // Instruction - Read
hisyamfs 0:23b97841e8dc 343 TxBuf[4] = 0x2;
hisyamfs 0:23b97841e8dc 344 sum += TxBuf[4];
hisyamfs 0:23b97841e8dc 345
hisyamfs 0:23b97841e8dc 346 // Start Address
hisyamfs 0:23b97841e8dc 347 TxBuf[5] = start;
hisyamfs 0:23b97841e8dc 348 sum += TxBuf[5];
hisyamfs 0:23b97841e8dc 349
hisyamfs 0:23b97841e8dc 350 // Bytes to read
hisyamfs 0:23b97841e8dc 351 TxBuf[6] = bytes;
hisyamfs 0:23b97841e8dc 352 sum += TxBuf[6];
hisyamfs 0:23b97841e8dc 353
hisyamfs 0:23b97841e8dc 354 // Checksum
hisyamfs 0:23b97841e8dc 355 TxBuf[7] = 0xFF - sum;
hisyamfs 0:23b97841e8dc 356
hisyamfs 0:23b97841e8dc 357 // Transmit the packet in one burst with no pausing
hisyamfs 0:23b97841e8dc 358 _ena_tx = 1;
hisyamfs 0:23b97841e8dc 359 for (int i = 0; i<8 ; i++)
hisyamfs 0:23b97841e8dc 360 {
hisyamfs 0:23b97841e8dc 361 _ax12.putc(TxBuf[i]);
hisyamfs 0:23b97841e8dc 362 }
hisyamfs 0:23b97841e8dc 363
hisyamfs 0:23b97841e8dc 364 // Wait for the bytes to be transmitted
hisyamfs 0:23b97841e8dc 365 wait_us (_wait_sent);
hisyamfs 0:23b97841e8dc 366 _ena_tx = 0;
hisyamfs 0:23b97841e8dc 367
hisyamfs 0:23b97841e8dc 368 // Skip if the read was to the broadcast address
hisyamfs 0:23b97841e8dc 369 if (_ID != 0xFE)
hisyamfs 0:23b97841e8dc 370 {
hisyamfs 0:23b97841e8dc 371 // response packet is always 6 + bytes
hisyamfs 0:23b97841e8dc 372 // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum
hisyamfs 0:23b97841e8dc 373 // timeout is a little more than the time to transmit
hisyamfs 0:23b97841e8dc 374 // the packet back, i.e. (6+bytes)*10 bit periods
hisyamfs 0:23b97841e8dc 375 int plen = 0;
hisyamfs 0:23b97841e8dc 376 int timeout = 0;
hisyamfs 0:23b97841e8dc 377 int state = 0;
hisyamfs 0:23b97841e8dc 378 while ((timeout < _timeout_tick) && (plen < (bytes + 3)))
hisyamfs 0:23b97841e8dc 379 {
hisyamfs 0:23b97841e8dc 380 if (_ax12.readable())
hisyamfs 0:23b97841e8dc 381 {
hisyamfs 0:23b97841e8dc 382 switch(state)
hisyamfs 0:23b97841e8dc 383 {
hisyamfs 0:23b97841e8dc 384 case 0:
hisyamfs 0:23b97841e8dc 385 case 1:
hisyamfs 0:23b97841e8dc 386 Status[state] = _ax12.getc();
hisyamfs 0:23b97841e8dc 387 if(Status[state] == 0xFF)
hisyamfs 0:23b97841e8dc 388 {
hisyamfs 0:23b97841e8dc 389 state++;
hisyamfs 0:23b97841e8dc 390 timeout = 0;
hisyamfs 0:23b97841e8dc 391 }
hisyamfs 0:23b97841e8dc 392 else
hisyamfs 0:23b97841e8dc 393 state = 0;
hisyamfs 0:23b97841e8dc 394 break;
hisyamfs 0:23b97841e8dc 395 case 2:
hisyamfs 0:23b97841e8dc 396 Status[state] = _ax12.getc();
hisyamfs 0:23b97841e8dc 397 if(Status[state] == ID)
hisyamfs 0:23b97841e8dc 398 {
hisyamfs 0:23b97841e8dc 399 state++;
hisyamfs 0:23b97841e8dc 400 timeout = 0;
hisyamfs 0:23b97841e8dc 401 }
hisyamfs 0:23b97841e8dc 402 else
hisyamfs 0:23b97841e8dc 403 state = 0;
hisyamfs 0:23b97841e8dc 404 break;
hisyamfs 0:23b97841e8dc 405 case 3:
hisyamfs 0:23b97841e8dc 406 Status[plen+state] = _ax12.getc();
hisyamfs 0:23b97841e8dc 407 plen++;
hisyamfs 0:23b97841e8dc 408 timeout = 0;
hisyamfs 0:23b97841e8dc 409 break;
hisyamfs 0:23b97841e8dc 410 default:
hisyamfs 0:23b97841e8dc 411 state = 0;
hisyamfs 0:23b97841e8dc 412 }
hisyamfs 0:23b97841e8dc 413 }
hisyamfs 0:23b97841e8dc 414
hisyamfs 0:23b97841e8dc 415 // wait for the bit period
hisyamfs 0:23b97841e8dc 416 wait_us(1);
hisyamfs 0:23b97841e8dc 417 timeout++;
hisyamfs 0:23b97841e8dc 418 }
hisyamfs 0:23b97841e8dc 419
hisyamfs 0:23b97841e8dc 420 if (timeout == 550)
hisyamfs 0:23b97841e8dc 421 {
hisyamfs 0:23b97841e8dc 422 return(-1);
hisyamfs 0:23b97841e8dc 423 }
hisyamfs 0:23b97841e8dc 424
hisyamfs 0:23b97841e8dc 425 // Copy the data from Status into data for return
hisyamfs 0:23b97841e8dc 426 for (int i=0; i < Status[3]-2 ; i++)
hisyamfs 0:23b97841e8dc 427 {
hisyamfs 0:23b97841e8dc 428 data[i] = Status[5+i];
hisyamfs 0:23b97841e8dc 429 }
hisyamfs 0:23b97841e8dc 430 }
hisyamfs 0:23b97841e8dc 431
hisyamfs 0:23b97841e8dc 432 // if (ID!=0xFE)
hisyamfs 0:23b97841e8dc 433 return(Status[4]);
hisyamfs 0:23b97841e8dc 434 }
hisyamfs 0:23b97841e8dc 435
hisyamfs 0:23b97841e8dc 436 int AX12::write(int ID, int start, int bytes, char* data, int flag) {
hisyamfs 0:23b97841e8dc 437 // 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum
hisyamfs 0:23b97841e8dc 438
hisyamfs 0:23b97841e8dc 439 // char TxBuf[7+bytes];
hisyamfs 0:23b97841e8dc 440 char sum = 0;
hisyamfs 0:23b97841e8dc 441 char Status[6];
hisyamfs 0:23b97841e8dc 442
hisyamfs 0:23b97841e8dc 443 // Build the TxPacket first in RAM, then we'll send in one go
hisyamfs 0:23b97841e8dc 444
hisyamfs 0:23b97841e8dc 445 TxBuf[0] = 0xff;
hisyamfs 0:23b97841e8dc 446 TxBuf[1] = 0xff;
hisyamfs 0:23b97841e8dc 447
hisyamfs 0:23b97841e8dc 448 // ID
hisyamfs 0:23b97841e8dc 449 TxBuf[2] = ID;
hisyamfs 0:23b97841e8dc 450 sum += TxBuf[2];
hisyamfs 0:23b97841e8dc 451
hisyamfs 0:23b97841e8dc 452 // packet Length
hisyamfs 0:23b97841e8dc 453 TxBuf[3] = 3+bytes;
hisyamfs 0:23b97841e8dc 454 sum += TxBuf[3];
hisyamfs 0:23b97841e8dc 455
hisyamfs 0:23b97841e8dc 456 // Instruction
hisyamfs 0:23b97841e8dc 457 if (flag == 1)
hisyamfs 0:23b97841e8dc 458 {
hisyamfs 0:23b97841e8dc 459 TxBuf[4]=0x04;
hisyamfs 0:23b97841e8dc 460 sum += TxBuf[4];
hisyamfs 0:23b97841e8dc 461 }
hisyamfs 0:23b97841e8dc 462 else
hisyamfs 0:23b97841e8dc 463 {
hisyamfs 0:23b97841e8dc 464 TxBuf[4]=0x03;
hisyamfs 0:23b97841e8dc 465 sum += TxBuf[4];
hisyamfs 0:23b97841e8dc 466 }
hisyamfs 0:23b97841e8dc 467
hisyamfs 0:23b97841e8dc 468 // Start Address
hisyamfs 0:23b97841e8dc 469 TxBuf[5] = start;
hisyamfs 0:23b97841e8dc 470 sum += TxBuf[5];
hisyamfs 0:23b97841e8dc 471
hisyamfs 0:23b97841e8dc 472 // data
hisyamfs 0:23b97841e8dc 473 for (uint8_t i=0; i<bytes ; i++)
hisyamfs 0:23b97841e8dc 474 {
hisyamfs 0:23b97841e8dc 475 TxBuf[6+i] = data[i];
hisyamfs 0:23b97841e8dc 476 sum += TxBuf[6+i];
hisyamfs 0:23b97841e8dc 477 }
hisyamfs 0:23b97841e8dc 478
hisyamfs 0:23b97841e8dc 479 // checksum
hisyamfs 0:23b97841e8dc 480 TxBuf[6+bytes] = 0xFF - sum;
hisyamfs 0:23b97841e8dc 481
hisyamfs 0:23b97841e8dc 482 // Transmit the packet in one burst with no pausing
hisyamfs 0:23b97841e8dc 483 _ena_tx = 1;
hisyamfs 0:23b97841e8dc 484 for (int i = 0; i < (7 + bytes) ; i++)
hisyamfs 0:23b97841e8dc 485 {
hisyamfs 0:23b97841e8dc 486 _ax12.putc(TxBuf[i]);
hisyamfs 0:23b97841e8dc 487 }
hisyamfs 0:23b97841e8dc 488
hisyamfs 0:23b97841e8dc 489 // Wait for data to transmit
hisyamfs 0:23b97841e8dc 490 wait_us (_wait_sent);
hisyamfs 0:23b97841e8dc 491 _ena_tx = 0;
hisyamfs 0:23b97841e8dc 492
hisyamfs 0:23b97841e8dc 493 // make sure we have a valid return
hisyamfs 0:23b97841e8dc 494 Status[4]=0x00;
hisyamfs 0:23b97841e8dc 495
hisyamfs 0:23b97841e8dc 496 // we'll only get a reply if it was not broadcast
hisyamfs 0:23b97841e8dc 497 if (_ID!=0xFE)
hisyamfs 0:23b97841e8dc 498 {
hisyamfs 0:23b97841e8dc 499 // response packet is always 6 bytes
hisyamfs 0:23b97841e8dc 500 // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum
hisyamfs 0:23b97841e8dc 501 // timeout is a little more than the time to transmit
hisyamfs 0:23b97841e8dc 502 // the packet back, i.e. 60 bit periods, round up to 100
hisyamfs 0:23b97841e8dc 503 int timeout = 0;
hisyamfs 0:23b97841e8dc 504 int state = 0;
hisyamfs 0:23b97841e8dc 505 int plen = 0;
hisyamfs 0:23b97841e8dc 506 while ((timeout < _timeout_tick) && (plen<3))
hisyamfs 0:23b97841e8dc 507 {
hisyamfs 0:23b97841e8dc 508 if (_ax12.readable())
hisyamfs 0:23b97841e8dc 509 {
hisyamfs 0:23b97841e8dc 510 switch(state)
hisyamfs 0:23b97841e8dc 511 {
hisyamfs 0:23b97841e8dc 512 case 0:
hisyamfs 0:23b97841e8dc 513 case 1:
hisyamfs 0:23b97841e8dc 514 Status[state] = _ax12.getc();
hisyamfs 0:23b97841e8dc 515 if(Status[state] == 0xFF)
hisyamfs 0:23b97841e8dc 516 {
hisyamfs 0:23b97841e8dc 517 state++;
hisyamfs 0:23b97841e8dc 518 timeout = 0;
hisyamfs 0:23b97841e8dc 519 }
hisyamfs 0:23b97841e8dc 520 else
hisyamfs 0:23b97841e8dc 521 state = 0;
hisyamfs 0:23b97841e8dc 522 break;
hisyamfs 0:23b97841e8dc 523 case 2:
hisyamfs 0:23b97841e8dc 524 Status[state] = _ax12.getc();
hisyamfs 0:23b97841e8dc 525 if(Status[state] == ID)
hisyamfs 0:23b97841e8dc 526 {
hisyamfs 0:23b97841e8dc 527 state++;
hisyamfs 0:23b97841e8dc 528 timeout = 0;
hisyamfs 0:23b97841e8dc 529 }
hisyamfs 0:23b97841e8dc 530 else
hisyamfs 0:23b97841e8dc 531 state = 0;
hisyamfs 0:23b97841e8dc 532 break;
hisyamfs 0:23b97841e8dc 533 case 3:
hisyamfs 0:23b97841e8dc 534 Status[plen+state] = _ax12.getc();
hisyamfs 0:23b97841e8dc 535 plen++;
hisyamfs 0:23b97841e8dc 536 timeout = 0;
hisyamfs 0:23b97841e8dc 537 break;
hisyamfs 0:23b97841e8dc 538 default:
hisyamfs 0:23b97841e8dc 539 state = 0;
hisyamfs 0:23b97841e8dc 540 }
hisyamfs 0:23b97841e8dc 541 }
hisyamfs 0:23b97841e8dc 542
hisyamfs 0:23b97841e8dc 543 // wait for the bit period
hisyamfs 0:23b97841e8dc 544 wait_us (1);
hisyamfs 0:23b97841e8dc 545 timeout++;
hisyamfs 0:23b97841e8dc 546 }
hisyamfs 0:23b97841e8dc 547
hisyamfs 0:23b97841e8dc 548 // Build the TxPacket first in RAM, then we'll send in one go
hisyamfs 0:23b97841e8dc 549 }
hisyamfs 0:23b97841e8dc 550 return(Status[4]); // return error code
hisyamfs 0:23b97841e8dc 551 }
hisyamfs 0:23b97841e8dc 552
hisyamfs 0:23b97841e8dc 553 int AX12::sync_write(char* data)
hisyamfs 0:23b97841e8dc 554 {
hisyamfs 0:23b97841e8dc 555 // 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum
hisyamfs 0:23b97841e8dc 556
hisyamfs 0:23b97841e8dc 557 char sum = 0;
hisyamfs 0:23b97841e8dc 558 char Status[6];
hisyamfs 0:23b97841e8dc 559 char TxBuf[(total_ID*(length_data+1)) + 8];
hisyamfs 0:23b97841e8dc 560
hisyamfs 0:23b97841e8dc 561 int pra_alamat, alamat;
hisyamfs 0:23b97841e8dc 562
hisyamfs 0:23b97841e8dc 563 // Set ID
hisyamfs 0:23b97841e8dc 564 char ID[total_ID+1];
hisyamfs 0:23b97841e8dc 565
hisyamfs 0:23b97841e8dc 566 for (uint8_t k=1; k<=total_ID; k++)
hisyamfs 0:23b97841e8dc 567 {
hisyamfs 0:23b97841e8dc 568 ID[k] = k;
hisyamfs 0:23b97841e8dc 569 }
hisyamfs 0:23b97841e8dc 570
hisyamfs 0:23b97841e8dc 571 TxBuf[0] = 0xFF;
hisyamfs 0:23b97841e8dc 572 TxBuf[1] = 0xFF;
hisyamfs 0:23b97841e8dc 573
hisyamfs 0:23b97841e8dc 574 // ID
hisyamfs 0:23b97841e8dc 575 TxBuf[2] = 0xFE;
hisyamfs 0:23b97841e8dc 576 sum += TxBuf[2];
hisyamfs 0:23b97841e8dc 577
hisyamfs 0:23b97841e8dc 578 // packet Length
hisyamfs 0:23b97841e8dc 579 TxBuf[3] = ((length_data+1) * total_ID) + 4;
hisyamfs 0:23b97841e8dc 580 sum += TxBuf[3];
hisyamfs 0:23b97841e8dc 581
hisyamfs 0:23b97841e8dc 582 // Instruction
hisyamfs 0:23b97841e8dc 583 TxBuf[4]=0x83;
hisyamfs 0:23b97841e8dc 584 sum += TxBuf[4];
hisyamfs 0:23b97841e8dc 585
hisyamfs 0:23b97841e8dc 586 // Start Address
hisyamfs 0:23b97841e8dc 587 TxBuf[5] = start_reg;
hisyamfs 0:23b97841e8dc 588 sum += TxBuf[5];
hisyamfs 0:23b97841e8dc 589
hisyamfs 0:23b97841e8dc 590 // Length of Data to Write
hisyamfs 0:23b97841e8dc 591 TxBuf[6] = length_data;
hisyamfs 0:23b97841e8dc 592 sum += TxBuf[6];
hisyamfs 0:23b97841e8dc 593
hisyamfs 0:23b97841e8dc 594 for (uint8_t j=1; j<=total_ID; j++)
hisyamfs 0:23b97841e8dc 595 {
hisyamfs 0:23b97841e8dc 596 pra_alamat = length_data*(j-1);
hisyamfs 0:23b97841e8dc 597 alamat = 6 + j + pra_alamat;
hisyamfs 0:23b97841e8dc 598
hisyamfs 0:23b97841e8dc 599 TxBuf[alamat] = ID[j];
hisyamfs 0:23b97841e8dc 600 sum += TxBuf[alamat];
hisyamfs 0:23b97841e8dc 601
hisyamfs 0:23b97841e8dc 602 for (char i=1; i<=length_data; i++)
hisyamfs 0:23b97841e8dc 603 {
hisyamfs 0:23b97841e8dc 604 TxBuf[alamat+i] = data[pra_alamat+i];
hisyamfs 0:23b97841e8dc 605 sum += TxBuf[alamat+i];
hisyamfs 0:23b97841e8dc 606 }
hisyamfs 0:23b97841e8dc 607 }
hisyamfs 0:23b97841e8dc 608
hisyamfs 0:23b97841e8dc 609 // checksum
hisyamfs 0:23b97841e8dc 610 TxBuf[((total_ID*(length_data+1)) + 7)] = 0xFF - (sum & 0xFF);
hisyamfs 0:23b97841e8dc 611
hisyamfs 0:23b97841e8dc 612 // Transmit the packet in one burst with no pausing
hisyamfs 0:23b97841e8dc 613 _ena_tx = 1;
hisyamfs 0:23b97841e8dc 614 for (int i = 0; i <= ((total_ID*(length_data+1)) + 7) ; i++)
hisyamfs 0:23b97841e8dc 615 {
hisyamfs 0:23b97841e8dc 616 _ax12.putc(TxBuf[i]);
hisyamfs 0:23b97841e8dc 617 }
hisyamfs 0:23b97841e8dc 618
hisyamfs 0:23b97841e8dc 619 // Wait for data to transmit
hisyamfs 0:23b97841e8dc 620 wait_us (_wait_sent);
hisyamfs 0:23b97841e8dc 621 _ena_tx = 0;
hisyamfs 0:23b97841e8dc 622
hisyamfs 0:23b97841e8dc 623 // make sure we have a valid return
hisyamfs 0:23b97841e8dc 624 Status[4]=0x00;
hisyamfs 0:23b97841e8dc 625
hisyamfs 0:23b97841e8dc 626 return(Status[4]); // return error code
hisyamfs 0:23b97841e8dc 627 }
hisyamfs 1:2759fc78aa04 628
hisyamfs 1:2759fc78aa04 629 void AX12::SetTorqueEnable(char val) {
hisyamfs 1:2759fc78aa04 630 // write(int ID, int start, int length, char* data, int flag=0);
hisyamfs 1:2759fc78aa04 631 char en = val;
hisyamfs 1:2759fc78aa04 632 write(_ID, AX12_TORQUE_ENABLE, 1, &en, 0);
hisyamfs 1:2759fc78aa04 633 }