Skip to content

Commit

Permalink
flip rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Sep 28, 2023
1 parent 400b36f commit d8acc0d
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 53 deletions.
104 changes: 53 additions & 51 deletions robot/cutebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,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);
let buf = pins.createBuffer(4)
/*
if (lspeed > 100) {
lspeed = 100;
Expand All @@ -16,66 +16,68 @@ namespace microcode {
rspeed = -100;
}*/
if (lspeed > 0) {
buf[0] = 0x01; //左右轮 0x01左轮 0x02右轮
buf[1] = 0x02; //正反转0x02前进 0x01后退
buf[2] = lspeed; //速度
buf[3] = 0; //补位
buf[0] = 0x01 //左右轮 0x01左轮 0x02右轮
buf[1] = 0x02 //正反转0x02前进 0x01后退
buf[2] = lspeed //速度
buf[3] = 0 //补位
} else {
buf[0] = 0x01
buf[1] = 0x01
buf[2] = lspeed * -1
buf[3] = 0 //补位
}
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; //补位
}
else {
buf[0] = 0x02;
buf[1] = 0x01;
buf[2] = rspeed * -1;
buf[3] = 0; //补位
buf[0] = 0x02
buf[1] = 0x02
buf[2] = rspeed
buf[3] = 0 //补位
} 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 {
const buf = pins.createBuffer(4);
buf[0] = 0x04;
buf[1] = r;
buf[2] = g;
buf[3] = b;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
buf[0] = 0x08;
pins.i2cWriteBuffer(STM8_ADDRESSS, buf);
const buf = pins.createBuffer(4)
buf[0] = 0x04
buf[1] = r
buf[2] = g
buf[3] = b
pins.i2cWriteBuffer(STM8_ADDRESSS, buf)
buf[0] = 0x08
pins.i2cWriteBuffer(STM8_ADDRESSS, buf)
}

function ultrasonic(): number {
// send pulse
pins.digitalWritePin(DigitalPin.P8, 0);
control.waitMicros(2);
pins.digitalWritePin(DigitalPin.P8, 1);
control.waitMicros(10);
pins.digitalWritePin(DigitalPin.P8, 0);
pins.digitalWritePin(DigitalPin.P8, 0)
control.waitMicros(2)
pins.digitalWritePin(DigitalPin.P8, 1)
control.waitMicros(10)
pins.digitalWritePin(DigitalPin.P8, 0)
// read pulse
const maxCmDistance = 100
const d = pins.pulseIn(DigitalPin.P12, PulseValue.High, maxCmDistance * 50);
return Math.floor(d * 34 / 2 / 1000);
const d = pins.pulseIn(
DigitalPin.P12,
PulseValue.High,
maxCmDistance * 50
)
return Math.floor((d * 34) / 2 / 1000)
}

class ElecfreaksCutebotRobot extends robots.Robot {
constructor() {
super()
this.musicVolume = 168;
this.maxRunSpeed = 50;
this.maxTurnSpeed = 40;
this.maxLineTrackingSpeed = 20;
this.musicVolume = 168
this.maxRunSpeed = 50
this.maxTurnSpeed = 40
this.maxLineTrackingSpeed = 20

pins.setPull(DigitalPin.P8, PinPullMode.PullNone);
pins.setPull(DigitalPin.P8, PinPullMode.PullNone)
pins.setPull(DigitalPin.P13, PinPullMode.PullNone)
pins.setPull(DigitalPin.P14, PinPullMode.PullNone)
}
Expand All @@ -85,10 +87,8 @@ namespace microcode {
}

motorTurn(speed: number) {
if (speed > 0)
motors(0, speed)
else
motors(-speed, 0)
if (speed > 0) motors(speed, 0)
else motors(0, -speed)
}

headlightsSetColor(red: number, green: number, blue: number) {
Expand All @@ -100,8 +100,8 @@ namespace microcode {
}

lineState(): RobotLineState {
const left = pins.digitalReadPin(DigitalPin.P13) ? 0 : 1;
const right = pins.digitalReadPin(DigitalPin.P14) ? 0:1;
const left = pins.digitalReadPin(DigitalPin.P13) ? 0 : 1
const right = pins.digitalReadPin(DigitalPin.P14) ? 0 : 1

return (left << 0) | (right << 1)
}
Expand All @@ -111,5 +111,7 @@ namespace microcode {
* Cute:bot from Elecfreaks
*/
//% fixedInstance whenUsed block="elecfreaks cutebot"
export const elecfreaksCuteBot = new RobotDriver(new ElecfreaksCutebotRobot())
}
export const elecfreaksCuteBot = new RobotDriver(
new ElecfreaksCutebotRobot()
)
}
5 changes: 3 additions & 2 deletions robot/robotdriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ namespace microcode {
})
}

private lastLines: RobotLineState = RobotLineState.None
private updateSpeed() {
// transition from one mode to the other, robot should stop
if (this.currentSpeedMode !== this.targetSpeedMode) {
Expand Down Expand Up @@ -126,7 +125,6 @@ namespace microcode {
else if (lines === RobotLineState.Right)
d -= speed * 0.8
}
this.lastLines = lines
}
const left = speed - d
const right = speed + d
Expand Down Expand Up @@ -291,6 +289,9 @@ namespace microcode {
this.lastReceivedMessageId = messageId
const cmd = msg.cmd
const payload = msg.payload

console.log({ cmd, payload: payload.toHex() })

switch (cmd) {
case robots.RobotCommand.MotorRun: {
const speed = Math.clamp(
Expand Down

0 comments on commit d8acc0d

Please sign in to comment.