introduction to robotics using dfrobot maqueen documentation

57
Introduction to Robotics using DFRobot Maqueen Documentation John Nelver Lastierre Jun 10, 2019

Upload: others

Post on 16-Oct-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics usingDFRobot Maqueen Documentation

John Nelver Lastierre

Jun 10, 2019

Page 2: Introduction to Robotics using DFRobot Maqueen Documentation
Page 3: Introduction to Robotics using DFRobot Maqueen Documentation

Contents:

1 Micro:bit - Getting Started 11.1 Basic Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2 Micro:bit Accessories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71.3 Powering your micro:bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141.4 What is a Computer Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151.5 Introduction to Block-Based Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151.6 Using the MakeCode Block Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171.7 Block-Based Programming Quick Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211.8 Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2 Hello World! 232.1 Your First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232.2 Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

3 Introduction to Maqueen Robotic Kit 293.1 DFRobot Maqueen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

4 Maqueen Programming Examples 334.1 Motor Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334.2 RGB Breathing Ambient Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344.3 LED Light Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354.4 Read Ultrasonic Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364.5 Read Infrared Key Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364.6 IR Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374.7 Line-tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394.8 Ultrasonic Obstacle-avoiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394.9 Light-operated Sprite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404.10 Wireless Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

5 Maqueen Programming using Python 435.1 Motor Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435.2 RGB Breathing Ambient Light . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445.3 LED Light Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455.4 Read Ultrasonic Distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465.5 Read Infrared Key Assignments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465.6 IR Remote Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465.7 Line-tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

i

Page 4: Introduction to Robotics using DFRobot Maqueen Documentation

5.8 Ultrasonic Obstacle-avoiding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495.9 Light-operated Sprite . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

6 Indices and tables 53

ii

Page 5: Introduction to Robotics using DFRobot Maqueen Documentation

CHAPTER 1

Micro:bit - Getting Started

The BBC micro:bit is a tiny computer that you can use to create all kinds of projects from robots to musical instruments– the possibilities are endless. It can be coded from any web browser in Blocks, Javascript, Python, Scratch and more;no software required. Micro:bit is widely used in schools around the world, from Finland and Iceland to Singaporeand Sri Lanka. Let’s take a look at the features that you can use in your designs:

• 25 red LED lights that can flash messages.

• Two programmable buttons (A and B) that can be used to tell the micro:bit when to start and stop things.

• 21 Physical input/output connection pins.

• A temperature sensor to measure temperature.

• An accelerometer to detect motion.

• A magnetometer to tell you which direction you’re heading in.

• A radio and a Bluetooth Low Energy connection to interact with other devices.

• A USB interface

1

Page 6: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.1 Basic Parts

Let’s examine what each of the parts do.

1.1.1 LEDs

LED stands for Light Emitting Diode. The micro:bit has 25 individually-programmable LEDs, arrange in a 5 by 5pattern, allowing you to display text, numbers, and images.

2 Chapter 1. Micro:bit - Getting Started

Page 7: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.1.2 Buttons

There are two buttons on the front of the micro:bit (labelled A and B). You can detect when these buttons are pressed,allowing you to trigger code on the device.

1.1.3 Pins

There are 25 external connectors on the edge connector of the micro:bit, which we refer to as ‘pins’. Program motors,LEDs, or other electrical components with the pins, or connect extra sensors to control your code!

1.1. Basic Parts 3

Page 8: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.1.4 Temperature Sensor

This sensor allows the micro:bit to detect the current ambient temperature, in degrees Celsius.

1.1.5 Light Sensor

By reversing the LEDs of the screen to become an input, the LED screen works as a basic light sensor, allowing you

4 Chapter 1. Micro:bit - Getting Started

Page 9: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

to detect ambient light.

1.1.6 Accelerometer

An accelerometer measures the acceleration of your micro:bit; this component senses when the micro:bit is moved. Itcan also detect other actions, like shake, tilt, and free-fall.

1.1.7 Magnetometer

1.1. Basic Parts 5

Page 10: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

The compass detects the earth’s magnetic field, allowing you to detect which direction the micro:bit is facing. Thecompass has t calibrated before it can be used.

1.1.8 Radio

The radio feature allows you to communicate wirelessly between micro:bits. Use the radio to send messages to othermicro:bits, build multiplayer games, and much more!

1.1.9 Bluetooth

6 Chapter 1. Micro:bit - Getting Started

Page 11: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

A BLE (Bluetooth Low Energy) antenna allows the micro:bit to send and receive Bluetooth signals. This allowsthe micro:bit to wirelessly communicate with PCs, Phones, and Tablets, so you can control your phone from yourmicro:bit and send code wirelessly to your device from your phone!

1.1.10 USB interface

The USB interface allows you to connect the micro:bit to your computer via a micro-USB cable, which will power thedevice and allow you to download programs onto the micro:bit.

1.2 Micro:bit Accessories

Besides the micro:bit microcontroller, there are other accessories that you will be needing in the succeeding exercisesand challenges.

1.2. Micro:bit Accessories 7

Page 12: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.2.1 Battery and Battery Holder

The battery holder and 2x AAA battery will power your micro:bit if it is not connected to the USB, so that you canbring your micro:bit anywhere.

8 Chapter 1. Micro:bit - Getting Started

Page 13: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.2.2 USB Cable

This both powers your micro:bit when connected to the PC and act as a USB mass storage to be use for transferringyour program from PC. On one end it is a USB type A to be connected to your PC and on other end it is a micro USBtype B to be connected to your micro:bit.

1.2.3 Crocodile Clips

This connects your micro:bit to other components such as LED lights. On one end has a banana clip for easy con-nection to your micro:bit, and on other end it has crocodile clip for easy connection to other components. Convention

1.2. Micro:bit Accessories 9

Page 14: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

is that black colored wires/clips are connected to GND and red colored wires/clips are connected to the 3 main pins(pin0, pin1 and pin2).

1.2.4 Jumper Wires

Also known as DuPont wires, these cables are connector at one end and usually used to connect components on abreadboard or a circuit module.

10 Chapter 1. Micro:bit - Getting Started

Page 15: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.2.5 Speaker

This device produces sound by converting electrical signal into audio. We will use this on later chapter on music.

1.2.6 LED Light

This is a simple while LED light, that we will be using for some exercises and challenges. Make sure to add a resistoron on end so that it will not overheat.

1.2. Micro:bit Accessories 11

Page 16: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.2.7 Resistor

This component has resistance and reduces the current running through your wire. Effective use of this componentwill protect your micro:bit and your other components from damage due to overcurrent.

1.2.8 Light-Dependent Resistor (LDR)

This component has variable resistance depending on how much light it is capturing. The more light it gets, the lowerthe resistance.

12 Chapter 1. Micro:bit - Getting Started

Page 17: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.2.9 Soil Moisture Sensor Module

This is a module that detects water moisture in soil and changes voltage depending on how much water it detects.

1.2.10 Traffic Light Module

This module has 3 LEDs, Red, Amber and Green, which you can powered on or off individually.

1.2. Micro:bit Accessories 13

Page 18: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.2.11 Passive Infrared Resistor (PIR) module

This module detects motion through the infrared (IR) light by detecting body heat through its sensors. It is passivesince it doesnt emit IR but dependent on the infrared light emitted by the objects.

1.3 Powering your micro:bit

The microbit can be powered by either connecting it to the USB or by 2 AAA batteries thru the battery case.

1.3.1 Using the USB

1. Make sure you have the usb adapter cable and a computer

2. Connect the micro usb (type B) connector of the usb cable to the micro usb port of the microbit

3. Connect the USB type A connector of your usb cable to the USB port of the computer

14 Chapter 1. Micro:bit - Getting Started

Page 19: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.3.2 Using the 2x AAA Batteries

1. Insert the 2 AAA batteries to the battery pack. Make sure the batteries are connected to the correct orientation.

2. THen connect the JST connector of the battery case to the battery connector of the microbit. THe JST connectorwill connect only to the micro bit in only one way.

3. When powering the microbit for the first time, it will automatically run the pre-loaded demo program. It willshow you text and images, interact with the buttons and the accelerometer.

Now you will create your first micro:bit program; after that, well we’ve listed a few ideas but it’s really up to you.

1.4 What is a Computer Program?

Everything runs on code.

You can turn on your computer because code was written (programmed) to let you, code runs your mobile phone, andis the reason why your flashy coffee machine actually makes something when you press a button.

These tutorials will introduce core concepts of coding and show the interaction between hardware and software. Bythe end of these tutorials you will be able to program your BBC micro:bit to do lots of things: from calculators togames to a digital dice. You can then use this knowledge and the BBC micro:bit hardware to create anything you want– once you can code, you can create anything.

Once you have worked through the tutorials and tasks of this book you will be able to program a million and one thingsfor your BBC Micro:bit and beyond. If you can come up with the idea then you can construct it from code.

This guide will leave you with enough knowledge to right your own programs, and with a level of understanding thatallows you to go on to create anything.

1.5 Introduction to Block-Based Programming

What is block-based programming? Block-based programming, sometimes known as block-based coding, is codingwithin a programming language where instructions are mainly represented as blocks. The Micro:bit’s MakeCodeBlock Editor (based on Microsoft MakeCode) is a block-based programming language. It looks like this:

1.4. What is a Computer Program? 15

Page 20: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

Micro:bit’s MakeCode Blocks Editor is a browser based block editor and can be used either online or offline. This is agraphical, drag and drop code editor, where coding blocks snap together like a jigsaw puzzle. There’s support for themain input and output functions of the BBC micro:bit, as well as standard programming constructs such as sequence,selection, repetition and variables. It also has a feature wherein blocks can be converted to JavaScript code and viceversa.

To go to the MakeCode Block Editor, use the link here.

Alternatively, if you have Windows 10 as OS, you can also install the MakeCode Blocks Editor by going to theMicrosoft Store, search for MakeCode for micro:bit, and press install. It has the same functionality as the browserbased editor. Here is the snapshot of the app.

16 Chapter 1. Micro:bit - Getting Started

Page 21: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.6 Using the MakeCode Block Editor

Let’s go through the different parts of the editor. Here is the snapshot below:

1. When clicked, you can create a new project, open your saved projects, open built-in examples, or import a hexfile. A hex file is a micro:bit readable file that you will be uploading to your micro:bit to program it.

2. Here you can switch between MakeCode block-based programming and JavaScript, which is a popular text-based programming that is mainly used for web development. As you progress with your coding skills onblock-based programming, text-based programming is the next step, as this type of programming can supportmore complex features. BBC micro:bit both supports JavaScript and Python programming languages.

3. This is where you select the blocks, arranged by functionality. Different block functions have different colors.

4. This is the workspace, where you put your blocks and create a program.

5. This is the simulator. This is where you can simulate what you code on the workspace. It is also interactive, youcan press the buttons to simulate the action, or shake the virtual micro:bit.

6. Here you can undo/redo and zoom in/out your workspace.

7. Here you can change your program name, and save it as well.

1.6. Using the MakeCode Block Editor 17

Page 22: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

8. This is the download button, pressing this will download the hex file. THe arrow button besides the downloadbutton is to show or hide the simulator.

1.6.1 Types of Blocks

There are several types of blocks in MakeCode Block Editor, determined by the shape of the block.

1. Event Block

These types of block indicate that the blocks inside this block will be executed upon an event. These blocks cannot bestacked on top, and without these blocks the other blocks cannot be executed.

2. Stack Block

These types of block are the main commands, and can be stack on top of the other. They are usually placed inside theevents block.

3. Reporter Block

18 Chapter 1. Micro:bit - Getting Started

Page 23: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

These types of block can hold a value, either a number or a string of characters. It reports a value, hence the name.

4. C Blocks

These are blocks that do conditional statements. They look like the letter C in shape, hence the name.

1.6. Using the MakeCode Block Editor 19

Page 24: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.6.2 Selecting Blocks

1. Select a block category from the list on the left hand side of the page.

2. Select a block from the selected category and drag it to the workspace area on the right.

3. Snap new blocks onto existing blocks in the workspace area. As the new blocks are dragged into the workspace,the editor highlights the connecting parts of each block when they are in a valid position to snap to existingblocks.

1.6.3 Saving

Your code is automatically saved as you work, you can access your saved work by clicking on the Projects icon shownin the above image. To make your projects easier to find, it is always good practice to name your project in the spaceprovided at the foot of the editor.

1.6.4 Downloading

In order to get your code onto a micro:bit, you must first download it. Towards the bottom left of the editor is a largedownload button, when you click this button the code in your workspace will be compiled as a HEX file and thendownloaded to your computers default download folder.

20 Chapter 1. Micro:bit - Getting Started

Page 25: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

1.6.5 Transferring A Program to the micro:bit

To tranfer a program, ensure your microbit is connected to your computer. In Explorer drag your file and drop it ontothe microbit removable drive. As your code transfers to the microbit the orange power LED on the rear of the boardwill begin to flash, Once it stops flashing the transfer is finished and your program is ready.

1.7 Block-Based Programming Quick Tips

• To avoid overwriting your last program click on the Projects folder (top left) and start a new project. Then nameyour new project.

• Find the block you require in the menus and drag it and drop it into the workspace.

• Only blocks that are supposed to go together will be able to be slotted together in the workspace.

• To delete an unwanted block either; right click and select Delete Block, or pick up the block and drag it to themenu and drop it in the bin that appears.

• To duplicate a block, right click and select Duplicate Block.

1.8 Questions

1. A ___________ is a pocket sized computer that you can make all sorts of projects like musical instruments orgames.

2. A ___________ can detect the direction the micro:bit is facing, like a compass.

3. A micro:bit can be powered on by _____________________ and ___________________________.

4. What are the 4 steps in coding the micro:bit. __________________, _______________, __________________and ___________________.

5. _______________________ is a coding with the programming language where instructions are represented byblocks.

1.7. Block-Based Programming Quick Tips 21

Page 26: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

6. True or False. To get your code to the micro;bit, you must download it clicking the download button on theupper right side of teh editor.

7. What is the difference between a block based programming and a text based programming?

22 Chapter 1. Micro:bit - Getting Started

Page 27: Introduction to Robotics using DFRobot Maqueen Documentation

CHAPTER 2

Hello World!

2.1 Your First Program

Coding using the micro:bit is composed of these 4 steps. You can expect to go around the loop quite a few times beforeyou get your code working.

23

Page 28: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

2.1.1 Design the Code

First of all you are going to write a program to display the message “Hello World!” followed by an image on thedisplay of your micro:bit. There’s not much planning and design to do here, but just so that you understand what aplan might look like:

My Plan1. Display "Hello World!!"2. Display a heart image3. Wait for 3 seconds4. Go back to Step 1

2.1.2 Write the Code

Using the JavaScript Blocks Editor, write your first code by drag and drop blocks and try your program on the simu-lator, like the one shown below:

24 Chapter 2. Hello World!

Page 29: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

Let’s go through each block elements:

The forever block will repeat all the blocks inside this forever. Blocks are executed from top to bottom. This meansif all the blocks inside the forever block were executed, it will repeat from the start. Note the shape of the block, thistype of block is called an event block. In this case, forever event means that this will be executed always, so theblocks inside it will be executed indefinitely.

The show string block will display the words inside the quotation marks, also known as strings, on the LEDdisplay. Note the shape of the block, this block is a stack block, they will perform the main command, in this case, toshow string.

The show icon block will display a built-in image in the LED display. You can choose among the different selectionby clicking the icon on the editor.

The pause (ms) block will pause the micro:bit for the 3000 milliseconds, or 3 seconds.

2.1. Your First Program 25

Page 30: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

Notice on the left side, there is a virtual micro:bit running your code at the same time. This is the simulator that wediscussed on the previous chapter. It will execute your code on the simulator, just like the real thing. This is so thatyou could try your code first before running it on the real thing. You could interact with the micro:bit simulator likepushing the buttons and shaking the microbit. There are small buttons below the simulator where you can stop andplay the micro:bit simulator, slo-mo the execution, mute an audio if there is an audio playing on your micro:bit (thiswill be discussed on later chapters), and expand the simulator to fullscreen.

2.1.3 Download the Code

Click the Download button in the editor. This will download a ‘hex’ file, which is a compact format of your programthat your micro:bit can read. Once the hex file has downloaded, copy it to your micro:bit just like copying a file to aUSB drive. On Windows you can right click and choose “Send To→MICROBIT.”

26 Chapter 2. Hello World!

Page 31: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

Or you can drag the hex file to the MICROBIT drive to download the code to your micro:bit.

You have written your first program. Carry on and see what else you can do with the micro:bit.

2.2 Challenge

1. Make a name tag by displaying your name on the LED screen.

2.2. Challenge 27

Page 32: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

28 Chapter 2. Hello World!

Page 33: Introduction to Robotics using DFRobot Maqueen Documentation

CHAPTER 3

Introduction to Maqueen Robotic Kit

3.1 DFRobot Maqueen

Maqueen is a graphical programming robot for STEM education, which inherits playability and simple operation ofmicro:bit. The Mini-body, interesting features and plug-and-play allow children to quickly learn graphic programmingin entertaining, Nurturing student’s interest in science and logical thinking.

3.1.1 Features

• Support for Makecode, will support Scratch and python later.

• Small size, flexible movement.

• All-metal miniature gear motor, good quality, strong driving force.

• Line patrol, ambient light, LED lights, ultrasonic interface, buzzer, I2C interface, mechanical expansion screwhole, etc. . . . full-featured, highly expandable.

29

Page 34: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

• Exclusive customized POM bearing wheel, flexible and reliable, strong obstacle crossing ability.

• Easy to install, easy to use.

3.1.2 Functional Diagram

3.1.3 Specification

• Supply Voltage: 3.5V~5V DC (Three AAA batteries or 3.6V~3.7V lithium battery )

• Infrared Grayscale Sensor(High-low level) x 2

• Buzzer x 1

• Infrared Receiver (NEC decoder) x 1

• LED Lights (High-low level control) x 2

• RGB Ambient Light (16 million colors) x 4

• SR04, SR04P Ultrasonic Interface (5V) x 1

• IIC Interface (3.3V) x 1

• Gravity Extension Interfaces (P1, P2) x 2

• N20 All-metal Gear Motor x 2

• Motor Reduction Ratio: 1:150

• Maximum Rotate Speed: 133 rpm

• Motor Drive Mode: PWM motor drive

• Bracket and Protective Cover Extension M3 Screw Hole x 6

• Programming Method: Makecode graphical programming, Mind graphical programming (based on Scratch3.0), Python

• Dimension: 81mm x 85 mm x 44mm/3.19 x 3.35 x 1.73in

• Weight: 75.55g

3.1.4 Product Configuration List

• Car Body x 1 (Motors and bearing wheel have been polymer before leaving factory)

• Wheel x 2

• Three AAA batteries Box x 1

30 Chapter 3. Introduction to Maqueen Robotic Kit

Page 35: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

• Double Sided Adhesive Tape x 1

3.1.5 Product Installation

3.1.6 Import the Makecode Graphical Library

1. Click the link: https://makecode.microbit.org , enter the makecode graphical online programming platform.(Note: Loading will be slow in the first time, please wait patiently)

2. Import the library: Copy the Maqueen library’s address: https://github.com/DFRobot/pxt-maqueen

3. Import the library by following the steps.

3.1. DFRobot Maqueen 31

Page 36: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

32 Chapter 3. Introduction to Maqueen Robotic Kit

Page 37: Introduction to Robotics using DFRobot Maqueen Documentation

CHAPTER 4

Maqueen Programming Examples

4.1 Motor Control

Motor Control * Learning Target: Mastering the basic method of motor control. * Effect: The car forward 1 second,right turn 1 second, left turn 1 second, back 1 second, back and right turn 1 second.

Makecode Program Link: https://makecode.microbit.org/_2Cc9gM5P5aDs

Screenshot of Makecode Graphical Program:

33

Page 38: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

4.2 RGB Breathing Ambient Light

• Learning Target: Learn the basic way of using ambient light.

• Effect: The RGB ambient light at the bottom of the Maqueen shows a variety of colors and presents a gradienteffect.

Makecode Program Link: https://makecode.microbit.org/_WkgPLpAotP3f

Screenshot of Makecode Graphical Program:

34 Chapter 4. Maqueen Programming Examples

Page 39: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

4.3 LED Light Flash

• Learning Target: Learn the using way of LED light and buzzer.

• Effect: The left and right LED lights flash alternately, and the buzzer emits two different tone frequencies atintervals of 500 milliseconds.

4.3. LED Light Flash 35

Page 40: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

Makecode Program Link: https://makecode.microbit.org/_6gKRm1RVsDxY

Screenshot of Makecode Graphical Program:

4.4 Read Ultrasonic Distance

• Learning Target: Learn to read the distance of ultrasound, so that later can be flexible use of these data.

• Effect: The ultrasonic detecting the obstruction in front and the distance will be displayed on the dot-matrixscreen in centimeters.

Makecode Program Link: https://makecode.microbit.org/_4gi4Dj7yTWgK

Screenshot of Makecode Graphical Program:

4.5 Read Infrared Key Assignments

• Learning Target: Learn to read the key assignments of infrared, so that later can be flexible use of these data.

• Effect: Put the the IR receiver toward the IR remote control, when you press any key on the IR remote control.The key assignments that corresponds to the pressed key will displayed on the dot matrix, in decimal notationthe last two digits of the key Assignments are displayed.

36 Chapter 4. Maqueen Programming Examples

Page 41: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

Makecode Program Link: https://makecode.microbit.org/_361V7bbp0UAg

Screenshot of Makecode Graphical Program:

IR Remote Control and Its Key Assignments The key assignments in the following table are in hexadecimal. In thisproduct, we read the last two digits of the key assignments and automatically convert them to decimal data.

4.6 IR Remote Control

• Learning Target: Learn to use the IR remote control to command the car.

• Effect: Control car forward, left, right, back with 4 keys of IR remote control 2, 4, 6, 8.

Makecode Program Link: https://makecode.microbit.org/_MfDXhX6MM35X

Screenshot of Makecode Graphical Program:

4.6. IR Remote Control 37

Page 42: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

38 Chapter 4. Maqueen Programming Examples

Page 43: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

4.7 Line-tracking

Effect: The car is running along the black line.

Makecode Program Link: https://makecode.microbit.org/_1VzX7LLAC3im

Screenshot of Makecode Graphical Program:

4.8 Ultrasonic Obstacle-avoiding

• Effect: Ultrasonic detects the distance between the car and the obstacle in front of it. If the distance is less than35cm, the car will randomly choose to turn left or right to avoid obstacles.

• Fittings: SR04 Ultrasonic Module x 1 or SR04-P Ultrasonic Module x 1

Makecode Program Link: https://makecode.microbit.org/_Fa4Ef3DwyXW7

Screenshot of Makecode Graphical Program:

4.7. Line-tracking 39

Page 44: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

4.9 Light-operated Sprite

• Effect: The car does not move in the darker light, and as the flashlight illuminates the LED, the vehicle’s forwardspeed begins to increase as the intensity of the light increases.

Makecode Program Link: https://makecode.microbit.org/_fi6DWjCKeM9v

Screenshot of Makecode Graphical Program:

40 Chapter 4. Maqueen Programming Examples

Page 45: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

4.10 Wireless Remote Control

• Learning Target: Learn the way of using micro:bit wireless.

• Effect: Use another micro:bit to control the car’s movement.

Screenshot of Makecode Graphical Program:

Screenshot of Car’s Makecode Graphical Program:

4.10. Wireless Remote Control 41

Page 46: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

42 Chapter 4. Maqueen Programming Examples

Page 47: Introduction to Robotics using DFRobot Maqueen Documentation

CHAPTER 5

Maqueen Programming using Python

Unlike Makecode/Javascript programming, Python programming on Maqueen is not straight forward since there is nobuilt-in extension.

5.1 Motor Control

Motor Control * Learning Target: Mastering the basic method of motor control. * Effect: The car forward 1 second,right turn 1 second, left turn 1 second, back 1 second, back and right turn 1 second.

5.1.1 Principle of Working

Motors drivers are a separate circuit on Maqueen and can be controlled via I2C protocol. Below are the associatedhardware IDs for the motors to be used for controlling the motors: * M1 (Left Motor) - 0x00 * M2 (Right Motor) -0x02

Direction of movement can be controlled to via a single bit:

• Clockwise - 0

• Counter Clockwise - 1

FInally, speed can be controlled via a value from 0-255

All of this information will be combined in a buffer, convert to a byte array and send to Maqueen via I2C.

5.1.2 Python Code

from microbit import *

# dfrobot maqueen motor control demo

(continues on next page)

43

Page 48: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

(continued from previous page)

i2c.init(freq=100000, sda=pin20, scl=pin19)

def MotorControl(motor, direction, speed):buf = bytearray(3)# control for motor (0 for M1 (L) and 1 for M2 (R)if motor == 0:

buf[0] = 0x00else:

buf[0] = 0x02# control for direction (0 for CW and 1 for CCW)if direction == 0:

buf[1] = 0else:

buf[1] = 1# set speedbuf[2] = speedi2c.write(0x10, buf)

while True:MotorControl(0, 0, 255)MotorControl(1, 0, 255)sleep(1000)MotorControl(0, 0, 255)MotorControl(1, 0, 0)sleep(1000)MotorControl(0, 0, 0)MotorControl(1, 0, 255)sleep(1000)MotorControl(0, 1, 255)MotorControl(1, 1, 255)sleep(1000)MotorControl(0, 1, 255)MotorControl(1, 1, 0)sleep(1000)

5.2 RGB Breathing Ambient Light

• Learning Target: Learn the basic way of using ambient light.

• Effect: The RGB ambient light at the bottom of the Maqueen shows a variety of colors and presents a gradienteffect.

5.2.1 Principle of Working

Neopixel is Adafruit’s individually-addressable RGB color pixels and strips based on WS2812, WS2811 and SK6812LED/drivers, using single wire control protocol. Micro:bit has a build in functions on NeoPixel and can be con-trolled using the ‘neopixel’ module. See micro:bit’s MicroPython documentation for more details. Mawueen has 4programmable RGB lights connected to pin15.

44 Chapter 5. Maqueen Programming using Python

Page 49: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

5.2.2 Python Code

from microbit import *import neopixelfrom random import randint

# need to declare pin associated with the RGB lights and the number of RGB lightsnp = neopixel.NeoPixel(pin15, 4)

while True:for pixel_id in range(0, len(np)):

red = randint(0, 60)green = randint(0, 60)blue = randint(0, 60)

# Assign the current LED a random red, green and blue value between 0 and 60np[pixel_id] = (red, green, blue)

# Display the current pixel data on the Neopixel stripnp.show()sleep(100)

5.3 LED Light Flash

• Learning Target: Learn the using way of LED light and buzzer.

• Effect: The left and right LED lights flash alternately, and the buzzer emits two different tone frequencies atintervals of 500 milliseconds.

5.3.1 Principle of Working

Maqueen has 2 red LED lights connected to both sides of the robot. It is connected to pin8 (L) and pin12 (R). Use themicro:bit’s I/O functions to operate these LEDs. It also has a buzzer with is connected to pin0. It can be switch off viaa physical switch on the board if you need to use pin0.

5.3.2 Python Code

from microbit import *import music

while True:# write to red LED (pin 8 is L, pin 12 is R)pin8.write_digital(1)pin12.write_digital(0)music.play("C4:1")sleep(500)pin8.write_digital(0)pin12.write_digital(1)music.play("E4:1")sleep(500)

5.3. LED Light Flash 45

Page 50: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

5.4 Read Ultrasonic Distance

• Learning Target: Learn to read the distance of ultrasound, so that later can be flexible use of these data.

• Effect: The ultrasonic detecting the obstruction in front and the distance will be displayed on the dot-matrixscreen in centimeters.

Makecode Program Link: https://makecode.microbit.org/_4gi4Dj7yTWgK

Screenshot of Makecode Graphical Program:

5.5 Read Infrared Key Assignments

• Learning Target: Learn to read the key assignments of infrared, so that later can be flexible use of these data.

• Effect: Put the the IR receiver toward the IR remote control, when you press any key on the IR remote control.The key assignments that corresponds to the pressed key will displayed on the dot matrix, in decimal notationthe last two digits of the key Assignments are displayed.

Makecode Program Link: https://makecode.microbit.org/_361V7bbp0UAg

Screenshot of Makecode Graphical Program:

IR Remote Control and Its Key Assignments The key assignments in the following table are in hexadecimal. In thisproduct, we read the last two digits of the key assignments and automatically convert them to decimal data.

5.6 IR Remote Control

• Learning Target: Learn to use the IR remote control to command the car.

• Effect: Control car forward, left, right, back with 4 keys of IR remote control 2, 4, 6, 8.

Makecode Program Link: https://makecode.microbit.org/_MfDXhX6MM35X

Screenshot of Makecode Graphical Program:

46 Chapter 5. Maqueen Programming using Python

Page 51: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

5.6. IR Remote Control 47

Page 52: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

5.7 Line-tracking

Effect: The car is running along the black line.

5.7.1 Principle of Working

IR sensors are a class of devices that transmits and/or receives infrared radiation. As black color absorb radiation andwhite color reflects it, when IR LED emits radiation it will only be reflected and detected by IR receiver/photodiodewhen the surface is white or light colors. Black or empty surfaces will absorb or not be reflected, thus no infraredradiation will be detected.

5.7.2 Python Code

from microbit import *

# motor controli2c.init(freq=100000, sda=pin20, scl=pin19)

def MotorControl(motor, direction, speed):buf = bytearray(3)# control for motor (0 for M1 (L) and 1 for M2 (R)if motor == 0:

buf[0] = 0x00else:

buf[0] = 0x02# control for direction (0 for CW and 1 for CCW)if direction == 0:

buf[1] = 0else:

buf[1] = 1# set speed, 0 - 255buf[2] = speedi2c.write(0x10, buf)

while True:# IR sensor reads 1 if white (reflective), 0 if black (or not reflecting the infrared→˓light)

#if pin13 (left IR sensor) reads white and pin14 (right IR sensor) reads black it→˓will turn right

(continues on next page)

48 Chapter 5. Maqueen Programming using Python

Page 53: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

(continued from previous page)

if pin13.read_digital() == 1 and pin14.read_digital() == 0:MotorControl(0,0,255)MotorControl(1,0,0)

#if pin13 (left IR sensor) reads black and pin14 (right IR sensor) reads white it→˓will turn left

elif pin13.read_digital() == 0 and pin14.read_digital() == 1:MotorControl(0,0,0)MotorControl(1,0,255)

# if both IR sensors read all black it will go straightelif pin13.read_digital() == 0 and pin14.read_digital() == 0:

MotorControl(0,0,255)MotorControl(1,0,255)

# if both IR sensors read all white, it will stopelse:

MotorControl(0,0,0)MotorControl(1,0,0)

5.8 Ultrasonic Obstacle-avoiding

• Effect: Ultrasonic detects the distance between the car and the obstacle in front of it. If the distance is less than35cm, the car will randomly choose to turn left or right to avoid obstacles.

• Fittings: SR04 Ultrasonic Module x 1 or SR04-P Ultrasonic Module x 1

Makecode Program Link: https://makecode.microbit.org/_Fa4Ef3DwyXW7

Screenshot of Makecode Graphical Program:

5.8. Ultrasonic Obstacle-avoiding 49

Page 54: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

5.9 Light-operated Sprite

• Effect: The car does not move in the darker light, and as the flashlight illuminates the LED, the vehicle’s forwardspeed begins to increase as the intensity of the light increases.

5.9.1 Principle of Working

Using the display?s LEDs in reverse-bias mode, micro:bit can sense the amount of light falling on the display. Returnsan integer between 0 and 255 representing the light level, with larger meaning more light.

5.9.2 Python Code

from microbit import *

(continues on next page)

50 Chapter 5. Maqueen Programming using Python

Page 55: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

(continued from previous page)

# motor controli2c.init(freq=100000, sda=pin20, scl=pin19)

def MotorControl(motor, direction, speed):buf = bytearray(3)# control for motor (0 for M1 (L) and 1 for M2 (R)if motor == 0:

buf[0] = 0x00else:

buf[0] = 0x02# control for direction (0 for CW and 1 for CCW)if direction == 0:

buf[1] = 0else:

buf[1] = 1# set speed, 0 - 255buf[2] = speedi2c.write(0x10, buf)

while True:# reads light level from 5x5 led and converts it to a valueif display.read_light_level() > 70:

MotorControl(0,0,display.read_light_level())MotorControl(1,0,display.read_light_level())

# stop motors if the light level is below 70else:

MotorControl(0,0,0)MotorControl(1,0,0)

5.9. Light-operated Sprite 51

Page 56: Introduction to Robotics using DFRobot Maqueen Documentation

Introduction to Robotics using DFRobot Maqueen Documentation

52 Chapter 5. Maqueen Programming using Python

Page 57: Introduction to Robotics using DFRobot Maqueen Documentation

CHAPTER 6

Indices and tables

• genindex

• modindex

• search

53