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
|
#include "AX12.h"
|
akichika |
0:db8f063d50f0
|
25
|
#include "mbed.h"
|
akichika |
0:db8f063d50f0
|
26
|
|
akichika |
0:db8f063d50f0
|
27
|
AX12::AX12(PinName tx, PinName rx, int ID, int baud)
|
akichika |
0:db8f063d50f0
|
28
|
: _ax12(tx,rx) {
|
akichika |
0:db8f063d50f0
|
29
|
_baud = baud;
|
akichika |
0:db8f063d50f0
|
30
|
_ID = ID;
|
akichika |
0:db8f063d50f0
|
31
|
_ax12.baud(_baud);
|
akichika |
0:db8f063d50f0
|
32
|
|
akichika |
0:db8f063d50f0
|
33
|
}
|
akichika |
0:db8f063d50f0
|
34
|
|
akichika |
0:db8f063d50f0
|
35
|
// Set the mode of the servo
|
akichika |
0:db8f063d50f0
|
36
|
// 0 = Positional (0-300 degrees)
|
akichika |
0:db8f063d50f0
|
37
|
// 1 = Rotational -1 to 1 speed
|
akichika |
0:db8f063d50f0
|
38
|
int AX12::SetMode(int mode) {
|
akichika |
0:db8f063d50f0
|
39
|
|
akichika |
0:db8f063d50f0
|
40
|
if (mode == 1) { // set CR
|
akichika |
0:db8f063d50f0
|
41
|
SetCWLimit(0);
|
akichika |
0:db8f063d50f0
|
42
|
SetCCWLimit(0);
|
akichika |
0:db8f063d50f0
|
43
|
SetCRSpeed(0.0);
|
akichika |
0:db8f063d50f0
|
44
|
} else {
|
akichika |
0:db8f063d50f0
|
45
|
SetCWLimit(0);
|
akichika |
0:db8f063d50f0
|
46
|
SetCCWLimit(300);
|
akichika |
0:db8f063d50f0
|
47
|
SetCRSpeed(0.0);
|
akichika |
0:db8f063d50f0
|
48
|
}
|
akichika |
0:db8f063d50f0
|
49
|
return(0);
|
akichika |
0:db8f063d50f0
|
50
|
}
|
akichika |
0:db8f063d50f0
|
51
|
|
akichika |
0:db8f063d50f0
|
52
|
|
akichika |
0:db8f063d50f0
|
53
|
// if flag[0] is set, were blocking
|
akichika |
0:db8f063d50f0
|
54
|
// if flag[1] is set, we're registering
|
akichika |
0:db8f063d50f0
|
55
|
// they are mutually exclusive operations
|
akichika |
0:db8f063d50f0
|
56
|
int AX12::SetGoal(int degrees, int flags) {
|
akichika |
0:db8f063d50f0
|
57
|
|
akichika |
0:db8f063d50f0
|
58
|
char reg_flag = 0;
|
akichika |
0:db8f063d50f0
|
59
|
char data[2];
|
akichika |
0:db8f063d50f0
|
60
|
|
akichika |
0:db8f063d50f0
|
61
|
// set the flag is only the register bit is set in the flag
|
akichika |
0:db8f063d50f0
|
62
|
if (flags == 0x2) {
|
akichika |
0:db8f063d50f0
|
63
|
reg_flag = 1;
|
akichika |
0:db8f063d50f0
|
64
|
}
|
akichika |
0:db8f063d50f0
|
65
|
|
akichika |
0:db8f063d50f0
|
66
|
// 1023 / 300 * degrees
|
akichika |
0:db8f063d50f0
|
67
|
short goal = (1023 * degrees) / 300;
|
akichika |
0:db8f063d50f0
|
68
|
#ifdef AX12_DEBUG
|
akichika |
0:db8f063d50f0
|
69
|
printf("SetGoal to 0x%x\n",goal);
|
akichika |
0:db8f063d50f0
|
70
|
#endif
|
akichika |
0:db8f063d50f0
|
71
|
|
akichika |
0:db8f063d50f0
|
72
|
data[0] = goal & 0xff; // bottom 8 bits
|
akichika |
0:db8f063d50f0
|
73
|
data[1] = goal >> 8; // top 8 bits
|
akichika |
0:db8f063d50f0
|
74
|
|
akichika |
0:db8f063d50f0
|
75
|
// write the packet, return the error code
|
akichika |
0:db8f063d50f0
|
76
|
int rVal = write(_ID, AX12_REG_GOAL_POSITION, 2, data, reg_flag);
|
akichika |
0:db8f063d50f0
|
77
|
|
akichika |
0:db8f063d50f0
|
78
|
if (flags == 1) {
|
akichika |
0:db8f063d50f0
|
79
|
// block until it comes to a halt
|
akichika |
0:db8f063d50f0
|
80
|
while (isMoving()) {}
|
akichika |
0:db8f063d50f0
|
81
|
}
|
akichika |
0:db8f063d50f0
|
82
|
return(rVal);
|
akichika |
0:db8f063d50f0
|
83
|
}
|
akichika |
0:db8f063d50f0
|
84
|
|
akichika |
0:db8f063d50f0
|
85
|
|
akichika |
0:db8f063d50f0
|
86
|
// Set continuous rotation speed from -1 to 1
|
akichika |
0:db8f063d50f0
|
87
|
int AX12::SetCRSpeed(float speed) {
|
akichika |
0:db8f063d50f0
|
88
|
|
akichika |
0:db8f063d50f0
|
89
|
// bit 10 = direction, 0 = CCW, 1=CW
|
akichika |
0:db8f063d50f0
|
90
|
// bits 9-0 = Speed
|
akichika |
0:db8f063d50f0
|
91
|
char data[2];
|
akichika |
0:db8f063d50f0
|
92
|
|
akichika |
0:db8f063d50f0
|
93
|
int goal = (0x3ff * abs(speed));
|
akichika |
0:db8f063d50f0
|
94
|
|
akichika |
0:db8f063d50f0
|
95
|
// Set direction CW if we have a negative speed
|
akichika |
0:db8f063d50f0
|
96
|
if (speed < 0) {
|
akichika |
0:db8f063d50f0
|
97
|
goal |= (0x1 << 10);
|
akichika |
0:db8f063d50f0
|
98
|
}
|
akichika |
0:db8f063d50f0
|
99
|
|
akichika |
0:db8f063d50f0
|
100
|
data[0] = goal & 0xff; // bottom 8 bits
|
akichika |
0:db8f063d50f0
|
101
|
data[1] = goal >> 8; // top 8 bits
|
akichika |
0:db8f063d50f0
|
102
|
|
akichika |
0:db8f063d50f0
|
103
|
// write the packet, return the error code
|
akichika |
0:db8f063d50f0
|
104
|
int rVal = write(_ID, 0x20, 2, data);
|
akichika |
0:db8f063d50f0
|
105
|
|
akichika |
0:db8f063d50f0
|
106
|
return(rVal);
|
akichika |
0:db8f063d50f0
|
107
|
}
|
akichika |
0:db8f063d50f0
|
108
|
|
akichika |
0:db8f063d50f0
|
109
|
|
akichika |
0:db8f063d50f0
|
110
|
int AX12::SetCWLimit (int degrees) {
|
akichika |
0:db8f063d50f0
|
111
|
|
akichika |
0:db8f063d50f0
|
112
|
char data[2];
|
akichika |
0:db8f063d50f0
|
113
|
|
akichika |
0:db8f063d50f0
|
114
|
// 1023 / 300 * degrees
|
akichika |
0:db8f063d50f0
|
115
|
short limit = (1023 * degrees) / 300;
|
akichika |
0:db8f063d50f0
|
116
|
|
akichika |
0:db8f063d50f0
|
117
|
#ifdef AX12_DEBUG
|
akichika |
0:db8f063d50f0
|
118
|
printf("SetCWLimit to 0x%x\n",limit);
|
akichika |
0:db8f063d50f0
|
119
|
#endif
|
akichika |
0:db8f063d50f0
|
120
|
|
akichika |
0:db8f063d50f0
|
121
|
data[0] = limit & 0xff; // bottom 8 bits
|
akichika |
0:db8f063d50f0
|
122
|
data[1] = limit >> 8; // top 8 bits
|
akichika |
0:db8f063d50f0
|
123
|
|
akichika |
0:db8f063d50f0
|
124
|
// write the packet, return the error code
|
akichika |
0:db8f063d50f0
|
125
|
return (write(_ID, AX12_REG_CW_LIMIT, 2, data));
|
akichika |
0:db8f063d50f0
|
126
|
|
akichika |
0:db8f063d50f0
|
127
|
}
|
akichika |
0:db8f063d50f0
|
128
|
|
akichika |
0:db8f063d50f0
|
129
|
int AX12::SetCCWLimit (int degrees) {
|
akichika |
0:db8f063d50f0
|
130
|
|
akichika |
0:db8f063d50f0
|
131
|
char data[2];
|
akichika |
0:db8f063d50f0
|
132
|
|
akichika |
0:db8f063d50f0
|
133
|
// 1023 / 300 * degrees
|
akichika |
0:db8f063d50f0
|
134
|
short limit = (1023 * degrees) / 300;
|
akichika |
0:db8f063d50f0
|
135
|
|
akichika |
0:db8f063d50f0
|
136
|
#ifdef AX12_DEBUG
|
akichika |
0:db8f063d50f0
|
137
|
printf("SetCCWLimit to 0x%x\n",limit);
|
akichika |
0:db8f063d50f0
|
138
|
#endif
|
akichika |
0:db8f063d50f0
|
139
|
|
akichika |
0:db8f063d50f0
|
140
|
data[0] = limit & 0xff; // bottom 8 bits
|
akichika |
0:db8f063d50f0
|
141
|
data[1] = limit >> 8; // top 8 bits
|
akichika |
0:db8f063d50f0
|
142
|
|
akichika |
0:db8f063d50f0
|
143
|
// write the packet, return the error code
|
akichika |
0:db8f063d50f0
|
144
|
return (write(_ID, AX12_REG_CCW_LIMIT, 2, data));
|
akichika |
0:db8f063d50f0
|
145
|
}
|
akichika |
0:db8f063d50f0
|
146
|
|
akichika |
0:db8f063d50f0
|
147
|
|
akichika |
0:db8f063d50f0
|
148
|
int AX12::SetID (int CurrentID, int NewID) {
|
akichika |
0:db8f063d50f0
|
149
|
|
akichika |
0:db8f063d50f0
|
150
|
char data[1];
|
akichika |
0:db8f063d50f0
|
151
|
data[0] = NewID;
|
akichika |
0:db8f063d50f0
|
152
|
|
akichika |
0:db8f063d50f0
|
153
|
#ifdef AX12_DEBUG
|
akichika |
0:db8f063d50f0
|
154
|
printf("Setting ID from 0x%x to 0x%x\n",CurrentID,NewID);
|
akichika |
0:db8f063d50f0
|
155
|
#endif
|
akichika |
0:db8f063d50f0
|
156
|
|
akichika |
0:db8f063d50f0
|
157
|
return (write(CurrentID, AX12_REG_ID, 1, data));
|
akichika |
0:db8f063d50f0
|
158
|
|
akichika |
0:db8f063d50f0
|
159
|
}
|
akichika |
0:db8f063d50f0
|
160
|
|
akichika |
0:db8f063d50f0
|
161
|
|
akichika |
0:db8f063d50f0
|
162
|
int AX12::SetBaud (int baud) {
|
akichika |
0:db8f063d50f0
|
163
|
|
akichika |
0:db8f063d50f0
|
164
|
char data[1];
|
akichika |
0:db8f063d50f0
|
165
|
data[0] = baud;
|
akichika |
0:db8f063d50f0
|
166
|
|
akichika |
0:db8f063d50f0
|
167
|
#ifdef AX12_DEBUG
|
akichika |
0:db8f063d50f0
|
168
|
printf("Setting Baud rate to %d\n",baud);
|
akichika |
0:db8f063d50f0
|
169
|
#endif
|
akichika |
0:db8f063d50f0
|
170
|
|
akichika |
0:db8f063d50f0
|
171
|
return (write(0xFE, AX12_REG_BAUD, 1, data));
|
akichika |
0:db8f063d50f0
|
172
|
|
akichika |
0:db8f063d50f0
|
173
|
}
|
akichika |
0:db8f063d50f0
|
174
|
|
akichika |
0:db8f063d50f0
|
175
|
int AX12::SetTorqueEnable (bool enable) {
|
akichika |
0:db8f063d50f0
|
176
|
|
akichika |
0:db8f063d50f0
|
177
|
char data[1];
|
akichika |
0:db8f063d50f0
|
178
|
data[0] = (char)enable; // 0x01 means enable, 0x00 means disable
|
akichika |
0:db8f063d50f0
|
179
|
|
akichika |
0:db8f063d50f0
|
180
|
#ifdef AX12_DEBUG
|
akichika |
0:db8f063d50f0
|
181
|
printf("Setting TorqueEnable bit to %d\n",enable);
|
akichika |
0:db8f063d50f0
|
182
|
#endif
|
akichika |
0:db8f063d50f0
|
183
|
|
akichika |
0:db8f063d50f0
|
184
|
return (write(_ID, AX12_REG_TORQUE_ENABLE, 1, data));
|
akichika |
0:db8f063d50f0
|
185
|
|
akichika |
0:db8f063d50f0
|
186
|
}
|
akichika |
0:db8f063d50f0
|
187
|
|
akichika |
0:db8f063d50f0
|
188
|
|
akichika |
0:db8f063d50f0
|
189
|
// return 1 is the servo is still in flight
|
akichika |
0:db8f063d50f0
|
190
|
int AX12::isMoving(void) {
|
akichika |
0:db8f063d50f0
|
191
|
|
akichika |
0:db8f063d50f0
|
192
|
char data[1];
|
akichika |
0:db8f063d50f0
|
193
|
read(_ID,AX12_REG_MOVING,1,data);
|
akichika |
0:db8f063d50f0
|
194
|
return(data[0]);
|
akichika |
0:db8f063d50f0
|
195
|
}
|
akichika |
0:db8f063d50f0
|
196
|
|
akichika |
0:db8f063d50f0
|
197
|
|
akichika |
0:db8f063d50f0
|
198
|
void AX12::trigger(void) {
|
akichika |
0:db8f063d50f0
|
199
|
|
akichika |
0:db8f063d50f0
|
200
|
char TxBuf[16];
|
akichika |
0:db8f063d50f0
|
201
|
char sum = 0;
|
akichika |
0:db8f063d50f0
|
202
|
|
akichika |
0:db8f063d50f0
|
203
|
#ifdef AX12_TRIGGER_DEBUG
|
akichika |
0:db8f063d50f0
|
204
|
// Build the TxPacket first in RAM, then we'll send in one go
|
akichika |
0:db8f063d50f0
|
205
|
printf("\nTriggered\n");
|
akichika |
0:db8f063d50f0
|
206
|
printf("\nTrigger Packet\n Header : 0xFF, 0xFF\n");
|
akichika |
0:db8f063d50f0
|
207
|
#endif
|
akichika |
0:db8f063d50f0
|
208
|
|
akichika |
0:db8f063d50f0
|
209
|
TxBuf[0] = 0xFF;
|
akichika |
0:db8f063d50f0
|
210
|
TxBuf[1] = 0xFF;
|
akichika |
0:db8f063d50f0
|
211
|
|
akichika |
0:db8f063d50f0
|
212
|
// ID - Broadcast
|
akichika |
0:db8f063d50f0
|
213
|
TxBuf[2] = 0xFE;
|
akichika |
0:db8f063d50f0
|
214
|
sum += TxBuf[2];
|
akichika |
0:db8f063d50f0
|
215
|
|
akichika |
0:db8f063d50f0
|
216
|
#ifdef AX12_TRIGGER_DEBUG
|
akichika |
0:db8f063d50f0
|
217
|
printf(" ID : %d\n",TxBuf[2]);
|
akichika |
0:db8f063d50f0
|
218
|
#endif
|
akichika |
0:db8f063d50f0
|
219
|
|
akichika |
0:db8f063d50f0
|
220
|
// Length
|
akichika |
0:db8f063d50f0
|
221
|
TxBuf[3] = 0x02;
|
akichika |
0:db8f063d50f0
|
222
|
sum += TxBuf[3];
|
akichika |
0:db8f063d50f0
|
223
|
|
akichika |
0:db8f063d50f0
|
224
|
#ifdef AX12_TRIGGER_DEBUG
|
akichika |
0:db8f063d50f0
|
225
|
printf(" Length %d\n",TxBuf[3]);
|
akichika |
0:db8f063d50f0
|
226
|
#endif
|
akichika |
0:db8f063d50f0
|
227
|
|
akichika |
0:db8f063d50f0
|
228
|
// Instruction - ACTION
|
akichika |
0:db8f063d50f0
|
229
|
TxBuf[4] = 0x04;
|
akichika |
0:db8f063d50f0
|
230
|
sum += TxBuf[4];
|
akichika |
0:db8f063d50f0
|
231
|
|
akichika |
0:db8f063d50f0
|
232
|
#ifdef AX12_TRIGGER_DEBUG
|
akichika |
0:db8f063d50f0
|
233
|
printf(" Instruction 0x%X\n",TxBuf[5]);
|
akichika |
0:db8f063d50f0
|
234
|
#endif
|
akichika |
0:db8f063d50f0
|
235
|
|
akichika |
0:db8f063d50f0
|
236
|
// Checksum
|
akichika |
0:db8f063d50f0
|
237
|
TxBuf[5] = 0xFF - sum;
|
akichika |
0:db8f063d50f0
|
238
|
#ifdef AX12_TRIGGER_DEBUG
|
akichika |
0:db8f063d50f0
|
239
|
printf(" Checksum 0x%X\n",TxBuf[5]);
|
akichika |
0:db8f063d50f0
|
240
|
#endif
|
akichika |
0:db8f063d50f0
|
241
|
|
akichika |
0:db8f063d50f0
|
242
|
// Transmit the packet in one burst with no pausing
|
akichika |
0:db8f063d50f0
|
243
|
for (int i = 0; i < 6 ; i++) {
|
akichika |
0:db8f063d50f0
|
244
|
_ax12.putc(TxBuf[i]);
|
akichika |
0:db8f063d50f0
|
245
|
}
|
akichika |
0:db8f063d50f0
|
246
|
|
akichika |
0:db8f063d50f0
|
247
|
// This is a broadcast packet, so there will be no reply
|
akichika |
0:db8f063d50f0
|
248
|
return;
|
akichika |
0:db8f063d50f0
|
249
|
}
|
akichika |
0:db8f063d50f0
|
250
|
|
akichika |
0:db8f063d50f0
|
251
|
|
akichika |
0:db8f063d50f0
|
252
|
float AX12::GetPosition(void) {
|
akichika |
0:db8f063d50f0
|
253
|
|
akichika |
0:db8f063d50f0
|
254
|
#ifdef AX12_DEBUG
|
akichika |
0:db8f063d50f0
|
255
|
printf("\nGetPosition(%d)",_ID);
|
akichika |
0:db8f063d50f0
|
256
|
#endif
|
akichika |
0:db8f063d50f0
|
257
|
|
akichika |
0:db8f063d50f0
|
258
|
char data[2];
|
akichika |
0:db8f063d50f0
|
259
|
|
akichika |
0:db8f063d50f0
|
260
|
int ErrorCode = read(_ID, AX12_REG_POSITION, 2, data);
|
akichika |
0:db8f063d50f0
|
261
|
short position = data[0] + (data[1] << 8);
|
akichika |
0:db8f063d50f0
|
262
|
float angle = (position * 300)/1024;
|
akichika |
0:db8f063d50f0
|
263
|
|
akichika |
0:db8f063d50f0
|
264
|
return (angle);
|
akichika |
0:db8f063d50f0
|
265
|
}
|
akichika |
0:db8f063d50f0
|
266
|
|
akichika |
0:db8f063d50f0
|
267
|
|
akichika |
0:db8f063d50f0
|
268
|
float AX12::GetTemp (void) {
|
akichika |
0:db8f063d50f0
|
269
|
|
akichika |
0:db8f063d50f0
|
270
|
#ifdef AX12_DEBUG
|
akichika |
0:db8f063d50f0
|
271
|
printf("\nGetTemp(%d)",_ID);
|
akichika |
0:db8f063d50f0
|
272
|
#endif
|
akichika |
0:db8f063d50f0
|
273
|
|
akichika |
0:db8f063d50f0
|
274
|
char data[1];
|
akichika |
0:db8f063d50f0
|
275
|
int ErrorCode = read(_ID, AX12_REG_TEMP, 1, data);
|
akichika |
0:db8f063d50f0
|
276
|
float temp = data[0];
|
akichika |
0:db8f063d50f0
|
277
|
return(temp);
|
akichika |
0:db8f063d50f0
|
278
|
}
|
akichika |
0:db8f063d50f0
|
279
|
|
akichika |
0:db8f063d50f0
|
280
|
|
akichika |
0:db8f063d50f0
|
281
|
float AX12::GetVolts (void) {
|
akichika |
0:db8f063d50f0
|
282
|
|
akichika |
0:db8f063d50f0
|
283
|
#ifdef AX12_DEBUG
|
akichika |
0:db8f063d50f0
|
284
|
printf("\nGetVolts(%d)",_ID);
|
akichika |
0:db8f063d50f0
|
285
|
#endif
|
akichika |
0:db8f063d50f0
|
286
|
|
akichika |
0:db8f063d50f0
|
287
|
char data[1];
|
akichika |
0:db8f063d50f0
|
288
|
int ErrorCode = read(_ID, AX12_REG_VOLTS, 1, data);
|
akichika |
0:db8f063d50f0
|
289
|
float volts = data[0]/10.0;
|
akichika |
0:db8f063d50f0
|
290
|
return(volts);
|
akichika |
0:db8f063d50f0
|
291
|
}
|
akichika |
0:db8f063d50f0
|
292
|
|
akichika |
0:db8f063d50f0
|
293
|
|
akichika |
0:db8f063d50f0
|
294
|
int AX12::read(int ID, int start, int bytes, char* data) {
|
akichika |
0:db8f063d50f0
|
295
|
|
akichika |
0:db8f063d50f0
|
296
|
char PacketLength = 0x4;
|
akichika |
0:db8f063d50f0
|
297
|
char TxBuf[16];
|
akichika |
0:db8f063d50f0
|
298
|
char sum = 0;
|
akichika |
0:db8f063d50f0
|
299
|
char Status[16];
|
akichika |
0:db8f063d50f0
|
300
|
|
akichika |
0:db8f063d50f0
|
301
|
Status[4] = 0xFE; // return code
|
akichika |
0:db8f063d50f0
|
302
|
|
akichika |
0:db8f063d50f0
|
303
|
#ifdef AX12_READ_DEBUG
|
akichika |
0:db8f063d50f0
|
304
|
printf("\nread(%d,0x%x,%d,data)\n",ID,start,bytes);
|
akichika |
0:db8f063d50f0
|
305
|
#endif
|
akichika |
0:db8f063d50f0
|
306
|
|
akichika |
0:db8f063d50f0
|
307
|
// Build the TxPacket first in RAM, then we'll send in one go
|
akichika |
0:db8f063d50f0
|
308
|
#ifdef AX12_READ_DEBUG
|
akichika |
0:db8f063d50f0
|
309
|
printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n");
|
akichika |
0:db8f063d50f0
|
310
|
#endif
|
akichika |
0:db8f063d50f0
|
311
|
|
akichika |
0:db8f063d50f0
|
312
|
TxBuf[0] = 0xff;
|
akichika |
0:db8f063d50f0
|
313
|
TxBuf[1] = 0xff;
|
akichika |
0:db8f063d50f0
|
314
|
|
akichika |
0:db8f063d50f0
|
315
|
// ID
|
akichika |
0:db8f063d50f0
|
316
|
TxBuf[2] = ID;
|
akichika |
0:db8f063d50f0
|
317
|
sum += TxBuf[2];
|
akichika |
0:db8f063d50f0
|
318
|
|
akichika |
0:db8f063d50f0
|
319
|
#ifdef AX12_READ_DEBUG
|
akichika |
0:db8f063d50f0
|
320
|
printf(" ID : %d\n",TxBuf[2]);
|
akichika |
0:db8f063d50f0
|
321
|
#endif
|
akichika |
0:db8f063d50f0
|
322
|
|
akichika |
0:db8f063d50f0
|
323
|
// Packet Length
|
akichika |
0:db8f063d50f0
|
324
|
TxBuf[3] = PacketLength; // Length = 4 ; 2 + 1 (start) = 1 (bytes)
|
akichika |
0:db8f063d50f0
|
325
|
sum += TxBuf[3]; // Accululate the packet sum
|
akichika |
0:db8f063d50f0
|
326
|
|
akichika |
0:db8f063d50f0
|
327
|
#ifdef AX12_READ_DEBUG
|
akichika |
0:db8f063d50f0
|
328
|
printf(" Length : 0x%x\n",TxBuf[3]);
|
akichika |
0:db8f063d50f0
|
329
|
#endif
|
akichika |
0:db8f063d50f0
|
330
|
|
akichika |
0:db8f063d50f0
|
331
|
// Instruction - Read
|
akichika |
0:db8f063d50f0
|
332
|
TxBuf[4] = 0x2;
|
akichika |
0:db8f063d50f0
|
333
|
sum += TxBuf[4];
|
akichika |
0:db8f063d50f0
|
334
|
|
akichika |
0:db8f063d50f0
|
335
|
#ifdef AX12_READ_DEBUG
|
akichika |
0:db8f063d50f0
|
336
|
printf(" Instruction : 0x%x\n",TxBuf[4]);
|
akichika |
0:db8f063d50f0
|
337
|
#endif
|
akichika |
0:db8f063d50f0
|
338
|
|
akichika |
0:db8f063d50f0
|
339
|
// Start Address
|
akichika |
0:db8f063d50f0
|
340
|
TxBuf[5] = start;
|
akichika |
0:db8f063d50f0
|
341
|
sum += TxBuf[5];
|
akichika |
0:db8f063d50f0
|
342
|
|
akichika |
0:db8f063d50f0
|
343
|
#ifdef AX12_READ_DEBUG
|
akichika |
0:db8f063d50f0
|
344
|
printf(" Start Address : 0x%x\n",TxBuf[5]);
|
akichika |
0:db8f063d50f0
|
345
|
#endif
|
akichika |
0:db8f063d50f0
|
346
|
|
akichika |
0:db8f063d50f0
|
347
|
// Bytes to read
|
akichika |
0:db8f063d50f0
|
348
|
TxBuf[6] = bytes;
|
akichika |
0:db8f063d50f0
|
349
|
sum += TxBuf[6];
|
akichika |
0:db8f063d50f0
|
350
|
|
akichika |
0:db8f063d50f0
|
351
|
#ifdef AX12_READ_DEBUG
|
akichika |
0:db8f063d50f0
|
352
|
printf(" No bytes : 0x%x\n",TxBuf[6]);
|
akichika |
0:db8f063d50f0
|
353
|
#endif
|
akichika |
0:db8f063d50f0
|
354
|
|
akichika |
0:db8f063d50f0
|
355
|
// Checksum
|
akichika |
0:db8f063d50f0
|
356
|
TxBuf[7] = 0xFF - sum;
|
akichika |
0:db8f063d50f0
|
357
|
#ifdef AX12_READ_DEBUG
|
akichika |
0:db8f063d50f0
|
358
|
printf(" Checksum : 0x%x\n",TxBuf[7]);
|
akichika |
0:db8f063d50f0
|
359
|
#endif
|
akichika |
0:db8f063d50f0
|
360
|
|
akichika |
0:db8f063d50f0
|
361
|
// Transmit the packet in one burst with no pausing
|
akichika |
0:db8f063d50f0
|
362
|
for (int i = 0; i<8 ; i++) {
|
akichika |
0:db8f063d50f0
|
363
|
_ax12.putc(TxBuf[i]);
|
akichika |
0:db8f063d50f0
|
364
|
}
|
akichika |
0:db8f063d50f0
|
365
|
|
akichika |
0:db8f063d50f0
|
366
|
// Wait for the bytes to be transmitted
|
akichika |
0:db8f063d50f0
|
367
|
wait (0.00002);
|
akichika |
0:db8f063d50f0
|
368
|
|
akichika |
0:db8f063d50f0
|
369
|
// Skip if the read was to the broadcast address
|
akichika |
0:db8f063d50f0
|
370
|
if (_ID != 0xFE) {
|
akichika |
0:db8f063d50f0
|
371
|
|
akichika |
0:db8f063d50f0
|
372
|
|
akichika |
0:db8f063d50f0
|
373
|
|
akichika |
0:db8f063d50f0
|
374
|
// response packet is always 6 + bytes
|
akichika |
0:db8f063d50f0
|
375
|
// 0xFF, 0xFF, ID, Length Error, Param(s) Checksum
|
akichika |
0:db8f063d50f0
|
376
|
// timeout is a little more than the time to transmit
|
akichika |
0:db8f063d50f0
|
377
|
// the packet back, i.e. (6+bytes)*10 bit periods
|
akichika |
0:db8f063d50f0
|
378
|
|
akichika |
0:db8f063d50f0
|
379
|
int timeout = 0;
|
akichika |
0:db8f063d50f0
|
380
|
int plen = 0;
|
akichika |
0:db8f063d50f0
|
381
|
while ((timeout < ((6+bytes)*10)) && (plen<(6+bytes))) {
|
akichika |
0:db8f063d50f0
|
382
|
|
akichika |
0:db8f063d50f0
|
383
|
if (_ax12.readable()) {
|
akichika |
0:db8f063d50f0
|
384
|
Status[plen] = _ax12.getc();
|
akichika |
0:db8f063d50f0
|
385
|
plen++;
|
akichika |
0:db8f063d50f0
|
386
|
timeout = 0;
|
akichika |
0:db8f063d50f0
|
387
|
}
|
akichika |
0:db8f063d50f0
|
388
|
|
akichika |
0:db8f063d50f0
|
389
|
// wait for the bit period
|
akichika |
0:db8f063d50f0
|
390
|
wait (1.0/_baud);
|
akichika |
0:db8f063d50f0
|
391
|
timeout++;
|
akichika |
0:db8f063d50f0
|
392
|
}
|
akichika |
0:db8f063d50f0
|
393
|
|
akichika |
0:db8f063d50f0
|
394
|
if (timeout == ((6+bytes)*10) ) {
|
akichika |
0:db8f063d50f0
|
395
|
return(-1);
|
akichika |
0:db8f063d50f0
|
396
|
}
|
akichika |
0:db8f063d50f0
|
397
|
|
akichika |
0:db8f063d50f0
|
398
|
// Copy the data from Status into data for return
|
akichika |
0:db8f063d50f0
|
399
|
for (int i=0; i < Status[3]-2 ; i++) {
|
akichika |
0:db8f063d50f0
|
400
|
data[i] = Status[5+i];
|
akichika |
0:db8f063d50f0
|
401
|
}
|
akichika |
0:db8f063d50f0
|
402
|
|
akichika |
0:db8f063d50f0
|
403
|
#ifdef AX12_READ_DEBUG
|
akichika |
0:db8f063d50f0
|
404
|
printf("\nStatus Packet\n");
|
akichika |
0:db8f063d50f0
|
405
|
printf(" Header : 0x%x\n",Status[0]);
|
akichika |
0:db8f063d50f0
|
406
|
printf(" Header : 0x%x\n",Status[1]);
|
akichika |
0:db8f063d50f0
|
407
|
printf(" ID : 0x%x\n",Status[2]);
|
akichika |
0:db8f063d50f0
|
408
|
printf(" Length : 0x%x\n",Status[3]);
|
akichika |
0:db8f063d50f0
|
409
|
printf(" Error Code : 0x%x\n",Status[4]);
|
akichika |
0:db8f063d50f0
|
410
|
|
akichika |
0:db8f063d50f0
|
411
|
for (int i=0; i < Status[3]-2 ; i++) {
|
akichika |
0:db8f063d50f0
|
412
|
printf(" Data : 0x%x\n",Status[5+i]);
|
akichika |
0:db8f063d50f0
|
413
|
}
|
akichika |
0:db8f063d50f0
|
414
|
|
akichika |
0:db8f063d50f0
|
415
|
printf(" Checksum : 0x%x\n",Status[5+(Status[3]-2)]);
|
akichika |
0:db8f063d50f0
|
416
|
#endif
|
akichika |
0:db8f063d50f0
|
417
|
|
akichika |
0:db8f063d50f0
|
418
|
} // if (ID!=0xFE)
|
akichika |
0:db8f063d50f0
|
419
|
|
akichika |
0:db8f063d50f0
|
420
|
return(Status[4]);
|
akichika |
0:db8f063d50f0
|
421
|
}
|
akichika |
0:db8f063d50f0
|
422
|
|
akichika |
0:db8f063d50f0
|
423
|
|
akichika |
0:db8f063d50f0
|
424
|
int AX12::write(int ID, int start, int bytes, char* data, int flag) {
|
akichika |
0:db8f063d50f0
|
425
|
// 0xff, 0xff, ID, Length, Intruction(write), Address, Param(s), Checksum
|
akichika |
0:db8f063d50f0
|
426
|
|
akichika |
0:db8f063d50f0
|
427
|
char TxBuf[16];
|
akichika |
0:db8f063d50f0
|
428
|
char sum = 0;
|
akichika |
0:db8f063d50f0
|
429
|
char Status[6];
|
akichika |
0:db8f063d50f0
|
430
|
|
akichika |
0:db8f063d50f0
|
431
|
#ifdef AX12_WRITE_DEBUG
|
akichika |
0:db8f063d50f0
|
432
|
printf("\nwrite(%d,0x%x,%d,data,%d)\n",ID,start,bytes,flag);
|
akichika |
0:db8f063d50f0
|
433
|
#endif
|
akichika |
0:db8f063d50f0
|
434
|
|
akichika |
0:db8f063d50f0
|
435
|
// Build the TxPacket first in RAM, then we'll send in one go
|
akichika |
0:db8f063d50f0
|
436
|
#ifdef AX12_WRITE_DEBUG
|
akichika |
0:db8f063d50f0
|
437
|
printf("\nInstruction Packet\n Header : 0xFF, 0xFF\n");
|
akichika |
0:db8f063d50f0
|
438
|
#endif
|
akichika |
0:db8f063d50f0
|
439
|
|
akichika |
0:db8f063d50f0
|
440
|
TxBuf[0] = 0xff;
|
akichika |
0:db8f063d50f0
|
441
|
TxBuf[1] = 0xff;
|
akichika |
0:db8f063d50f0
|
442
|
|
akichika |
0:db8f063d50f0
|
443
|
// ID
|
akichika |
0:db8f063d50f0
|
444
|
TxBuf[2] = ID;
|
akichika |
0:db8f063d50f0
|
445
|
sum += TxBuf[2];
|
akichika |
0:db8f063d50f0
|
446
|
|
akichika |
0:db8f063d50f0
|
447
|
#ifdef AX12_WRITE_DEBUG
|
akichika |
0:db8f063d50f0
|
448
|
printf(" ID : %d\n",TxBuf[2]);
|
akichika |
0:db8f063d50f0
|
449
|
#endif
|
akichika |
0:db8f063d50f0
|
450
|
|
akichika |
0:db8f063d50f0
|
451
|
// packet Length
|
akichika |
0:db8f063d50f0
|
452
|
TxBuf[3] = 3+bytes;
|
akichika |
0:db8f063d50f0
|
453
|
sum += TxBuf[3];
|
akichika |
0:db8f063d50f0
|
454
|
|
akichika |
0:db8f063d50f0
|
455
|
#ifdef AX12_WRITE_DEBUG
|
akichika |
0:db8f063d50f0
|
456
|
printf(" Length : %d\n",TxBuf[3]);
|
akichika |
0:db8f063d50f0
|
457
|
#endif
|
akichika |
0:db8f063d50f0
|
458
|
|
akichika |
0:db8f063d50f0
|
459
|
// Instruction
|
akichika |
0:db8f063d50f0
|
460
|
if (flag == 1) {
|
akichika |
0:db8f063d50f0
|
461
|
TxBuf[4]=0x04;
|
akichika |
0:db8f063d50f0
|
462
|
sum += TxBuf[4];
|
akichika |
0:db8f063d50f0
|
463
|
} else {
|
akichika |
0:db8f063d50f0
|
464
|
TxBuf[4]=0x03;
|
akichika |
0:db8f063d50f0
|
465
|
sum += TxBuf[4];
|
akichika |
0:db8f063d50f0
|
466
|
}
|
akichika |
0:db8f063d50f0
|
467
|
|
akichika |
0:db8f063d50f0
|
468
|
#ifdef AX12_WRITE_DEBUG
|
akichika |
0:db8f063d50f0
|
469
|
printf(" Instruction : 0x%x\n",TxBuf[4]);
|
akichika |
0:db8f063d50f0
|
470
|
#endif
|
akichika |
0:db8f063d50f0
|
471
|
|
akichika |
0:db8f063d50f0
|
472
|
// Start Address
|
akichika |
0:db8f063d50f0
|
473
|
TxBuf[5] = start;
|
akichika |
0:db8f063d50f0
|
474
|
sum += TxBuf[5];
|
akichika |
0:db8f063d50f0
|
475
|
|
akichika |
0:db8f063d50f0
|
476
|
#ifdef AX12_WRITE_DEBUG
|
akichika |
0:db8f063d50f0
|
477
|
printf(" Start : 0x%x\n",TxBuf[5]);
|
akichika |
0:db8f063d50f0
|
478
|
#endif
|
akichika |
0:db8f063d50f0
|
479
|
|
akichika |
0:db8f063d50f0
|
480
|
// data
|
akichika |
0:db8f063d50f0
|
481
|
for (char i=0; i<bytes ; i++) {
|
akichika |
0:db8f063d50f0
|
482
|
TxBuf[6+i] = data[i];
|
akichika |
0:db8f063d50f0
|
483
|
sum += TxBuf[6+i];
|
akichika |
0:db8f063d50f0
|
484
|
|
akichika |
0:db8f063d50f0
|
485
|
#ifdef AX12_WRITE_DEBUG
|
akichika |
0:db8f063d50f0
|
486
|
printf(" Data : 0x%x\n",TxBuf[6+i]);
|
akichika |
0:db8f063d50f0
|
487
|
#endif
|
akichika |
0:db8f063d50f0
|
488
|
|
akichika |
0:db8f063d50f0
|
489
|
}
|
akichika |
0:db8f063d50f0
|
490
|
|
akichika |
0:db8f063d50f0
|
491
|
// checksum
|
akichika |
0:db8f063d50f0
|
492
|
TxBuf[6+bytes] = 0xFF - sum;
|
akichika |
0:db8f063d50f0
|
493
|
|
akichika |
0:db8f063d50f0
|
494
|
#ifdef AX12_WRITE_DEBUG
|
akichika |
0:db8f063d50f0
|
495
|
printf(" Checksum : 0x%x\n",TxBuf[6+bytes]);
|
akichika |
0:db8f063d50f0
|
496
|
#endif
|
akichika |
0:db8f063d50f0
|
497
|
|
akichika |
0:db8f063d50f0
|
498
|
// Transmit the packet in one burst with no pausing
|
akichika |
0:db8f063d50f0
|
499
|
for (int i = 0; i < (7 + bytes) ; i++) {
|
akichika |
0:db8f063d50f0
|
500
|
_ax12.putc(TxBuf[i]);
|
akichika |
0:db8f063d50f0
|
501
|
}
|
akichika |
0:db8f063d50f0
|
502
|
|
akichika |
0:db8f063d50f0
|
503
|
// Wait for data to transmit
|
akichika |
0:db8f063d50f0
|
504
|
wait (0.00002);
|
akichika |
0:db8f063d50f0
|
505
|
|
akichika |
0:db8f063d50f0
|
506
|
// make sure we have a valid return
|
akichika |
0:db8f063d50f0
|
507
|
Status[4]=0x00;
|
akichika |
0:db8f063d50f0
|
508
|
|
akichika |
0:db8f063d50f0
|
509
|
// we'll only get a reply if it was not broadcast
|
akichika |
0:db8f063d50f0
|
510
|
if (_ID!=0xFE) {
|
akichika |
0:db8f063d50f0
|
511
|
|
akichika |
0:db8f063d50f0
|
512
|
|
akichika |
0:db8f063d50f0
|
513
|
// response packet is always 6 bytes
|
akichika |
0:db8f063d50f0
|
514
|
// 0xFF, 0xFF, ID, Length Error, Param(s) Checksum
|
akichika |
0:db8f063d50f0
|
515
|
// timeout is a little more than the time to transmit
|
akichika |
0:db8f063d50f0
|
516
|
// the packet back, i.e. 60 bit periods, round up to 100
|
akichika |
0:db8f063d50f0
|
517
|
int timeout = 0;
|
akichika |
0:db8f063d50f0
|
518
|
int plen = 0;
|
akichika |
0:db8f063d50f0
|
519
|
while ((timeout < 100) && (plen<6)) {
|
akichika |
0:db8f063d50f0
|
520
|
|
akichika |
0:db8f063d50f0
|
521
|
if (_ax12.readable()) {
|
akichika |
0:db8f063d50f0
|
522
|
Status[plen] = _ax12.getc();
|
akichika |
0:db8f063d50f0
|
523
|
plen++;
|
akichika |
0:db8f063d50f0
|
524
|
timeout = 0;
|
akichika |
0:db8f063d50f0
|
525
|
}
|
akichika |
0:db8f063d50f0
|
526
|
|
akichika |
0:db8f063d50f0
|
527
|
// wait for the bit period
|
akichika |
0:db8f063d50f0
|
528
|
wait (1.0/_baud);
|
akichika |
0:db8f063d50f0
|
529
|
timeout++;
|
akichika |
0:db8f063d50f0
|
530
|
}
|
akichika |
0:db8f063d50f0
|
531
|
|
akichika |
0:db8f063d50f0
|
532
|
|
akichika |
0:db8f063d50f0
|
533
|
// Build the TxPacket first in RAM, then we'll send in one go
|
akichika |
0:db8f063d50f0
|
534
|
#ifdef AX12_WRITE_DEBUG
|
akichika |
0:db8f063d50f0
|
535
|
printf("\nStatus Packet\n Header : 0x%X, 0x%X\n",Status[0],Status[1]);
|
akichika |
0:db8f063d50f0
|
536
|
printf(" ID : %d\n",Status[2]);
|
akichika |
0:db8f063d50f0
|
537
|
printf(" Length : %d\n",Status[3]);
|
akichika |
0:db8f063d50f0
|
538
|
printf(" Error : 0x%x\n",Status[4]);
|
akichika |
0:db8f063d50f0
|
539
|
printf(" Checksum : 0x%x\n",Status[5]);
|
akichika |
0:db8f063d50f0
|
540
|
#endif
|
akichika |
0:db8f063d50f0
|
541
|
|
akichika |
0:db8f063d50f0
|
542
|
|
akichika |
0:db8f063d50f0
|
543
|
}
|
akichika |
0:db8f063d50f0
|
544
|
|
akichika |
0:db8f063d50f0
|
545
|
return(Status[4]); // return error code
|
akichika |
0:db8f063d50f0
|
546
|
}
|