This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
SCIP_library.cs
200 lines (174 loc) · 6.42 KB
/
SCIP_library.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
// http://sourceforge.net/p/urgnetwork/wiki/cs_sample_jp/
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace SCIP_library
{
public class SCIP_Writer
{
public static string END
{
get{ return "\n"; }
}
/// <param name="start">measurement start step</param>
/// <param name="end">measurement end step</param>
/// <param name="grouping">grouping step number</param>
/// <param name="skips">skip scan number</param>
/// <param name="scans">get scan numbar</param>
/// <returns>created command</returns>
public static string MD(int start, int end, int grouping = 1, int skips = 0, int scans = 0)
{
return UrgDevice.GetCMDString(UrgDevice.CMD.MD) + start.ToString("D4") + end.ToString("D4") + grouping.ToString("D2") + skips.ToString("D1") + scans.ToString("D2") + END;
}
public static string ME(int start, int end, int grouping = 1, int skips = 0, int scans = 0)
{
return UrgDevice.GetCMDString(UrgDevice.CMD.ME) + start.ToString("D4") + end.ToString("D4") + grouping.ToString("D2") + skips.ToString("D1") + scans.ToString("D2") + END;
}
public static string BM()
{
return UrgDevice.GetCMDString(UrgDevice.CMD.BM) + END;
}
public static string GD(int start, int end, int grouping = 1)
{
return UrgDevice.GetCMDString(UrgDevice.CMD.GD) + start.ToString("D4") + end.ToString("D4") + grouping.ToString("D2") + END;
}
public static string VV()
{
return UrgDevice.CMD.VV.ToString() + END;
}
public static string II()
{
return UrgDevice.GetCMDString(UrgDevice.CMD.II) + END;
}
public static string PP()
{
return UrgDevice.GetCMDString(UrgDevice.CMD.PP) + END;
}
public static string SCIP2()
{
return "SCIP2.0" + END;
}
public static string QT()
{
return UrgDevice.GetCMDString(UrgDevice.CMD.QT) + END;
}
}
public class SCIP_Reader
{
/// <summary>
/// read MD command
/// </summary>
/// <param name="get_command">received command</param>
/// <param name="time_stamp">timestamp data</param>
/// <param name="distances">distance data</param>
/// <returns>is successful</returns>
public static bool MD(string get_command, ref long time_stamp, ref List<long> distances)
{
//distances.Clear();
string[] split_command = get_command.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
// if (!split_command[0].StartsWith("MD")) {
// return false;
// }
if (split_command[1].StartsWith("00")) {
return true;
} else if (split_command[1].StartsWith("99")) {
time_stamp = SCIP_Reader.decode(split_command[2], 4);
distance_data(split_command, 3, ref distances);
return true;
} else {
return false;
}
}
public static bool GD(string get_command, ref long time_stamp, ref List<long> distances)
{
//distances.Clear();
string[] split_command = get_command.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
// if (!split_command[0].StartsWith("GD")) {
// return false;
// }
if (split_command[1].StartsWith("00")) {
time_stamp = SCIP_Reader.decode(split_command[2], 4);
distance_data(split_command, 3, ref distances);
return true;
} else {
return false;
}
}
/// <summary>
/// read distance data
/// </summary>
/// <param name="lines"></param>
/// <param name="start_line"></param>
/// <returns></returns>
public static bool distance_data(string[] lines, int start_line, ref List<long> distances)
{
StringBuilder sb = new StringBuilder();
for (int i = start_line; i < lines.Length; ++i) {
sb.Append(lines[i].Substring(0, lines[i].Length - 1));
}
return SCIP_Reader.decode_array(sb.ToString(), 3, ref distances);
}
/// <summary>
/// decode part of string
/// </summary>
/// <param name="data">encoded string</param>
/// <param name="size">encode size</param>
/// <param name="offset">decode start position</param>
/// <returns>decode result</returns>
public static long decode(string data, int size, int offset = 0)
{
long value = 0;
for (int i = 0; i < size; ++i) {
value <<= 6;
value |= (long)data[offset + i] - 0x30;
}
return value;
}
/// <summary>
/// decode multiple data
/// </summary>
/// <param name="data">encoded string</param>
/// <param name="size">encode size</param>
/// <returns>decode result</returns>
public static bool decode_array(string data, int size, ref List<long> decoded_data)
{
for (int pos = 0; pos <= data.Length - size; pos += size) {
decoded_data.Add(decode(data, size, pos));
}
return true;
}
public static bool ME(string get_command, ref long time_stamp, ref List<long> distances, ref List<long> strengths)
{
string[] split_command = get_command.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
// if (!split_command[0].StartsWith("ME")) {
// return false;
// }
if (split_command[1].StartsWith("00")) {
return true;
} else if (split_command[1].StartsWith("99")) {
time_stamp = SCIP_Reader.decode(split_command[2], 4);
distance_strength_data(split_command, 3, ref distances, ref strengths);
return true;
} else {
return false;
}
}
public static bool distance_strength_data(string[] lines, int start_line, ref List<long> distances, ref List<long> strengths)
{
StringBuilder sb = new StringBuilder();
for (int i = start_line; i < lines.Length; ++i) {
sb.Append(lines[i].Substring(0, lines[i].Length - 1));
}
return SCIP_Reader.decode_array(sb.ToString(), 3, ref distances, ref strengths);
}
public static bool decode_array(string data, int size, ref List<long> decoded_data, ref List<long> stdecoded_data)
{
for (int pos = 0; pos <= data.Length - size * 2; pos += size * 2) {
decoded_data.Add(decode(data, size, pos));
stdecoded_data.Add(decode(data, size, pos + size));
}
return true;
}
}
}