... | ... | @@ -118,89 +118,113 @@ You can check to make sure that you have the library by searching for the librar |
|
|
|
|
|
5. Flash the robot hex file to one Micro::Bit and the controller hex file to the other. You should be able to choose a radio group on each and lock it in. From there, you should be able to drive your robot using the controller. To help you in the debugging process, we have provided a potential solution. The completed code for these solutions (as well as pictures of the completed code blocks) is included in the directory you downloaded during setup.
|
|
|
|
|
|
<details>
|
|
|
<summary>
|
|
|
Potential JavaScript Solution, robot.js (Click to Expand)
|
|
|
</summary>
|
|
|
<pre>
|
|
|
let radioGroup = 0
|
|
|
let groupSet = 0
|
|
|
input.onButtonPressed(Button.AB, () => {
|
|
|
if (groupSet != 1) {
|
|
|
groupSet = 1
|
|
|
radio.setGroup(radioGroup)
|
|
|
}
|
|
|
})
|
|
|
radio.onDataPacketReceived( ({ receivedString: remoteString }) => {
|
|
|
if (remoteString == "forward") {
|
|
|
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 100)
|
|
|
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 100)
|
|
|
basic.showLeds(`
|
|
|
. . # . .
|
|
|
. # # # .
|
|
|
# . # . #
|
|
|
. . # . .
|
|
|
. . # . .
|
|
|
`)
|
|
|
basic.pause(200)
|
|
|
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 0)
|
|
|
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 0)
|
|
|
} else if (remoteString == "left") {
|
|
|
motobit.setMotorSpeed(Motor.Left, MotorDirection.Reverse, 50)
|
|
|
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 50)
|
|
|
basic.showLeds(`
|
|
|
. . # . .
|
|
|
. # . . .
|
|
|
# # # # #
|
|
|
. # . . .
|
|
|
. . # . .
|
|
|
`)
|
|
|
basic.pause(200)
|
|
|
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 0)
|
|
|
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 0)
|
|
|
} else if (remoteString == "right") {
|
|
|
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 50)
|
|
|
motobit.setMotorSpeed(Motor.Right, MotorDirection.Reverse, 50)
|
|
|
basic.showLeds(`
|
|
|
. . # . .
|
|
|
. . . # .
|
|
|
# # # # #
|
|
|
. . . # .
|
|
|
. . # . .
|
|
|
`)
|
|
|
basic.pause(200)
|
|
|
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 0)
|
|
|
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 0)
|
|
|
} else {
|
|
|
|
|
|
}
|
|
|
})
|
|
|
input.onButtonPressed(Button.A, () => {
|
|
|
if (groupSet != 1 && radioGroup > 0) {
|
|
|
radioGroup = radioGroup - 1
|
|
|
}
|
|
|
})
|
|
|
input.onButtonPressed(Button.B, () => {
|
|
|
if (groupSet != 1 && radioGroup < 255) {
|
|
|
radioGroup = radioGroup + 1
|
|
|
}
|
|
|
})
|
|
|
while (groupSet != 1) {
|
|
|
basic.showNumber(radioGroup)
|
|
|
}
|
|
|
basic.showIcon(IconNames.Happy)
|
|
|
basic.pause(100)
|
|
|
basic.showNumber(radioGroup)
|
|
|
motobit.enable(MotorPower.On)
|
|
|
|
|
|
</pre>
|
|
|
</details>
|
|
|
<details>
|
|
|
<summary>
|
|
|
Potential JavaScript Solution (Click to Expand)
|
|
|
</summary>
|
|
|
<img src=/uploads/17d1c0e0d04fa0fa9dff9bf961820f3e/Receiver-0-Standard.PNG>
|
|
|
<pre>
|
|
|
radio.onDataPacketReceived( ({ signal, receivedNumber }) => {
|
|
|
if (signal > -50) {
|
|
|
basic.showIcon(IconNames.Yes)
|
|
|
} else if (signal > -60) {
|
|
|
basic.showLeds(`
|
|
|
. # # # .
|
|
|
# # # # #
|
|
|
# # # # #
|
|
|
# # # # #
|
|
|
. # # # .
|
|
|
`)
|
|
|
} else if (signal > -80) {
|
|
|
basic.showLeds(`
|
|
|
. . # . .
|
|
|
. # # # .
|
|
|
# # # # #
|
|
|
. # # # .
|
|
|
. . # . .
|
|
|
`)
|
|
|
} else if (signal > -120) {
|
|
|
basic.showLeds(`
|
|
|
. . . . .
|
|
|
. . # . .
|
|
|
. # # # .
|
|
|
. . # . .
|
|
|
. . . . .
|
|
|
`)
|
|
|
} else {
|
|
|
basic.showLeds(`
|
|
|
. . . . .
|
|
|
. . . . .
|
|
|
. . # . .
|
|
|
. . . . .
|
|
|
. . . . .
|
|
|
`)
|
|
|
}
|
|
|
})
|
|
|
radio.setGroup(1)
|
|
|
basic.showIcon(IconNames.Diamond)
|
|
|
</pre>
|
|
|
</details>
|
|
|
<details>
|
|
|
<summary>
|
|
|
Potential Python Solution (Click to Expand)
|
|
|
</summary>
|
|
|
<pre>
|
|
|
from microbit import *
|
|
|
import radio
|
|
|
|
|
|
strength_1 = Image("00000:"
|
|
|
"00000:"
|
|
|
"00900:"
|
|
|
"00000:"
|
|
|
"00000")
|
|
|
|
|
|
strength_2 = Image("00000:"
|
|
|
"00900:"
|
|
|
"09990:"
|
|
|
"00900:"
|
|
|
"00000")
|
|
|
|
|
|
strength_3 = Image("00900:"
|
|
|
"09990:"
|
|
|
"99999:"
|
|
|
"09990:"
|
|
|
"00900")
|
|
|
|
|
|
strength_4 = Image("09990:"
|
|
|
"99999:"
|
|
|
"99999:"
|
|
|
"99999:"
|
|
|
"09990")
|
|
|
|
|
|
display.show(Image.DIAMOND)
|
|
|
radio.on()
|
|
|
radio.config(group = 1)
|
|
|
|
|
|
while True:
|
|
|
details = radio.receive_full()
|
|
|
if details is not None:
|
|
|
x, rssi, y = details
|
|
|
if (rssi>-50):
|
|
|
display.show(Image.YES)
|
|
|
elif (rssi>-60):
|
|
|
display.show(strength_4)
|
|
|
elif (rssi>-80):
|
|
|
display.show(strength_3)
|
|
|
elif (rssi>-120):
|
|
|
display.show(strength_2)
|
|
|
else:
|
|
|
display.show(strength_1)
|
|
|
</pre>
|
|
|
</details>
|
|
|
<br></br>
|
|
|
|
|
|
6. Test your code. Your robot should:
|
|
|
1.
|
|
|
7. Now try to intercept other robots
|
|
|
|
|
|
|
|
|
|
|
|
### Step 7: Further Exploration
|
|
|
* Can you think of a way to incorporate a reverse function for the microbot? What if you needed to push and hold a+b to make it move in a direction? How would you select between forward and reverse? Spend some time exploring with your Micro::Bit and see what kind of improvements you can make.
|
|
|
|
... | ... | |