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
|
||||
*/
|
||||
async gpsLocate() {
|
||||
const result = await this.exec('return gps.locate(5)');
|
||||
if (result && typeof result === 'object' && result[1] != null) {
|
||||
this.position = { x: Math.floor(result[1]), y: Math.floor(result[2]), z: Math.floor(result[3]) };
|
||||
} 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) };
|
||||
}
|
||||
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 && result.x != null) {
|
||||
this.position = { x: Math.floor(result.x), y: Math.floor(result.y), z: Math.floor(result.z) };
|
||||
}
|
||||
return this._position;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user