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.

640

Don't forget the pull-up resistor!

Committer:
ms523
Date:
Sun Oct 07 08:52:25 2012 +0000
Revision:
2:3000c6778d1c
Parent:
1:1dd9cd18975d
Improved documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ms523 2:3000c6778d1c 1 /* Copyright (c) 2012 Martin Smith, MIT License
ms523 2:3000c6778d1c 2 *
ms523 2:3000c6778d1c 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ms523 2:3000c6778d1c 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
ms523 2:3000c6778d1c 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ms523 2:3000c6778d1c 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ms523 2:3000c6778d1c 7 * furnished to do so, subject to the following conditions:
ms523 2:3000c6778d1c 8 *
ms523 2:3000c6778d1c 9 * The above copyright notice and this permission notice shall be included in all copies or
ms523 2:3000c6778d1c 10 * substantial portions of the Software.
ms523 2:3000c6778d1c 11 *
ms523 2:3000c6778d1c 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ms523 2:3000c6778d1c 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ms523 2:3000c6778d1c 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ms523 2:3000c6778d1c 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ms523 2:3000c6778d1c 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ms523 2:3000c6778d1c 17 */
ms523 0:d4af99baf76f 18
ms523 0:d4af99baf76f 19 #ifndef MBED_AX12_H
ms523 0:d4af99baf76f 20 #define MBED_AX12_H
ms523 0:d4af99baf76f 21
ms523 0:d4af99baf76f 22 #include "mbed.h"
ms523 0:d4af99baf76f 23
ms523 0:d4af99baf76f 24 #define AX12_WRITE_DEBUG 0
ms523 0:d4af99baf76f 25 #define AX12_READ_DEBUG 0
ms523 0:d4af99baf76f 26 #define AX12_TRIGGER_DEBUG 0
ms523 0:d4af99baf76f 27 #define AX12_DEBUG 0
ms523 0:d4af99baf76f 28
ms523 0:d4af99baf76f 29 #define AX12_REG_GOAL_POSITION 0x1E
ms523 0:d4af99baf76f 30 #define AX12_REG_MOVING_SPEED 0x20
ms523 0:d4af99baf76f 31 #define AX12_REG_POSITION 0x24
ms523 0:d4af99baf76f 32
ms523 0:d4af99baf76f 33 #define TRANSMIT 1
ms523 0:d4af99baf76f 34 #define RECEIVE 0
ms523 0:d4af99baf76f 35
ms523 2:3000c6778d1c 36 /** mbed AX-12+ Servo Library - External hardware version
ms523 2:3000c6778d1c 37 *
ms523 2:3000c6778d1c 38 * Example:
ms523 2:3000c6778d1c 39 * @code
ms523 2:3000c6778d1c 40 * // Move a servo back and forth...
ms523 2:3000c6778d1c 41 * #include "mbed.h"
ms523 2:3000c6778d1c 42 * #include "Servo.h"
ms523 2:3000c6778d1c 43 *
ms523 2:3000c6778d1c 44 * Serial ax12_bus(p28,p27);
ms523 2:3000c6778d1c 45 * AX12 my_ax12(ax12_bus, p29, 1);
ms523 2:3000c6778d1c 46 *
ms523 2:3000c6778d1c 47 * int main() {
ms523 2:3000c6778d1c 48 * while(1) {
ms523 2:3000c6778d1c 49 * my_ax12.SetGoal(0); // move to 0
ms523 2:3000c6778d1c 50 * wait(2);
ms523 2:3000c6778d1c 51 * my_ax12.SetGoal(300); // move to 300
ms523 2:3000c6778d1c 52 * wait(2);
ms523 2:3000c6778d1c 53 * }
ms523 2:3000c6778d1c 54 * }
ms523 2:3000c6778d1c 55 * @endcode
ms523 2:3000c6778d1c 56 */
ms523 2:3000c6778d1c 57
ms523 2:3000c6778d1c 58 class AX12
ms523 2:3000c6778d1c 59 {
ms523 0:d4af99baf76f 60
ms523 0:d4af99baf76f 61 public:
ms523 2:3000c6778d1c 62 /** define which pins are used, and the ID of this instance
ms523 2:3000c6778d1c 63 */
ms523 2:3000c6778d1c 64 AX12(Serial& bus, PinName dir, int ID);
ms523 0:d4af99baf76f 65
ms523 2:3000c6778d1c 66 /** Read info from the specified AX-12 register
ms523 2:3000c6778d1c 67 *
ms523 2:3000c6778d1c 68 * @param ID the ID of the AX-12 servo
ms523 2:3000c6778d1c 69 * @param start the address of the 1st register to be read
ms523 2:3000c6778d1c 70 * @param length the number of bytes to read
ms523 2:3000c6778d1c 71 * @param data where the read data is stored
ms523 2:3000c6778d1c 72 * @returns servo status
ms523 2:3000c6778d1c 73 */
ms523 0:d4af99baf76f 74 int read(int ID, int start, int length, char* data);
ms523 2:3000c6778d1c 75
ms523 2:3000c6778d1c 76 /** Write info to the specified AX-12 register
ms523 2:3000c6778d1c 77 *
ms523 2:3000c6778d1c 78 * @param ID the ID of the AX-12 servo
ms523 2:3000c6778d1c 79 * @param start the address of the 1st register to be written to
ms523 2:3000c6778d1c 80 * @param length the number of bytes to write
ms523 2:3000c6778d1c 81 * @param data where the data to be written is stored
ms523 2:3000c6778d1c 82 * @returns servo status
ms523 2:3000c6778d1c 83 */
ms523 1:1dd9cd18975d 84 int write(int ID, int start, int length, char* data);
ms523 0:d4af99baf76f 85
ms523 2:3000c6778d1c 86 /** Sets the goal position for the servo
ms523 2:3000c6778d1c 87 *
ms523 2:3000c6778d1c 88 * @param degrees the new target position
ms523 2:3000c6778d1c 89 * @returns error code
ms523 2:3000c6778d1c 90 */
ms523 1:1dd9cd18975d 91 int SetGoal(int degrees);
ms523 2:3000c6778d1c 92
ms523 2:3000c6778d1c 93 /** Getss the current position for the servo
ms523 2:3000c6778d1c 94 *
ms523 2:3000c6778d1c 95 * @returns current position
ms523 2:3000c6778d1c 96 */
ms523 2:3000c6778d1c 97 float GetPosition();
ms523 0:d4af99baf76f 98
ms523 0:d4af99baf76f 99 private :
ms523 2:3000c6778d1c 100 Serial& _bus;
ms523 0:d4af99baf76f 101 DigitalOut _dir;
ms523 0:d4af99baf76f 102 int _ID;
ms523 0:d4af99baf76f 103 };
ms523 1:1dd9cd18975d 104 #endif