Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/microsoft/microcode into ro…
Browse files Browse the repository at this point in the history
…bot_car
  • Loading branch information
tballmsft committed Sep 27, 2023
2 parents 2003078 + f0851b6 commit 7e01ed4
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 57 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/makecode-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ jobs:
- name: build hardware N3
run: |
makecode --hw N3
- name: build robot
run: |
cd robot
makecode
- name: copy files
run: |
cp ./built/binary.js ./assets/js/binary.js
cp ./built/n3/binary.hex ./assets/firmware.hex
cp ./robot/built/binary.hex ./assets/robot.hex
- name: build localized hex files
run: |
yarn install --frozen-lockfile
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/makecode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ jobs:
- name: build hardware N3
run: |
makecode --hw N3
- name: build robot
run: |
cd robot
makecode
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
3 changes: 3 additions & 0 deletions robot/mkc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"targetWebsite": "https://makecode.microbit.org/v6.0.18"
}
31 changes: 23 additions & 8 deletions robot/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@ namespace microcode.robots {
* Report ultrasonic distance in cm
* distance: f32
*/
UltrasonicDistance = 0x10
UltrasonicDistance = 0x10,
}

/**
* Compact commands through radio numbers
*/
export const enum RobotCompactCommand {
MotorRunForward = 0xfffff001,
MotorTurnBackward = 0xfffff002,
MotorRunBackward = 0xfffff002,
MotorTurnLeft = 0xfffff003,
MotorTurnRight = 0xfffff004,
MotorStop = 0xfffff005,

Obstacle0 = 0xfffff10,
Obstacle1 = 0xfffff11,
Obstacle2 = 0xfffff12,
Obstacle3 = 0xfffff13,
Obstacle4 = 0xfffff14,
Obstacle5 = 0xfffff15,
}

export interface RobotMessage {
Expand All @@ -50,7 +57,7 @@ namespace microcode.robots {

/**
* Encodes the command and payload into a buffer that can be sent via radio
*
*
* 0 magic, u16
* 2 message id, u8
* 3 cmd, u8
Expand Down Expand Up @@ -84,7 +91,7 @@ namespace microcode.robots {
return <RobotMessage>{
messageId: messageId,
cmd: cmd,
payload: payload
payload: payload,
}
}

Expand All @@ -98,19 +105,27 @@ namespace microcode.robots {
let payload: Buffer
switch (msg) {
case RobotCompactCommand.MotorRunForward:
case RobotCompactCommand.MotorTurnBackward:
case RobotCompactCommand.MotorRunBackward:
case RobotCompactCommand.MotorStop: {
cmd = RobotCommand.MotorRun
payload = Buffer.create(2)
if (msg !== RobotCompactCommand.MotorStop)
payload.setNumber(NumberFormat.Int16LE, 0, msg === RobotCompactCommand.MotorRunForward ? 100 : -100)
payload.setNumber(
NumberFormat.Int16LE,
0,
msg === RobotCompactCommand.MotorRunForward ? 100 : -100
)
break
}
case RobotCompactCommand.MotorTurnLeft:
case RobotCompactCommand.MotorTurnRight: {
cmd = RobotCommand.MotorTurn
payload = Buffer.create(2)
payload.setNumber(NumberFormat.Int16LE, 0, msg === RobotCompactCommand.MotorTurnRight ? 100 : -100)
payload.setNumber(
NumberFormat.Int16LE,
0,
msg === RobotCompactCommand.MotorTurnRight ? 100 : -100
)
break
}
default:
Expand All @@ -119,4 +134,4 @@ namespace microcode.robots {

return { messageId, cmd, payload }
}
}
}
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)
}
}
Loading

0 comments on commit 7e01ed4

Please sign in to comment.