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

View File

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

View File

@ -7,46 +7,39 @@ import lib.Pos3;
using tink.CoreApi;
class INS {
public static var instance:INS;
private var heading: Null<Pos3> = null;
private var alingment: Int = 1; // 0 = degraded, 1 = not aligned, 2 = aligned
@:allow(kernel.Init)
private function new() {
}
private static var heading: Null<Pos3> = null;
private static var alingment: Int = 1; // 0 = degraded, 1 = not aligned, 2 = aligned
@:allow(kernel.turtle.Turtle)
private function moveForward() {
private static function moveForward() {
if (heading == null) {
this.alingment = 0;
alingment = 0;
return;
}
move(heading);
}
@:allow(kernel.turtle.Turtle)
private function moveBackward() {
private static function moveBackward() {
if (heading == null) {
this.alingment = 0;
alingment = 0;
return;
}
move(heading.negate());
}
@:allow(kernel.turtle.Turtle)
private function moveUp() {
private static function moveUp() {
move({x: 0, y: 1, z: 0});
}
@:allow(kernel.turtle.Turtle)
private function moveDown() {
private static function moveDown() {
move({x: 0, y: -1, z: 0});
}
@:allow(kernel.turtle.Turtle)
private function turnLeft() {
private static function turnLeft() {
if (heading == null) return;
if (heading.x == 0 && heading.z == -1) {
heading = {x: -1, y: 0, z: 0};
@ -60,7 +53,7 @@ class INS {
}
@:allow(kernel.turtle.Turtle)
private function turnRight() {
private static function turnRight() {
if (heading == null) return;
if (heading.x == 0 && heading.z == -1) {
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');
var pos = GPS.getPosition();
var newPos = pos + dir;
GPS.setINSPosition(newPos);
}
public function getHeading():Null<Pos3> {
public static function getHeading():Null<Pos3> {
return heading;
}
public function align(): Promise<Noise> {
public static function align(): Promise<Noise> {
Log.info("Aligning INS");
return new Promise<Noise>((resolve,reject)->{
@ -118,14 +111,14 @@ class INS {
return;
}
var heading = calcHeading(pos1,pos2,moved);
if (heading == null) {
var cHeading = calcHeading(pos1,pos2,moved);
if (cHeading == null) {
Log.error("Can't calculate heading");
reject(new Error("Can't calculate heading"));
return;
}
this.heading = heading;
heading = cHeading;
moveBack(moved);
GPS.setINSPosition(pos1);
@ -138,7 +131,7 @@ class INS {
}
// -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()) {
return 0;
} 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) {
return pos1 - pos2;
} else if (moved == 1) {
@ -170,7 +163,7 @@ class INS {
}
}
private function moveBack(moved:Int) {
private static function moveBack(moved:Int) {
if (moved == 0) {
Turtle.instance.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) {
return {x: 1, y: 0, 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)));
}

View File

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