14. Case 11: Following with A Fixed Distance#

14.1. Purpose#


  • Programme to set the TPBot following another car with a fixed distance.

14.2. Material#


../../_images/TPBot_tianpeng_case_01_01.png

14.3. Software#


MicroSoftmakecode

14.4. Programming#


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

../../_images/TPBot_tianpeng_case_01_02.png

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

../../_images/TPBot_tianpeng_case_01_03.png

Sample#

  • Set an icon on the micro:bit display.

  • Save the variable as the distance detected from the TPBot to its front car in the forever brick, judge if the the distance is between 10 and 20(exl. 10&20), if yes, set the speed of both wheels in 0; if the distance is below 10, set the speed of both wheels in -30%; or set the speed both in 30%.

../../_images/TPBot_tianpeng_case_11_04.png

Conclusion#

Power up to show an icon on the micro:bit display and the TPBot keeps a distance to its front car. It goes slower if the distance detected was close or it goes forward if the distance detected was far; and it stops if the distance detected was between the set interval value.

14.5. Exploration#


14.6. FAQ#


Q: The car does not work with the code in the wiki.
A: It should be the batteries that are lack of power, please try to fix it by adding the value of the speed in the code.

14.7. Relevant File#