-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDJI_SDK.cs
More file actions
607 lines (535 loc) · 23.4 KB
/
DJI_SDK.cs
File metadata and controls
607 lines (535 loc) · 23.4 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
using System;
using UnityEngine;
using SimpleJSON;
using ROSBridgeLib;
using ROSBridgeLib.geometry_msgs;
using ROSBridgeLib.sensor_msgs;
using ROSBridgeLib.std_msgs;
using ROSBridgeLib.dji_msgs;
public class DJI_SDK : MonoBehaviour, ROSTopicSubscriber
{
static int CLIENT_ID = 0;
string client_id;
public enum FlightStatusM100
{
ON_GROUND_STANDBY = 1,
TAKEOFF = 2,
IN_AIR_STANDBY = 3,
LANDING = 4,
FINISHING_LANDING = 5
}
public enum FlightStatus
{
STOPED = 0,
ON_GROUND = 1,
IN_AIR = 2
}
public enum CameraAction
{
SHOOT_PHOTO = 0,
START_VIDEO = 1,
STOP_VIDEO = 2
}
public enum DroneTask
{
GO_HOME = 1,
TAKEOFF = 4,
LAND = 6
}
public enum WaypointMissionAction
{
START = 0,
STOP = 1,
PAUSE = 2,
RESUME = 3
}
public string onboard_computer_address = "";
public int rosbridge_port = 9090;
public bool isM100 = true;
private ROSBridgeWebSocketConnection ros = null;
public bool sdk_ready
{
get
{
return ros != null;
}
}
//bool requested_authority = false;
bool has_authority = false;
BatteryStateMsg battery_state;
FlightStatusM100 m100_flight_status;
FlightStatus flight_status;
JoyMsg remote_controller_msg;
Quaternion attitude = Quaternion.identity;
Quaternion offset = Quaternion.Euler(90, 180, 0);
IMUMsg imu;
Vector3 velocity;
float relative_altitude; // Height above takeoff height.
Vector3 local_position;
Vector3 gimble_joint_angles;
uint gps_health;
NavSatFixMsg gps_position;
// Use this for initialization
void Start()
{
client_id = (CLIENT_ID++).ToString();
ConnectToRos();
}
void ConnectToRos()
{
if (ros == null)
{
ROSBridgeConnectionsManager manager = ROSBridgeConnectionsManager.Instance;
if (manager != null)
{
ros = manager.GetConnection(onboard_computer_address, rosbridge_port);
if (ros != null)
{
subscribe_to_all_sdk_topics(ros);
publish_to_all_sdk_topics(ros);
}
}
}
}
// Update is called once per frame in Unity
void Update()
{
if (ros == null)
{
ConnectToRos();
}
}
public void FetchDroneVersion()
{
string service_name = "dji_sdk/query_drone_version";
ros.CallService(HandleDroneVersionResponse, service_name, string.Format("{0} {1}", client_id, service_name));
}
void HandleDroneVersionResponse(JSONNode response)
{
response = response["values"];
Debug.LogFormat("Drone: {0} (Version {1})", response["hardware"].Value, response["version"].AsInt);
}
public void ActivateDrone()
{
string service_name = "/dji_sdk/activation";
ros.CallService(HandleActivationResponse, service_name, string.Format("{0} {1}", client_id, service_name));
}
void HandleActivationResponse(JSONNode response)
{
response = response["values"];
Debug.LogFormat("Activation {0} (ACK: {1})", (response["result"].AsBool ? "succeeded" : "failed"), response["ack_data"].AsInt);
}
public void SetSDKControl(bool control)
{
string service_name = "/dji_sdk/sdk_control_authority";
ros.CallService(HandleSetSDKControlResponse, service_name, string.Format("{0} {1}", client_id, service_name), string.Format("[{0}]", (control ? 1 : 0)));
has_authority = control;
}
void HandleSetSDKControlResponse(JSONNode response)
{
response = response["values"];
Debug.Log(response.ToString());
Debug.LogFormat("Control request {0} (ACK: {1})", (response["result"].AsBool ? "succeeded" : "failed"), response["ack_data"].AsInt);
//if (response["result"].AsBool == true)
//{
// has_authority = requested_authority;
//}
}
public void ChangeArmStatusTo(bool armed)
{
string service_name = "/dji_sdk/drone_arm_control";
ros.CallService(HandleArmResponse, service_name, string.Format("{0} {1}", client_id, service_name), string.Format("[{0}]", (armed ? 1 : 0)));
}
void HandleArmResponse(JSONNode response)
{
response = response["values"];
Debug.LogFormat("Arm/Disarm request {0} (ACK: {1})", (response["result"].AsBool ? "succeeded" : "failed"), response["ack_data"].AsInt);
}
public void ExecuteTask(DroneTask task)
{
string service_name = "/dji_sdk/drone_task_control";
ros.CallService(HandleTaskResponse, service_name, string.Format("{0} {1}", client_id, service_name), string.Format("[{0}]", (int)task));
}
void HandleTaskResponse(JSONNode response)
{
response = response["values"];
Debug.LogFormat("Task request {0} (ACK: {1})", (response["result"].AsBool ? "succeeded" : "failed"), response["ack_data"].AsInt);
}
public void SetLocalPosOriginToCurrentLocation()
{
string service_name = "/dji_sdk/set_local_pos_ref";
ros.CallService(HandleSetLocalPosOriginResponse, service_name, string.Format("{0} {1}", client_id, service_name));
}
void HandleSetLocalPosOriginResponse(JSONNode response)
{
response = response["values"];
Debug.LogFormat("Local position origin set {0}", (response["result"].AsBool ? "succeeded" : "failed"));
}
public void ExecuteCameraAction(CameraAction action)
{
string service_name = "/dji_sdk/camera_action";
ros.CallService(HandleCameraActionResponse, service_name, string.Format("{0} {1}", client_id, service_name), args: string.Format("[{0}]", (int)action));
}
void HandleCameraActionResponse(JSONNode response)
{
response = response["values"];
Debug.LogFormat("Camera action {0}", (response["result"].AsBool ? "succeeded" : "failed"));
}
public void FetchMissionStatus()
{
string service_name = "/dji_sdk/mission_status";
ros.CallService(HandleMissionStatusResponse, service_name, string.Format("{0} {1}", client_id, service_name));
}
void HandleMissionStatusResponse(JSONNode response)
{
response = response["values"];
Debug.LogFormat("Waypoint Count: {0}\nHotpoint Count: {1}", response["waypoint_mission_count"], response["hotpoint_mission_count"]);
}
public void UploadWaypointsTask(MissionWaypointTaskMsg task)
{
string service_name = "/dji_sdk/mission_waypoint_upload";
ros.CallService(HandleUploadWaypointsTaskResponse, service_name, string.Format("{0} {1}", client_id, service_name), args: string.Format("[{0}]", task.ToYAMLString()));
}
void HandleUploadWaypointsTaskResponse(JSONNode response)
{
response = response["values"];
Debug.LogFormat("Waypoint task upload {0} (ACK: {1})", (response["result"].AsBool ? "succeeded" : "failed"), response["ack_data"].AsInt);
}
public void SendWaypointAction(WaypointMissionAction action)
{
string service_name = "/dji_sdk/mission_waypoint_action";
ros.CallService(HandleWaypointActionResponse, service_name, string.Format("{0} {1}", client_id, service_name), args: string.Format("[{0}]", action));
}
void HandleWaypointActionResponse(JSONNode response)
{
response = response["values"];
Debug.LogFormat("Waypoint action {0} (ACK: {1})", (response["result"].AsBool ? "succeeded" : "failed"), response["ack_data"].AsInt);
}
public void FetchCurrentWaypointMission()
{
string service_name = "/dji_sdk/mission_waypoint_getInfo";
ros.CallService(HandleCurrentWaypointMissionResponse, service_name, string.Format("{0} {1}", client_id, service_name));
}
void HandleCurrentWaypointMissionResponse(JSONNode response)
{
MissionWaypointTaskMsg waypoint_task = new MissionWaypointTaskMsg(response["values"]);
Debug.LogFormat("Current waypoint mission: \n{0}", waypoint_task.ToYAMLString());
}
public void FetchWaypointSpeed()
{
string service_name = "/dji_sdk/mission_waypoint_getSpeed";
ros.CallService(HandleWaypointSpeedResponse, service_name, string.Format("{0} {1}", client_id, service_name));
}
void HandleWaypointSpeedResponse(JSONNode response)
{
response = response["values"];
Debug.LogFormat("Current waypoint speed: {0}", response["speed"].AsFloat);
}
public void SetWaypointSpeed(float speed)
{
string service_name = "/dji_sdk/mission_waypoint_setSpeed";
ros.CallService(HandleSetWaypointSpeedResponse, service_name, string.Format("{0} {1}", client_id, service_name), args: string.Format("[{0}]", speed));
}
void HandleSetWaypointSpeedResponse(JSONNode response)
{
response = response["values"];
Debug.LogFormat("Set waypoint speed {0} (ACK: {1})", (response["result"].AsBool ? "succeeded" : "failed"), response["ack_data"].AsInt);
}
public void Subscribe240p(bool front_right, bool front_left, bool down_front, bool down_back)
{
string serviceName = "/dji_sdk/stereo_240p_subscription";
string id = string.Format("{0} {1} subscribe", client_id, serviceName);
string args = string.Format("[{0} {1} {2} {3} 0]", front_right ? 1 : 0, front_left ? 1 : 0, down_front ? 1 : 0, down_back ? 1 : 0);
ros.CallService(HandleSubscribe240pResponse, serviceName, id, args);
}
void HandleSubscribe240pResponse(JSONNode response)
{
response = response["values"];
Debug.Log("Subscribe to 240p feeds " + ((response["result"].AsBool) ? "succeeded" : "failed"));
}
public void Unsubscribe240p()
{
string serviceName = "/dji_sdk/stereo_240p_subscription";
string id = string.Format("{0} {1} unsubscribe", client_id, serviceName);
ros.CallService(HandleUnsubscribe240pResponse, serviceName, id, "[0 0 0 0 1]");
}
void HandleUnsubscribe240pResponse(JSONNode response)
{
response = response["values"];
Debug.Log("Unsubscribe to 240p feeds " + ((response["result"].AsBool) ? "succeeded" : "failed"));
}
public void SubscribeDepthFront()
{
string serviceName = "/dji_sdk/stereo_depth_subscription";
string id = string.Format("{0} {1} subscribe", client_id, serviceName);
ros.CallService(HandleSubscribeDepthFrontResponse, serviceName, id, "[1 0]");
}
void HandleSubscribeDepthFrontResponse(JSONNode response)
{
response = response["values"];
Debug.Log("Subscribe front depth feed " + ((response["result"].AsBool) ? "succeeded" : "failed"));
}
public void UnsubscribeDepthFront()
{
string serviceName = "/dji_sdk/stereo_depth_subscription";
string id = string.Format("{0} {1} unsubscribe", client_id, serviceName);
ros.CallService(HandleUnsubscribeDepthFrontResponse, serviceName, id, "[0 1]");
}
void HandleUnsubscribeDepthFrontResponse(JSONNode response)
{
response = response["values"];
Debug.Log("Unsubscribe front depth feed " + ((response["result"].AsBool) ? "succeeded" : "failed"));
}
public void SubscribeVGAFront(bool use_20Hz)
{
string serviceName = "/dji_sdk/stereo_vga_subscription";
string id = string.Format("{0} {1} subscribe", client_id, serviceName);
ros.CallService(HandleSubscribeVGAFrontResponse, serviceName, id, string.Format("[{0} 1 0]", use_20Hz ? 0 : 1));
}
void HandleSubscribeVGAFrontResponse(JSONNode response)
{
response = response["values"];
Debug.Log("Subscribe VGA front feed " + ((response["result"].AsBool) ? "succeeded" : "failed"));
}
public void UnsubscribeVGAFront()
{
string serviceName = "/dji_sdk/stereo_vga_subscription";
string id = string.Format("{0} {1} unsubscribe", client_id, serviceName);
ros.CallService(HandleUnsubscribeVGAFrontResponse, serviceName, id, "[0 0 1]");
}
void HandleUnsubscribeVGAFrontResponse(JSONNode response)
{
response = response["values"];
Debug.Log("Unsubscribe VGA front feed " + ((response["result"].AsBool) ? "succeeded" : "failed"));
}
public void SubscribeFPV()
{
string serviceName = "/dji_sdk/setup_camera_stream";
string id = string.Format("{0} {1} subscribe FPV", client_id, serviceName);
ros.CallService(HandleSubscribeFPVResponse, serviceName, id, "[0 1]");
}
void HandleSubscribeFPVResponse(JSONNode response)
{
response = response["values"];
Debug.Log("Subscribe FPV feed " + ((response["result"].AsBool) ? "succeeded" : "failed"));
}
public void UnsubscribeFPV()
{
string serviceName = "/dji_sdk/setup_camera_stream";
string id = string.Format("{0} {1} unsubscribe FPV", client_id, serviceName);
ros.CallService(HandleUnsubscribeFPVResponse, serviceName, id, "[0 0]");
}
void HandleUnsubscribeFPVResponse(JSONNode response)
{
response = response["values"];
Debug.Log("Unsubscribe FPV feed " + ((response["result"].AsBool) ? "succeeded" : "failed"));
}
public void SubscribeMainCamera()
{
string serviceName = "/dji_sdk/setup_camera_stream";
string id = string.Format("{0} {1} subscribe MainCamera", client_id, serviceName);
ros.CallService(HandleSubscribeMainCameraResponse, serviceName, id, "[1 1]");
}
void HandleSubscribeMainCameraResponse(JSONNode response)
{
response = response["values"];
Debug.Log("Subscribe MainCamera feed " + ((response["result"].AsBool) ? "succeeded" : "failed"));
}
public void UnsubscribeMainCamera()
{
string serviceName = "/dji_sdk/setup_camera_stream";
string id = string.Format("{0} {1} unsubscribe MainCamera", client_id, serviceName);
ros.CallService(HandleUnsubscribeMainCameraResponse, serviceName, id, "[1 0]");
}
void HandleUnsubscribeMainCameraResponse(JSONNode response)
{
response = response["values"];
Debug.Log("Unsubscribe MainCamera feed " + ((response["result"].AsBool) ? "succeeded" : "failed"));
}
public bool HasAuthority()
{
return has_authority;
}
public FlightStatusM100 GetFlightStatusM100()
{
return m100_flight_status;
}
public FlightStatus GetFlightStatus()
{
return flight_status;
}
public Quaternion GetAttitude()
{
return attitude;
}
public NavSatFixMsg GetGPSPosition()
{
return gps_position;
}
public float GetHeightAboveTakeoff()
{
return relative_altitude;
}
public Vector3 GetLocalPosition()
{
return local_position;
}
DateTime epoch = new DateTime(1970, 1, 1);
int[] dummy = new int[0];
int seq_no_point = 0;
public void PublishRelativeSetPoint(Vector2 position_offset, float relative_altitude, float yaw_angle)
{
int num_seconds = DateTime.UtcNow.Second - epoch.Second;
HeaderMsg header = new HeaderMsg(seq_no_point++, new TimeMsg(num_seconds, (int)(DateTime.UtcNow.Ticks - ((long)num_seconds) * (10000000))), "");
float[] axes = new float[4];
axes[0] = position_offset.x;
axes[1] = position_offset.y;
axes[2] = relative_altitude;
axes[3] = yaw_angle;
JoyMsg msg = new JoyMsg(header, axes, dummy);
ros.Publish("/dji_sdk/flight_control_setpoint_ENUposition_yaw", msg);
}
int seq_no_rate = 0;
public void PublishRateSetpoint(float roll, float pitch, float height, float yaw_rate)
{
int num_seconds = DateTime.UtcNow.Second - epoch.Second;
HeaderMsg header = new HeaderMsg(seq_no_rate++, new TimeMsg(num_seconds, (int)(DateTime.UtcNow.Ticks - ((long)num_seconds) * (10000000))), "");
float[] axes = new float[4];
axes[0] = Mathf.Deg2Rad * roll;
axes[1] = Mathf.Deg2Rad * pitch;
axes[2] = height;
axes[3] = yaw_rate;
JoyMsg msg = new JoyMsg(header, axes, dummy);
ros.Publish("/dji_sdk/flight_control_setpoint_rollpitch_yawrate_zposition", msg);
}
public ROSBridgeMsg OnReceiveMessage(string topic, JSONNode raw_msg, ROSBridgeMsg parsed = null)
{
ROSBridgeMsg result = null;
// Writing all code in here for now. May need to move out to separate handler functions when it gets too unwieldy.
switch (topic)
{
case "/dji_sdk/attitude":
QuaternionMsg attitudeMsg = (parsed == null) ? new QuaternionMsg(raw_msg["quaternion"]) : (QuaternionMsg)parsed;
attitude = offset * (new Quaternion(attitudeMsg.GetX(), attitudeMsg.GetY(), attitudeMsg.GetZ(), attitudeMsg.GetW()));
result = attitudeMsg;
break;
case "/dji_sdk/battery_state":
battery_state = (parsed == null) ? new BatteryStateMsg(raw_msg) : (BatteryStateMsg)parsed;
result = battery_state;
break;
case "/dji_sdk/flight_status":
if (isM100)
{
m100_flight_status = (FlightStatusM100)(new UInt8Msg(raw_msg)).GetData();
} else
{
flight_status = (FlightStatus)(new UInt8Msg(raw_msg)).GetData();
}
break;
case "/dji_sdk/gimbal_angle":
Vector3Msg gimbleAngleMsg = (parsed == null) ? new Vector3Msg(raw_msg["vector"]) : (Vector3Msg)parsed;
gimble_joint_angles = new Vector3((float)gimbleAngleMsg.GetX(), (float)gimbleAngleMsg.GetY(), (float)gimbleAngleMsg.GetZ());
result = gimbleAngleMsg;
break;
case "/dji_sdk/gps_health":
gps_health = (parsed == null) ? (new UInt8Msg(raw_msg)).GetData() : ((UInt8Msg)parsed).GetData();
break;
case "/dji_sdk/gps_position":
gps_position = (parsed == null) ? new NavSatFixMsg(raw_msg) : (NavSatFixMsg)parsed;
result = gps_position;
break;
case "/dji_sdk/imu":
imu = (parsed == null) ? new IMUMsg(raw_msg) : (IMUMsg)parsed;
result = imu;
break;
case "/dji_sdk/rc":
remote_controller_msg = (parsed == null) ? new JoyMsg(raw_msg) : (JoyMsg)parsed;
result = remote_controller_msg;
break;
case "/dji_sdk/velocity":
Vector3Msg velocityMsg = (parsed == null) ? new Vector3Msg(raw_msg["vector"]) : (Vector3Msg)parsed;
velocity = new Vector3((float)velocityMsg.GetX(), (float)velocityMsg.GetY(), (float)velocityMsg.GetZ());
result = velocityMsg;
break;
case "/dji_sdk/height_above_takeoff":
relative_altitude = (parsed == null) ? (new Float32Msg(raw_msg)).GetData() : ((Float32Msg)parsed).GetData();
break;
case "/dji_sdk/local_position":
PointMsg pointMsg = (parsed == null) ? new PointMsg(raw_msg["point"]) : (PointMsg)parsed;
local_position = new Vector3(pointMsg.GetX(), pointMsg.GetY(), pointMsg.GetZ());
result = pointMsg;
Debug.Log(result);
break;
default:
Debug.LogError("Topic not implemented: " + topic);
break;
}
return result;
}
public string GetMessageType(string topic)
{
switch (topic)
{
case "/dji_sdk/attitude":
return "geometry_msgs/QuaternionStamped";
case "/dji_sdk/battery_state":
return "sensor_msgs/BatteryState";
case "/dji_sdk/flight_status":
return "std_msgs/UInt8";
case "/dji_sdk/gimbal_angle":
return "geometry_msgs/Vector3Stamped";
case "/dji_sdk/gps_health":
return "std_msgs/UInt8";
case "/dji_sdk/gps_position":
return "sensor_msgs/NavSatFix";
case "/dji_sdk/imu":
return "sensor_msgs/Imu";
case "/dji_sdk/rc":
return "sensor_msgs/Joy";
case "/dji_sdk/velocity":
return "geometry_msgs/Vector3Stamped";
case "/dji_sdk/height_above_takeoff":
return "std_msgs/Float32";
case "/dji_sdk/local_position":
return "geometry_msgs/PointStamped";
}
Debug.LogError("Topic " + topic + " not registered.");
return "";
}
private void subscribe_to_all_sdk_topics(ROSBridgeWebSocketConnection connection)
{
connection.AddSubscriber("/dji_sdk/attitude", this);
connection.AddSubscriber("/dji_sdk/battery_state", this);
connection.AddSubscriber("/dji_sdk/flight_status", this);
connection.AddSubscriber("/dji_sdk/gimbal_angle", this);
connection.AddSubscriber("/dji_sdk/gps_health", this);
connection.AddSubscriber("/dji_sdk/gps_position", this);
connection.AddSubscriber("/dji_sdk/imu", this);
connection.AddSubscriber("/dji_sdk/rc", this);
connection.AddSubscriber("/dji_sdk/velocity", this);
connection.AddSubscriber("/dji_sdk/height_above_takeoff", this);
connection.AddSubscriber("/dji_sdk/local_position", this);
}
private void publish_to_all_sdk_topics(ROSBridgeWebSocketConnection connection)
{
connection.AddPublisher("/dji_sdk/flight_control_setpoint_ENUposition_yaw", "sensor_msgs/Joy");
connection.AddPublisher("/dji_sdk/flight_control_setpoint_rollpitch_yawrate_zposition", "sensor_msgs/Joy");
}
const float EARTH_RADIUS_METERS = 6378100;
public static Vector2 VectorFromGPS(NavSatFixMsg position1, NavSatFixMsg position2)
{
double lon_diff_rad = Mathf.Deg2Rad * (position2.GetLongitude() - position1.GetLongitude());
double a = Math.Pow(Math.Sin(Mathf.Deg2Rad * (position2.GetLatitude() - position1.GetLatitude()) * 0.5), 2) +
Math.Cos(Mathf.Deg2Rad * position1.GetLatitude()) * Math.Cos(Mathf.Deg2Rad * position2.GetLatitude()) *
Math.Pow(Math.Sin(lon_diff_rad * 0.5), 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double distance = EARTH_RADIUS_METERS * c;
double x = Math.Cos(Mathf.Deg2Rad * position1.GetLatitude()) * Math.Sin(Mathf.Deg2Rad * position2.GetLatitude()) -
Math.Sin(Mathf.Deg2Rad * position1.GetLatitude()) * Math.Cos(Mathf.Deg2Rad * position2.GetLatitude()) *
Math.Cos(lon_diff_rad);
double y = Math.Cos(Mathf.Deg2Rad * position2.GetLatitude()) * Math.Sin(lon_diff_rad);
double bearing_angle = Math.Atan2(y, x);
Vector2 bearing = new Vector2((float)Math.Sin(bearing_angle), (float)Math.Cos(bearing_angle));
return bearing * (float)distance;
}
}