Question: NXC Programming for Lego Robot. Drawing patterns In the previous two robot labs you developed functions to control the driving of your robot in both
NXC Programming for Lego Robot.
Drawing patterns
In the previous two robot labs you developed functions to control the driving of your robot in both straight-line and turning motions. In this exercise you are going to use these functions to drive your robot through paths that correspond to the outlines of particular shapes like triangles, squares, etc. The shape will be specified by a user who is pressing buttons on the brick.
1.1 Objectives
To practice using and writing functions with multiple parameters.
To learn how to detect when buttons are pressed.
To learn how to display values on the NXT LCD.
To practice using nested loops.
1.2 Materials
1 NXC manual
2 Your robot
3 Ruler/tape measure
1.3 Language elements
This experiment will continue using the functional elements of the NXC language used in the first two robot labs. In addition, read in the NXC manual section 3.10.1 on how to detect if a button is pressed using the ButtonPressed() function, section 3.5.1 on how to play sounds using PlayTone(), and section
3.7.1 on how to display text and numbers on the LCD using TextOut() and NumOut().
1.4 Lab tasks
The overall goal of the lab tasks is to use your motion functions to drive the robot along a path that resembles a geometric shape that is specified by a user.
1.4.1 Simple Input/Output
While there is no keyboard on the NXT brick, there are a few buttons that we can use to allow the user to input a value in a primitive way. There is also a small LCD screen that is 64 pixels tall by 100 pixels wide that can be used to display short messages and numeric values.
1 Get the NXC program file, getinput.nxc, from blackboard that demonstrates the use of a function named GetInput(). This function will display a simple input prompt given to it on the screen, and then the user can press the right button to increase an input value, the left button to decrease the input value, or the orange center button to finalize entry of the value.
2 Modify this simple program to include a validation loop in the main task to make sure that the value input by the user is positive, by displaying an error message and repeating the input process if it is not, just as you have done in other regular programs. Do not change any code inside the GetInput() function. You should then use the returned input value to control the frequency of a tone that you generate using the PlayTone() function. The frequency of the tone should be 1000 Hz times the input value, i.e. an input value of 3 should create a 3KHz tone.
3 Add another loop in main to allow the program to repeatedly input values from the user and play different frequency tones, only stopping when the value of 0 is input.
1
1.4.2 Drawing a shape
In this step you will write and test a function that is capable of guiding the robot along a path that resembles an N-sided geometric shape.
1 This function will have two parameters: the number of sides of the geometric shape and the length of each side in inches.
2 Each side of the geometric shape can be traversed by first driving straight forward for the specified distance, and then turning a proper number of degrees in the clockwise direction. For an N-sided shape, the angle to turn is given by 360 / N.
3 The shape can be traversed by repeating the motion of traveling a side and turning N times.
4 Write a function as just described, that will call the motion functions you wrote in the prior labs to cause the robot to traverse the path describing the shape. Do not put any motor commands in this function, they should all be contained in your driving and turning functions. You may need to adjust some of the constants in your movement equations if you are using a new robot, the batteries have been drained, etc.
5 Test how well your function works by writing a short main task to cause the robot to traverse a triangle and then a square shape by making a simple call to your new function.
1.4.3 The entire program
1. Combine the code elements you have generated in the previous two steps to make a complete program. Allow a user to input two positive values to your robot via the buttons by calling the GetInput() function twice. The first number should indicate the number of sides of the shape. The second number should represent the length of each side of the shape in inches. The robot should be driven around the perimeter of this shape. Additionally, modify your shape function from the previous step so that as each side of the shape is traversed a value should be updated on the LCD indicating what side of the polygon you are driving on now and a different frequency tone is played.
1.4.4 Testing
1. Theoretically your robot should return to the same spot where it started, but it may be off a bit. Make sure that you are within a inch on return.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
