style: Refactor gpsLocate method for improved position handling and code clarity
This commit is contained in:
@@ -745,15 +745,9 @@ export class Turtle extends EventEmitter {
|
|||||||
* GPS locate
|
* GPS locate
|
||||||
*/
|
*/
|
||||||
async gpsLocate() {
|
async gpsLocate() {
|
||||||
const result = await this.exec('return gps.locate(5)');
|
const result = await this.exec('local x,y,z = gps.locate(5); if x then return {x=x, y=y, z=z} else return nil end');
|
||||||
if (result && typeof result === 'object' && result[1] != null) {
|
if (result && result.x != null) {
|
||||||
this.position = { x: Math.floor(result[1]), y: Math.floor(result[2]), z: Math.floor(result[3]) };
|
this.position = { x: Math.floor(result.x), y: Math.floor(result.y), z: Math.floor(result.z) };
|
||||||
} else if (typeof result === 'number') {
|
|
||||||
// Some versions return x,y,z as separate values
|
|
||||||
const pos = await this.exec('local x,y,z = gps.locate(5); return {x=x, y=y, z=z}');
|
|
||||||
if (pos && pos.x != null) {
|
|
||||||
this.position = { x: Math.floor(pos.x), y: Math.floor(pos.y), z: Math.floor(pos.z) };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return this._position;
|
return this._position;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user