tes ir atas semua

Dependencies:   mbed ADS1115 StepperMotor SRF05 TPA81new

Committer:
hisyamfs
Date:
Sun Dec 16 01:53:07 2018 +0000
Revision:
12:1e3227a6fcd7
test board v1

Who changed what in which revision?

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