Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 14, 2023
2 parents cfbf3c4 + 40911d6 commit 4500545
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 334 deletions.
24 changes: 4 additions & 20 deletions robot/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,12 @@ namespace microcode {
throw "Add 'robot start' block"
}

/**
* Sets both motors of the robot to the speed in percent.
*/
//% block="robot run at %speed=speedPicker \\%"
//% blockid="microcoderobotmotorrun"
//% group="Motors"
//% weight=100
//% speed.defl=100
//% speed.min=-100
//% speed.max=100
export function motorRun(speed: number) {
checkRobotDriver()
robot.motorRun(speed)
}

/**
* Turns the robot.
*/
//% weight=98
//% group="Motors"
//% block="robot turn $turnRatio at $speed \\%"
//% block="robot run with turn $turnRatio at speed $speed \\%"
//% blockid="microcoderobotmotorturn"
//% speed.defl=100
//% speed.min=-100
Expand All @@ -35,10 +20,9 @@ namespace microcode {
//% turnRatio.shadow=turnRatioPicker
//% turnRatio.min=-200
//% turnRatio.max=200
//% turnRatio.defl=100
export function motorTurn(turnRatio: number, speed: number) {
export function motorRun(turnRatio: number, speed: number) {
checkRobotDriver()
robot.motorTurn(turnRatio, speed)
robot.motorRun(turnRatio, speed)
}

/**
Expand Down Expand Up @@ -75,6 +59,6 @@ namespace microcode {
//% group="Sensors"
export function detectLines(state: RobotLineState): boolean {
checkRobotDriver()
return robot.lineState() === state
return robot.currentLineState === state
}
}
6 changes: 1 addition & 5 deletions robot/cutebot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ namespace microcode {
constructor() {
super()
this.musicVolume = 168
this.maxRunSpeed = 100
this.maxBackSpeed = 30
this.maxTurnSpeed = 60
this.maxLineRunSpeed = 28
this.maxLineTurnSpeed = 60
this.maxLineSpeed = 28

pins.setPull(DigitalPin.P8, PinPullMode.PullNone)
pins.setPull(DigitalPin.P13, PinPullMode.PullNone)
Expand Down
6 changes: 1 addition & 5 deletions robot/cutebotpro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,7 @@ namespace microcode {
constructor() {
super()
this.musicVolume = 168
this.maxRunSpeed = 50
this.maxBackSpeed = 50
this.maxTurnSpeed = 50
this.maxLineRunSpeed = 30
this.maxLineTurnSpeed = 30
this.maxLineSpeed = 30

const v = readVersions()
console.log(`cutebot pro version: ${v}`)
Expand Down
164 changes: 1 addition & 163 deletions robot/messages.ts
Original file line number Diff line number Diff line change
@@ -1,163 +1 @@
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.
* turnRatio: i16 [-200,200]
* speed: i16 %
*/
MotorTurn = 0x02,

/**
* Controls the opening angle of the arm
*/
MotorArm = 0x03,

/**
* 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.MotorRunForwardFast:
case RobotCompactCommand.MotorRunForward:
case RobotCompactCommand.MotorRunBackward:
case RobotCompactCommand.MotorStop: {
cmd = RobotCommand.MotorRun
payload = Buffer.create(3)
if (msg !== RobotCompactCommand.MotorStop) {
let speed = 0
switch (msg) {
case RobotCompactCommand.MotorRunForward: speed = 40; break;
case RobotCompactCommand.MotorRunForwardFast: speed = 100; break;
case RobotCompactCommand.MotorRunBackward: speed = -100; break;
}
payload.setNumber(
NumberFormat.Int16LE,
0,
speed
)
if(msg !== RobotCompactCommand.MotorRunForwardFast)
payload[2] = 1
}
break
}
case RobotCompactCommand.MotorSpinLeft:
case RobotCompactCommand.MotorSpinRight:
case RobotCompactCommand.MotorTurnLeft:
case RobotCompactCommand.MotorTurnRight: {
cmd = RobotCommand.MotorTurn
payload = Buffer.create(4)
let turnRatio = 0
let speed = 100
switch (msg) {
case RobotCompactCommand.MotorTurnLeft: turnRatio = -50; speed = 100; break;
case RobotCompactCommand.MotorTurnRight: turnRatio = 50; speed = 100; break;
case RobotCompactCommand.MotorSpinLeft: turnRatio = -200; speed = 80; break;
case RobotCompactCommand.MotorSpinRight: turnRatio = 200; speed = 80; break;
}
payload.setNumber(
NumberFormat.Int16LE,
0,
turnRatio
)
payload.setNumber(
NumberFormat.Int16LE,
2,
speed
)
break
}
case RobotCompactCommand.MotorArmClose:
case RobotCompactCommand.MotorArmOpen: {
cmd = RobotCommand.MotorArm
payload = Buffer.create(2)
payload.setNumber(NumberFormat.UInt16LE, 0, msg === RobotCompactCommand.MotorArmClose ? 0 : 100)
break
}
default:
return undefined
}

return { messageId, cmd, payload }
}
}
// obsolete
11 changes: 0 additions & 11 deletions robot/radio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ namespace microcode.robots {
radio.setGroup(radioGroup)
}

let nextMessageId = 0
export function sendCommand(cmd: RobotCommand, payload: Buffer) {
nextMessageId = (nextMessageId + 1) % 0xff
const buf = encodeRobotMessage({
messageId: nextMessageId,
cmd,
payload,
})
radio.sendBuffer(buf)
}

export function sendCompactCommand(cmd: RobotCompactCommand) {
radio.sendNumber(cmd)
}
Expand Down
6 changes: 1 addition & 5 deletions robot/robot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ namespace microcode.robots {

export class Robot {
musicVolume = 64
maxRunSpeed = 80
maxBackSpeed = 50
maxTurnSpeed = 60
maxLineRunSpeed = 40
maxLineTurnSpeed = 50
maxLineSpeed = 40

constructor() {

Expand Down
Loading

0 comments on commit 4500545

Please sign in to comment.