... | ... | @@ -65,6 +65,7 @@ In the packages pop-up, search for "Motobit". Select the moto-bit library in the |
|
|
You can check to make sure that you have the library by searching for the library in your libraries toolbar.
|
|
|
|
|
|
![microbot_lib4](/uploads/50880517646fef4065f781d017771d02/microbot_lib4.PNG)
|
|
|
|
|
|
### Step 6: Programming!
|
|
|
1. Partner up
|
|
|
2. Open the robot base code by:
|
... | ... | @@ -115,8 +116,307 @@ You can check to make sure that you have the library by searching for the librar |
|
|
|
|
|
![flash_py](/uploads/f88092bbd58733b96896c29481a875b1/flash_py.PNG)
|
|
|
|
|
|
5. Flash the robot hex file to one Micro::Bit and the controller hex file to the other.
|
|
|
1.
|
|
|
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>
|
|
|
<br></br>
|
|
|
|
|
|
<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
|
... | ... | |