Skip to content

Commit

Permalink
drop fancy font
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Sep 26, 2023
1 parent 413bb6a commit f0851b6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 49 deletions.
1 change: 1 addition & 0 deletions robot/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"arrowParens":"avoid","semi":false,"tabWidth":4}
12 changes: 5 additions & 7 deletions robot/cutebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace microcode {
//https://github.com/elecfreaks/pxt-cutebot/blob/master/cutebot.ts
function motors(lspeed: number, rspeed: number): void {
let buf = pins.createBuffer(4);
/*
if (lspeed > 100) {
lspeed = 100;
} else if (lspeed < -100) {
Expand All @@ -13,40 +14,37 @@ namespace microcode {
rspeed = 100;
} else if (rspeed < -100) {
rspeed = -100;
}
}*/
if (lspeed > 0) {
buf[0] = 0x01; //左右轮 0x01左轮 0x02右轮
buf[1] = 0x02; //正反转0x02前进 0x01后退
buf[2] = lspeed; //速度
buf[3] = 0; //补位
pins.i2cWriteBuffer(STM8_ADDRESSS, buf); //写入左轮
}
else {
buf[0] = 0x01;
buf[1] = 0x01;
buf[2] = lspeed * -1;
buf[3] = 0; //补位
pins.i2cWriteBuffer(STM8_ADDRESSS, buf); //写入左轮
}
pins.i2cWriteBuffer(STM8_ADDRESSS, buf); //写入左轮
if (rspeed > 0) {
buf[0] = 0x02;
buf[1] = 0x02;
buf[2] = rspeed;
buf[3] = 0; //补位
pins.i2cWriteBuffer(STM8_ADDRESSS, buf); //写入左轮
}
else {
buf[0] = 0x02;
buf[1] = 0x01;
buf[2] = rspeed * -1;
buf[3] = 0; //补位
pins.i2cWriteBuffer(STM8_ADDRESSS, buf); //写入左轮
}

pins.i2cWriteBuffer(STM8_ADDRESSS, buf); //写入左轮
}

function singleheadlights(r: number, g: number, b: number): void {
let buf = pins.createBuffer(4);
const buf = pins.createBuffer(4);
buf[0] = 0x04;
buf[1] = r;
buf[2] = g;
Expand Down
1 change: 1 addition & 0 deletions robot/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace microcode.robots {
MotorTurnRight = 0xfffff004,
MotorStop = 0xfffff005,

Obstacle0 = 0xfffff10,
Obstacle1 = 0xfffff11,
Obstacle2 = 0xfffff12,
Obstacle3 = 0xfffff13,
Expand Down
3 changes: 1 addition & 2 deletions robot/pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"name": "microcode-robot",
"dependencies": {
"core": "*",
"radio": "*",
"WhaleySansFont": "github:makecode-extensions/whaleysansfont#v1.1.0"
"radio": "*"
},
"files": [
"README.md",
Expand Down
24 changes: 14 additions & 10 deletions robot/radio.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace microcode.robots {
const MAX_GROUPS = 21
let group = control.deviceSerialNumber() % 9 + 1
const MAX_GROUPS = 10
let group = (control.deviceSerialNumber() % 9) + 1
radio.setGroup(group)

export function previousGroup() {
Expand All @@ -12,24 +12,28 @@ namespace microcode.robots {
}

export function setGroup(newGroup: number) {
if (newGroup < 0)
newGroup += MAX_GROUPS
if (newGroup < 0) newGroup += MAX_GROUPS
group = newGroup % MAX_GROUPS
radio.setGroup(group)
}

export function showRadioStatus() {
led.stopAnimation()
if (group < 10)
basic.showNumber(group, 10)
else
whaleysans.showNumber(group)
basic.showNumber(group, 10)
}

let nextMessageId = 0
export function sendCommand(cmd: RobotCommand, payload: Buffer) {
nextMessageId = (nextMessageId + 1) % 0xff
const buf = encodeRobotMessage({ messageId: nextMessageId, cmd, payload })
const buf = encodeRobotMessage({
messageId: nextMessageId,
cmd,
payload,
})
radio.sendBuffer(buf)
}
}

export function sendCompactCommand(cmd: RobotCompactCommand) {
radio.sendNumber(cmd)
}
}
94 changes: 64 additions & 30 deletions robot/robotdriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ namespace microcode {
const MODE_SWITCH_THRESHOLD = 2

enum RobotSpeedMode {
Run, Turn
Run,
Turn,
}

/**
*
*
*/
//% fixedInstances
export class RobotDriver {
Expand Down Expand Up @@ -102,24 +103,26 @@ namespace microcode {
this.currentSpeed = 0
this.currentSpeedMode = this.targetSpeedMode
}
}
else {
} else {
const alpha = 0.8
this.currentSpeed = this.currentSpeed * alpha + this.targetSpeed * (1 - alpha)
this.currentSpeed =
this.currentSpeed * alpha + this.targetSpeed * (1 - alpha)
if (Math.abs(this.currentSpeed - this.targetSpeed) < 10)
this.currentSpeed = this.targetSpeed
}

let speed = Math.abs(this.currentSpeed) < RUN_STOP_THRESHOLD ? 0 : this.currentSpeed
let speed =
Math.abs(this.currentSpeed) < RUN_STOP_THRESHOLD
? 0
: this.currentSpeed
if (this.currentSpeedMode === RobotSpeedMode.Run) {
let d = speed > this.runDrift ? this.runDrift >> 1 : 0
if (speed > 0) {
const lines = this.lineState()
if (lines) {
speed = Math.min(speed, this.robot.maxLineTrackingSpeed)
this.currentSpeed = Math.min(this.currentSpeed, speed)
if (lines === RobotLineState.Left)
d += speed * 0.8
if (lines === RobotLineState.Left) d += speed * 0.8
else if (lines === RobotLineState.Right)
d -= speed * 0.8
}
Expand All @@ -129,10 +132,12 @@ namespace microcode {
const right = speed + d
this.robot.motorRun(left, right)
this.showMotorState(left, right)
}
else {
} else {
this.robot.motorTurn(speed)
this.showMotorState(speed > 0 ? speed : 0, speed <= 0 ? 0 : -speed)
this.showMotorState(
speed > 0 ? speed : 0,
speed <= 0 ? 0 : -speed
)
}
}

Expand All @@ -143,36 +148,48 @@ namespace microcode {
}

private showSingleMotorState(x: number, speed: number) {
if (Math.abs(speed) < 30) led.unplot(x, 2); else led.plot(x, 2)
if (speed >= 30) led.plot(x, 1); else led.unplot(x, 1)
if (speed >= 50) led.plot(x, 0); else led.unplot(x, 0)
if (speed <= -30) led.plot(x, 3); else led.unplot(x, 3)
if (speed <= -50) led.plot(x, 4); else led.unplot(x, 4)
if (Math.abs(speed) < 30) led.unplot(x, 2)
else led.plot(x, 2)
if (speed >= 30) led.plot(x, 1)
else led.unplot(x, 1)
if (speed >= 50) led.plot(x, 0)
else led.unplot(x, 0)
if (speed <= -30) led.plot(x, 3)
else led.unplot(x, 3)
if (speed <= -50) led.plot(x, 4)
else led.unplot(x, 4)
}

private showLineState() {
if (this.showRadio > 0) return

const lineState = this.lineState()
// render left/right lines
const left = (lineState & RobotLineState.Left) === RobotLineState.Left
const right = (lineState & RobotLineState.Right) === RobotLineState.Right
const left =
(lineState & RobotLineState.Left) === RobotLineState.Left
const right =
(lineState & RobotLineState.Right) === RobotLineState.Right
for (let i = 0; i < 5; ++i) {
if (left) led.plot(4, i); else led.unplot(4, i)
if (right) led.plot(0, i); else led.unplot(0, i)
if (left) led.plot(4, i)
else led.unplot(4, i)
if (right) led.plot(0, i)
else led.unplot(0, i)
}
}


private showSonar() {
if (this.showRadio > 0) return

const dist = this.ultrasonicDistance()
console.log({ dist })
// render sonar
if (dist > 0) {
for (let y = 0; y < 5; y++)
if (dist > 0 && dist < 5 + y * 5) led.plot(2, y); else led.unplot(2, y)
if (dist > 0 && dist < 5 + y * 5) led.plot(2, y)
else led.unplot(2, y)

const d = Math.min(1, Math.max(5, Math.round(dist / 5)))
const msg = microcode.robots.RobotCompactCommand.Obstacle0 | d
microcode.robots.sendCompactCommand(msg)
}
}

Expand All @@ -189,7 +206,10 @@ namespace microcode {
motorRun(speed: number) {
this.start()
this.keepAlive()
speed = speed > 0 ? Math.min(this.robot.maxRunSpeed, speed) : Math.max(-this.robot.maxRunSpeed, speed)
speed =
speed > 0
? Math.min(this.robot.maxRunSpeed, speed)
: Math.max(-this.robot.maxRunSpeed, speed)
this.setHeadlingSpeedColor(speed)
this.targetSpeedMode = RobotSpeedMode.Run
this.targetSpeed = speed
Expand All @@ -198,7 +218,10 @@ namespace microcode {
motorTurn(speed: number) {
this.start()
this.keepAlive()
speed = speed > 0 ? Math.min(this.robot.maxTurnSpeed, speed) : Math.max(-this.robot.maxTurnSpeed, speed)
speed =
speed > 0
? Math.min(this.robot.maxTurnSpeed, speed)
: Math.max(-this.robot.maxTurnSpeed, speed)
this.setHeadlingSpeedColor(speed)
this.targetSpeedMode = RobotSpeedMode.Turn
this.targetSpeed = speed
Expand Down Expand Up @@ -230,7 +253,7 @@ namespace microcode {
}

checkAlive() {
if (!this.safe) return;
if (!this.safe) return
if (control.millis() - this.lastCommandTime > ROBOT_TIMEOUT)
this.stop()
}
Expand All @@ -243,7 +266,10 @@ namespace microcode {
playMelody(melody: Melodies) {
if (this.robot.musicVolume <= 0) return
music.setVolume(this.robot.musicVolume)
music.play(music.builtInPlayableMelody(melody), music.PlaybackMode.InBackground)
music.play(
music.builtInPlayableMelody(melody),
music.PlaybackMode.InBackground
)
}

playTone(frequency: number) {
Expand All @@ -267,18 +293,26 @@ namespace microcode {
const payload = msg.payload
switch (cmd) {
case robots.RobotCommand.MotorRun: {
const speed = Math.clamp(-100, 100, payload.getNumber(NumberFormat.Int16LE, 0))
const speed = Math.clamp(
-100,
100,
payload.getNumber(NumberFormat.Int16LE, 0)
)
console.log(`motor run ${speed}`)
this.motorRun(speed)
break
}
case robots.RobotCommand.MotorTurn: {
const speed = Math.clamp(-100, 100, payload.getNumber(NumberFormat.Int16LE, 0))
const speed = Math.clamp(
-100,
100,
payload.getNumber(NumberFormat.Int16LE, 0)
)
console.log(`motor turn ${speed}`)
this.motorTurn(speed)
break
}
}
}
}
}
}

0 comments on commit f0851b6

Please sign in to comment.