1. Case 20: Road Indicator for TPBot with AI Lens

1.1. Purpose


  • Guide the TPBot via the AI Lens to drive by the road indicators.

1.2. Materials required


../../../_images/TPBot_tianpeng_case_20_01.png

../../../_images/TPBot_tianpeng_case_20_02.png

1.3. Hardware Connections

Connect the AI Lens to the IIC port on TPBot.

../../../_images/TPBot_tianpeng_case_20_03.png

1.4. Software


MicroSoft MakeCode

1.5. MakeCode Programming


  • Click “Advanced” to see more choices in the MakeCode drawer.

../../../_images/TPBot_tianpeng_case_20_04.png

  • We need to add a package for programming. Click “Extensions” in the bottom of the drawer and seach with “tpbot” in the dialogue box to download it.

../../../_images/TPBot_tianpeng_case_20_05.png

  • We need to add a package for programming the AI Lens kit. Click “Extensions” in the bottom of the drawer and seach with https://github.com/elecfreaks/pxt-PlanetX-AI in the dialogue box to download it.

../../../_images/TPBot_tianpeng_case_20_06.png

##Sample Code

  • Initialize the AI lens kit in the On start brick and switch its function to cards recognition.

../../../_images/TPBot_tianpeng_case_20_07.png

  • In forever brick, get an image from the AI lens, if the image contains the traffic signals of driving forward, set the TPBot to drive at the speed of 40%.

../../../_images/TPBot_tianpeng_case_20_08.png

  • If the image says turn left, set the TPBot to turn left at the speed of 30% for 0.5s and keep driving forward. As the AI Lens has three buffer zones, here we need to repeat collecting image for three times and discard to clear the buffer.

../../../_images/TPBot_tianpeng_case_20_09.png

  • If the image says turn right, set the TPBot to turn right at the speed of 30% for 0.5s and keep driving forward. As the AI Lens has three buffer zones, here we need to repeat collecting image for three times and discard to clear the buffer.

../../../_images/TPBot_tianpeng_case_20_10.png

  • If the card says stop, stop the TPBot immediately.

../../../_images/TPBot_tianpeng_case_20_11.png

  • Complete code:

../../../_images/TPBot_tianpeng_case_20_12.png

Reference

-- ---

1.6. Python Programming


Add TPBot extension: https://www.elecfreaks.com/learn-cn/microbitKit/TPbot_tianpeng/TPbot-python.html Add AI Lens extension: https://www.elecfreaks.com/learn-cn/microbitplanetX/ai/Plant-X-EF05035-python.html

Code

from microbit import *
from AILens import *
from TPBot import *

tp = TPBOT()
ai = AILENS()
# Set the function of AI Lens in cards recognition
ai.switch_function(Card)
while True:
    # Get an image
    ai.get_image()
    if (ai.get_card_content() == "Forward"):
        tp.set_motors_speed(40,40)
    if (ai.get_card_content() == "Turn left"):
        tp.set_motors_speed(0,30)
        sleep(1000)
        tp.set_motors_speed(40,40)
        # Repeat getting images for three times to clear the buffer zones
        i = 0
        while i < 3:
          ai.get_image()
          i += 1
    if (ai.get_card_content() == "Turn right"):
        tp.set_motors_speed(30,0)        
        sleep(1000)
        tp.set_motors_speed(40,40)
         # Repeat getting images for three times to clear the buffer zones
        i = 0
        while i < 3:
          ai.get_image()
          i += 1
    if (ai.get_card_content() == "Stop"):
        tp.set_motors_speed(0,0)
   

1.7. Result


The TPBot acts as the cards indicate, if the card contains moving forward signal, it drives forward; if it is turning left signal, the TPBot turns left and goes forward; if it is turning right signal, the TPBot turns right and goes forward; if it is stop signal, the TPBot stops moving.

1.8. Exporation


1.9. FAQ


Q: TPBot doesn’t work with the sample code. A: It is probably due to the lack of battery power, please try adding the speed of the TPBot or replacing with new batteries. Q: The AI Lens is not working, and it does not go to the function page with the sample code. A: Please try replacing with new batteries.

1.10. Relevant File