Skip to content

Commit

Permalink
cutebot support
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Sep 20, 2023
1 parent 369d8fa commit c85edb7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
79 changes: 79 additions & 0 deletions robot/cutebot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace microcode {
const STM8_ADDRESSS = 0x10

//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) {
lspeed = -100;
}
if (rspeed > 100) {
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); //写入左轮
}
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); //写入左轮
}

}

export function singleheadlights(r: number, g: number, b: number): void {
let 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);
}

export class ElecfreaksCutebotRobot extends Robot {
constructor() {
super()
}

motorRun(speed: number) {
motors(speed, speed)
}

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

headlightsSetColor(red: number, green: number, blue: number) {
singleheadlights(red, green, blue)
}
}
}
3 changes: 2 additions & 1 deletion robot/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const robot = new microcode.YahboomTinybitRobot()
//const robot = new microcode.YahboomTinybitRobot()
//const robot = new microcode.KeyStudioMiniSmartRobot()
const robot = new microcode.ElecfreaksCutebotRobot()
const robotDriver = new microcode.RobotDriver(robot)

// configure group using button A/B, cycle through groups 1-99
Expand Down
3 changes: 2 additions & 1 deletion robot/pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"keystudiominismartrobot.ts",
"main.ts",
"robotdriver.ts",
"yahboomtinybit.ts"
"yahboomtinybit.ts",
"cutebot.ts"
],
"testFiles": [
"test.ts"
Expand Down

0 comments on commit c85edb7

Please sign in to comment.