A library for the AX-12+ servo using external ICs instead of the SerialHalf-Duplex class
A library based on the AX-12+ library but using external hardware to convert from full to half duplex as suggested in the AX-12 datasheet. The buffers and NOT gate need to connected as shown below.
Don't forget the pull-up resistor!
Revision 2:3000c6778d1c, committed 2012-10-07
- Comitter:
- ms523
- Date:
- Sun Oct 07 08:52:25 2012 +0000
- Parent:
- 1:1dd9cd18975d
- Commit message:
- Improved documentation
Changed in this revision
AX12.cpp | Show annotated file Show diff for this revision Revisions of this file |
AX12.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/AX12.cpp Sun Aug 07 13:06:04 2011 +0000 +++ b/AX12.cpp Sun Oct 07 08:52:25 2012 +0000 @@ -3,10 +3,10 @@ #include "AX12.h" #include "mbed.h" -AX12::AX12(PinName tx, PinName rx, PinName dir, int ID) - : _ax12(tx,rx), _dir(dir) { +AX12::AX12(Serial& bus, PinName dir, int ID) + : _bus(bus), _dir(dir) { - _ax12.baud(1000000); + _bus.baud(1000000); _ID = ID; _dir = TRANSMIT; } @@ -63,7 +63,7 @@ // And send the packet to th AX-12+ Servo... _dir = TRANSMIT; // Switch the hardware to transmit... for (int i = 0; i < 8 ; i++) { // Transmit the packet in one burst with no pausing - _ax12.putc(TxBuf[i]); + _bus.putc(TxBuf[i]); } wait (0.00004); // Wait for data to transmit _dir = RECEIVE; // Switch the hardware back to receive... @@ -74,7 +74,7 @@ Status[4] = 0xFE; // Initailise status[4] return code if (_ID!=0xFE) { // We'll only get a reply if it was not broadcast for (int i=0; i<(6+bytes) ; i++) { // Receive the Status packet 6+ number of bytes read - Status[i] = _ax12.getc(); + Status[i] = _bus.getc(); } for (int i=0; i < Status[3]-2 ; i++) { // Copy the data from Status into data for return data[i] = Status[5+i]; @@ -115,7 +115,7 @@ // And send the packet to th AX-12+ Servo... _dir = TRANSMIT; // Switch the hardware to transmit... for (int i = 0; i < (7 + bytes) ; i++) { // Transmit the packet in one burst with no pausing - _ax12.putc(TxBuf[i]); + _bus.putc(TxBuf[i]); } wait (0.00004); // Wait for data to transmit _dir = RECEIVE; // Switch the hardware back to receive... @@ -126,7 +126,7 @@ Status[4]=0x00; // Initailise status[4] to get correct response if (_ID!=0xFE) { // We'll only get a reply if it was not broadcast for (int i=0; i < 6 ; i++) { // Response is 6 bytes - 0xFF, 0xFF, ID, Length Error, Param(s) Checksum - Status[i] = _ax12.getc(); + Status[i] = _bus.getc(); } } // if (ID!=0xFE) return(Status[4]); // return error code - if no error will return 0x00
--- a/AX12.h Sun Aug 07 13:06:04 2011 +0000 +++ b/AX12.h Sun Oct 07 08:52:25 2012 +0000 @@ -1,4 +1,20 @@ -/* mbed AX-12+ Servo Library - External hardware version */ +/* Copyright (c) 2012 Martin Smith, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ #ifndef MBED_AX12_H #define MBED_AX12_H @@ -17,23 +33,71 @@ #define TRANSMIT 1 #define RECEIVE 0 -class AX12 { +/** mbed AX-12+ Servo Library - External hardware version + * + * Example: + * @code + * // Move a servo back and forth... + * #include "mbed.h" + * #include "Servo.h" + * + * Serial ax12_bus(p28,p27); + * AX12 my_ax12(ax12_bus, p29, 1); + * + * int main() { + * while(1) { + * my_ax12.SetGoal(0); // move to 0 + * wait(2); + * my_ax12.SetGoal(300); // move to 300 + * wait(2); + * } + * } + * @endcode + */ + +class AX12 +{ public: - // define which pins are used, and the ID of this instance - AX12(PinName tx, PinName rx, PinName dir, int ID); + /** define which pins are used, and the ID of this instance + */ + AX12(Serial& bus, PinName dir, int ID); - // methods for writing and reading registers + /** Read info from the specified AX-12 register + * + * @param ID the ID of the AX-12 servo + * @param start the address of the 1st register to be read + * @param length the number of bytes to read + * @param data where the read data is stored + * @returns servo status + */ int read(int ID, int start, int length, char* data); + + /** Write info to the specified AX-12 register + * + * @param ID the ID of the AX-12 servo + * @param start the address of the 1st register to be written to + * @param length the number of bytes to write + * @param data where the data to be written is stored + * @returns servo status + */ int write(int ID, int start, int length, char* data); - // set goal angle in integer degrees + /** Sets the goal position for the servo + * + * @param degrees the new target position + * @returns error code + */ int SetGoal(int degrees); - - float GetPosition(); // Get current angle, only valid 0-300 + + /** Getss the current position for the servo + * + * @returns current position + */ + float GetPosition(); private : - Serial _ax12; + Serial& _bus; DigitalOut _dir; int _ID; };