made GPS and INS a static class

This commit is contained in:
Djeeberjr 2023-07-30 15:45:30 +02:00
parent 5a9d463192
commit 3b3c69ee56
4 changed files with 29 additions and 37 deletions

View File

@ -39,7 +39,7 @@ class GPS extends CLIAppBase {
handle.writeLine("Accuracy: High"); handle.writeLine("Accuracy: High");
} }
var ins = INS.instance.getHeading(); var ins = INS.getHeading();
if (ins != null) { if (ins != null) {
handle.writeLine('INS heading: ${ins.x} y:${ins.y} z:${ins.z}'); handle.writeLine('INS heading: ${ins.x} y:${ins.y} z:${ins.z}');
} else { } else {
@ -61,7 +61,7 @@ class GPS extends CLIAppBase {
}); });
registerAsyncSubcommand("ins",(args)->{ registerAsyncSubcommand("ins",(args)->{
return INS.instance.align().map((_)->{ return INS.align().map((_)->{
handle.writeLine("INS aligned"); handle.writeLine("INS aligned");
return true; return true;
}); });

View File

@ -38,7 +38,6 @@ class Init {
Net.init(); Net.init();
GPS.init(); GPS.init();
INS.instance = new INS();
// Register default terminate handler // Register default terminate handler
KernelEvents.onTerminate.handle(_->{ KernelEvents.onTerminate.handle(_->{

View File

@ -7,46 +7,39 @@ import lib.Pos3;
using tink.CoreApi; using tink.CoreApi;
class INS { class INS {
public static var instance:INS; private static var heading: Null<Pos3> = null;
private static var alingment: Int = 1; // 0 = degraded, 1 = not aligned, 2 = aligned
private var heading: Null<Pos3> = null;
private var alingment: Int = 1; // 0 = degraded, 1 = not aligned, 2 = aligned
@:allow(kernel.Init)
private function new() {
}
@:allow(kernel.turtle.Turtle) @:allow(kernel.turtle.Turtle)
private function moveForward() { private static function moveForward() {
if (heading == null) { if (heading == null) {
this.alingment = 0; alingment = 0;
return; return;
} }
move(heading); move(heading);
} }
@:allow(kernel.turtle.Turtle) @:allow(kernel.turtle.Turtle)
private function moveBackward() { private static function moveBackward() {
if (heading == null) { if (heading == null) {
this.alingment = 0; alingment = 0;
return; return;
} }
move(heading.negate()); move(heading.negate());
} }
@:allow(kernel.turtle.Turtle) @:allow(kernel.turtle.Turtle)
private function moveUp() { private static function moveUp() {
move({x: 0, y: 1, z: 0}); move({x: 0, y: 1, z: 0});
} }
@:allow(kernel.turtle.Turtle) @:allow(kernel.turtle.Turtle)
private function moveDown() { private static function moveDown() {
move({x: 0, y: -1, z: 0}); move({x: 0, y: -1, z: 0});
} }
@:allow(kernel.turtle.Turtle) @:allow(kernel.turtle.Turtle)
private function turnLeft() { private static function turnLeft() {
if (heading == null) return; if (heading == null) return;
if (heading.x == 0 && heading.z == -1) { if (heading.x == 0 && heading.z == -1) {
heading = {x: -1, y: 0, z: 0}; heading = {x: -1, y: 0, z: 0};
@ -60,7 +53,7 @@ class INS {
} }
@:allow(kernel.turtle.Turtle) @:allow(kernel.turtle.Turtle)
private function turnRight() { private static function turnRight() {
if (heading == null) return; if (heading == null) return;
if (heading.x == 0 && heading.z == -1) { if (heading.x == 0 && heading.z == -1) {
heading = {x: 1, y: 0, z: 0}; heading = {x: 1, y: 0, z: 0};
@ -73,18 +66,18 @@ class INS {
} }
} }
private function move(dir: Null<Pos3>) { private static function move(dir: Null<Pos3>) {
Log.debug('INS move: $dir'); Log.debug('INS move: $dir');
var pos = GPS.getPosition(); var pos = GPS.getPosition();
var newPos = pos + dir; var newPos = pos + dir;
GPS.setINSPosition(newPos); GPS.setINSPosition(newPos);
} }
public function getHeading():Null<Pos3> { public static function getHeading():Null<Pos3> {
return heading; return heading;
} }
public function align(): Promise<Noise> { public static function align(): Promise<Noise> {
Log.info("Aligning INS"); Log.info("Aligning INS");
return new Promise<Noise>((resolve,reject)->{ return new Promise<Noise>((resolve,reject)->{
@ -118,14 +111,14 @@ class INS {
return; return;
} }
var heading = calcHeading(pos1,pos2,moved); var cHeading = calcHeading(pos1,pos2,moved);
if (heading == null) { if (cHeading == null) {
Log.error("Can't calculate heading"); Log.error("Can't calculate heading");
reject(new Error("Can't calculate heading")); reject(new Error("Can't calculate heading"));
return; return;
} }
this.heading = heading; heading = cHeading;
moveBack(moved); moveBack(moved);
GPS.setINSPosition(pos1); GPS.setINSPosition(pos1);
@ -138,7 +131,7 @@ class INS {
} }
// -1 = not moved, 0 = back, 1 = forward, 2 = left, 3 = right // -1 = not moved, 0 = back, 1 = forward, 2 = left, 3 = right
private function tryMoving():Int { private static function tryMoving():Int {
if (Turtle.instance.back().isSuccess()) { if (Turtle.instance.back().isSuccess()) {
return 0; return 0;
} else if (Turtle.instance.forward().isSuccess()) { } else if (Turtle.instance.forward().isSuccess()) {
@ -156,7 +149,7 @@ class INS {
} }
} }
private function calcHeading(pos1: Pos3,pos2:Pos3,moved:Int): Null<Pos3> { private static function calcHeading(pos1: Pos3,pos2:Pos3,moved:Int): Null<Pos3> {
if (moved == 0) { if (moved == 0) {
return pos1 - pos2; return pos1 - pos2;
} else if (moved == 1) { } else if (moved == 1) {
@ -170,7 +163,7 @@ class INS {
} }
} }
private function moveBack(moved:Int) { private static function moveBack(moved:Int) {
if (moved == 0) { if (moved == 0) {
Turtle.instance.forward(); Turtle.instance.forward();
// cc.Turtle.forward(); // cc.Turtle.forward();
@ -190,7 +183,7 @@ class INS {
} }
} }
private function rotatePos3ToRight(pos:Pos3):Pos3 { private static function rotatePos3ToRight(pos:Pos3):Pos3 {
if (pos.x == 0 && pos.z == -1) { if (pos.x == 0 && pos.z == -1) {
return {x: 1, y: 0, z: 0}; return {x: 1, y: 0, z: 0};
} else if (pos.x == -1 && pos.z == 0) { } else if (pos.x == -1 && pos.z == 0) {
@ -204,7 +197,7 @@ class INS {
} }
} }
private function rotatePos3ToLeft(pos3:Pos3):Pos3 { private static function rotatePos3ToLeft(pos3:Pos3):Pos3 {
return rotatePos3ToRight(rotatePos3ToRight(rotatePos3ToRight(pos3))); return rotatePos3ToRight(rotatePos3ToRight(rotatePos3ToRight(pos3)));
} }

View File

@ -36,42 +36,42 @@ class Turtle {
public function forward():Outcome<Noise, String> { public function forward():Outcome<Noise, String> {
var r = cc.Turtle.forward(); var r = cc.Turtle.forward();
var r2 = conterToOutcome(r); var r2 = conterToOutcome(r);
if (r2.isSuccess()) INS.instance.moveForward(); if (r2.isSuccess()) INS.moveForward();
return r2; return r2;
} }
public function back():Outcome<Noise, String> { public function back():Outcome<Noise, String> {
var r = cc.Turtle.back(); var r = cc.Turtle.back();
var r2 = conterToOutcome(r); var r2 = conterToOutcome(r);
if (r2.isSuccess()) INS.instance.moveBackward(); if (r2.isSuccess()) INS.moveBackward();
return r2; return r2;
} }
public function up():Outcome<Noise, String> { public function up():Outcome<Noise, String> {
var r = cc.Turtle.up(); var r = cc.Turtle.up();
var r2 = conterToOutcome(r); var r2 = conterToOutcome(r);
if (r2.isSuccess()) INS.instance.moveUp(); if (r2.isSuccess()) INS.moveUp();
return r2; return r2;
} }
public function down():Outcome<Noise, String> { public function down():Outcome<Noise, String> {
var r = cc.Turtle.down(); var r = cc.Turtle.down();
var r2 = conterToOutcome(r); var r2 = conterToOutcome(r);
if (r2.isSuccess()) INS.instance.moveDown(); if (r2.isSuccess()) INS.moveDown();
return r2; return r2;
} }
public function turnLeft():Outcome<Noise, String> { public function turnLeft():Outcome<Noise, String> {
var r = cc.Turtle.turnLeft(); var r = cc.Turtle.turnLeft();
var r2 = conterToOutcome(r); var r2 = conterToOutcome(r);
if (r2.isSuccess()) INS.instance.turnRight(); if (r2.isSuccess()) INS.turnRight();
return r2; return r2;
} }
public function turnRight():Outcome<Noise, String> { public function turnRight():Outcome<Noise, String> {
var r = cc.Turtle.turnRight(); var r = cc.Turtle.turnRight();
var r2 = conterToOutcome(r); var r2 = conterToOutcome(r);
if (r2.isSuccess()) INS.instance.turnRight(); if (r2.isSuccess()) INS.turnRight();
return r2; return r2;
} }