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:
Sat Aug 06 12:20:27 2011 +0000
Revision:
0:d4af99baf76f
Child:
1:1dd9cd18975d
Initial revision

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ms523 0:d4af99baf76f 1 /* mbed AX-12+ Servo Library
ms523 0:d4af99baf76f 2 *
ms523 0:d4af99baf76f 3 * Copyright (c) 2010, cstyles (http://mbed.org)
ms523 0:d4af99baf76f 4 *
ms523 0:d4af99baf76f 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
ms523 0:d4af99baf76f 6 * of this software and associated documentation files (the "Software"), to deal
ms523 0:d4af99baf76f 7 * in the Software without restriction, including without limitation the rights
ms523 0:d4af99baf76f 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ms523 0:d4af99baf76f 9 * copies of the Software, and to permit persons to whom the Software is
ms523 0:d4af99baf76f 10 * furnished to do so, subject to the following conditions:
ms523 0:d4af99baf76f 11 *
ms523 0:d4af99baf76f 12 * The above copyright notice and this permission notice shall be included in
ms523 0:d4af99baf76f 13 * all copies or substantial portions of the Software.
ms523 0:d4af99baf76f 14 *
ms523 0:d4af99baf76f 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ms523 0:d4af99baf76f 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ms523 0:d4af99baf76f 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ms523 0:d4af99baf76f 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ms523 0:d4af99baf76f 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ms523 0:d4af99baf76f 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ms523 0:d4af99baf76f 21 * THE SOFTWARE.
ms523 0:d4af99baf76f 22 */
ms523 0:d4af99baf76f 23
ms523 0:d4af99baf76f 24 #ifndef MBED_AX12_H
ms523 0:d4af99baf76f 25 #define MBED_AX12_H
ms523 0:d4af99baf76f 26
ms523 0:d4af99baf76f 27 #include "mbed.h"
ms523 0:d4af99baf76f 28
ms523 0:d4af99baf76f 29 #define AX12_WRITE_DEBUG 0
ms523 0:d4af99baf76f 30 #define AX12_READ_DEBUG 0
ms523 0:d4af99baf76f 31 #define AX12_TRIGGER_DEBUG 0
ms523 0:d4af99baf76f 32 #define AX12_DEBUG 0
ms523 0:d4af99baf76f 33
ms523 0:d4af99baf76f 34 #define AX12_REG_ID 0x3
ms523 0:d4af99baf76f 35 #define AX12_REG_CW_LIMIT 0x06
ms523 0:d4af99baf76f 36 #define AX12_REG_CCW_LIMIT 0x08
ms523 0:d4af99baf76f 37 #define AX12_REG_GOAL_POSITION 0x1E
ms523 0:d4af99baf76f 38 #define AX12_REG_MOVING_SPEED 0x20
ms523 0:d4af99baf76f 39 #define AX12_REG_VOLTS 0x2A
ms523 0:d4af99baf76f 40 #define AX12_REG_TEMP 0x2B
ms523 0:d4af99baf76f 41 #define AX12_REG_MOVING 0x2E
ms523 0:d4af99baf76f 42 #define AX12_REG_POSITION 0x24
ms523 0:d4af99baf76f 43
ms523 0:d4af99baf76f 44 #define AX12_MODE_POSITION 0
ms523 0:d4af99baf76f 45 #define AX12_MODE_ROTATION 1
ms523 0:d4af99baf76f 46
ms523 0:d4af99baf76f 47 #define AX12_CW 1
ms523 0:d4af99baf76f 48 #define AX12_CCW 0
ms523 0:d4af99baf76f 49
ms523 0:d4af99baf76f 50 #define TRANSMIT 1
ms523 0:d4af99baf76f 51 #define RECEIVE 0
ms523 0:d4af99baf76f 52
ms523 0:d4af99baf76f 53 class AX12 {
ms523 0:d4af99baf76f 54
ms523 0:d4af99baf76f 55 public:
ms523 0:d4af99baf76f 56
ms523 0:d4af99baf76f 57 // define which pins are used, and the ID of this instance
ms523 0:d4af99baf76f 58 // Mutiple AX12's can share the same pins.
ms523 0:d4af99baf76f 59 AX12(PinName tx, PinName rx, PinName dir, int ID);
ms523 0:d4af99baf76f 60
ms523 0:d4af99baf76f 61 // methods for writing and reading registers
ms523 0:d4af99baf76f 62 int read(int ID, int start, int length, char* data);
ms523 0:d4af99baf76f 63 int write(int ID, int start, int length, char* data, int flag=0);
ms523 0:d4af99baf76f 64
ms523 0:d4af99baf76f 65 // set goal angle in integer degrees
ms523 0:d4af99baf76f 66 // flags[0] = blocking (only returns when goal reached
ms523 0:d4af99baf76f 67 // flags[1] = register. Requires broadcast trigger to activate
ms523 0:d4af99baf76f 68 int SetGoal(int degrees, int flags = 0);
ms523 0:d4af99baf76f 69
ms523 0:d4af99baf76f 70 // Mode = 0, positional
ms523 0:d4af99baf76f 71 // Mode = 1, continuous rotation
ms523 0:d4af99baf76f 72 int SetMode(int mode);
ms523 0:d4af99baf76f 73
ms523 0:d4af99baf76f 74 // Speed is -1.0 (CCW) to 1.0 (CW)
ms523 0:d4af99baf76f 75 int SetCRSpeed(float speed);
ms523 0:d4af99baf76f 76
ms523 0:d4af99baf76f 77 // set these in degrees, CCW limit is 300
ms523 0:d4af99baf76f 78 // If both are set to zero, we are continuous rotation
ms523 0:d4af99baf76f 79 int SetCWLimit(int degrees);
ms523 0:d4af99baf76f 80 int SetCCWLimit(int degrees);
ms523 0:d4af99baf76f 81
ms523 0:d4af99baf76f 82 // Change the ID
ms523 0:d4af99baf76f 83 int SetID(int CurrentID, int NewID);
ms523 0:d4af99baf76f 84
ms523 0:d4af99baf76f 85 // returns true if the motor is in motion
ms523 0:d4af99baf76f 86 int isMoving(void);
ms523 0:d4af99baf76f 87
ms523 0:d4af99baf76f 88 // broadcast to trigger registered goals
ms523 0:d4af99baf76f 89 void trigger(void);
ms523 0:d4af99baf76f 90
ms523 0:d4af99baf76f 91 // Get current angle, only valid 0-300
ms523 0:d4af99baf76f 92 float GetPosition();
ms523 0:d4af99baf76f 93
ms523 0:d4af99baf76f 94 // In volts and ^C
ms523 0:d4af99baf76f 95 float GetTemp(void);
ms523 0:d4af99baf76f 96 float GetVolts(void);
ms523 0:d4af99baf76f 97
ms523 0:d4af99baf76f 98 private :
ms523 0:d4af99baf76f 99 Serial _ax12;
ms523 0:d4af99baf76f 100 DigitalOut _dir;
ms523 0:d4af99baf76f 101 int _ID;
ms523 0:d4af99baf76f 102
ms523 0:d4af99baf76f 103 };
ms523 0:d4af99baf76f 104
ms523 0:d4af99baf76f 105 #endif