diff --git a/robot/cutebot.ts b/robot/cutebot.ts index f7ee1d20..db43b060 100644 --- a/robot/cutebot.ts +++ b/robot/cutebot.ts @@ -23,7 +23,7 @@ namespace microcode { } else { buf[0] = 0x01 buf[1] = 0x01 - buf[2] = lspeed * -1 + buf[2] = -lspeed buf[3] = 0 //补位 } pins.i2cWriteBuffer(STM8_ADDRESSS, buf) //写入左轮 @@ -35,7 +35,7 @@ namespace microcode { } else { buf[0] = 0x02 buf[1] = 0x01 - buf[2] = rspeed * -1 + buf[2] = rspeed buf[3] = 0 //补位 } pins.i2cWriteBuffer(STM8_ADDRESSS, buf) //写入左轮 @@ -73,11 +73,11 @@ namespace microcode { constructor() { super() this.musicVolume = 168 - this.maxRunSpeed = 35 + this.maxRunSpeed = 40 this.maxBackSpeed = 20 - this.maxTurnSpeed = 30 - this.maxLineRunSpeed = 25 - this.maxLineTurnSpeed = 27 + this.maxTurnSpeed = 60 + this.maxLineRunSpeed = 28 + this.maxLineTurnSpeed = 60 pins.setPull(DigitalPin.P8, PinPullMode.PullNone) pins.setPull(DigitalPin.P13, PinPullMode.PullNone) @@ -88,12 +88,6 @@ namespace microcode { motors(left, right) } - motorTurn(speed: number) { - const op = Math.abs(speed) >> 1 - if (speed > 0) motors(speed, Math.constrain(this.maxTurnSpeed - speed, 0, op)) - else motors(Math.constrain(this.maxTurnSpeed + speed, 0, op), -speed) - } - headlightsSetColor(red: number, green: number, blue: number) { singleheadlights(red, green, blue) } diff --git a/robot/cutebotpro.ts b/robot/cutebotpro.ts index 0acdea57..70754ecb 100644 --- a/robot/cutebotpro.ts +++ b/robot/cutebotpro.ts @@ -230,13 +230,6 @@ namespace microcode { pwmCruiseControl(left, right) } - motorTurn(speed: number) { - console.log(`speed: ${speed}`) - const op = Math.abs(speed) / 3 - if (speed > 0) pwmCruiseControl(speed, Math.constrain(this.maxTurnSpeed - speed, 0, op)) - else pwmCruiseControl(Math.constrain(this.maxTurnSpeed + speed, 0, op), -speed) - } - headlightsSetColor(red: number, green: number, blue: number) { singleHeadlights(CutebotProRGBLight.RGBA, red, green, blue) } diff --git a/robot/keystudiominismartrobot.ts b/robot/keystudiominismartrobot.ts index af957187..2a75dd21 100644 --- a/robot/keystudiominismartrobot.ts +++ b/robot/keystudiominismartrobot.ts @@ -163,15 +163,6 @@ namespace microcode { carStop() } - motorTurn(speed: number) { - if (speed === 0) { - this.motorStop() - } else { - const dir = speed >= 0 ? DIR.TurnRight : DIR.TurnLeft - run(dir, Math.abs(speed)) - } - } - ultrasonicDistance(): number { //send trig pulse pins.digitalWritePin(TRIG_PIN, 0) diff --git a/robot/pxt.json b/robot/pxt.json index 8f0d69de..4ab50d1b 100644 --- a/robot/pxt.json +++ b/robot/pxt.json @@ -22,7 +22,7 @@ "test.ts" ], "targetVersions": { - "target": "6.0.18", + "target": "6.0.19", "targetId": "microbit" }, "supportedTargets": [ diff --git a/robot/robot.ts b/robot/robot.ts index 2d69bb49..cc80354a 100644 --- a/robot/robot.ts +++ b/robot/robot.ts @@ -19,14 +19,6 @@ namespace microcode.robots { } - - /** - * Makes the robot turn at % `speed`. Positive turns clock-wize/right, negative turns counter-clockwize/left. - */ - motorTurn(speed: number): void { - - } - /** * Optional: sets the color on the LED array as a 24bit RGB color */ diff --git a/robot/robotdriver.ts b/robot/robotdriver.ts index 6b01bfb6..fd1d7e6e 100644 --- a/robot/robotdriver.ts +++ b/robot/robotdriver.ts @@ -136,6 +136,11 @@ namespace microcode { } private updateSpeed() { + console.log(`tmode: ${this.targetSpeedMode}`) + console.log(`tspeed: ${this.targetSpeed}`) + console.log(`cmode: ${this.currentSpeedMode}`) + console.log(`cspeed: ${this.currentSpeed}`) + // transition from one mode to the other, robot should stop if (this.currentSpeedMode !== this.targetSpeedMode) { const alpha = MODE_TRANSITION_ALPHA @@ -174,25 +179,32 @@ namespace microcode { } } } - console.log(`left: ${left}`) - console.log(`right: ${right}`) - this.robot.motorRun(left, right) - this.showMotorState(left, right) + this.setMotorState(left, right) } else { let s = this.currentSpeed if (lines) - s = s * Math.min(Math.abs(s), this.robot.maxLineTurnSpeed) - console.log(`speed: ${s}`) - this.robot.motorTurn(s) - this.showMotorState( - s > 0 ? s : 0, - s <= 0 ? 0 : -s - ) + s = Math.sign(s) * Math.min(Math.abs(s), this.robot.maxLineTurnSpeed) + let left = 0 + let right = 0 + const op = Math.abs(s) / 3 + if (s > 0) { + right = Math.constrain(this.robot.maxTurnSpeed + s, 0, op) + left = s + } else { + right = -s + left = Math.constrain(this.robot.maxTurnSpeed - s, 0, op) + } + this.setMotorState(left, right) } } - private showMotorState(left: number, right: number) { + private setMotorState(left: number, right: number) { + left = Math.round(left) + right = Math.round(right) + this.robot.motorRun(left, right) if (this.showConfiguration) return + console.log(`left: ${left}`) + console.log(`right: ${right}`) this.showSingleMotorState(3, left) this.showSingleMotorState(1, right) } @@ -269,9 +281,11 @@ namespace microcode { speed > 0 ? Math.min(this.robot.maxRunSpeed, speed) : Math.max(-this.robot.maxBackSpeed, speed) - this.setHeadlingSpeedColor(speed) - this.targetSpeedMode = RobotSpeedMode.Run - this.targetSpeed = speed + if (this.targetSpeedMode !== RobotSpeedMode.Run || this.targetSpeed !== speed) { + this.setHeadlingSpeedColor(speed) + this.targetSpeedMode = RobotSpeedMode.Run + this.targetSpeed = speed + } } motorTurn(speed: number) { @@ -281,10 +295,11 @@ namespace microcode { speed > 0 ? Math.min(this.robot.maxTurnSpeed, speed) : Math.max(-this.robot.maxTurnSpeed, speed) - this.setHeadlingSpeedColor(speed) - this.targetSpeedMode = RobotSpeedMode.Turn - this.targetSpeed = speed - this.currentSpeed = 0 + if (this.targetSpeedMode !== RobotSpeedMode.Turn || this.targetSpeed !== speed) { + this.setHeadlingSpeedColor(speed) + this.targetSpeedMode = RobotSpeedMode.Turn + this.targetSpeed = speed + } } motorStop() { diff --git a/robot/test.ts b/robot/test.ts index 4c22937a..4f9e7178 100644 --- a/robot/test.ts +++ b/robot/test.ts @@ -1,17 +1,23 @@ -//microcode.elecfreaksCuteBot.start() -microcode.elecfreaksCuteBotPro.start() +microcode.elecfreaksCuteBot.start() +//microcode.elecfreaksCuteBotPro.start() //microcode.yahboomTinyBit.start() //microcode.keyStudioMiniSmartRobot.start() //microcode.setMotorDrift(6) microcode.robotDriver.motorRun(100) +let i = 0 basic.forever(() => { const lines = microcode.robotDriver.currentLineState - if (lines === microcode.robots.RobotLineState.Left) + console.log(`lines: ${lines}`) + if (lines === microcode.robots.RobotLineState.Left) { microcode.robotDriver.motorTurn(-100) - else if (lines === microcode.robots.RobotLineState.Right) + } + else if (lines === microcode.robots.RobotLineState.Right) { microcode.robotDriver.motorTurn(100) + } + else if (lines === microcode.robots.RobotLineState.None) + microcode.robotDriver.motorTurn(50) else microcode.robotDriver.motorRun(100) }) diff --git a/robot/yahboomtinybit.ts b/robot/yahboomtinybit.ts index e3ffaad3..8733666c 100644 --- a/robot/yahboomtinybit.ts +++ b/robot/yahboomtinybit.ts @@ -140,6 +140,7 @@ namespace microcode { Car_back(-speed, -speed) } + /* motorTurn(speed: number): void { console.log(`turn: ${speed}`) if (speed === 0) @@ -149,6 +150,7 @@ namespace microcode { else Car_left(-speed / 2, -speed) } + */ headlightsSetColor(red: number, green: number, blue: number) { setPwmRGB(red, green, blue)