-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenericGPSService.cs
168 lines (142 loc) · 6.08 KB
/
GenericGPSService.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
using Microsoft.Ccr.Core;
using Microsoft.Dss.Core;
using Microsoft.Dss.Core.Attributes;
using Microsoft.Dss.ServiceModel.Dssp;
using Microsoft.Dss.Core.DsspHttp;
using Microsoft.Dss.ServiceModel.DsspServiceBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml;
using W3C.Soap;
using robomagellan = RoboMagellan;
using submgr = Microsoft.Dss.Services.SubscriptionManager;
namespace RoboMagellan.GenericGPS
{
[DisplayName("GenericGPS")]
[Description("The GenericGPS Service")]
[Contract(Contract.Identifier)]
public class GenericGPSService : DsspServiceBase
{
private static string PORT = "COM8";
private const int MAX_SAT = 12;
private GenericGPSState _state = new GenericGPSState();
private AC12GPS _gps = null;
private GpsDataPort _gpsDataPort = new GpsDataPort();
private bool first_coord = true;
private const double FILTER_DIVISOR = 1.5;
private const double FILTER_MULTIPLIER = 0.3;
private const double FILTER_CAP = 2.0;
private const double FILTER_OFFSET = 0.4;
[ServicePort("/gps", AllowMultipleInstances = false)]
private GenericGPSOperations _mainPort = new GenericGPSOperations();
[Partner("SubMgr", Contract = submgr.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.CreateAlways)]
submgr.SubscriptionManagerPort _subMgrPort = new submgr.SubscriptionManagerPort();
public GenericGPSService(DsspServiceCreationPort creationPort)
: base(creationPort)
{
}
protected override void Start()
{
DirectoryInsert();
_gps = new AC12GPS(PORT, _gpsDataPort);
SpawnIterator(activateGPSIterator);
Interleave mainInterleave = ActivateDsspOperationHandlers();
mainInterleave.CombineWith(new Interleave(new ExclusiveReceiverGroup(
Arbiter.Receive<UTMData>(true, _gpsDataPort, DataReceivedHandler)),
new ConcurrentReceiverGroup()));
//base.Start();
}
public IEnumerator<ITask> activateGPSIterator()
{
if (!_gps.initializePort())
{
yield break;
}
else
{
_gps.command("$PASHS,PWR,ON");
yield return Arbiter.Receive(false, TimeoutPort(10), delegate(DateTime time) { });
_gps.command("$PASHQ,PRT");
yield return Arbiter.Receive(false, TimeoutPort(10), delegate(DateTime time) { });
_gps.command("$PASHQ,RID");
yield return Arbiter.Receive(false, TimeoutPort(10), delegate(DateTime time) { });
_gps.command("$PASHS,OUT,A,NMEA");
yield return Arbiter.Receive(false, TimeoutPort(10), delegate(DateTime time) { });
_gps.command("$PASHS,NME,UTM,A,ON"); //Make GPS send UTM coordinate string
yield return Arbiter.Receive(false, TimeoutPort(10), delegate(DateTime time) { });
_gps.activateHandler();
}
}
[ServiceHandler(ServiceHandlerBehavior.Concurrent)]
public IEnumerator<ITask> GetHandler(Get get)
{
get.ResponsePort.Post(_state);
yield break;
}
[ServiceHandler(ServiceHandlerBehavior.Concurrent)]
public virtual IEnumerator<ITask> HttpGetHandler(HttpGet httpGet)
{
httpGet.ResponsePort.Post(new HttpResponseType(_state.Coords));
yield break;
}
[ServiceHandler(ServiceHandlerBehavior.Concurrent)]
public IEnumerator<ITask> HttpPostHandler(HttpPost httpPost)
{
yield break;
}
[ServiceHandler(ServiceHandlerBehavior.Concurrent)]
public virtual IEnumerator<ITask> SubscribeHandler(Subscribe subscribe)
{
SubscribeRequestType request = subscribe.Body;
Console.WriteLine("GPS received subscribe request");
yield return Arbiter.Choice(
SubscribeHelper(_subMgrPort, request, subscribe.ResponsePort),
delegate(SuccessResult success)
{
Console.WriteLine("Subscription confirmed");
SendNotification<UTMNotification>(_subMgrPort, request.Subscriber, _state.Coords);
},
delegate(Exception e)
{
LogError(null, "Subscribe failed", e);
}
);
yield break;
}
private void DataReceivedHandler(UTMData d)
{
//Console.WriteLine("Received GPS data");
/*Implements a simple filter that uses the DOP (Number of Satellites)
* information from the measurements to weight the GPS data */
if (!first_coord)
{
/*float alpha = 2 * (float)d.NumSat / (float)MAX_SAT;
d.East = (1 - alpha) * _state.Coords.East + d.East;
d.North = (1 - alpha) * _state.Coords.North + d.North;
_state.Coords = d;*/
d = getFilteredData(d);
}
else
{
_state.Coords = d;
first_coord = false;
}
SendNotification(_subMgrPort, new UTMNotification(_state.Coords));
}
private UTMData getFilteredData(UTMData n)
{
double dx = n.East - _state.Coords.East;
double dy = n.North - _state.Coords.North;
double dist = Math.Sqrt(dx * dx + dy * dy);
dist /= FILTER_DIVISOR;
if (dist > FILTER_CAP) dist = FILTER_CAP;
dist *= FILTER_MULTIPLIER;
dist += FILTER_OFFSET;
n.North = _state.Coords.North + dist * dy;
n.East = _state.Coords.East + dist*dx;
_state.Coords = n;
return n;
}
}
}