The following examples are based on the standard rover assembly as shown before using two EV3 motors connected to port MA and MB (NXT motors may also be used). The motors have a build-in encoder that reports the current rotating position, but there is no feedback regulation algorithm included in the library. So the motor speed depends somewhat on the current battery voltage.
Single motor moving forward for a given time
The forward()/backward() methods return immediately while the motor continues to run. So we may think that these commands "put the motor in a forward/backward-state". The exit() command stops all running motors automatically.
There are several methods in the Motor class that uses the rotation counter, among them getMotorCount(), rotateTo(), continueTo(), continueRelativeTo().
Two motors moving together
To handle the moving rover with ease, a class Gear is defined that combines two motors at port MA and MB. To most simple action is to drive the rover forward and then backward a while.
The motors are not really synchronized using the rotation counter. So if some deviation of the straight motion may result. The methods left() and right() turn one wheel forward and the other backward and the rover rotates at the same spot. For turning smoothly in the forward or backward direction, the methods leftArc() and rightArc() are used.
There are overloaded versions of these methods with a time parameter. These are blocking functions that returns only at the end of the time interval. If within a short time delay, no other moving command is issued, the gear stops, otherwise there is no stop between consecutive movements. This feature ensures that the movement is smooth with no jerks.
This is the most simple program to move the rover and it shows the strength of the object oriented design.