Events are the driving force behind moving around in any interactive environment. We human beings are constantly reacting to what we sense in our environment, adjusting our behavior accordingly. Why shouldn't our robots behave in the same way? Unfortunately, our NXT rover doesn't have eyes to sense its environment with. But it does have touch sensors, which when pressed can make the rover adjust its behavior or make it move in a certain way.
In Bumper.java you see the following code:
NxtRover myNxtRover = new NxtRover(5.6, 13.2, 2);
Touch myTouch = new Touch(Sensor.S1);
myNxtRover.moveForward();
boolean notDone = true;
while (notDone){
if(myTouch.isPressed()){
notDone = false;
}
}
myNxtRover.halt();
In task1 we create a NxtRover object. In last class lab we introduced the NxtRover class that can creates rover objects with certain data and behaviors. The methods you worked in the past are:
moveForward(int n) : Move forward for n milliseconds. moveBackward(int n) : Move backward for n milliseconds. turn(int n) : Turn counterclockwise (clockwise if negative) for n milliseconds. halt() : Stop the motors and wait for them to stop. turnLeft(d) : Turn left d degrees turnRight(d) : Turn right d degrees
For the touch sensor, a class called Touch is already written for you and can be found under the icommand.platform.nxt.Touch (hence the import statement). When the Touch sensor object is created we need pass parameter that tells the Touch object to which port 1, 2, 3, or 4 it is connected on the NXT Brick. In this example, we are using Sensor.S1, and hence the touch sensor should be connected to port 1 on the NXT Brick. The Touch class provides method calles isPressed, which returns boolean value true if the touch sensor is pressed, otherwise false.
So in this task, we make the rover move forward until the touch sensor is pressed. Once pressed, it comes to halt. Compile Bumper.java and run the program. To execute the program:
|
Extend your solution to task one in Bumper.java so that if the rover encounters an obstacle (i.e., a bump sensor is pressed), the rover stops, backs up a bit, turns 90 degrees (right or left direction) and then continues forward in that new direction.
In this task we will make the rover draw a polygon shape based on the number of sides as an input. This input will be received from the touch sensor. The number of times a touch sensor is pressed corresponds to the shaped of n-sided polygon. After you have entered your input, there needs to be indication that your done inputing the number of sides. To simplify things, we will use a second touch sensor. When the second touch sensor is pressed, we are done entering number of sides and now we can go ahead and draw the polygon. When you connect second sensor, make sure that you connect to one of the free ports on the NXT brick.
To draw polygon shape recollect that that you need to know the interior angle of the polygon. Hint:
sum of interior angle of polygon = 180 * (n - 2); where n = number of sides
Test you program with n = 3, 4, 5, 6.
After you have figured out the angles, you can use the turnRight or turnLeft methods to move the rover by certain degrees. Remember that the turn methods are depedent upon motor speed, batteries vary in charge, and surfaces provide different amount of friction, we might do a little extra work. Only if you observe that turnLeft or turnRight methods are of approximately +/- 5 degrees off the target degrees then do either of the following:
OR
NXTRover rover = new NXTRover(56.0, 12.7, 2.0);
rover.changeRoverSpeed(value); //changes the speed of the rover and the value can be in range 0 - 100
//Rest of of the commands
Complete this task in class Polygon.java. Try to incrementally code. You can use System.out.println( ) method in code to help debug your program. E.g. You can try to print the actual number of presses you made for number and sides and whether the other touch sensor was pressed etc.
You have implemented the wall hugging mouse mechanically as well as programmed the NXT in lego programming language. Now we will implement this behavior using Java. For the mouse (which is our rover) to detect the wall, we use the touch sensor along with an extended arm. This arm is placed on the left side of the rover and hence the mouse hugs the left wall for movement.
Your main goal is to implement the wall hugging behavior with an extra challenge that if the wall is removed, after some point the mouse (rover) should stop looking for the wall (in the mechanical implementation, the mouse kept circling, until power button was turned off).
This is an open ended question, and hence try to discuss in group for the approach you will take in designing this behavior. Hint: Use the mechnaical implemetation as source of guidance. Make sure to comment the code enough to explain your group approach.
Use any method in NxTRover class to acheive this behavior. Also to additional methods that might be helpful are added to nxtRover class. These are:
moveClockwise( ) - Move in Clockwise direction until overridden by another movement command moveAntiClockwise( ) - Move in AntiClockwise direction until overridden by another movement command
Note: Both of the above methods reduce the rover speed to 30 (from the default 50 set during the constructor call). This is to slow down the movement down of the rover. If you use these methods and want to increase or decrease the speed feel to change it.
Complete WallMouse.java to implement the wall hugging mouse behavior.