Skip to content

Commit

Permalink
simplify motor interface
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 12, 2023
1 parent 219cdd1 commit f9c77e5
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 60 deletions.
18 changes: 6 additions & 12 deletions robot/cutebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) //写入左轮
Expand All @@ -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) //写入左轮
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
7 changes: 0 additions & 7 deletions robot/cutebotpro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 0 additions & 9 deletions robot/keystudiominismartrobot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion robot/pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"test.ts"
],
"targetVersions": {
"target": "6.0.18",
"target": "6.0.19",
"targetId": "microbit"
},
"supportedTargets": [
Expand Down
8 changes: 0 additions & 8 deletions robot/robot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
53 changes: 34 additions & 19 deletions robot/robotdriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down
14 changes: 10 additions & 4 deletions robot/test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
2 changes: 2 additions & 0 deletions robot/yahboomtinybit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ namespace microcode {
Car_back(-speed, -speed)
}

/*
motorTurn(speed: number): void {
console.log(`turn: ${speed}`)
if (speed === 0)
Expand All @@ -149,6 +150,7 @@ namespace microcode {
else
Car_left(-speed / 2, -speed)
}
*/

headlightsSetColor(red: number, green: number, blue: number) {
setPwmRGB(red, green, blue)
Expand Down

0 comments on commit f9c77e5

Please sign in to comment.