+ All Categories
Home > Documents > A tiny turtle robot DEI The University of Padova.

A tiny turtle robot DEI The University of Padova.

Date post: 02-Jan-2016
Category:
Upload: rosamund-thornton
View: 218 times
Download: 0 times
Share this document with a friend
44
A tiny turtle robot DEI The University of Padova
Transcript
Page 1: A tiny turtle robot DEI The University of Padova.

A tiny turtle robot

DEIThe University of Padova

Page 2: A tiny turtle robot DEI The University of Padova.

A Tiny RobotTurtle

Page 3: A tiny turtle robot DEI The University of Padova.

Objectives

A very simple moving robotic construction

Straight line and rotation moving allowed

The robot can execute single moving commands like a Logo turtle (forward, backward, rotate left and right)

For demonstration purposes Logo-like commands are implemented as NXT-G user commands and NXC functions

Page 4: A tiny turtle robot DEI The University of Padova.

Building instructions

Run the TinyRobotTurtle model using Lego Digital Designer (file TinyRobotTurtle.lxf) (also the local mode is ok)

You can run the animation of the building instruction selecting the Building Instruction mode (F7)

In the Building Instruction Player you can choose the type (sugg. Vehicles) and how many bricks to be added with each step (sugg. 1..3)

Page 5: A tiny turtle robot DEI The University of Padova.

Programming with NXT-G

We would define: The 'macro-commands' forward (fd) and

back (bk) with a parameter specifying a distance

The 'macro-commands' right and left with a parameter specifying an angle

NXT-G supports only user block definitions without explicit parameters

We substitute parameters respectively with the common variables Steps and Angle

Page 6: A tiny turtle robot DEI The University of Padova.

NXT-G Step 1 a In a new program define the new

numeric variable Steps

Page 7: A tiny turtle robot DEI The University of Padova.

NXT-G Step 1 b Common variables must be defined

with the same name and type in main program and all subprograms

Page 8: A tiny turtle robot DEI The University of Padova.

NXT-G Step 2 a Add a Variable block to read the input

parameter

Page 9: A tiny turtle robot DEI The University of Padova.

NXT-G Step 2 b Set the block as reading the Steps

variable

Page 10: A tiny turtle robot DEI The University of Padova.

NXT-G Step 3 a Add a Move block to move forward the

number of steps specified by the input parameter

Page 11: A tiny turtle robot DEI The University of Padova.

NXT-G Step 3 b Select the two output port (A-C)

The direction depends on how the motors are put on the robot

The power is set on a medium value (40) Brake mode allows a more precise move

Page 12: A tiny turtle robot DEI The University of Padova.

NXT-G Step 3 b For simplicity, we decide to set the

duration in motor degree units and to make this unit correspondent to the Turtle forward/backward 'unit'

In the motor block you can set an arbitrary duration: the actual duration is expressed by the Steps input parameter in motor degree units

Page 13: A tiny turtle robot DEI The University of Padova.

NXT-G Step 3 c Connect with a data

wire the Variable block output with the Duration input of the Move block

Its yellow colour confirms that it is a numeric data wire

Page 14: A tiny turtle robot DEI The University of Padova.

NXT-G Step 4 a Select all the

blocks and choose Make A New My Block from the Edit Menu

Assign Fd as block name and write a description

Page 15: A tiny turtle robot DEI The University of Padova.

NXT-G Step 4 b Choose an

appropriate icon (you can drag more than one icon)

With Finish the block is recorded in your profile and listed as a My Block

Page 16: A tiny turtle robot DEI The University of Padova.

NXT-G Step 5 a Define Bk as a new

My Block from a new (sub)program similar to Fd but moving the robot backward (a different direction in the Move block)

Page 17: A tiny turtle robot DEI The University of Padova.

NXT-G Step 5 b Assign an

appropriate icon also to this My Block

Page 18: A tiny turtle robot DEI The University of Padova.

NXT-G Step 6 a Define two other similar (sub)programs

as new My Block named Lt and Rt for the two types of rotations

For both commands the input parameter is represented by the Angle common variable which expresses the rotation of the robot turtle in degree

We determine experimentally the ratio between the robot and the motor degree units: in the example such a ratio is about 2.22 and, for the sake of precision, the Angle value is multiplied by 12445 and then divided by 5600

Page 19: A tiny turtle robot DEI The University of Padova.

NXT-G Step 6 b In a new (sub)program define the

Angle numeric variable and read it

Page 20: A tiny turtle robot DEI The University of Padova.

NXT-G Step 7 a Add a first Math block as a

multiplication with the value 12445

Page 21: A tiny turtle robot DEI The University of Padova.

NXT-G Step 7 b Connect the output of the Variable

block with first operand input (A) of the Math block

Page 22: A tiny turtle robot DEI The University of Padova.

NXT-G Step 8 a Add a second Math block as a division

with the value 5600

Page 23: A tiny turtle robot DEI The University of Padova.

NXT-G Step 8 b Connect the output of the first Math

block with first operand input (A) of the second Math block

Page 24: A tiny turtle robot DEI The University of Padova.

NXT-G Step 9 a We decide to produce the rotation of

the robot controlling singularly each one of the two motors with the same power and opposite direction

The synchronization is guaranteed giving the two commands in immediate sequence and waiting for completion only after the second command

Page 25: A tiny turtle robot DEI The University of Padova.

NXT-G Step 9 a Add a Motor

block for moving backwards the first motor (A) and connect the previous data output to the Duration input

Page 26: A tiny turtle robot DEI The University of Padova.

NXT-G Step 9 b Do the same

for the other motor (C) but moving forwards and waiting for completion

Page 27: A tiny turtle robot DEI The University of Padova.

NXT-G Step 9 c Select all the

elements and define the new My Block named Lt

Page 28: A tiny turtle robot DEI The University of Padova.

NXT-G Step 10 a The Rt

(sub)program is almost identical to Lt: exchange the direction in the two Motor blocks

Page 29: A tiny turtle robot DEI The University of Padova.

NXT-G Step 10 b Define a

new My Block named Rt with an appropriate icon

Page 30: A tiny turtle robot DEI The University of Padova.

NXT-G Step 11 a The demo main program simulates the

well known lists of Logo instructions to draw a square with an edge of <dim> pixels, forwards and backwardsrepeat 4 [fd <dim> rt 90]repeat 4 [lt 90 bk <dim>]

Some sounds are added to separate the various commands

Page 31: A tiny turtle robot DEI The University of Padova.

NXT-G Step 11 a Start the program defining the two

variables Steps and Angle

Page 32: A tiny turtle robot DEI The University of Padova.

NXT-G Step 12 Insert a Loop block

with Control type set to Count and Until set to 4 to simulate the repeat command

Page 33: A tiny turtle robot DEI The University of Padova.

NXT-G Step 13 Add a

Variable block to set the Steps value to 300 (corresponding to a movement of the robot of some centimetres)

Page 34: A tiny turtle robot DEI The University of Padova.

NXT-G Step 14 a Add a Fd

(user) block using the Custom palette

Page 35: A tiny turtle robot DEI The University of Padova.

NXT-G Step 14 b Notice that no

data wired is usable with user blocks: this is why we use common variables as parameters

Page 36: A tiny turtle robot DEI The University of Padova.

NXT-G Step 15 Add a Sound

block to produce a Click (!Click sound file) at the end of the previous forward command

Page 37: A tiny turtle robot DEI The University of Padova.

NXT-G Step 16 With similar operations add a Variable

block to assign the constant 90 to Angle, to rotate right the robot, and to produce a Click sound

The first loop is now complete

Page 38: A tiny turtle robot DEI The University of Padova.

NXT-G Step 17 Add a Wait block of 2 seconds to

separate the two motions

Page 39: A tiny turtle robot DEI The University of Padova.

NXT-G Step 18 With similar sub-steps complete the

second loop (pay attention to its order of blocks)

The last sound file is Applause!

Page 40: A tiny turtle robot DEI The University of Padova.

NXT-G Step 19 Now the example is complete Download the program and run it

Page 41: A tiny turtle robot DEI The University of Padova.

Programming with NXC

To compare the solution in NXT-G with a solution based on a textual language, we use NXC (Not eXactly C) to defined the four basic movements (fd, bk, rt, lt) and the main program

Page 42: A tiny turtle robot DEI The University of Padova.

Programming with NXC#define RATIO 12445/5600

void fd(long steps)

{

RotateMotorEx(OUT_AC, -30, steps, 0, true, true);

}

void bk(long steps)

{

RotateMotorEx(OUT_AC, 30, steps, 0, true, true);

}

Page 43: A tiny turtle robot DEI The University of Padova.

Programming with NXCvoid rt(long angle)

{

angle = angle * 12445 / 5600;

NumOut (0, LCD_LINE1, angle, true);

RotateMotorEx(OUT_AC, -30, angle, 90, true, true);

}

void lt(long angle)

{

angle = angle * 12445 / 5600;

NumOut (0, LCD_LINE1, angle, true);

RotateMotorEx(OUT_AC, -30, angle, -90, true, true);

}

Page 44: A tiny turtle robot DEI The University of Padova.

Programming with NXC

task main(){ repeat(4) { fd(300); PlayFile("! Click.rso"); rt(90); PlayFile("! Click.rso"); } Wait(2000); repeat(4) { lt(90); PlayFile("! Click.rso"); bk(300); PlayFile("! Click.rso"); } Wait(500); PlayFile("! Applause.rso"); Wait(4000);}


Recommended