Skip to content

Commit f9c77e5

Browse files
committed
simplify motor interface
1 parent 219cdd1 commit f9c77e5

File tree

8 files changed

+53
-60
lines changed

8 files changed

+53
-60
lines changed

robot/cutebot.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace microcode {
2323
} else {
2424
buf[0] = 0x01
2525
buf[1] = 0x01
26-
buf[2] = lspeed * -1
26+
buf[2] = -lspeed
2727
buf[3] = 0 //补位
2828
}
2929
pins.i2cWriteBuffer(STM8_ADDRESSS, buf) //写入左轮
@@ -35,7 +35,7 @@ namespace microcode {
3535
} else {
3636
buf[0] = 0x02
3737
buf[1] = 0x01
38-
buf[2] = rspeed * -1
38+
buf[2] = rspeed
3939
buf[3] = 0 //补位
4040
}
4141
pins.i2cWriteBuffer(STM8_ADDRESSS, buf) //写入左轮
@@ -73,11 +73,11 @@ namespace microcode {
7373
constructor() {
7474
super()
7575
this.musicVolume = 168
76-
this.maxRunSpeed = 35
76+
this.maxRunSpeed = 40
7777
this.maxBackSpeed = 20
78-
this.maxTurnSpeed = 30
79-
this.maxLineRunSpeed = 25
80-
this.maxLineTurnSpeed = 27
78+
this.maxTurnSpeed = 60
79+
this.maxLineRunSpeed = 28
80+
this.maxLineTurnSpeed = 60
8181

8282
pins.setPull(DigitalPin.P8, PinPullMode.PullNone)
8383
pins.setPull(DigitalPin.P13, PinPullMode.PullNone)
@@ -88,12 +88,6 @@ namespace microcode {
8888
motors(left, right)
8989
}
9090

91-
motorTurn(speed: number) {
92-
const op = Math.abs(speed) >> 1
93-
if (speed > 0) motors(speed, Math.constrain(this.maxTurnSpeed - speed, 0, op))
94-
else motors(Math.constrain(this.maxTurnSpeed + speed, 0, op), -speed)
95-
}
96-
9791
headlightsSetColor(red: number, green: number, blue: number) {
9892
singleheadlights(red, green, blue)
9993
}

robot/cutebotpro.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,6 @@ namespace microcode {
230230
pwmCruiseControl(left, right)
231231
}
232232

233-
motorTurn(speed: number) {
234-
console.log(`speed: ${speed}`)
235-
const op = Math.abs(speed) / 3
236-
if (speed > 0) pwmCruiseControl(speed, Math.constrain(this.maxTurnSpeed - speed, 0, op))
237-
else pwmCruiseControl(Math.constrain(this.maxTurnSpeed + speed, 0, op), -speed)
238-
}
239-
240233
headlightsSetColor(red: number, green: number, blue: number) {
241234
singleHeadlights(CutebotProRGBLight.RGBA, red, green, blue)
242235
}

robot/keystudiominismartrobot.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,6 @@ namespace microcode {
163163
carStop()
164164
}
165165

166-
motorTurn(speed: number) {
167-
if (speed === 0) {
168-
this.motorStop()
169-
} else {
170-
const dir = speed >= 0 ? DIR.TurnRight : DIR.TurnLeft
171-
run(dir, Math.abs(speed))
172-
}
173-
}
174-
175166
ultrasonicDistance(): number {
176167
//send trig pulse
177168
pins.digitalWritePin(TRIG_PIN, 0)

robot/pxt.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"test.ts"
2323
],
2424
"targetVersions": {
25-
"target": "6.0.18",
25+
"target": "6.0.19",
2626
"targetId": "microbit"
2727
},
2828
"supportedTargets": [

robot/robot.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,6 @@ namespace microcode.robots {
1919

2020
}
2121

22-
23-
/**
24-
* Makes the robot turn at % `speed`. Positive turns clock-wize/right, negative turns counter-clockwize/left.
25-
*/
26-
motorTurn(speed: number): void {
27-
28-
}
29-
3022
/**
3123
* Optional: sets the color on the LED array as a 24bit RGB color
3224
*/

robot/robotdriver.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ namespace microcode {
136136
}
137137

138138
private updateSpeed() {
139+
console.log(`tmode: ${this.targetSpeedMode}`)
140+
console.log(`tspeed: ${this.targetSpeed}`)
141+
console.log(`cmode: ${this.currentSpeedMode}`)
142+
console.log(`cspeed: ${this.currentSpeed}`)
143+
139144
// transition from one mode to the other, robot should stop
140145
if (this.currentSpeedMode !== this.targetSpeedMode) {
141146
const alpha = MODE_TRANSITION_ALPHA
@@ -174,25 +179,32 @@ namespace microcode {
174179
}
175180
}
176181
}
177-
console.log(`left: ${left}`)
178-
console.log(`right: ${right}`)
179-
this.robot.motorRun(left, right)
180-
this.showMotorState(left, right)
182+
this.setMotorState(left, right)
181183
} else {
182184
let s = this.currentSpeed
183185
if (lines)
184-
s = s * Math.min(Math.abs(s), this.robot.maxLineTurnSpeed)
185-
console.log(`speed: ${s}`)
186-
this.robot.motorTurn(s)
187-
this.showMotorState(
188-
s > 0 ? s : 0,
189-
s <= 0 ? 0 : -s
190-
)
186+
s = Math.sign(s) * Math.min(Math.abs(s), this.robot.maxLineTurnSpeed)
187+
let left = 0
188+
let right = 0
189+
const op = Math.abs(s) / 3
190+
if (s > 0) {
191+
right = Math.constrain(this.robot.maxTurnSpeed + s, 0, op)
192+
left = s
193+
} else {
194+
right = -s
195+
left = Math.constrain(this.robot.maxTurnSpeed - s, 0, op)
196+
}
197+
this.setMotorState(left, right)
191198
}
192199
}
193200

194-
private showMotorState(left: number, right: number) {
201+
private setMotorState(left: number, right: number) {
202+
left = Math.round(left)
203+
right = Math.round(right)
204+
this.robot.motorRun(left, right)
195205
if (this.showConfiguration) return
206+
console.log(`left: ${left}`)
207+
console.log(`right: ${right}`)
196208
this.showSingleMotorState(3, left)
197209
this.showSingleMotorState(1, right)
198210
}
@@ -269,9 +281,11 @@ namespace microcode {
269281
speed > 0
270282
? Math.min(this.robot.maxRunSpeed, speed)
271283
: Math.max(-this.robot.maxBackSpeed, speed)
272-
this.setHeadlingSpeedColor(speed)
273-
this.targetSpeedMode = RobotSpeedMode.Run
274-
this.targetSpeed = speed
284+
if (this.targetSpeedMode !== RobotSpeedMode.Run || this.targetSpeed !== speed) {
285+
this.setHeadlingSpeedColor(speed)
286+
this.targetSpeedMode = RobotSpeedMode.Run
287+
this.targetSpeed = speed
288+
}
275289
}
276290

277291
motorTurn(speed: number) {
@@ -281,10 +295,11 @@ namespace microcode {
281295
speed > 0
282296
? Math.min(this.robot.maxTurnSpeed, speed)
283297
: Math.max(-this.robot.maxTurnSpeed, speed)
284-
this.setHeadlingSpeedColor(speed)
285-
this.targetSpeedMode = RobotSpeedMode.Turn
286-
this.targetSpeed = speed
287-
this.currentSpeed = 0
298+
if (this.targetSpeedMode !== RobotSpeedMode.Turn || this.targetSpeed !== speed) {
299+
this.setHeadlingSpeedColor(speed)
300+
this.targetSpeedMode = RobotSpeedMode.Turn
301+
this.targetSpeed = speed
302+
}
288303
}
289304

290305
motorStop() {

robot/test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
//microcode.elecfreaksCuteBot.start()
2-
microcode.elecfreaksCuteBotPro.start()
1+
microcode.elecfreaksCuteBot.start()
2+
//microcode.elecfreaksCuteBotPro.start()
33
//microcode.yahboomTinyBit.start()
44
//microcode.keyStudioMiniSmartRobot.start()
55
//microcode.setMotorDrift(6)
66

77
microcode.robotDriver.motorRun(100)
88

9+
let i = 0
910
basic.forever(() => {
1011
const lines = microcode.robotDriver.currentLineState
11-
if (lines === microcode.robots.RobotLineState.Left)
12+
console.log(`lines: ${lines}`)
13+
if (lines === microcode.robots.RobotLineState.Left) {
1214
microcode.robotDriver.motorTurn(-100)
13-
else if (lines === microcode.robots.RobotLineState.Right)
15+
}
16+
else if (lines === microcode.robots.RobotLineState.Right) {
1417
microcode.robotDriver.motorTurn(100)
18+
}
19+
else if (lines === microcode.robots.RobotLineState.None)
20+
microcode.robotDriver.motorTurn(50)
1521
else
1622
microcode.robotDriver.motorRun(100)
1723
})

robot/yahboomtinybit.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ namespace microcode {
140140
Car_back(-speed, -speed)
141141
}
142142

143+
/*
143144
motorTurn(speed: number): void {
144145
console.log(`turn: ${speed}`)
145146
if (speed === 0)
@@ -149,6 +150,7 @@ namespace microcode {
149150
else
150151
Car_left(-speed / 2, -speed)
151152
}
153+
*/
152154

153155
headlightsSetColor(red: number, green: number, blue: number) {
154156
setPwmRGB(red, green, blue)

0 commit comments

Comments
 (0)