User | Revision | Line number | New contents of line |
akichika |
0:db8f063d50f0
|
1
|
/* mbed AX-12+ Servo Library
|
akichika |
0:db8f063d50f0
|
2
|
*
|
akichika |
0:db8f063d50f0
|
3
|
* Copyright (c) 2010, cstyles (http://mbed.org)
|
akichika |
0:db8f063d50f0
|
4
|
*
|
akichika |
0:db8f063d50f0
|
5
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
akichika |
0:db8f063d50f0
|
6
|
* of this software and associated documentation files (the "Software"), to deal
|
akichika |
0:db8f063d50f0
|
7
|
* in the Software without restriction, including without limitation the rights
|
akichika |
0:db8f063d50f0
|
8
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
akichika |
0:db8f063d50f0
|
9
|
* copies of the Software, and to permit persons to whom the Software is
|
akichika |
0:db8f063d50f0
|
10
|
* furnished to do so, subject to the following conditions:
|
akichika |
0:db8f063d50f0
|
11
|
*
|
akichika |
0:db8f063d50f0
|
12
|
* The above copyright notice and this permission notice shall be included in
|
akichika |
0:db8f063d50f0
|
13
|
* all copies or substantial portions of the Software.
|
akichika |
0:db8f063d50f0
|
14
|
*
|
akichika |
0:db8f063d50f0
|
15
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
akichika |
0:db8f063d50f0
|
16
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
akichika |
0:db8f063d50f0
|
17
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
akichika |
0:db8f063d50f0
|
18
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
akichika |
0:db8f063d50f0
|
19
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
akichika |
0:db8f063d50f0
|
20
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
akichika |
0:db8f063d50f0
|
21
|
* THE SOFTWARE.
|
akichika |
0:db8f063d50f0
|
22
|
*/
|
akichika |
0:db8f063d50f0
|
23
|
|
akichika |
0:db8f063d50f0
|
24
|
#ifndef MBED_AX12_H
|
akichika |
0:db8f063d50f0
|
25
|
#define MBED_AX12_H
|
akichika |
0:db8f063d50f0
|
26
|
|
akichika |
0:db8f063d50f0
|
27
|
#include "mbed.h"
|
akichika |
0:db8f063d50f0
|
28
|
|
akichika |
0:db8f063d50f0
|
29
|
//#define AX12_WRITE_DEBUG 0
|
akichika |
0:db8f063d50f0
|
30
|
//#define AX12_READ_DEBUG 0
|
akichika |
0:db8f063d50f0
|
31
|
//#define AX12_TRIGGER_DEBUG 0
|
akichika |
0:db8f063d50f0
|
32
|
//#define AX12_DEBUG 0
|
akichika |
0:db8f063d50f0
|
33
|
|
akichika |
0:db8f063d50f0
|
34
|
#define AX12_REG_ID 0x3
|
akichika |
0:db8f063d50f0
|
35
|
#define AX12_REG_BAUD 0x4
|
akichika |
0:db8f063d50f0
|
36
|
#define AX12_REG_CW_LIMIT 0x06
|
akichika |
0:db8f063d50f0
|
37
|
#define AX12_REG_CCW_LIMIT 0x08
|
akichika |
0:db8f063d50f0
|
38
|
#define AX12_REG_GOAL_POSITION 0x1E
|
akichika |
0:db8f063d50f0
|
39
|
#define AX12_REG_TORQUE_ENABLE 0x18
|
akichika |
0:db8f063d50f0
|
40
|
#define AX12_REG_MOVING_SPEED 0x20
|
akichika |
0:db8f063d50f0
|
41
|
#define AX12_REG_VOLTS 0x2A
|
akichika |
0:db8f063d50f0
|
42
|
#define AX12_REG_TEMP 0x2B
|
akichika |
0:db8f063d50f0
|
43
|
#define AX12_REG_MOVING 0x2E
|
akichika |
0:db8f063d50f0
|
44
|
#define AX12_REG_POSITION 0x24
|
akichika |
0:db8f063d50f0
|
45
|
|
akichika |
0:db8f063d50f0
|
46
|
#define AX12_MODE_POSITION 0
|
akichika |
0:db8f063d50f0
|
47
|
#define AX12_MODE_ROTATION 1
|
akichika |
0:db8f063d50f0
|
48
|
|
akichika |
0:db8f063d50f0
|
49
|
#define AX12_CW 1
|
akichika |
0:db8f063d50f0
|
50
|
#define AX12_CCW 0
|
akichika |
0:db8f063d50f0
|
51
|
|
akichika |
0:db8f063d50f0
|
52
|
/** Servo control class, based on a PwmOut
|
akichika |
0:db8f063d50f0
|
53
|
*
|
akichika |
0:db8f063d50f0
|
54
|
* Example:
|
akichika |
0:db8f063d50f0
|
55
|
* @code
|
akichika |
0:db8f063d50f0
|
56
|
* #include "mbed.h"
|
akichika |
0:db8f063d50f0
|
57
|
* #include "AX12.h"
|
akichika |
0:db8f063d50f0
|
58
|
*
|
akichika |
0:db8f063d50f0
|
59
|
* int main() {
|
akichika |
0:db8f063d50f0
|
60
|
*
|
akichika |
0:db8f063d50f0
|
61
|
* AX12 myax12 (p9, p10, 1);
|
akichika |
0:db8f063d50f0
|
62
|
*
|
akichika |
0:db8f063d50f0
|
63
|
* while (1) {
|
akichika |
0:db8f063d50f0
|
64
|
* myax12.SetGoal(0); // go to 0 degrees
|
akichika |
0:db8f063d50f0
|
65
|
* wait (2.0);
|
akichika |
0:db8f063d50f0
|
66
|
* myax12.SetGoal(300); // go to 300 degrees
|
akichika |
0:db8f063d50f0
|
67
|
* wait (2.0);
|
akichika |
0:db8f063d50f0
|
68
|
* }
|
akichika |
0:db8f063d50f0
|
69
|
* }
|
akichika |
0:db8f063d50f0
|
70
|
* @endcode
|
akichika |
0:db8f063d50f0
|
71
|
*/
|
akichika |
0:db8f063d50f0
|
72
|
class AX12 {
|
akichika |
0:db8f063d50f0
|
73
|
|
akichika |
0:db8f063d50f0
|
74
|
public:
|
akichika |
0:db8f063d50f0
|
75
|
|
akichika |
0:db8f063d50f0
|
76
|
/** Create an AX12 servo object connected to the specified serial port, with the specified ID
|
akichika |
0:db8f063d50f0
|
77
|
*
|
akichika |
0:db8f063d50f0
|
78
|
* @param pin tx pin
|
akichika |
0:db8f063d50f0
|
79
|
* @param pin rx pin
|
akichika |
0:db8f063d50f0
|
80
|
* @param int ID, the Bus ID of the servo 1-255
|
akichika |
0:db8f063d50f0
|
81
|
*/
|
akichika |
0:db8f063d50f0
|
82
|
AX12(PinName tx, PinName rx, int ID, int baud=1000000);
|
akichika |
0:db8f063d50f0
|
83
|
|
akichika |
0:db8f063d50f0
|
84
|
/** Set the mode of the servo
|
akichika |
0:db8f063d50f0
|
85
|
* @param mode
|
akichika |
0:db8f063d50f0
|
86
|
* 0 = Positional, default
|
akichika |
0:db8f063d50f0
|
87
|
* 1 = Continuous rotation
|
akichika |
0:db8f063d50f0
|
88
|
*/
|
akichika |
0:db8f063d50f0
|
89
|
int SetMode(int mode);
|
akichika |
0:db8f063d50f0
|
90
|
|
akichika |
0:db8f063d50f0
|
91
|
/** Set baud rate of all attached servos
|
akichika |
0:db8f063d50f0
|
92
|
* @param mode
|
akichika |
0:db8f063d50f0
|
93
|
* 0x01 = 1,000,000 bps
|
akichika |
0:db8f063d50f0
|
94
|
* 0x03 = 500,000 bps
|
akichika |
0:db8f063d50f0
|
95
|
* 0x04 = 400,000 bps
|
akichika |
0:db8f063d50f0
|
96
|
* 0x07 = 250,000 bps
|
akichika |
0:db8f063d50f0
|
97
|
* 0x09 = 200,000 bps
|
akichika |
0:db8f063d50f0
|
98
|
* 0x10 = 115,200 bps
|
akichika |
0:db8f063d50f0
|
99
|
* 0x22 = 57,600 bps
|
akichika |
0:db8f063d50f0
|
100
|
* 0x67 = 19,200 bps
|
akichika |
0:db8f063d50f0
|
101
|
* 0xCF = 9,600 bp
|
akichika |
0:db8f063d50f0
|
102
|
*/
|
akichika |
0:db8f063d50f0
|
103
|
int SetBaud(int baud);
|
akichika |
0:db8f063d50f0
|
104
|
|
akichika |
0:db8f063d50f0
|
105
|
|
akichika |
0:db8f063d50f0
|
106
|
/** Set goal angle in integer degrees, in positional mode
|
akichika |
0:db8f063d50f0
|
107
|
*
|
akichika |
0:db8f063d50f0
|
108
|
* @param degrees 0-300
|
akichika |
0:db8f063d50f0
|
109
|
* @param flags, defaults to 0
|
akichika |
0:db8f063d50f0
|
110
|
* flags[0] = blocking, return when goal position reached
|
akichika |
0:db8f063d50f0
|
111
|
* flags[1] = register, activate with a broadcast trigger
|
akichika |
0:db8f063d50f0
|
112
|
*
|
akichika |
0:db8f063d50f0
|
113
|
*/
|
akichika |
0:db8f063d50f0
|
114
|
int SetGoal(int degrees, int flags = 0);
|
akichika |
0:db8f063d50f0
|
115
|
|
akichika |
0:db8f063d50f0
|
116
|
|
akichika |
0:db8f063d50f0
|
117
|
/** Set the speed of the servo in continuous rotation mode
|
akichika |
0:db8f063d50f0
|
118
|
*
|
akichika |
0:db8f063d50f0
|
119
|
* @param speed, -1.0 to 1.0
|
akichika |
0:db8f063d50f0
|
120
|
* -1.0 = full speed counter clock wise
|
akichika |
0:db8f063d50f0
|
121
|
* 1.0 = full speed clock wise
|
akichika |
0:db8f063d50f0
|
122
|
*/
|
akichika |
0:db8f063d50f0
|
123
|
int SetCRSpeed(float speed);
|
akichika |
0:db8f063d50f0
|
124
|
|
akichika |
0:db8f063d50f0
|
125
|
|
akichika |
0:db8f063d50f0
|
126
|
/** Set the clockwise limit of the servo
|
akichika |
0:db8f063d50f0
|
127
|
*
|
akichika |
0:db8f063d50f0
|
128
|
* @param degrees, 0-300
|
akichika |
0:db8f063d50f0
|
129
|
*/
|
akichika |
0:db8f063d50f0
|
130
|
int SetCWLimit(int degrees);
|
akichika |
0:db8f063d50f0
|
131
|
|
akichika |
0:db8f063d50f0
|
132
|
/** Set the counter-clockwise limit of the servo
|
akichika |
0:db8f063d50f0
|
133
|
*
|
akichika |
0:db8f063d50f0
|
134
|
* @param degrees, 0-300
|
akichika |
0:db8f063d50f0
|
135
|
*/
|
akichika |
0:db8f063d50f0
|
136
|
int SetCCWLimit(int degrees);
|
akichika |
0:db8f063d50f0
|
137
|
|
akichika |
0:db8f063d50f0
|
138
|
// Change the ID
|
akichika |
0:db8f063d50f0
|
139
|
|
akichika |
0:db8f063d50f0
|
140
|
/** Change the ID of a servo
|
akichika |
0:db8f063d50f0
|
141
|
*
|
akichika |
0:db8f063d50f0
|
142
|
* @param CurentID 1-255
|
akichika |
0:db8f063d50f0
|
143
|
* @param NewID 1-255
|
akichika |
0:db8f063d50f0
|
144
|
*
|
akichika |
0:db8f063d50f0
|
145
|
* If a servo ID is not know, the broadcast address of 0 can be used for CurrentID.
|
akichika |
0:db8f063d50f0
|
146
|
* In this situation, only one servo should be connected to the bus
|
akichika |
0:db8f063d50f0
|
147
|
*/
|
akichika |
0:db8f063d50f0
|
148
|
int SetID(int CurrentID, int NewID);
|
akichika |
0:db8f063d50f0
|
149
|
|
akichika |
0:db8f063d50f0
|
150
|
/** Set to enable the torque of the servo
|
akichika |
0:db8f063d50f0
|
151
|
*
|
akichika |
0:db8f063d50f0
|
152
|
* @param enable, true|false
|
akichika |
0:db8f063d50f0
|
153
|
*/
|
akichika |
0:db8f063d50f0
|
154
|
int SetTorqueEnable(bool enable);
|
akichika |
0:db8f063d50f0
|
155
|
|
akichika |
0:db8f063d50f0
|
156
|
/** Poll to see if the servo is moving
|
akichika |
0:db8f063d50f0
|
157
|
*
|
akichika |
0:db8f063d50f0
|
158
|
* @returns true is the servo is moving
|
akichika |
0:db8f063d50f0
|
159
|
*/
|
akichika |
0:db8f063d50f0
|
160
|
int isMoving(void);
|
akichika |
0:db8f063d50f0
|
161
|
|
akichika |
0:db8f063d50f0
|
162
|
/** Send the broadcast "trigger" command, to activate any outstanding registered commands
|
akichika |
0:db8f063d50f0
|
163
|
*/
|
akichika |
0:db8f063d50f0
|
164
|
void trigger(void);
|
akichika |
0:db8f063d50f0
|
165
|
|
akichika |
0:db8f063d50f0
|
166
|
/** Read the current angle of the servo
|
akichika |
0:db8f063d50f0
|
167
|
*
|
akichika |
0:db8f063d50f0
|
168
|
* @returns float in the range 0.0-300.0
|
akichika |
0:db8f063d50f0
|
169
|
*/
|
akichika |
0:db8f063d50f0
|
170
|
float GetPosition();
|
akichika |
0:db8f063d50f0
|
171
|
|
akichika |
0:db8f063d50f0
|
172
|
/** Read the temperature of the servo
|
akichika |
0:db8f063d50f0
|
173
|
*
|
akichika |
0:db8f063d50f0
|
174
|
* @returns float temperature
|
akichika |
0:db8f063d50f0
|
175
|
*/
|
akichika |
0:db8f063d50f0
|
176
|
float GetTemp(void);
|
akichika |
0:db8f063d50f0
|
177
|
|
akichika |
0:db8f063d50f0
|
178
|
/** Read the supply voltage of the servo
|
akichika |
0:db8f063d50f0
|
179
|
*
|
akichika |
0:db8f063d50f0
|
180
|
* @returns float voltage
|
akichika |
0:db8f063d50f0
|
181
|
*/
|
akichika |
0:db8f063d50f0
|
182
|
float GetVolts(void);
|
akichika |
0:db8f063d50f0
|
183
|
|
akichika |
0:db8f063d50f0
|
184
|
int read(int ID, int start, int length, char* data);
|
akichika |
0:db8f063d50f0
|
185
|
int write(int ID, int start, int length, char* data, int flag=0);
|
akichika |
0:db8f063d50f0
|
186
|
|
akichika |
0:db8f063d50f0
|
187
|
private :
|
akichika |
0:db8f063d50f0
|
188
|
|
akichika |
0:db8f063d50f0
|
189
|
SerialHalfDuplex _ax12;
|
akichika |
0:db8f063d50f0
|
190
|
int _ID;
|
akichika |
0:db8f063d50f0
|
191
|
int _baud;
|
akichika |
0:db8f063d50f0
|
192
|
|
akichika |
0:db8f063d50f0
|
193
|
|
akichika |
0:db8f063d50f0
|
194
|
};
|
akichika |
0:db8f063d50f0
|
195
|
|
akichika |
0:db8f063d50f0
|
196
|
#endif
|