This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAX12.cs
265 lines (225 loc) · 7.72 KB
/
AX12.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.IO;
using System.Threading;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
//using GHI.Premium.Hardware;
//using GHI.Premium.Hardware.LowLevel;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GHI.Processor;
namespace PR
{
enum AX12Mode { joint, wheel };
enum speed { stop = 0, reverse = 700, forward = 1700 }//1023 //2047
enum Instruction : byte
{
AX_PING = 0x01,
AX_READ_DATA = 0x02,
AX_WRITE_DATA = 0x03,
AX_REG_WRITE = 0x04,
AX_ACTION = 0x05,
AX_RESET = 0x06,
AX_SYNC_WRITE = 0x83,
}
enum Address : byte
{
AX_ID = 0x03,
AX_CW_LIMIT = 0x06,
AX_CCW_LIMIT = 0x08,
AX_LED = 0x19,
}
class CAX12
{
OutputPort m_Direction;
SerialPort m_serial;
private byte[] m_commande = new byte[20];
byte m_ID = 0;
AX12Mode m_mode = 0;
uint m_posCourrante = 0, m_posPrecedente = 0;
int m_nbTour = 0;
speed m_speed = speed.stop;
public CAX12(byte ID, SerialPort portserie, OutputPort direction)
{
m_serial = portserie;
m_ID = ID;
m_Direction = direction;
Register U0FDR = new Register(0xE000C028, (8 << 4) | 1);
// U0FDR.Write((8 << 4) | 1);//fix the fractional divider register
Register PINSEL0 = new Register(0xE002C000);
}
public int setMode(AX12Mode modeAX)
{
byte len, error = 1;
byte outID;
m_mode = modeAX;
if (m_mode == AX12Mode.joint)
{
int CW = 0;
int CCW = 1023;
byte[] limitsCW = { 0x06, (byte)CW, (byte)(CW >> 8) };
byte[] limitsCCW = { 0x08, (byte)CCW, (byte)(CCW >> 8) };
sendCommand(m_ID, Instruction.AX_WRITE_DATA, limitsCW);
getReponse(out outID, out len, out error, null);
sendCommand(m_ID, Instruction.AX_WRITE_DATA, limitsCCW);
getReponse(out outID, out len, out error, null);
}
else if (m_mode == AX12Mode.wheel)
{
int CW = 0;
int CCW = 0;
byte[] limitsCW = { 0x06, (byte)CW, (byte)(CW >> 8) };
byte[] limitsCCW = { 0x08, (byte)CCW, (byte)(CCW >> 8) };
sendCommand(m_ID, Instruction.AX_WRITE_DATA, limitsCW);
getReponse(out outID, out len, out error, null);
sendCommand(m_ID, Instruction.AX_WRITE_DATA, limitsCCW);
getReponse(out outID, out len, out error, null);
}
return (int)error;
}
private byte calculeCRC()
{
int taille = m_commande[3] + 2;
byte crc = 0;
for (int i = 2; i < taille + 1; i++)
{
crc += m_commande[i];
}
return (byte)(0xFF - crc);
}
public bool sendCommand(byte ID, Instruction instruction, byte[] parametres)
{
bool error = false;
int length = 0;
if (parametres != null)
{
length = parametres.Length;
}
m_commande[0] = 0xFF;
m_commande[1] = 0XFF;
m_commande[2] = ID;
m_commande[3] = (byte)(length + 2);//len
m_commande[4] = (byte)instruction;
for (int i = 5; i < length + 5; i++)
{
m_commande[i] = parametres[i - 5];
}
m_commande[length + 5] = calculeCRC();
// send data
if (m_serial.IsOpen)
{
// UART en transmission TX activé
m_Direction.Write(true);
// m_serial.DiscardInBuffer();
//m_serial.DiscardOutBuffer();
Thread.Sleep(100);
m_serial.Write(m_commande, 0, length + 6);
//wait till all is sent
while (m_serial.BytesToWrite > 0) ;
// UART en transmission TX desactivé
m_Direction.Write(false);
// Thread.Sleep(100);
error = true;
}
return error;
//the response is now coming back so you must read it
}
public bool getReponse(out byte ID, out byte taille, out byte error, byte[] parametres)
{
bool erreur = false;
for (int i = 0; i < m_commande.Length; i++)
m_commande[i] = 0;
int temp = 0;
int nbByte = m_serial.BytesToRead;
do
{
temp = m_serial.Read(m_commande, 0, 20);
} while (temp == 0);
if (temp < 5)
{
ID = 0;
taille = 0;
error = 0xff;
}
else
{
ID = m_commande[2];
taille = (byte)(m_commande[3] - 2);
error = m_commande[4]; // 16 = CRC error
if (error != 16)
erreur = true;
if (parametres != null)
{
for (int i = 0; i < taille; i++)
{
parametres[i] = m_commande[5 + i];
}
}
}
return erreur;
}
public bool move(int value)
{
byte len, error = 1;
byte outID;
bool erreur = false;
if (m_mode == AX12Mode.joint)
{
byte[] buf = { 0x1E, (byte)(value), (byte)(value >> 8) };
erreur = sendCommand(m_ID, Instruction.AX_WRITE_DATA, buf);
getReponse(out outID, out len, out error, null);
}
return erreur;
}
public int setMovingSpeed(int vitesse)
{
byte len, error = 1;
int value = (int)vitesse;
if (true)
{
byte[] buf = { 0x20, (byte)(value), (byte)(value >> 8) };
sendCommand(m_ID, Instruction.AX_WRITE_DATA, buf);
Thread.Sleep(100);
getReponse(out m_ID, out len, out error, null);
}
return (int)error;
}
public int setMovingSpeed(speed vitesse)
{
m_speed = vitesse;
byte len, error = 1;
int value = (int)vitesse;
if (true)
{
byte[] buf = { 0x20, (byte)(value), (byte)(value >> 8) };
sendCommand(m_ID, Instruction.AX_WRITE_DATA, buf);
Thread.Sleep(100);
getReponse(out m_ID, out len, out error, null);
}
return (int)error;
}
public bool getPosition(out uint position)
{
bool erreur = false;
byte len, error;
byte[] pos = new byte[2];
byte[] buf = { 0x24, 0x02 };
sendCommand(m_ID, Instruction.AX_READ_DATA, buf);
Thread.Sleep(100);
m_posPrecedente = m_posCourrante;
if (getReponse(out m_ID, out len, out error, pos))
erreur = true;
m_posCourrante = (uint)pos[0] + (uint)(pos[1] << 8);
if (m_mode == AX12Mode.wheel)
{
if (m_speed == speed.reverse && m_posCourrante < m_posPrecedente && m_posCourrante >= 0 && m_posPrecedente <= 1023)
m_nbTour--;
if (m_speed == speed.forward && m_posCourrante > m_posPrecedente && m_posCourrante <= 1023 && m_posPrecedente >= 0)
m_nbTour++;
}
position = m_posCourrante;
return erreur;
}
}
}