... | ... | @@ -193,231 +193,9 @@ You can check to make sure that you have the library by searching for the librar |
|
|
basic.showNumber(radioGroup)
|
|
|
motobit.enable(MotorPower.On)
|
|
|
|
|
|
</pre>
|
|
|
</details>
|
|
|
|
|
|
<details>
|
|
|
<summary>
|
|
|
Potential JavaScript Solution, controller.js (Click to Expand)
|
|
|
</summary>
|
|
|
<pre>
|
|
|
|
|
|
let groupSet = 0
|
|
|
let radioGroup = 0
|
|
|
input.onButtonPressed(Button.A, () => {
|
|
|
if (groupSet == 0 && radioGroup > 0) {
|
|
|
radioGroup = radioGroup - 1
|
|
|
} else {
|
|
|
radio.sendString("left")
|
|
|
basic.showLeds(`
|
|
|
. . # . .
|
|
|
. # . . .
|
|
|
# # # # #
|
|
|
. # . . .
|
|
|
. . # . .
|
|
|
`)
|
|
|
}
|
|
|
})
|
|
|
input.onButtonPressed(Button.AB, () => {
|
|
|
if (groupSet == 0) {
|
|
|
groupSet = 1
|
|
|
radio.setGroup(radioGroup)
|
|
|
basic.showIcon(IconNames.Happy)
|
|
|
} else {
|
|
|
radio.sendString("forward")
|
|
|
basic.showLeds(`
|
|
|
. . # . .
|
|
|
. # # # .
|
|
|
# . # . #
|
|
|
. . # . .
|
|
|
. . # . .
|
|
|
`)
|
|
|
}
|
|
|
})
|
|
|
input.onButtonPressed(Button.B, () => {
|
|
|
if (groupSet == 0 && radioGroup < 255) {
|
|
|
radioGroup = radioGroup + 1
|
|
|
} else {
|
|
|
radio.sendString("right")
|
|
|
basic.showLeds(`
|
|
|
. . # . .
|
|
|
. . . # .
|
|
|
# # # # #
|
|
|
. . . # .
|
|
|
. . # . .
|
|
|
`)
|
|
|
}
|
|
|
})
|
|
|
radioGroup = 0
|
|
|
while (groupSet != 1) {
|
|
|
basic.showNumber(radioGroup)
|
|
|
}
|
|
|
basic.showIcon(IconNames.Happy)
|
|
|
basic.pause(100)
|
|
|
basic.showNumber(radioGroup)
|
|
|
|
|
|
</pre>
|
|
|
</details>
|
|
|
<br></br>
|
|
|
|
|
|
|
|
|
<details>
|
|
|
<summary>
|
|
|
Potential Python 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>
|
|
|
<br></br>
|
|
|
|
|
|
|
|
|
<details>
|
|
|
<summary>
|
|
|
Potential Python 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>
|
|
|
6. Test your code. Your robot should:
|
|
|
1.
|
|
|
7. Now try to intercept other robots
|
... | ... | |