Dyanamixel
Dependents: YOZAKURA_ARM YOZAKURA_ARM_USB YOZAKURA_ARM_USB_Keyboard YOZAKURA_ARM_Keyboard0424 ... more
MX28.h@5:950c4f4f73c6, 2015-04-21 (annotated)
- Committer:
- yusuke_kyo
- Date:
- Tue Apr 21 16:08:24 2015 +0000
- Revision:
- 5:950c4f4f73c6
- Parent:
- 3:97fc13b4168c
- Child:
- 6:5ce46b6748fd
Dyna_SetGoal, Dyna_GetPosition
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yusuke_kyo | 0:10c9fb86002f | 1 | /* mbed AX-12+ Servo Library |
yusuke_kyo | 0:10c9fb86002f | 2 | * |
yusuke_kyo | 0:10c9fb86002f | 3 | * Copyright (c) 2010, cstyles (http://mbed.org) |
yusuke_kyo | 0:10c9fb86002f | 4 | * |
yusuke_kyo | 0:10c9fb86002f | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
yusuke_kyo | 0:10c9fb86002f | 6 | * of this software and associated documentation files (the "Software"), to deal |
yusuke_kyo | 0:10c9fb86002f | 7 | * in the Software without restriction, including without limitation the rights |
yusuke_kyo | 0:10c9fb86002f | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yusuke_kyo | 0:10c9fb86002f | 9 | * copies of the Software, and to permit persons to whom the Software is |
yusuke_kyo | 0:10c9fb86002f | 10 | * furnished to do so, subject to the following conditions: |
yusuke_kyo | 0:10c9fb86002f | 11 | * |
yusuke_kyo | 0:10c9fb86002f | 12 | * The above copyright notice and this permission notice shall be included in |
yusuke_kyo | 0:10c9fb86002f | 13 | * all copies or substantial portions of the Software. |
yusuke_kyo | 0:10c9fb86002f | 14 | * |
yusuke_kyo | 0:10c9fb86002f | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yusuke_kyo | 0:10c9fb86002f | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yusuke_kyo | 0:10c9fb86002f | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yusuke_kyo | 0:10c9fb86002f | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yusuke_kyo | 0:10c9fb86002f | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yusuke_kyo | 0:10c9fb86002f | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yusuke_kyo | 0:10c9fb86002f | 21 | * THE SOFTWARE. |
yusuke_kyo | 0:10c9fb86002f | 22 | */ |
yusuke_kyo | 0:10c9fb86002f | 23 | |
yusuke_kyo | 0:10c9fb86002f | 24 | #ifndef MBED_MX28_H |
yusuke_kyo | 0:10c9fb86002f | 25 | #define MBED_MX28_H |
yusuke_kyo | 0:10c9fb86002f | 26 | |
yusuke_kyo | 0:10c9fb86002f | 27 | #include "mbed.h" |
yusuke_kyo | 3:97fc13b4168c | 28 | #include "SerialHalfDuplex.h" |
yusuke_kyo | 0:10c9fb86002f | 29 | |
yusuke_kyo | 0:10c9fb86002f | 30 | #define MX28_WRITE_DEBUG 0 |
yusuke_kyo | 0:10c9fb86002f | 31 | #define MX28_READ_DEBUG 0 |
yusuke_kyo | 0:10c9fb86002f | 32 | #define MX28_TRIGGER_DEBUG 0 |
yusuke_kyo | 0:10c9fb86002f | 33 | #define MX28_DEBUG 0 |
yusuke_kyo | 0:10c9fb86002f | 34 | |
yusuke_kyo | 0:10c9fb86002f | 35 | #define MX28_REG_ID 0x3 |
yusuke_kyo | 0:10c9fb86002f | 36 | #define MX28_REG_CW_LIMIT 0x06 |
yusuke_kyo | 0:10c9fb86002f | 37 | #define MX28_REG_CCW_LIMIT 0x08 |
yusuke_kyo | 1:3ac0919c093f | 38 | #define MX28_REG_TORQUE_ENABLE 0x18 |
yusuke_kyo | 0:10c9fb86002f | 39 | #define MX28_REG_GOAL_POSITION 0x1E |
yusuke_kyo | 0:10c9fb86002f | 40 | #define MX28_REG_MOVING_SPEED 0x20 |
yusuke_kyo | 0:10c9fb86002f | 41 | #define MX28_REG_VOLTS 0x2A |
yusuke_kyo | 0:10c9fb86002f | 42 | #define MX28_REG_TEMP 0x2B |
yusuke_kyo | 0:10c9fb86002f | 43 | #define MX28_REG_MOVING 0x2E |
yusuke_kyo | 0:10c9fb86002f | 44 | #define MX28_REG_POSITION 0x24 |
yusuke_kyo | 1:3ac0919c093f | 45 | #define MX28_REG_TORQUE_LIMIT 0x22 |
yusuke_kyo | 0:10c9fb86002f | 46 | #define MX28_REG_CURRENT 0x44 |
yusuke_kyo | 0:10c9fb86002f | 47 | |
yusuke_kyo | 0:10c9fb86002f | 48 | #define MX28_MODE_POSITION 0 |
yusuke_kyo | 0:10c9fb86002f | 49 | #define MX28_MODE_ROTATION 1 |
yusuke_kyo | 0:10c9fb86002f | 50 | |
yusuke_kyo | 0:10c9fb86002f | 51 | #define MX28_CW 1 |
yusuke_kyo | 0:10c9fb86002f | 52 | #define MX28_CCW 0 |
yusuke_kyo | 0:10c9fb86002f | 53 | |
yusuke_kyo | 1:3ac0919c093f | 54 | class MX28 |
yusuke_kyo | 1:3ac0919c093f | 55 | { |
yusuke_kyo | 0:10c9fb86002f | 56 | |
yusuke_kyo | 0:10c9fb86002f | 57 | public: |
yusuke_kyo | 0:10c9fb86002f | 58 | |
yusuke_kyo | 0:10c9fb86002f | 59 | /** Create an MX28 servo object connected to the specified serial port, with the specified ID |
yusuke_kyo | 0:10c9fb86002f | 60 | * |
yusuke_kyo | 0:10c9fb86002f | 61 | * @param pin tx pin |
yusuke_kyo | 1:3ac0919c093f | 62 | * @param pin rx pin |
yusuke_kyo | 1:3ac0919c093f | 63 | * @param int ID, the Bus ID of the servo 1-255 |
yusuke_kyo | 0:10c9fb86002f | 64 | */ |
yusuke_kyo | 5:950c4f4f73c6 | 65 | MX28(PinName tx, PinName rx, int ID, int baudrate); |
yusuke_kyo | 0:10c9fb86002f | 66 | |
yusuke_kyo | 0:10c9fb86002f | 67 | /** Set the mode of the servo |
yusuke_kyo | 0:10c9fb86002f | 68 | * @param mode |
yusuke_kyo | 0:10c9fb86002f | 69 | * 0 = Positional, default |
yusuke_kyo | 0:10c9fb86002f | 70 | * 1 = Continuous rotation |
yusuke_kyo | 0:10c9fb86002f | 71 | */ |
yusuke_kyo | 0:10c9fb86002f | 72 | int SetMode(int mode); |
yusuke_kyo | 0:10c9fb86002f | 73 | |
yusuke_kyo | 0:10c9fb86002f | 74 | /** Set goal angle in integer degrees, in positional mode |
yusuke_kyo | 0:10c9fb86002f | 75 | * |
yusuke_kyo | 0:10c9fb86002f | 76 | * @param degrees 0-360 |
yusuke_kyo | 0:10c9fb86002f | 77 | * @param flags, defaults to 0 |
yusuke_kyo | 1:3ac0919c093f | 78 | * flags[0] = blocking, return when goal position reached |
yusuke_kyo | 0:10c9fb86002f | 79 | * flags[1] = register, activate with a broadcast trigger |
yusuke_kyo | 0:10c9fb86002f | 80 | * |
yusuke_kyo | 0:10c9fb86002f | 81 | */ |
yusuke_kyo | 0:10c9fb86002f | 82 | int SetGoal(int degrees, int flags = 0); |
yusuke_kyo | 0:10c9fb86002f | 83 | |
yusuke_kyo | 0:10c9fb86002f | 84 | /** Set the speed of the servo in continuous rotation mode |
yusuke_kyo | 0:10c9fb86002f | 85 | * |
yusuke_kyo | 0:10c9fb86002f | 86 | * @param speed, -1.0 to 1.0 |
yusuke_kyo | 0:10c9fb86002f | 87 | * -1.0 = full speed counter clock wise |
yusuke_kyo | 0:10c9fb86002f | 88 | * 1.0 = full speed clock wise |
yusuke_kyo | 0:10c9fb86002f | 89 | */ |
yusuke_kyo | 0:10c9fb86002f | 90 | int SetCRSpeed(float speed); |
yusuke_kyo | 0:10c9fb86002f | 91 | |
yusuke_kyo | 0:10c9fb86002f | 92 | |
yusuke_kyo | 0:10c9fb86002f | 93 | /** Set the clockwise limit of the servo |
yusuke_kyo | 0:10c9fb86002f | 94 | * |
yusuke_kyo | 0:10c9fb86002f | 95 | * @param degrees, 0-360 |
yusuke_kyo | 0:10c9fb86002f | 96 | */ |
yusuke_kyo | 0:10c9fb86002f | 97 | int SetCWLimit(int degrees); |
yusuke_kyo | 1:3ac0919c093f | 98 | |
yusuke_kyo | 0:10c9fb86002f | 99 | /** Set the counter-clockwise limit of the servo |
yusuke_kyo | 0:10c9fb86002f | 100 | * |
yusuke_kyo | 0:10c9fb86002f | 101 | * @param degrees, 0-360 |
yusuke_kyo | 0:10c9fb86002f | 102 | */ |
yusuke_kyo | 0:10c9fb86002f | 103 | int SetCCWLimit(int degrees); |
yusuke_kyo | 0:10c9fb86002f | 104 | |
yusuke_kyo | 0:10c9fb86002f | 105 | // Change the ID |
yusuke_kyo | 0:10c9fb86002f | 106 | |
yusuke_kyo | 0:10c9fb86002f | 107 | /** Change the ID of a servo |
yusuke_kyo | 0:10c9fb86002f | 108 | * |
yusuke_kyo | 0:10c9fb86002f | 109 | * @param CurentID 1-255 |
yusuke_kyo | 0:10c9fb86002f | 110 | * @param NewID 1-255 |
yusuke_kyo | 0:10c9fb86002f | 111 | * |
yusuke_kyo | 0:10c9fb86002f | 112 | * If a servo ID is not know, the broadcast address of 0 can be used for CurrentID. |
yusuke_kyo | 0:10c9fb86002f | 113 | * In this situation, only one servo should be connected to the bus |
yusuke_kyo | 0:10c9fb86002f | 114 | */ |
yusuke_kyo | 0:10c9fb86002f | 115 | int SetID(int CurrentID, int NewID); |
yusuke_kyo | 0:10c9fb86002f | 116 | |
yusuke_kyo | 0:10c9fb86002f | 117 | |
yusuke_kyo | 0:10c9fb86002f | 118 | /** Poll to see if the servo is moving |
yusuke_kyo | 0:10c9fb86002f | 119 | * |
yusuke_kyo | 0:10c9fb86002f | 120 | * @returns true is the servo is moving |
yusuke_kyo | 0:10c9fb86002f | 121 | */ |
yusuke_kyo | 0:10c9fb86002f | 122 | int isMoving(void); |
yusuke_kyo | 0:10c9fb86002f | 123 | |
yusuke_kyo | 0:10c9fb86002f | 124 | /** Send the broadcast "trigger" command, to activate any outstanding registered commands |
yusuke_kyo | 0:10c9fb86002f | 125 | */ |
yusuke_kyo | 0:10c9fb86002f | 126 | void trigger(void); |
yusuke_kyo | 0:10c9fb86002f | 127 | |
yusuke_kyo | 0:10c9fb86002f | 128 | /** Read the current angle of the servo |
yusuke_kyo | 0:10c9fb86002f | 129 | * |
yusuke_kyo | 0:10c9fb86002f | 130 | * @returns float in the range 0.0-360.0 |
yusuke_kyo | 0:10c9fb86002f | 131 | */ |
yusuke_kyo | 0:10c9fb86002f | 132 | float GetPosition(); |
yusuke_kyo | 0:10c9fb86002f | 133 | |
yusuke_kyo | 0:10c9fb86002f | 134 | /** Read the temperature of the servo |
yusuke_kyo | 0:10c9fb86002f | 135 | * |
yusuke_kyo | 1:3ac0919c093f | 136 | * @returns float temperature |
yusuke_kyo | 0:10c9fb86002f | 137 | */ |
yusuke_kyo | 0:10c9fb86002f | 138 | float GetTemp(void); |
yusuke_kyo | 0:10c9fb86002f | 139 | |
yusuke_kyo | 0:10c9fb86002f | 140 | /** Read the supply voltage of the servo |
yusuke_kyo | 0:10c9fb86002f | 141 | * |
yusuke_kyo | 0:10c9fb86002f | 142 | * @returns float voltage |
yusuke_kyo | 0:10c9fb86002f | 143 | */ |
yusuke_kyo | 0:10c9fb86002f | 144 | float GetVolts(void); |
yusuke_kyo | 1:3ac0919c093f | 145 | |
yusuke_kyo | 0:10c9fb86002f | 146 | /** Read the supply current of the servo |
yusuke_kyo | 0:10c9fb86002f | 147 | * |
yusuke_kyo | 0:10c9fb86002f | 148 | * @returns float current |
yusuke_kyo | 0:10c9fb86002f | 149 | */ |
yusuke_kyo | 0:10c9fb86002f | 150 | float GetCurrent(void); |
yusuke_kyo | 0:10c9fb86002f | 151 | |
yusuke_kyo | 1:3ac0919c093f | 152 | int TorqueEnable(int mode); |
yusuke_kyo | 1:3ac0919c093f | 153 | |
yusuke_kyo | 1:3ac0919c093f | 154 | int SetTorqueLimit(float torque_limit); |
yusuke_kyo | 1:3ac0919c093f | 155 | |
yusuke_kyo | 0:10c9fb86002f | 156 | private : |
yusuke_kyo | 0:10c9fb86002f | 157 | |
yusuke_kyo | 3:97fc13b4168c | 158 | SerialHalfDuplex _mx28; |
yusuke_kyo | 3:97fc13b4168c | 159 | // Serial _mx28; |
yusuke_kyo | 0:10c9fb86002f | 160 | int _ID; |
yusuke_kyo | 0:10c9fb86002f | 161 | |
yusuke_kyo | 0:10c9fb86002f | 162 | int read(int ID, int start, int length, char* data); |
yusuke_kyo | 0:10c9fb86002f | 163 | int write(int ID, int start, int length, char* data, int flag=0); |
yusuke_kyo | 0:10c9fb86002f | 164 | |
yusuke_kyo | 0:10c9fb86002f | 165 | }; |
yusuke_kyo | 0:10c9fb86002f | 166 | |
yusuke_kyo | 0:10c9fb86002f | 167 | #endif |