Quantcast
Channel: CAVEDU教育團隊技術部落格
Viewing all articles
Browse latest Browse all 678

RK-micro:bit robot – line-following programming tutorial(MakeCode)

$
0
0

Introduction

This step-by-step tutorial will show you how to control the RK-micro:bit robot by MakeCode. For the assemly tutorial, please follow this link.

In this tutorial, the robot will follow the black track by its IR light sensor which is located in its front end. Check the video:

author 楊國立 (intern)
Time 1~ 2 hours BOM
Difficulty 2 / 5 

Content

How to control the motor and robot behavior

In RK-micro:bit Car, the two motor are placed back-to-back, we call this a differential drive platform, which means the robot behaviors are decided by the direction/speed of these two motors.

Since we are using a L298n motor driver module to control the two motors, we are acutually control four pins, two pins for one motor:

  • P1, P2 – the left motor
  • P14, P15 – the right motor

First we take a look of how to control one motor, if the motor rotate in the opposite direction, just switch the red/ black wire:

P1 – H P1 – L
P2 – H Brake (can NOT rotate the shaft) Rotate CW
P2 – L Rotate CCW Nothing (can rotate the shaft)

Combining two motors then we can make robot moving around, you can further adjust the motors’ speed to make more detailed contol of our robot:

Right motor – Forward Right motor – Backward
Left motor – Forward Robot moves forward Robot turns right
Left motor – Backward Robot turns left Robot moves backward

Makecode

We will use MakeCode graphical IDE to program RK-micro:bit Car. The code should be quite straight-forward and fun, enjoy!

STEP1: In the on start event, qw declare a variable for the motor speed and use it to P13 and P0.

  

STEP2: The code in forever will run continuously, which means that it will keep looping until you unplug the microbit. 

Since the IR light sensors is connected to P12, and it will return 1 and 0 when it detects the black track and white ground. We can take advantage of this to make a zigzag line-following robot.

In the video, you can see we put the robot (IR light sensor) at the left-hand side of the black track, this means the robot will move toward the front right to seek for black track. If yes, then it will move toward the front left a little bit -> these two-conditional pattern will repeat forever.

So when the light sensor detects the black track (digital read pin P12 = 1), the robot will move toward the front right, otherwise it will move toward the front left.

Note: please modify the motor parameters to make your robot move even better!

Resource

The complete code can be accessed at the following webpage or download the HEX file.

 

 

 

RK-micro:bit robot – line-following programming tutorial(MakeCode)〉這篇文章最早發佈於《CAVEDU教育團隊技術部落格》。


Viewing all articles
Browse latest Browse all 678

Trending Articles