Skip to content

Commit

Permalink
build robot
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Sep 26, 2023
1 parent 63e5185 commit fadae86
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
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
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"
}
30 changes: 22 additions & 8 deletions robot/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,24 @@ 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,

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

export interface RobotMessage {
Expand All @@ -50,7 +56,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 +90,7 @@ namespace microcode.robots {
return <RobotMessage>{
messageId: messageId,
cmd: cmd,
payload: payload
payload: payload,
}
}

Expand All @@ -98,19 +104,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 +133,4 @@ namespace microcode.robots {

return { messageId, cmd, payload }
}
}
}

0 comments on commit fadae86

Please sign in to comment.