Skip to content

Commit c9ccdc6

Browse files
committed
fix send/get angles&coords
1 parent 8b56cad commit c9ccdc6

File tree

2 files changed

+49
-21
lines changed

2 files changed

+49
-21
lines changed

Mycobot.csharp/MyCobot.cs

+22-13
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,10 @@ public void PowerOff()
123123
/// <param name="jointNo">joint number: 1 ~ 6</param>
124124
/// <param name="angle">angle value: -180 ~ 180 </param>
125125
/// <param name="speed">speed value: 0 ~ 100</param>
126-
public void SendOneAngle(int jointNo, int angle, int speed)
126+
public void SendOneAngle(int jointNo, double angle, int speed)
127127
{
128-
int _angle = angle * 100, idx = 0;
128+
int _angle = (int)angle * 100;
129+
int idx = 0;
129130
var command = new byte[9];
130131
// set header
131132
command[idx++] = 0xfe;
@@ -147,19 +148,22 @@ public void SendOneAngle(int jointNo, int angle, int speed)
147148
/// </summary>
148149
/// <param name="angles">angles[], length: 6</param>
149150
/// <param name="speed">speed value: 0 ~ 100</param>
150-
public void SendAngles(int[] angles, int speed)
151+
public void SendAngles(double[] angles, int speed)
151152
{
152153
var command = new byte[18];
153154
var idx = 0;
155+
int[] angles1 = {0,0,0,0,0,0};
154156
command[idx++] = 0xfe;
155157
command[idx++] = 0xfe;
156158
command[idx++] = 0x0f;
157159
command[idx++] = 0x22;
158160
for (var j = 0; j < angles.Length; ++j)
159161
{
160162
angles[j] *= 100;
163+
angles1[j] = (int)(angles[j]);
164+
//angles1[j] = (int)(angles[j]*100);
161165
}
162-
var a = Int16ArrToBytes(angles);
166+
var a = Int16ArrToBytes(angles1);
163167
foreach (var t in a)
164168
{
165169
command[idx++] = t;
@@ -174,7 +178,7 @@ public void SendAngles(int[] angles, int speed)
174178
/// Get all angles
175179
/// </summary>
176180
/// <returns>int[], length: 6</returns>
177-
public int[] GetAngles()
181+
public float[] GetAngles()
178182
{
179183
byte[] command = { 0xfe, 0xfe, 0x02, 0x20, 0xfa };
180184
Write(command, 0, command.Length);
@@ -186,16 +190,16 @@ public int[] GetAngles()
186190
var m_recvBytes = new byte[_serialPort.BytesToRead];
187191
var result = _serialPort.Read(m_recvBytes, 0, m_recvBytes.Length);
188192
if (result <= 0)
189-
return Array.Empty<int>();
193+
return Array.Empty<float>();
190194

191195
// get valid index
192196
var idx = getValidIndex(m_recvBytes);
193197
if (idx == -1)
194-
return Array.Empty<int>();
198+
return Array.Empty<float>();
195199

196200
// process data
197-
var len = (int)m_recvBytes[idx] - 1;
198-
var res = new int[6];
201+
var len = (float)m_recvBytes[idx] - 1;
202+
var res = new float[6];
199203
var resIdx = 0;
200204
for (var i = idx + 1; i < idx + len; i += 2)
201205
{
@@ -211,9 +215,11 @@ public int[] GetAngles()
211215
/// <param name="coord">coord No: 1 - 6</param>
212216
/// <param name="value">coord value</param>
213217
/// <param name="speed">speed: 0 ~ 100</param>
214-
public void SendOneCoord(int coord, int value, int speed)
218+
public void SendOneCoord(double coord, int value, int speed)
215219
{
216-
int idx = 0, _value = coord < 4 ? coord * 10 : coord * 100;
220+
int idx = 0 ;
221+
int _value = (int)(coord < 4 ? value * 10 : value * 100);
222+
217223
var command = new byte[9];
218224
// set header
219225
command[idx++] = 0xfe;
@@ -238,10 +244,11 @@ public void SendOneCoord(int coord, int value, int speed)
238244
/// <param name="coords">int[], length: 6</param>
239245
/// <param name="speed">speed: int, value: 0 ~ 100</param>
240246
/// <param name="mode">mode: 0 - angular, 1 - linear</param>
241-
public void SendCoords(int[] coords, int speed, int mode)
247+
public void SendCoords(double[] coords, int speed, int mode)
242248
{
243249
var command = new byte[19];
244250
var idx = 0;
251+
int[] coords1= {0,0,0,0,0,0};
245252
// set header
246253
command[idx++] = 0xfe;
247254
command[idx++] = 0xfe;
@@ -251,13 +258,15 @@ public void SendCoords(int[] coords, int speed, int mode)
251258
for (var i = 0; i < 3; ++i)
252259
{
253260
coords[i] *= 10;
261+
coords1[i] = (int)coords[i];
254262
}
255263
for (var i = 3; i < 6; ++i)
256264
{
257265
coords[i] *= 100;
266+
coords1[i] = (int)coords[i];
258267
}
259268
// append to command
260-
var a = Int16ArrToBytes(coords);
269+
var a = Int16ArrToBytes(coords1);
261270
foreach (var t in a)
262271
{
263272
command[idx++] = t;

Mycobot.csharp/Program.cs

+27-8
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,32 @@ class Test
77
{
88
static void Main(string[] args)
99
{
10-
MyCobot mc = new MyCobot("COM57");//树莓派机械臂串口名称:/dev/ttyAMA0
10+
MyCobot mc = new MyCobot("COM14");//树莓派机械臂串口名称:/dev/ttyAMA0
1111
mc.Open();
1212
Thread.Sleep(5000);//windows打开串口后,需要等待5s,Windows打开串口底部basic会重启
1313
// int[] angles = new[] {100, 100, 100, 100, 100, 100};
1414
// mc.SendAngles(angles, 50);
15-
var recv = mc.GetAngles();
16-
foreach (var v in recv)
17-
{
18-
Console.WriteLine(v);
19-
}
15+
double[] a = { 0, 0, 90, 0, 0, 0 };
16+
mc.SendAngles(a, 70);
2017

18+
Thread.Sleep(1000);
19+
float[] cdd = mc.GetAngles();
20+
foreach (var v in cdd)
21+
{
22+
Console.WriteLine(v);
23+
}
24+
mc.SendOneCoord(1, -60, 70);
25+
Thread.Sleep(1000);
26+
mc.SendOneCoord(1, 20, 70);
27+
/*
28+
mc.SendOneCoord(1,20, 70);
29+
Thread.Sleep(1000);
30+
var recv = mc.GetAngles();
31+
foreach (var v in recv)
32+
{
33+
Console.WriteLine(v);
34+
}
35+
*/
2136
// int[] coords = new[] {160, 160, 160, 0, 0, 0};
2237
// mc.SendCoords(coords, 90, 1);
2338
// Thread.Sleep(5000);
@@ -26,9 +41,13 @@ static void Main(string[] args)
2641
// {
2742
// Console.WriteLine(v);
2843
// }
44+
/* float[] b = { 0,0,0,0,0,0 };
45+
mc.SendAngles(b, 70);
46+
Thread.Sleep(1000);
47+
mc.SendOneAngle(3, 90, 70);
48+
Thread.Sleep(1000);
49+
*/
2950

30-
mc.SendOneAngle(1,0, 70);
31-
Thread.Sleep(100);
3251
/*var angle = new int[6];
3352
angle = mc.GetAngles();
3453
foreach (var v in angle)

0 commit comments

Comments
 (0)