made GPS a static class

This commit is contained in:
Djeeberjr 2023-07-30 15:42:02 +02:00
parent 97c906e013
commit 5a9d463192
6 changed files with 37 additions and 39 deletions

View File

@ -16,13 +16,13 @@ class GPS extends CLIAppBase {
var pos: Pos3 = new Vec3<Float>(x, y, z); var pos: Pos3 = new Vec3<Float>(x, y, z);
kernel.gps.GPS.instance.setManualPosition(pos); kernel.gps.GPS.setManualPosition(pos);
return true; return true;
},"<x> <y> <z>"); },"<x> <y> <z>");
registerSyncSubcommand("status",(args)->{ registerSyncSubcommand("status",(args)->{
var pos = kernel.gps.GPS.instance.getPosition(); var pos = kernel.gps.GPS.getPosition();
if (pos != null) { if (pos != null) {
handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}'); handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}');
} else { } else {
@ -30,7 +30,7 @@ class GPS extends CLIAppBase {
return true; return true;
} }
var acc = kernel.gps.GPS.instance.getAccuracy(); var acc = kernel.gps.GPS.getAccuracy();
if (acc == 1){ if (acc == 1){
handle.writeLine("Accuracy: Low"); handle.writeLine("Accuracy: Low");
} else if (acc == 2){ } else if (acc == 2){
@ -50,7 +50,7 @@ class GPS extends CLIAppBase {
}); });
registerAsyncSubcommand("locate",(args)->{ registerAsyncSubcommand("locate",(args)->{
return kernel.gps.GPS.instance.locate().map((pos)->{ return kernel.gps.GPS.locate().map((pos)->{
if (pos != null) { if (pos != null) {
handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}'); handle.writeLine('Position x:${pos.x} y:${pos.y} z:${pos.z}');
} else { } else {

View File

@ -35,14 +35,14 @@ class PFClient implements Process {
} }
private function render() { private function render() {
var acc = kernel.gps.GPS.instance.getAccuracy(); var acc = kernel.gps.GPS.getAccuracy();
var pos: Pos3 = kernel.gps.GPS.instance.getPosition() ?? {x: 0, y: 0, z: 0}; var pos: Pos3 = kernel.gps.GPS.getPosition() ?? {x: 0, y: 0, z: 0};
var childre: Array<UIElement> = [ var childre: Array<UIElement> = [
new TextElement('Acc: ${acc}'), new TextElement('Acc: ${acc}'),
new TextElement('Pos: X:${pos.x} Y:${pos.y} Z:${pos.z}'), new TextElement('Pos: X:${pos.x} Y:${pos.y} Z:${pos.z}'),
new TextElement('UPDATE', { style: {bgColor: Gray}, uiEvents: {onClick: () -> { new TextElement('UPDATE', { style: {bgColor: Gray}, uiEvents: {onClick: () -> {
kernel.gps.GPS.instance.locate().handle((pos) ->{ kernel.gps.GPS.locate().handle((pos) ->{
this.requestRender(); this.requestRender();
}); });
}}}), }}}),

View File

@ -37,7 +37,7 @@ class Init {
Routing.init(); Routing.init();
Net.init(); Net.init();
GPS.instance = new GPS(); GPS.init();
INS.instance = new INS(); INS.instance = new INS();
// Register default terminate handler // Register default terminate handler

View File

@ -15,22 +15,20 @@ using tink.CoreApi;
You need at least 3 computers that know their position to determine the position of the computer. You need at least 3 computers that know their position to determine the position of the computer.
**/ **/
class GPS { class GPS {
public static var instance:GPS; private static var shouldRespond = true;
private static var shouldDoWholeNumberCheck = true;
private static var posAccuracy = 0; // 0 = unkown, 1 = (ins,best guess), 2 = (stored/manual,should be right), 3 = (gps,confirmed)
private static var cachedPosition:Pos3;
private static var lastPositionResponse: Array<{pos:Pos3,dist:Float}> = [];
private var shouldRespond = true; private static var futureResolve: (pos:Null<Pos3>) -> Void = null;
private var shouldDoWholeNumberCheck = true;
private var posAccuracy = 0; // 0 = unkown, 1 = (ins,best guess), 2 = (stored/manual,should be right), 3 = (gps,confirmed)
private var cachedPosition:Pos3;
private var lastPositionResponse: Array<{pos:Pos3,dist:Float}> = [];
private var futureResolve: (pos:Null<Pos3>) -> Void = null;
@:allow(kernel.Init) @:allow(kernel.Init)
private function new() { private static function init() {
this.loadCachedPosition(); loadCachedPosition();
} }
public function setManualPosition(pos:Pos3) { public static function setManualPosition(pos:Pos3) {
var kvstore = new KVStore("gps"); var kvstore = new KVStore("gps");
kvstore.set("mpos",pos); kvstore.set("mpos",pos);
kvstore.save(); kvstore.save();
@ -42,39 +40,39 @@ class GPS {
} }
@:allow(kernel.gps.INS) @:allow(kernel.gps.INS)
private function setINSPosition(pos:Pos3) { private static function setINSPosition(pos:Pos3) {
cachedPosition = pos; cachedPosition = pos;
posAccuracy = 1; posAccuracy = 1;
} }
public function getPosition():Null<Pos3> { public static function getPosition():Null<Pos3> {
return cachedPosition; return cachedPosition;
} }
public function getAccuracy():Int { public static function getAccuracy():Int {
return posAccuracy; return posAccuracy;
} }
public function invalidatePosition() { public static function invalidatePosition() {
cachedPosition = null; cachedPosition = null;
posAccuracy = 0; posAccuracy = 0;
} }
public function locate():Future<Null<Pos3>> { public static function locate():Future<Null<Pos3>> {
// TODO: implenet a timeout // TODO: implenet a timeout
// TODO: dont send a request twice if the last one is still pending or we moved // TODO: dont send a request twice if the last one is still pending or we moved
return new Future<Null<Pos3>>((resolve)->{ return new Future<Null<Pos3>>((resolve)->{
this.futureResolve = resolve; futureResolve = resolve;
sendPositionRequest(); sendPositionRequest();
return null; return null;
}); });
} }
private function resolveFuture(pos:Null<Pos3>) { private static function resolveFuture(pos:Null<Pos3>) {
this.futureResolve(pos); futureResolve(pos);
} }
private function persistCachedPositon() { private static function persistCachedPositon() {
if (cachedPosition == null) return; if (cachedPosition == null) return;
var kvstore = new KVStore("gps"); var kvstore = new KVStore("gps");
@ -82,7 +80,7 @@ class GPS {
kvstore.save(); kvstore.save();
} }
private function loadCachedPosition() { private static function loadCachedPosition() {
var kvstore = new KVStore("gps"); var kvstore = new KVStore("gps");
kvstore.load(); kvstore.load();
@ -119,12 +117,12 @@ class GPS {
} }
private function sendPositionRequest() { private static function sendPositionRequest() {
Net.brodcastGPSRequest(); Net.brodcastGPSRequest();
} }
@:allow(kernel.net.Net) @:allow(kernel.net.Net)
private function handlePackage(pack:Package<Noise>, dist: Float,iface: INetworkInterface) { private static function handlePackage(pack:Package<Noise>, dist: Float,iface: INetworkInterface) {
switch (pack.type) { switch (pack.type) {
case GPSRequest: case GPSRequest:
if (!shouldRespond) return; if (!shouldRespond) return;
@ -159,7 +157,7 @@ class GPS {
} }
} }
private function calculatePosition():Null<Pos3> { private static function calculatePosition():Null<Pos3> {
if (lastPositionResponse.length < 3) return null; if (lastPositionResponse.length < 3) return null;
// do a simple trilateration with the last 3 responses for now // do a simple trilateration with the last 3 responses for now
@ -210,7 +208,7 @@ class GPS {
/** /**
Determines the position(s) of a point given 3 other points and the distance to each of them. Determines the position(s) of a point given 3 other points and the distance to each of them.
**/ **/
private function trilateration(p1:Pos3,p2:Pos3,p3: Pos3,r1:Float,r2:Float,r3:Float):Pair<Pos3,Pos3> { private static function trilateration(p1:Pos3,p2:Pos3,p3: Pos3,r1:Float,r2:Float,r3:Float):Pair<Pos3,Pos3> {
var a2b = p2 - p1; var a2b = p2 - p1;
var a2c = p3 - p1; var a2c = p3 - p1;

View File

@ -75,9 +75,9 @@ class INS {
private function move(dir: Null<Pos3>) { private function move(dir: Null<Pos3>) {
Log.debug('INS move: $dir'); Log.debug('INS move: $dir');
var pos = GPS.instance.getPosition(); var pos = GPS.getPosition();
var newPos = pos + dir; var newPos = pos + dir;
GPS.instance.setINSPosition(newPos); GPS.setINSPosition(newPos);
} }
public function getHeading():Null<Pos3> { public function getHeading():Null<Pos3> {
@ -94,7 +94,7 @@ class INS {
return null; return null;
} }
GPS.instance.locate().handle((pos1)->{ GPS.locate().handle((pos1)->{
Log.debug('pos1: $pos1'); Log.debug('pos1: $pos1');
if (pos1 == null) { if (pos1 == null) {
Log.warn("GPS not available for 1st position"); Log.warn("GPS not available for 1st position");
@ -110,7 +110,7 @@ class INS {
return; return;
} }
GPS.instance.locate().handle((pos2)->{ GPS.locate().handle((pos2)->{
Log.debug('pos2: $pos2'); Log.debug('pos2: $pos2');
if (pos2 == null) { if (pos2 == null) {
Log.warn("GPS not available for 2nd position"); Log.warn("GPS not available for 2nd position");
@ -127,7 +127,7 @@ class INS {
this.heading = heading; this.heading = heading;
moveBack(moved); moveBack(moved);
GPS.instance.setINSPosition(pos1); GPS.setINSPosition(pos1);
resolve(Noise); resolve(Noise);
}); });

View File

@ -90,7 +90,7 @@ class Net {
return; return;
} }
// Delegate to GPS // Delegate to GPS
GPS.instance.handlePackage(cast pack,dist,interf); GPS.handlePackage(cast pack,dist,interf);
} }
}else{ }else{
// New message received but its not ment for us. Forward if possible. // New message received but its not ment for us. Forward if possible.