]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/tests/peripherals/AX12/AX12.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / tests / peripherals / AX12 / AX12.cpp
1 /* mbed AX-12+ Servo Library
2  *
3  * Copyright (c) 2010, cstyles (http://mbed.org)
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  * THE SOFTWARE.
22  */
23
24 #include "AX12.h"
25 #include "mbed.h"
26
27 AX12::AX12(PinName tx, PinName rx, int ID, int baud)
28         : _ax12(tx,rx) {
29     _baud = baud;
30     _ID = ID;
31     _ax12.baud(_baud);
32
33 }
34
35 // Set the mode of the servo
36 //  0 = Positional (0-300 degrees)
37 //  1 = Rotational -1 to 1 speed
38 int AX12::SetMode(int mode) {
39
40     if (mode == 1) { // set CR
41         SetCWLimit(0);
42         SetCCWLimit(0);
43         SetCRSpeed(0.0);
44     } else {
45         SetCWLimit(0);
46         SetCCWLimit(300);
47         SetCRSpeed(0.0);
48     }
49     return(0);
50 }
51
52
53 // if flag[0] is set, were blocking
54 // if flag[1] is set, we're registering
55 // they are mutually exclusive operations
56 int AX12::SetGoal(int degrees, int flags) {
57
58     char reg_flag = 0;
59     char data[2];
60
61     // set the flag is only the register bit is set in the flag
62     if (flags == 0x2) {
63         reg_flag = 1;
64     }
65
66     // 1023 / 300 * degrees
67     short goal = (1023 * degrees) / 300;
68 #ifdef AX12_DEBUG
69     printf("SetGoal to 0x%x\n",goal);
70 #endif
71
72     data[0] = goal & 0xff; // bottom 8 bits
73     data[1] = goal >> 8;   // top 8 bits
74
75     // write the packet, return the error code
76     int rVal = write(_ID, AX12_REG_GOAL_POSITION, 2, data, reg_flag);
77
78     if (flags == 1) {
79         // block until it comes to a halt
80         while (isMoving()) {}
81     }
82     return(rVal);
83 }
84
85
86 // Set continuous rotation speed from -1 to 1
87 int AX12::SetCRSpeed(float speed) {
88
89     // bit 10     = direction, 0 = CCW, 1=CW
90     // bits 9-0   = Speed
91     char data[2];
92
93     int goal = (0x3ff * abs(speed));
94
95     // Set direction CW if we have a negative speed
96     if (speed < 0) {
97         goal |= (0x1 << 10);
98     }
99
100     data[0] = goal & 0xff; // bottom 8 bits
101     data[1] = goal >> 8;   // top 8 bits
102
103     // write the packet, return the error code
104     int rVal = write(_ID, 0x20, 2, data);
105
106     return(rVal);
107 }
108
109
110 int AX12::SetCWLimit (int degrees) {
111
112     char data[2];
113
114     // 1023 / 300 * degrees
115     short limit = (1023 * degrees) / 300;
116
117 #ifdef AX12_DEBUG
118     printf("SetCWLimit to 0x%x\n",limit);
119 #endif
120
121     data[0] = limit & 0xff; // bottom 8 bits
122     data[1] = limit >> 8;   // top 8 bits
123
124     // write the packet, return the error code
125     return (write(_ID, AX12_REG_CW_LIMIT, 2, data));
126
127 }
128
129 int AX12::SetCCWLimit (int degrees) {
130
131     char data[2];
132
133     // 1023 / 300 * degrees
134     short limit = (1023 * degrees) / 300;
135
136 #ifdef AX12_DEBUG
137     printf("SetCCWLimit to 0x%x\n",limit);
138 #endif
139
140     data[0] = limit & 0xff; // bottom 8 bits
141     data[1] = limit >> 8;   // top 8 bits
142
143     // write the packet, return the error code
144     return (write(_ID, AX12_REG_CCW_LIMIT, 2, data));
145 }
146
147
148 int AX12::SetID (int CurrentID, int NewID) {
149
150     char data[1];
151     data[0] = NewID;
152
153 #ifdef AX12_DEBUG
154     printf("Setting ID from 0x%x to 0x%x\n",CurrentID,NewID);
155 #endif
156
157     return (write(CurrentID, AX12_REG_ID, 1, data));
158
159 }
160
161
162 int AX12::SetBaud (int baud) {
163
164     char data[1];
165     data[0] = baud;
166
167 #ifdef AX12_DEBUG
168     printf("Setting Baud rate to %d\n",baud);
169 #endif
170
171     return (write(0xFE, AX12_REG_BAUD, 1, data));
172
173 }
174
175
176
177 // return 1 is the servo is still in flight
178 int AX12::isMoving(void) {
179
180     char data[1];
181     read(_ID,AX12_REG_MOVING,1,data);
182     return(data[0]);
183 }
184
185
186 void AX12::trigger(void) {
187
188     char TxBuf[16];
189     char sum = 0;
190
191 #ifdef AX12_TRIGGER_DEBUG
192     // Build the TxPacket first in RAM, then we'll send in one go
193     printf("\nTriggered\n");
194     printf("\nTrigger Packet\n  Header : 0xFF, 0xFF\n");
195 #endif
196
197     TxBuf[0] = 0xFF;
198     TxBuf[1] = 0xFF;
199
200     // ID - Broadcast
201     TxBuf[2] = 0xFE;
202     sum += TxBuf[2];
203
204 #ifdef AX12_TRIGGER_DEBUG
205     printf("  ID : %d\n",TxBuf[2]);
206 #endif
207
208     // Length
209     TxBuf[3] = 0x02;
210     sum += TxBuf[3];
211
212 #ifdef AX12_TRIGGER_DEBUG
213     printf("  Length %d\n",TxBuf[3]);
214 #endif
215
216     // Instruction - ACTION
217     TxBuf[4] = 0x04;
218     sum += TxBuf[4];
219
220 #ifdef AX12_TRIGGER_DEBUG
221     printf("  Instruction 0x%X\n",TxBuf[5]);
222 #endif
223
224     // Checksum
225     TxBuf[5] = 0xFF - sum;
226 #ifdef AX12_TRIGGER_DEBUG
227     printf("  Checksum 0x%X\n",TxBuf[5]);
228 #endif
229
230     // Transmit the packet in one burst with no pausing
231     for (int i = 0; i < 6 ; i++) {
232         _ax12.putc(TxBuf[i]);
233     }
234
235     // This is a broadcast packet, so there will be no reply
236     return;
237 }
238
239
240 float AX12::GetPosition(void) {
241
242 #ifdef AX12_DEBUG
243     printf("\nGetPosition(%d)",_ID);
244 #endif
245
246     char data[2];
247
248     int ErrorCode = read(_ID, AX12_REG_POSITION, 2, data);
249     short position = data[0] + (data[1] << 8);
250     float angle = (position * 300)/1024;
251
252     return (angle);
253 }
254
255
256 float AX12::GetTemp (void) {
257
258 #ifdef AX12_DEBUG
259     printf("\nGetTemp(%d)",_ID);
260 #endif
261
262     char data[1];
263     int ErrorCode = read(_ID, AX12_REG_TEMP, 1, data);
264     float temp = data[0];
265     return(temp);
266 }
267
268
269 float AX12::GetVolts (void) {
270
271 #ifdef AX12_DEBUG
272     printf("\nGetVolts(%d)",_ID);
273 #endif
274
275     char data[1];
276     int ErrorCode = read(_ID, AX12_REG_VOLTS, 1, data);
277     float volts = data[0]/10.0;
278     return(volts);
279 }
280
281
282 int AX12::read(int ID, int start, int bytes, char* data) {
283
284     char PacketLength = 0x4;
285     char TxBuf[16];
286     char sum = 0;
287     char Status[16];
288
289     Status[4] = 0xFE; // return code
290
291 #ifdef AX12_READ_DEBUG
292     printf("\nread(%d,0x%x,%d,data)\n",ID,start,bytes);
293 #endif
294
295     // Build the TxPacket first in RAM, then we'll send in one go
296 #ifdef AX12_READ_DEBUG
297     printf("\nInstruction Packet\n  Header : 0xFF, 0xFF\n");
298 #endif
299
300     TxBuf[0] = 0xff;
301     TxBuf[1] = 0xff;
302
303     // ID
304     TxBuf[2] = ID;
305     sum += TxBuf[2];
306
307 #ifdef AX12_READ_DEBUG
308     printf("  ID : %d\n",TxBuf[2]);
309 #endif
310
311     // Packet Length
312     TxBuf[3] = PacketLength;    // Length = 4 ; 2 + 1 (start) = 1 (bytes)
313     sum += TxBuf[3];            // Accululate the packet sum
314
315 #ifdef AX12_READ_DEBUG
316     printf("  Length : 0x%x\n",TxBuf[3]);
317 #endif
318
319     // Instruction - Read
320     TxBuf[4] = 0x2;
321     sum += TxBuf[4];
322
323 #ifdef AX12_READ_DEBUG
324     printf("  Instruction : 0x%x\n",TxBuf[4]);
325 #endif
326
327     // Start Address
328     TxBuf[5] = start;
329     sum += TxBuf[5];
330
331 #ifdef AX12_READ_DEBUG
332     printf("  Start Address : 0x%x\n",TxBuf[5]);
333 #endif
334
335     // Bytes to read
336     TxBuf[6] = bytes;
337     sum += TxBuf[6];
338
339 #ifdef AX12_READ_DEBUG
340     printf("  No bytes : 0x%x\n",TxBuf[6]);
341 #endif
342
343     // Checksum
344     TxBuf[7] = 0xFF - sum;
345 #ifdef AX12_READ_DEBUG
346     printf("  Checksum : 0x%x\n",TxBuf[7]);
347 #endif
348
349     // Transmit the packet in one burst with no pausing
350     for (int i = 0; i<8 ; i++) {
351         _ax12.putc(TxBuf[i]);
352     }
353
354     // Wait for the bytes to be transmitted
355     wait (0.00002);
356
357     // Skip if the read was to the broadcast address
358     if (_ID != 0xFE) {
359
360
361
362         // response packet is always 6 + bytes
363         // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum
364         // timeout is a little more than the time to transmit
365         // the packet back, i.e. (6+bytes)*10 bit periods
366
367         int timeout = 0;
368         int plen = 0;
369         while ((timeout < ((6+bytes)*10)) && (plen<(6+bytes))) {
370
371             if (_ax12.readable()) {
372                 Status[plen] = _ax12.getc();
373                 plen++;
374                 timeout = 0;
375             }
376
377             // wait for the bit period
378             wait (1.0/_baud);
379             timeout++;
380         }
381
382         if (timeout == ((6+bytes)*10) ) {
383             return(-1);
384         }
385
386         // Copy the data from Status into data for return
387         for (int i=0; i < Status[3]-2 ; i++) {
388             data[i] = Status[5+i];
389         }
390
391 #ifdef AX12_READ_DEBUG
392         printf("\nStatus Packet\n");
393         printf("  Header : 0x%x\n",Status[0]);
394         printf("  Header : 0x%x\n",Status[1]);
395         printf("  ID : 0x%x\n",Status[2]);
396         printf("  Length : 0x%x\n",Status[3]);
397         printf("  Error Code : 0x%x\n",Status[4]);
398
399         for (int i=0; i < Status[3]-2 ; i++) {
400             printf("  Data : 0x%x\n",Status[5+i]);
401         }
402
403         printf("  Checksum : 0x%x\n",Status[5+(Status[3]-2)]);
404 #endif
405
406     } // if (ID!=0xFE)
407
408     return(Status[4]);
409 }
410
411
412 int AX12::write(int ID, int start, int bytes, char* data, int flag) {
413 // 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum
414
415     char TxBuf[16];
416     char sum = 0;
417     char Status[6];
418
419 #ifdef AX12_WRITE_DEBUG
420     printf("\nwrite(%d,0x%x,%d,data,%d)\n",ID,start,bytes,flag);
421 #endif
422
423     // Build the TxPacket first in RAM, then we'll send in one go
424 #ifdef AX12_WRITE_DEBUG
425     printf("\nInstruction Packet\n  Header : 0xFF, 0xFF\n");
426 #endif
427
428     TxBuf[0] = 0xff;
429     TxBuf[1] = 0xff;
430
431     // ID
432     TxBuf[2] = ID;
433     sum += TxBuf[2];
434
435 #ifdef AX12_WRITE_DEBUG
436     printf("  ID : %d\n",TxBuf[2]);
437 #endif
438
439     // packet Length
440     TxBuf[3] = 3+bytes;
441     sum += TxBuf[3];
442
443 #ifdef AX12_WRITE_DEBUG
444     printf("  Length : %d\n",TxBuf[3]);
445 #endif
446
447     // Instruction
448     if (flag == 1) {
449         TxBuf[4]=0x04;
450         sum += TxBuf[4];
451     } else {
452         TxBuf[4]=0x03;
453         sum += TxBuf[4];
454     }
455
456 #ifdef AX12_WRITE_DEBUG
457     printf("  Instruction : 0x%x\n",TxBuf[4]);
458 #endif
459
460     // Start Address
461     TxBuf[5] = start;
462     sum += TxBuf[5];
463
464 #ifdef AX12_WRITE_DEBUG
465     printf("  Start : 0x%x\n",TxBuf[5]);
466 #endif
467
468     // data
469     for (char i=0; i<bytes ; i++) {
470         TxBuf[6+i] = data[i];
471         sum += TxBuf[6+i];
472
473 #ifdef AX12_WRITE_DEBUG
474         printf("  Data : 0x%x\n",TxBuf[6+i]);
475 #endif
476
477     }
478
479     // checksum
480     TxBuf[6+bytes] = 0xFF - sum;
481
482 #ifdef AX12_WRITE_DEBUG
483     printf("  Checksum : 0x%x\n",TxBuf[6+bytes]);
484 #endif
485
486     // Transmit the packet in one burst with no pausing
487     for (int i = 0; i < (7 + bytes) ; i++) {
488         _ax12.putc(TxBuf[i]);
489     }
490
491     // Wait for data to transmit
492     wait (0.00002);
493
494     // make sure we have a valid return
495     Status[4]=0x00;
496
497     // we'll only get a reply if it was not broadcast
498     if (_ID!=0xFE) {
499
500
501         // response packet is always 6 bytes
502         // 0xFF, 0xFF, ID, Length Error, Param(s) Checksum
503         // timeout is a little more than the time to transmit
504         // the packet back, i.e. 60 bit periods, round up to 100
505         int timeout = 0;
506         int plen = 0;
507         while ((timeout < 100) && (plen<6)) {
508
509             if (_ax12.readable()) {
510                 Status[plen] = _ax12.getc();
511                 plen++;
512                 timeout = 0;
513             }
514
515             // wait for the bit period
516             wait (1.0/_baud);
517             timeout++;
518         }
519
520
521         // Build the TxPacket first in RAM, then we'll send in one go
522 #ifdef AX12_WRITE_DEBUG
523         printf("\nStatus Packet\n  Header : 0x%X, 0x%X\n",Status[0],Status[1]);
524         printf("  ID : %d\n",Status[2]);
525         printf("  Length : %d\n",Status[3]);
526         printf("  Error : 0x%x\n",Status[4]);
527         printf("  Checksum : 0x%x\n",Status[5]);
528 #endif
529
530
531     }
532
533     return(Status[4]); // return error code
534 }