From 555b540071f63cbf41cb3966024ec06a896fc069 Mon Sep 17 00:00:00 2001 From: demoCrash <173336348@qq.com> Date: Wed, 14 Nov 2018 16:31:36 +0800 Subject: [PATCH] modify 020 error when rotate at speed --- ovobot.ts | 35 ++++++++++++++++++++--------------- pxt.json | 2 +- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/ovobot.ts b/ovobot.ts index 239d190..8597167 100644 --- a/ovobot.ts +++ b/ovobot.ts @@ -134,6 +134,7 @@ namespace ovobot { let rotate_total = 0; let rotate_target = 0; let previous_rotateMicroTimes = 0; + let start_heading = 0; //private function function setupMotorPWM() { @@ -387,7 +388,7 @@ namespace ovobot { } function updateLightStrength() { - + _lightStrength = input.lightLevel(); } function updateLineSensor() { @@ -408,6 +409,20 @@ namespace ovobot { updateDistance(); updateLightStrength(); updateLineSensor(); + if (robot.rotateSpeed != 0) { + //music.playTone(262, music.beat(BeatFraction.Whole)) + let current = input.runningTimeMicros(); + let delta = current - previous_rotateMicroTimes; + robot.heading = start_heading + robot.rotateSpeed * delta / 1000000.0; + robot.heading = robot.heading % 360; + if (robot.heading < 0) { + robot.heading += 360; + } + if (robot.heading > 360) { + robot.heading -= 360; + } + appMove(robot.heading, robot.speed, RobotMoveType.RobotMove); + } pidController(); // serial.writeNumber(_lineSensorLeft); // serial.writeString(" "); @@ -578,23 +593,13 @@ namespace ovobot { while (!gyroWorked) { basic.pause(20); } + if (angleSpeed == robot.rotateSpeed) { + return; + } angleSpeed = constract(angleSpeed, -255, 255); robot.rotateSpeed = angleSpeed; + start_heading = robot.heading; previous_rotateMicroTimes = input.runningTimeMicros(); - continueRotateAtSpeed(robot.heading); - //appMove(robot.heading, robot.speed, RobotMoveType.RobotMove); - } - - function continueRotateAtSpeed(startAngle: number) { - if (robot.rotateSpeed != 0) { - let current = input.runningTimeMicros(); - let delta = current - previous_rotateMicroTimes; - robot.heading = startAngle + robot.rotateSpeed * delta / 1000000.0; - appMove(robot.heading, robot.speed, RobotMoveType.RobotMove); - control.inBackground(() => { - continueRotateAtSpeed(startAngle); - }); - } } /** diff --git a/pxt.json b/pxt.json index 46da826..043e9a2 100644 --- a/pxt.json +++ b/pxt.json @@ -1,6 +1,6 @@ { "name": "ovobot", - "version": "0.0.4", + "version": "0.0.5", "dependencies": { "core": "*", "radio": "*"