]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/tests/peripherals/AX12/AX12.cpp
1 /* mbed AX-12+ Servo Library
3 * Copyright (c) 2010, cstyles (http://mbed.org)
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
27 AX12::AX12(PinName tx
, PinName rx
, int ID
, int baud
)
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
) {
40 if (mode
== 1) { // set CR
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
) {
61 // set the flag is only the register bit is set in the flag
66 // 1023 / 300 * degrees
67 short goal
= (1023 * degrees
) / 300;
69 printf("SetGoal to 0x%x\n",goal
);
72 data
[0] = goal
& 0xff; // bottom 8 bits
73 data
[1] = goal
>> 8; // top 8 bits
75 // write the packet, return the error code
76 int rVal
= write(_ID
, AX12_REG_GOAL_POSITION
, 2, data
, reg_flag
);
79 // block until it comes to a halt
86 // Set continuous rotation speed from -1 to 1
87 int AX12::SetCRSpeed(float speed
) {
89 // bit 10 = direction, 0 = CCW, 1=CW
93 int goal
= (0x3ff * abs(speed
));
95 // Set direction CW if we have a negative speed
100 data
[0] = goal
& 0xff; // bottom 8 bits
101 data
[1] = goal
>> 8; // top 8 bits
103 // write the packet, return the error code
104 int rVal
= write(_ID
, 0x20, 2, data
);
110 int AX12::SetCWLimit (int degrees
) {
114 // 1023 / 300 * degrees
115 short limit
= (1023 * degrees
) / 300;
118 printf("SetCWLimit to 0x%x\n",limit
);
121 data
[0] = limit
& 0xff; // bottom 8 bits
122 data
[1] = limit
>> 8; // top 8 bits
124 // write the packet, return the error code
125 return (write(_ID
, AX12_REG_CW_LIMIT
, 2, data
));
129 int AX12::SetCCWLimit (int degrees
) {
133 // 1023 / 300 * degrees
134 short limit
= (1023 * degrees
) / 300;
137 printf("SetCCWLimit to 0x%x\n",limit
);
140 data
[0] = limit
& 0xff; // bottom 8 bits
141 data
[1] = limit
>> 8; // top 8 bits
143 // write the packet, return the error code
144 return (write(_ID
, AX12_REG_CCW_LIMIT
, 2, data
));
148 int AX12::SetID (int CurrentID
, int NewID
) {
154 printf("Setting ID from 0x%x to 0x%x\n",CurrentID
,NewID
);
157 return (write(CurrentID
, AX12_REG_ID
, 1, data
));
162 int AX12::SetBaud (int baud
) {
168 printf("Setting Baud rate to %d\n",baud
);
171 return (write(0xFE, AX12_REG_BAUD
, 1, data
));
177 // return 1 is the servo is still in flight
178 int AX12::isMoving(void) {
181 read(_ID
,AX12_REG_MOVING
,1,data
);
186 void AX12::trigger(void) {
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");
204 #ifdef AX12_TRIGGER_DEBUG
205 printf(" ID : %d\n",TxBuf
[2]);
212 #ifdef AX12_TRIGGER_DEBUG
213 printf(" Length %d\n",TxBuf
[3]);
216 // Instruction - ACTION
220 #ifdef AX12_TRIGGER_DEBUG
221 printf(" Instruction 0x%X\n",TxBuf
[5]);
225 TxBuf
[5] = 0xFF - sum
;
226 #ifdef AX12_TRIGGER_DEBUG
227 printf(" Checksum 0x%X\n",TxBuf
[5]);
230 // Transmit the packet in one burst with no pausing
231 for (int i
= 0; i
< 6 ; i
++) {
232 _ax12
.putc(TxBuf
[i
]);
235 // This is a broadcast packet, so there will be no reply
240 float AX12::GetPosition(void) {
243 printf("\nGetPosition(%d)",_ID
);
248 int ErrorCode
= read(_ID
, AX12_REG_POSITION
, 2, data
);
249 short position
= data
[0] + (data
[1] << 8);
250 float angle
= (position
* 300)/1024;
256 float AX12::GetTemp (void) {
259 printf("\nGetTemp(%d)",_ID
);
263 int ErrorCode
= read(_ID
, AX12_REG_TEMP
, 1, data
);
264 float temp
= data
[0];
269 float AX12::GetVolts (void) {
272 printf("\nGetVolts(%d)",_ID
);
276 int ErrorCode
= read(_ID
, AX12_REG_VOLTS
, 1, data
);
277 float volts
= data
[0]/10.0;
282 int AX12::read(int ID
, int start
, int bytes
, char* data
) {
284 char PacketLength
= 0x4;
289 Status
[4] = 0xFE; // return code
291 #ifdef AX12_READ_DEBUG
292 printf("\nread(%d,0x%x,%d,data)\n",ID
,start
,bytes
);
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");
307 #ifdef AX12_READ_DEBUG
308 printf(" ID : %d\n",TxBuf
[2]);
312 TxBuf
[3] = PacketLength
; // Length = 4 ; 2 + 1 (start) = 1 (bytes)
313 sum
+= TxBuf
[3]; // Accululate the packet sum
315 #ifdef AX12_READ_DEBUG
316 printf(" Length : 0x%x\n",TxBuf
[3]);
319 // Instruction - Read
323 #ifdef AX12_READ_DEBUG
324 printf(" Instruction : 0x%x\n",TxBuf
[4]);
331 #ifdef AX12_READ_DEBUG
332 printf(" Start Address : 0x%x\n",TxBuf
[5]);
339 #ifdef AX12_READ_DEBUG
340 printf(" No bytes : 0x%x\n",TxBuf
[6]);
344 TxBuf
[7] = 0xFF - sum
;
345 #ifdef AX12_READ_DEBUG
346 printf(" Checksum : 0x%x\n",TxBuf
[7]);
349 // Transmit the packet in one burst with no pausing
350 for (int i
= 0; i
<8 ; i
++) {
351 _ax12
.putc(TxBuf
[i
]);
354 // Wait for the bytes to be transmitted
357 // Skip if the read was to the broadcast address
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
369 while ((timeout
< ((6+bytes
)*10)) && (plen
<(6+bytes
))) {
371 if (_ax12
.readable()) {
372 Status
[plen
] = _ax12
.getc();
377 // wait for the bit period
382 if (timeout
== ((6+bytes
)*10) ) {
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
];
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]);
399 for (int i
=0; i
< Status
[3]-2 ; i
++) {
400 printf(" Data : 0x%x\n",Status
[5+i
]);
403 printf(" Checksum : 0x%x\n",Status
[5+(Status
[3]-2)]);
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
419 #ifdef AX12_WRITE_DEBUG
420 printf("\nwrite(%d,0x%x,%d,data,%d)\n",ID
,start
,bytes
,flag
);
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");
435 #ifdef AX12_WRITE_DEBUG
436 printf(" ID : %d\n",TxBuf
[2]);
443 #ifdef AX12_WRITE_DEBUG
444 printf(" Length : %d\n",TxBuf
[3]);
456 #ifdef AX12_WRITE_DEBUG
457 printf(" Instruction : 0x%x\n",TxBuf
[4]);
464 #ifdef AX12_WRITE_DEBUG
465 printf(" Start : 0x%x\n",TxBuf
[5]);
469 for (char i
=0; i
<bytes
; i
++) {
470 TxBuf
[6+i
] = data
[i
];
473 #ifdef AX12_WRITE_DEBUG
474 printf(" Data : 0x%x\n",TxBuf
[6+i
]);
480 TxBuf
[6+bytes
] = 0xFF - sum
;
482 #ifdef AX12_WRITE_DEBUG
483 printf(" Checksum : 0x%x\n",TxBuf
[6+bytes
]);
486 // Transmit the packet in one burst with no pausing
487 for (int i
= 0; i
< (7 + bytes
) ; i
++) {
488 _ax12
.putc(TxBuf
[i
]);
491 // Wait for data to transmit
494 // make sure we have a valid return
497 // we'll only get a reply if it was not broadcast
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
507 while ((timeout
< 100) && (plen
<6)) {
509 if (_ax12
.readable()) {
510 Status
[plen
] = _ax12
.getc();
515 // wait for the bit period
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]);
533 return(Status
[4]); // return error code