Fork from Dynamixel AX12 Servo for MX64 use and not-finishi now

Dependents:   2014-Mx64 2014-mx64-test

Fork of AX12 by Chris Styles

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MX64.h Source File

MX64.h

00001 /* mbed MX-64 Servo Library
00002  *
00003  * Copyright (c) 2010, cstyles (http://mbed.org)
00004  *
00005  * Permission is hereby granted, free of charge, to any person obtaining a copy
00006  * of this software and associated documentation files (the "Software"), to deal
00007  * in the Software without restriction, including without limitation the rights
00008  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009  * copies of the Software, and to permit persons to whom the Software is
00010  * furnished to do so, subject to the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be included in
00013  * all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021  * THE SOFTWARE.
00022  */
00023 
00024 #ifndef MBED_MX64_H
00025 #define MBED_MX64_H
00026 
00027 #include "mbed.h"
00028 #include "SerialHalfDuplex.h"
00029 
00030 //#define MX64_WRITE_DEBUG 0
00031 //#define MX64_READ_DEBUG 0
00032 //#define MX64_TRIGGER_DEBUG 0
00033 //#define MX64_DEBUG 0
00034 
00035 #define MX64_REG_ID 0x3
00036 #define MX64_REG_BAUD 0x4
00037 #define MX64_REG_CW_LIMIT 0x06
00038 #define MX64_REG_CCW_LIMIT 0x08
00039 #define MX64_REG_GOAL_POSITION 0x1E
00040 #define MX64_REG_MOVING_SPEED 0x20
00041 #define MX64_REG_VOLTS 0x2A
00042 #define MX64_REG_TEMP 0x2B
00043 #define MX64_REG_MOVING 0x2E
00044 #define MX64_REG_POSITION 0x24
00045 
00046 #define MX64_MODE_POSITION  0
00047 #define MX64_MODE_ROTATION  1
00048 
00049 #define MX64_CW 1
00050 #define MX64_CCW 0
00051 
00052 /** Servo control class, based on a PwmOut
00053  *
00054  * Example:
00055  * @code
00056  * #include "mbed.h"
00057  * #include "MX64.h"
00058  * 
00059  * int main() {
00060  * 
00061  *   //MX64 mymx64 (p9, p10, 1); //for LPC1768
00062  *   MX64 mymx64 (PTD2, PTD3, 1); //for KL25Z
00063  *
00064  *   while (1) {
00065  *       mymx64.SetGoal(0);    // go to 0 degrees
00066  *       wait (2.0);
00067  *       mymx64.SetGoal(360);  // go to 360 degrees
00068  *       wait (2.0);
00069  *   }
00070  * }
00071  * @endcode
00072  */
00073 class MX64 {
00074 
00075 public:
00076 
00077     /** Create an MX64 servo object connected to the specified serial port, with the specified ID
00078      *
00079      * @param pin tx pin
00080      * @param pin rx pin 
00081      * @param int ID, the Bus ID of the servo 1-255 
00082      */
00083     MX64(PinName tx, PinName rx, int ID, int baud=1000000);
00084 
00085     /** Set the mode of the servo
00086      * @param mode
00087      *    0 = Positional, default
00088      *    1 = Continuous rotation
00089      */
00090     int SetMode(int mode);
00091 
00092     /** Set baud rate of all attached servos
00093      * @param mode
00094      *    0x01 = 1,000,000 bps
00095      *    0x03 =   500,000 bps
00096      *    0x04 =   400,000 bps
00097      *    0x07 =   250,000 bps
00098      *    0x09 =   200,000 bps
00099      *    0x10 =   115,200 bps
00100      *    0x22 =    57,600 bps
00101      *    0x67 =    19,200 bps
00102      *    0xCF =     9,600 bp
00103      */
00104     int SetBaud(int baud);
00105 
00106 
00107     /** Set goal angle in integer degrees, in positional mode
00108      *
00109      * @param degrees 0-300
00110      * @param flags, defaults to 0
00111      *    flags[0] = blocking, return when goal position reached 
00112      *    flags[1] = register, activate with a broadcast trigger
00113      *
00114      */
00115     int SetGoal(int degrees, int flags = 0);
00116 
00117     /** Set goal angle in integer degrees, in positional mode
00118      *
00119      * @param degrees 0-300
00120      * @param speed 0-100
00121      * @param flags, defaults to 0
00122      *    flags[0] = blocking, return when goal position reached 
00123      *    flags[1] = register, activate with a broadcast trigger
00124      *
00125      */
00126     int SetGoalSpeed(int degrees, int speed, int flags = 0);
00127     
00128 
00129     /** Set the speed of the servo in continuous rotation mode
00130      *
00131      * @param speed, -1.0 to 1.0
00132      *   -1.0 = full speed counter clock wise
00133      *    1.0 = full speed clock wise
00134      */
00135     int SetCRSpeed(float speed);
00136 
00137 
00138     /** Set the clockwise limit of the servo
00139      *
00140      * @param degrees, 0-360
00141      */
00142     int SetCWLimit(int degrees);
00143     
00144     /** Set the counter-clockwise limit of the servo
00145      *
00146      * @param degrees, 0-360
00147      */
00148     int SetCCWLimit(int degrees);
00149 
00150     // Change the ID
00151 
00152     /** Change the ID of a servo
00153      *
00154      * @param CurentID 1-255
00155      * @param NewID 1-255
00156      *
00157      * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID.
00158      * In this situation, only one servo should be connected to the bus
00159      */
00160     int SetID(int CurrentID, int NewID);
00161 
00162 
00163     /** Poll to see if the servo is moving
00164      *
00165      * @returns true is the servo is moving
00166      */
00167     int isMoving(void);
00168 
00169     /** Send the broadcast "trigger" command, to activate any outstanding registered commands
00170      */
00171     void trigger(void);
00172 
00173     /** Read the current angle of the servo
00174      *
00175      * @returns float in the range 0.0-300.0
00176      */
00177     float GetPosition();
00178 
00179     /** Read the temperature of the servo
00180      *
00181      * @returns float temperature 
00182      */
00183     float GetTemp(void);
00184 
00185     /** Read the supply voltage of the servo
00186      *
00187      * @returns float voltage
00188      */
00189     float GetVolts(void);
00190 
00191     int read(int ID, int start, int length, char* data);
00192     int write(int ID, int start, int length, char* data, int flag=0);
00193 
00194 private :
00195 
00196     SerialHalfDuplex _mx64;
00197     int _ID;
00198     int _baud;
00199 
00200 
00201 };
00202 
00203 #endif