Skip to content

Commit

Permalink
don't use magic numbers, use protocol.ts instead
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 3, 2023
1 parent 75eaefc commit 4422070
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 162 deletions.
61 changes: 47 additions & 14 deletions jacs_topwriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,21 +639,30 @@ namespace jacs {
if (p.jdParam2 !== undefined)
args.push(literal(p.jdParam2))
this.callLinked(p.jdParam, args)
} else if (p.jdKind == microcode.JdKind.NumFmt && p.jdParam == NumFmt.F64) {
} else if (
p.jdKind == microcode.JdKind.NumFmt &&
p.jdParam == NumFmt.F64
) {
// TODO: generalize this to work with other formats
const fmt: NumFmt = NumFmt.F64
const sz = bitSize(fmt) >> 3
wr.emitStmt(Op.STMT1_SETUP_PKT_BUFFER, [literal(sz)])
wr.emitBufStore(literal(p.jdParam2, Op.EXPRx_LITERAL_F64),
NumFmt.F64, 0)
wr.emitBufStore(
literal(p.jdParam2, Op.EXPRx_LITERAL_F64),
NumFmt.F64,
0
)
this.emitSendCmd(role, actuator.serviceCommand)
this.emitSleep(5)
wr.emitStmt(Op.STMT1_SETUP_PKT_BUFFER, [literal(sz)])
wr.emitBufStore(literal(p.jdParam2, Op.EXPRx_LITERAL_F64),
NumFmt.F64, 0)
wr.emitBufStore(
literal(p.jdParam2, Op.EXPRx_LITERAL_F64),
NumFmt.F64,
0
)
this.emitSendCmd(role, actuator.serviceCommand)
this.emitSleep(500)
} else {
} else {
throw "oops"
}
}
Expand Down Expand Up @@ -1114,27 +1123,51 @@ namespace jacs {
wr.emitBufLoad(NumFmt.F64, 12)
)
// hack for keeping car radio from interfering with user radio
if (sensor.tid == microcode.TID_SENSOR_CAR_WALL) {
if (
sensor.tid == microcode.TID_SENSOR_CAR_WALL
) {
wr.emitIf(
wr.emitExpr(Op.EXPR2_LT, [
literal(0xfffff10),
literal(
microcode.robots
.RobotCompactCommand
.ObstacleState
),
radioVar.read(wr),
]),
() => {
radioVar.write(
wr,
wr.emitExpr(Op.EXPR2_SUB, [radioVar.read(wr), literal(0xfffff10)]))
filterValueIn(() => radioVar.read(wr))
})
wr.emitExpr(Op.EXPR2_SUB, [
radioVar.read(wr),
literal(
microcode.robots
.RobotCompactCommand
.ObstacleState
),
])
)
filterValueIn(() =>
radioVar.read(wr)
)
}
)
} else {
wr.emitIf(
wr.emitExpr(Op.EXPR2_LT, [
radioVar.read(wr),
literal(0xfffff10)
literal(
microcode.robots
.RobotCompactCommand
.ObstacleState
),
]),
() => {
filterValueIn(() => radioVar.read(wr))
})
filterValueIn(() =>
radioVar.read(wr)
)
}
)
}
}
)
Expand Down
3 changes: 2 additions & 1 deletion pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"docs.ts",
"gallery.ts",
"pointerevents.ts",
"tooltips.ts"
"tooltips.ts",
"robot/protocol.ts"
],
"testFiles": [],
"targetVersions": {
Expand Down
124 changes: 124 additions & 0 deletions robot/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
namespace microcode.robots {
const MESSAGE_MAGIC = 0xf498

/**
* List of commands supported by the micro:bit robot program
*/
export const enum RobotCommand {
/**
* Runs the robot forward and backward.
* speed: i16 %
*/
MotorRun = 0x01,
/**
* Turn the robot left and right. Right is positive values.
* speed: i16 %
*/
MotorTurn = 0x02,
/**
* Report ultrasonic distance in cm
* distance: f32
*/
UltrasonicDistance = 0x10,

/**
* The line sensor state changed
* state: RobotLineState
*/
LineState = 0x11,
}

export interface RobotMessage {
/**
* message identifier to drop repeated messages; u8
*/
messageId: number
/**
* Robot command
*/
cmd: RobotCommand
/**
* Command payload
*/
payload: Buffer
}

/**
* Encodes the command and payload into a buffer that can be sent via radio
*
* 0 magic, u16
* 2 message id, u8
* 3 cmd, u8
* 4 payload, u8[]
*/
export function encodeRobotMessage(msg: RobotMessage) {
const payload = msg.payload
const messageid = msg.messageId
const cmd = msg.cmd

const buf = pins.createBuffer(6 + payload.length)
buf.setNumber(NumberFormat.UInt16LE, 0, MESSAGE_MAGIC)
buf.setNumber(NumberFormat.UInt8LE, 2, messageid)
buf.setNumber(NumberFormat.UInt8LE, 3, cmd)
buf.write(4, payload)
return buf
}

/**
* Decodes message buffer
*/
export function decodeRobotCommand(msg: Buffer): RobotMessage {
if (!msg || msg.length < 4) return undefined

const magic = msg.getNumber(NumberFormat.UInt16LE, 0)
if (magic !== MESSAGE_MAGIC) return undefined

const messageId = msg.getNumber(NumberFormat.UInt8LE, 2)
const cmd = msg.getNumber(NumberFormat.UInt8LE, 3)
const payload = msg.slice(4)
return <RobotMessage>{
messageId: messageId,
cmd: cmd,
payload: payload,
}
}

/**
* Decode compact radio message
*/
export function decodeRobotCompactCommand(msg: number): RobotMessage {
const messageId = control.micros()
let cmd: RobotCommand
let payload: Buffer
switch (msg) {
case RobotCompactCommand.MotorRunForward:
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
)
break
}
case RobotCompactCommand.MotorTurnLeft:
case RobotCompactCommand.MotorTurnRight: {
cmd = RobotCommand.MotorTurn
payload = Buffer.create(2)
payload.setNumber(
NumberFormat.Int16LE,
0,
msg === RobotCompactCommand.MotorTurnRight ? 100 : -100
)
break
}
default:
return undefined
}

return { messageId, cmd, payload }
}
}
Loading

0 comments on commit 4422070

Please sign in to comment.