440 likes | 457 Vues
A tiny turtle robot. DEI The University of Padova. A Tiny RobotTurtle. 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)
E N D
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
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)
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
NXT-G Step 1 a • In a new program define the new numeric variable Steps
NXT-G Step 1 b • Common variables must be defined with the same name and type in main program and all subprograms
NXT-G Step 2 a • Add a Variable block to read the input parameter
NXT-G Step 2 b • Set the block as reading the Steps variable
NXT-G Step 3 a • Add a Move block to move forward the number of steps specified by the input parameter
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
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
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
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
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
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)
NXT-G Step 5 b • Assign an appropriate icon also to this My Block
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
NXT-G Step 6 b • In a new (sub)program define the Angle numeric variable and read it
NXT-G Step 7 a • Add a first Math block as a multiplication with the value 12445
NXT-G Step 7 b • Connect the output of the Variable block with first operand input (A) of the Math block
NXT-G Step 8 a • Add a second Math block as a division with the value 5600
NXT-G Step 8 b • Connect the output of the first Math block with first operand input (A) of the second Math block
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
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
NXT-G Step 9 b • Do the same for the other motor (C) but moving forwards and waiting for completion
NXT-G Step 9 c • Select all the elements and define the new My Block named Lt
NXT-G Step 10 a • The Rt (sub)program is almost identical to Lt: exchange the direction in the two Motor blocks
NXT-G Step 10 b • Define a new My Block named Rt with an appropriate icon
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
NXT-G Step 11 a • Start the program defining the two variables Steps and Angle
NXT-G Step 12 • Insert a Loop block with Control type set to Count and Until set to 4 to simulate the repeat command
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)
NXT-G Step 14 a • Add a Fd (user) block using the Custom palette
NXT-G Step 14 b • Notice that no data wired is usable with user blocks: this is why we use common variables as parameters
NXT-G Step 15 • Add a Sound block to produce a Click (!Click sound file) at the end of the previous forward command
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
NXT-G Step 17 • Add a Wait block of 2 seconds to separate the two motions
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!
NXT-G Step 19 • Now the example is complete • Download the program and run it
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
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); • }
Programming with NXC • void 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); • }
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); • }