... | @@ -120,103 +120,302 @@ You can check to make sure that you have the library by searching for the librar |
... | @@ -120,103 +120,302 @@ You can check to make sure that you have the library by searching for the librar |
|
|
|
|
|
<details>
|
|
<details>
|
|
<summary>
|
|
<summary>
|
|
Potential JavaScript Solution (Click to Expand)
|
|
Potential JavaScript Robot Solution, robot.js (Click to Expand)
|
|
</summary>
|
|
</summary>
|
|
<img src=/uploads/17d1c0e0d04fa0fa9dff9bf961820f3e/Receiver-0-Standard.PNG>
|
|
|
|
<pre>
|
|
<pre>
|
|
radio.onDataPacketReceived( ({ signal, receivedNumber }) => {
|
|
let radioGroup = 0
|
|
if (signal > -50) {
|
|
let groupSet = 0
|
|
basic.showIcon(IconNames.Yes)
|
|
input.onButtonPressed(Button.AB, () => {
|
|
} else if (signal > -60) {
|
|
if (groupSet != 1) {
|
|
basic.showLeds(`
|
|
groupSet = 1
|
|
. # # # .
|
|
radio.setGroup(radioGroup)
|
|
# # # # #
|
|
}
|
|
# # # # #
|
|
})
|
|
# # # # #
|
|
radio.onDataPacketReceived( ({ receivedString: remoteString }) => {
|
|
. # # # .
|
|
if (remoteString == "forward") {
|
|
`)
|
|
motobit.setMotorSpeed(Motor.Left, MotorDirection.Forward, 100)
|
|
} else if (signal > -80) {
|
|
motobit.setMotorSpeed(Motor.Right, MotorDirection.Forward, 100)
|
|
basic.showLeds(`
|
|
basic.showLeds(`
|
|
. . # . .
|
|
. . # . .
|
|
. # # # .
|
|
. # # # .
|
|
# # # # #
|
|
# . # . #
|
|
. # # # .
|
|
. . # . .
|
|
. . # . .
|
|
. . # . .
|
|
`)
|
|
`)
|
|
} else if (signal > -120) {
|
|
basic.pause(200)
|
|
basic.showLeds(`
|
|
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(`
|
|
`)
|
|
. . # . .
|
|
} else {
|
|
. # . . .
|
|
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)
|
|
radio.setGroup(1)
|
|
motobit.setMotorSpeed(Motor.Right, MotorDirection.Reverse, 50)
|
|
basic.showIcon(IconNames.Diamond)
|
|
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>
|
|
</pre>
|
|
</details>
|
|
</details>
|
|
|
|
|
|
<details>
|
|
<details>
|
|
<summary>
|
|
<summary>
|
|
Potential Python Solution (Click to Expand)
|
|
Potential JavaScript Controller Solution, controller.js (Click to Expand)
|
|
</summary>
|
|
</summary>
|
|
<pre>
|
|
<pre>
|
|
from microbit import *
|
|
let groupSet = 0
|
|
import radio
|
|
let radioGroup = 0
|
|
|
|
input.onButtonPressed(Button.A, () => {
|
|
strength_1 = Image("00000:"
|
|
if (groupSet == 0 && radioGroup > 0) {
|
|
"00000:"
|
|
radioGroup = radioGroup - 1
|
|
"00900:"
|
|
} else {
|
|
"00000:"
|
|
radio.sendString("left")
|
|
"00000")
|
|
basic.showLeds(`
|
|
|
|
. . # . .
|
|
strength_2 = Image("00000:"
|
|
. # . . .
|
|
"00900:"
|
|
# # # # #
|
|
"09990:"
|
|
. # . . .
|
|
"00900:"
|
|
. . # . .
|
|
"00000")
|
|
`)
|
|
|
|
}
|
|
strength_3 = Image("00900:"
|
|
})
|
|
"09990:"
|
|
input.onButtonPressed(Button.AB, () => {
|
|
"99999:"
|
|
if (groupSet == 0) {
|
|
"09990:"
|
|
groupSet = 1
|
|
"00900")
|
|
radio.setGroup(radioGroup)
|
|
|
|
basic.showIcon(IconNames.Happy)
|
|
strength_4 = Image("09990:"
|
|
} else {
|
|
"99999:"
|
|
radio.sendString("forward")
|
|
"99999:"
|
|
basic.showLeds(`
|
|
"99999:"
|
|
. . # . .
|
|
"09990")
|
|
. # # # .
|
|
|
|
# . # . #
|
|
display.show(Image.DIAMOND)
|
|
. . # . .
|
|
radio.on()
|
|
. . # . .
|
|
radio.config(group = 1)
|
|
`)
|
|
|
|
}
|
|
while True:
|
|
})
|
|
details = radio.receive_full()
|
|
input.onButtonPressed(Button.B, () => {
|
|
if details is not None:
|
|
if (groupSet == 0 && radioGroup < 255) {
|
|
x, rssi, y = details
|
|
radioGroup = radioGroup + 1
|
|
if (rssi>-50):
|
|
} else {
|
|
display.show(Image.YES)
|
|
radio.sendString("right")
|
|
elif (rssi>-60):
|
|
basic.showLeds(`
|
|
display.show(strength_4)
|
|
. . # . .
|
|
elif (rssi>-80):
|
|
. . . # .
|
|
display.show(strength_3)
|
|
# # # # #
|
|
elif (rssi>-120):
|
|
. . . # .
|
|
display.show(strength_2)
|
|
. . # . .
|
|
else:
|
|
`)
|
|
display.show(strength_1)
|
|
}
|
|
|
|
})
|
|
|
|
radioGroup = 0
|
|
|
|
while (groupSet != 1) {
|
|
|
|
basic.showNumber(radioGroup)
|
|
|
|
}
|
|
|
|
basic.showIcon(IconNames.Happy)
|
|
|
|
basic.pause(100)
|
|
|
|
basic.showNumber(radioGroup)
|
|
|
|
|
|
</pre>
|
|
</pre>
|
|
</details>
|
|
</details>
|
|
|
|
|
|
|
|
<details>
|
|
|
|
<summary>
|
|
|
|
Potential Python Robot Solution, excerpt from robot.py (Click to Expand)
|
|
|
|
</summary>
|
|
|
|
<pre>
|
|
|
|
from microbit import *
|
|
|
|
import radio
|
|
|
|
|
|
|
|
############################
|
|
|
|
#NEW CODE
|
|
|
|
|
|
|
|
from microbit import *
|
|
|
|
import radio
|
|
|
|
|
|
|
|
class MotoBit:
|
|
|
|
moto_l = 0x21
|
|
|
|
moto_r = 0x20
|
|
|
|
moto_on = 0x70
|
|
|
|
|
|
|
|
def __init__(self, address = 0x59):
|
|
|
|
self.ADDR = address
|
|
|
|
|
|
|
|
def write16(self,a,b):
|
|
|
|
i2c.write(self.ADDR, bytes([a,b]), repeat=False)
|
|
|
|
|
|
|
|
# True or False
|
|
|
|
def enable(self, pwr):
|
|
|
|
if pwr:
|
|
|
|
self.write16(0x70,1)
|
|
|
|
else:
|
|
|
|
self.write16(0x70,0)
|
|
|
|
|
|
|
|
# 0 for right, 1 for left, speed -127 to 127
|
|
|
|
def set_speed(self, motor, speed):
|
|
|
|
motor = motor + 32
|
|
|
|
if speed>=0:
|
|
|
|
self.write16(motor,128 + speed)
|
|
|
|
else:
|
|
|
|
speed = speed + 127
|
|
|
|
self.write16(motor, speed)
|
|
|
|
# left and right speeds
|
|
|
|
def drive(self,left,right):
|
|
|
|
self.set_speed(0,right)
|
|
|
|
self.set_speed(1,left)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
radio_group = 0 #number of radio frequency to use
|
|
|
|
group_set = False #has the radio group been set?
|
|
|
|
motobit = MotoBit()
|
|
|
|
motobit.enable(True) # Enable motor driver
|
|
|
|
radio.on()
|
|
|
|
radio.config(power=7)
|
|
|
|
|
|
|
|
stop_sign = Image( "09990:"
|
|
|
|
"90099:"
|
|
|
|
"90909:"
|
|
|
|
"99009:"
|
|
|
|
"09990")
|
|
|
|
|
|
|
|
while True:# do forever
|
|
|
|
if not group_set:
|
|
|
|
display.show(str(radio_group))
|
|
|
|
if button_a.is_pressed() and button_b.is_pressed():
|
|
|
|
group_set = True
|
|
|
|
radio.config(channel = radio_group)
|
|
|
|
display.show(Image.HAPPY)
|
|
|
|
|
|
|
|
elif button_a.is_pressed():
|
|
|
|
if radio_group > 0:
|
|
|
|
radio_group = radio_group - 1
|
|
|
|
|
|
|
|
elif button_b.is_pressed():
|
|
|
|
if radio_group < 255:
|
|
|
|
radio_group = radio_group + 1
|
|
|
|
sleep(150)
|
|
|
|
|
|
|
|
else:# radio group has been set
|
|
|
|
incoming = radio.receive()
|
|
|
|
|
|
|
|
if incoming is "forward":
|
|
|
|
motobit.drive(127, 127)
|
|
|
|
display.show(Image.ARROW_N)
|
|
|
|
sleep(200)
|
|
|
|
motobit.drive(0,0)
|
|
|
|
elif incoming is "right":
|
|
|
|
motobit.drive(60,-60)
|
|
|
|
display.show(Image.ARROW_E)
|
|
|
|
sleep(200)
|
|
|
|
motobit.drive(0,0)
|
|
|
|
elif incoming is "left":
|
|
|
|
motobit.drive(-60, 60)
|
|
|
|
display.show(Image.ARROW_W)
|
|
|
|
sleep(200)
|
|
|
|
motobit.drive(0,0)
|
|
|
|
sleep(100)
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
</details>
|
|
|
|
|
|
|
|
<details>
|
|
|
|
<summary>
|
|
|
|
Potential Python Controller Solution, controller.py (Click to Expand)
|
|
|
|
</summary>
|
|
|
|
<pre>
|
|
|
|
from microbit import *
|
|
|
|
import radio
|
|
|
|
|
|
|
|
radio_group = 0 #number of radio frequency to use
|
|
|
|
forward = False
|
|
|
|
group_set = False #has the radio group been set?
|
|
|
|
radio.on()
|
|
|
|
radio.config(power=7)
|
|
|
|
|
|
|
|
stop_sign = Image( "09990:"
|
|
|
|
"90099:"
|
|
|
|
"90909:"
|
|
|
|
"99009:"
|
|
|
|
"09990")
|
|
|
|
|
|
|
|
while True:# do forever
|
|
|
|
if group_set is False:
|
|
|
|
display.show(str(radio_group))
|
|
|
|
|
|
|
|
if button_a.is_pressed() and button_b.is_pressed():
|
|
|
|
if group_set is False:
|
|
|
|
group_set = True
|
|
|
|
radio.config(channel = radio_group)
|
|
|
|
display.show(Image.HAPPY)
|
|
|
|
forward = False
|
|
|
|
else:
|
|
|
|
if forward == False:
|
|
|
|
radio.send("forward")
|
|
|
|
forward = True
|
|
|
|
display.show(Image.ARROW_N)
|
|
|
|
else:
|
|
|
|
radio.send("stop")
|
|
|
|
forward = False
|
|
|
|
display.show(stop_sign)
|
|
|
|
|
|
|
|
elif button_a.is_pressed():
|
|
|
|
if group_set == False and radio_group > 0 :
|
|
|
|
radio_group = radio_group - 1
|
|
|
|
else:
|
|
|
|
radio.send("left")
|
|
|
|
display.show(Image.ARROW_W)
|
|
|
|
|
|
|
|
elif button_b.is_pressed():
|
|
|
|
if group_set is False and radio_group < 255:
|
|
|
|
radio_group = radio_group + 1
|
|
|
|
else:
|
|
|
|
radio.send("right")
|
|
|
|
display.show(Image.ARROW_E)
|
|
|
|
sleep(200)
|
|
|
|
|
|
|
|
</pre>
|
|
|
|
</details>
|
|
|
|
|
|
<br></br>
|
|
<br></br>
|
|
|
|
|
|
6. Test your code. Your robot should:
|
|
6. Test your code. Your robot should:
|
... | @@ -224,7 +423,6 @@ while True: |
... | @@ -224,7 +423,6 @@ while True: |
|
7. Now try to intercept other robots
|
|
7. Now try to intercept other robots
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Step 7: Further Exploration
|
|
### 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.
|
|
* 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.
|
|
|
|
|
... | | ... | |