Instructional Material: Exercise: Drive in a Circle
Now lets look at an example that drives the robot in a circle. The key idea here is that if you drive the motors in the same direction but at different power levels (speeds) the robot will drive in an arc and if it runs long enough that arc becomes a circle.
We are going to add a new hardware element to this exercise. We will use a touch sensor to determine when to stop driving.
Create a new Java class file called DriveCircle in the ev3.exercises package, as we have for the previous exercises. Copy the code below to the class file:
The first thing to notice is that we set the motor powers to different levels. This will cause the robot to drive in an arc. You may need to adjust the power to get that arc to be a circle.
Next, we use a while loop let the motors run until the touch sensor is touched. We plug the sensor into sensor port 1. Reading a sensor requires several objects and multiple steps. We will put those steps in a private method to make the code easier to read and to keep the while statement comparison simple. In leJOS, sensors are accessed though an object representing that sensor, like the EV3TouchSensor object instance called sensor1 in our example.
The EV3 has 4 sensor ports numbered 1-4. When you create sensor object you tell the object which sensor port the sensor is connected to by specifying the port on the sensor object constructor. The leJOS API provides an enum called SensorPort with members that specify the port number to use.
The sensor object returns a SampleProvider object which is used to read values from the sensor. Data read from sensors is returned as an array of floating point numbers using the SampleProvider.fetchSample() method. An array is a variable that represents a list of numbers or strings or objects. Touch sensors return a single floating point number, 0 = not touched 1 = touched. The SampleProvider object tells us how big the array of floats needs to be for the number of results that fetchSampe() will return for the particular sensor. We create an array of floats of that size to receive the results of the fetchSample() method. We know all this from reading the documentation for the EV3TouchSensor.
The isTouched() method shows an example of passing a variable from the calling code to the called method (called an argument), the reference (touchSP) to the SampleProvider instance in our case. The isTouched() method fetches the sample value from the touch sensor each time we call it and returns true or false depending on the value of the sample returned in the first position (0) of the array of floats returned by fetchSample().
Finally, the variables SensorPort and MotorPort are variables defined by leJOS for you to use to identify sensor and motor ports. These variables are of the enum or enumeration type. Enums are used to assign meaningful names to a list of numeric values. You can read more about enums here.
Note that when you type the name of any variable, if that variable is a data type that has methods or internal items, you can type the name of the variable followed by a . (dot) and stop. Eclipse will display a list of the items available for the variable. You can then select an item on the list to complete the reference. Documentation, if available, is shown for the items in the list.