-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
166 lines (150 loc) · 5.86 KB
/
Extensions.cs
File metadata and controls
166 lines (150 loc) · 5.86 KB
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
using AGVSystemCommonNet6.AGVDispatch.Messages;
using AGVSystemCommonNet6.MAP;
using Newtonsoft.Json;
using RosSharp.RosBridgeClient.MessageTypes.Geometry;
using RosSharp.RosBridgeClient.MessageTypes.Std;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static AGVSystemCommonNet6.clsEnums;
namespace AGVSystemCommonNet6
{
public static class Extensions
{
public static Time ToStdTime(this DateTime _time)
{
return new Time()
{
secs = (uint)(_time.Subtract(new DateTime(1970, 1, 1)).TotalSeconds),
nsecs = (uint)(_time.Millisecond * 1000000),
};
}
public static string ToAGVSTimeFormat(this DateTime _time)
{
return _time.ToString("yyyyMMdd HH:mm:ss");
}
/// <summary>
/// 將角度值轉換為 Quaternion(四位元)
/// </summary>
/// <param name="Theta"></param>
/// <returns></returns>
public static Quaternion ToQuaternion(this double Theta)
{
double yaw_radians = (float)Theta * Math.PI / 180.0;
double cos_yaw = Math.Cos(yaw_radians / 2.0);
double sin_yaw = Math.Sin(yaw_radians / 2.0);
return new Quaternion(0.0f, 0.0f, (float)sin_yaw, (float)cos_yaw);
}
public static int[] GetRemainPath(this IEnumerable<clsMapPoint> points, int startTag)
{
if (points.Count() == 0)
return new int[1] { startTag };
int find_index(int tag)
{
return points.ToList().FindIndex(p => p.Point_ID == tag);
}
var startTag_index = find_index(startTag);
return points.ToList().FindAll(p => find_index(p.Point_ID) >= startTag_index).Select(pt => pt.Point_ID).ToArray();
}
public static double ToTheta(this RosSharp.RosBridgeClient.MessageTypes.Geometry.Quaternion orientation)
{
double yaw;
double x = orientation.x;
double y = orientation.y;
double z = orientation.z;
double w = orientation.w;
// 計算角度
double siny_cosp = 2.0 * (w * z + x * y);
double cosy_cosp = 1.0 - 2.0 * (y * y + z * z);
yaw = Math.Atan2(siny_cosp, cosy_cosp);
return yaw * 180.0 / Math.PI;
}
public static string ToJson(this object obj)
{
try
{
return JsonConvert.SerializeObject(obj, Formatting.Indented);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return "{}";
}
}
/// <summary>
/// 該站點是否可充電
/// </summary>
/// <param name="map_station"></param>
/// <returns></returns>
public static bool IsChargeAble(this MapStation map_station)
{
STATION_TYPE station_type = map_station.StationType;
return station_type == STATION_TYPE.Charge | station_type == STATION_TYPE.Charge_Buffer | station_type == STATION_TYPE.Charge_STK;
}
/// <summary>
/// 該站點是否可供AGV Load/Unload
/// </summary>
/// <param name="map_station"></param>
/// <returns></returns>
public static bool IsLoadAble(this MapStation map_station)
{
STATION_TYPE station_type = map_station.StationType;
return station_type == STATION_TYPE.EQ | station_type == STATION_TYPE.EQ_LD
| station_type == STATION_TYPE.STK | station_type == STATION_TYPE.STK_LD
| station_type == STATION_TYPE.Charge_STK;
}
/// <summary>
/// 該站點是否可供AGV Load/Unload
/// </summary>
/// <param name="map_station"></param>
/// <returns></returns>
public static bool IsUnloadAble(this MapStation map_station)
{
STATION_TYPE station_type = map_station.StationType;
return station_type == STATION_TYPE.EQ | station_type == STATION_TYPE.EQ_ULD
| station_type == STATION_TYPE.STK | station_type == STATION_TYPE.STK_ULD
| station_type == STATION_TYPE.Charge_STK;
}
/// <summary>
/// 計算與站點的距離
/// </summary>
/// <param name="map_station"></param>
/// <param name="from_loc_x"></param>
/// <param name="from_loc_y"></param>
/// <returns></returns>
public static double CalculateDistance(this MapStation map_station, double from_loc_x, double from_loc_y)
{
return Math.Sqrt(Math.Pow(map_station.X - from_loc_x, 2) + Math.Pow(map_station.Y - from_loc_y, 2));
}
/// <summary>
/// 計算與站點的距離
/// </summary>
/// <param name="map_station"></param>
/// <param name="from_loc_x"></param>
/// <param name="from_loc_y"></param>
/// <returns></returns>
public static double CalculateDistance(this MapStation map_station, MapStation from_station)
{
return map_station.CalculateDistance(from_station.X, from_station.Y);
}
/// <summary>
/// 將整數轉成2進位,每個bit用boolean表示0/1 (0:false, 1:ture)
/// </summary>
/// <param name="int_val"></param>
/// <returns></returns>
public static bool[] To4Booleans(this int int_val)
{
bool[] bool_ary = new bool[4];
for (int i = 0; i < 4; i++)
{
bool_ary[i] = ((int_val >> i) & 1) != 1;
}
return bool_ary;
}
public static int ToInt(this bool[] bool_ary)
{
return BitConverter.ToInt16(bool_ary.Select(b => b ? (byte)0x1 : (byte)0x00).Reverse().ToArray(), 0);
}
}
}