Exercise: Drive in a Square Pattern

Now lets try something more difficult. Lets write a program to drive your robot in a square pattern. This requires 8 moves: drive straight for some amount of time, turn, drive straight, turn, drive straight, turn, drive strait, turn. Lets use the Java for loop to program only 2 moves, drive straight and turn but then repeat those two moves to get the total driving moves we need.

Select the package ev3.exercises. Right-click and create a Java class called DriveSquare then copy and paste the code below into that class:

Exercise: Drive Forward and Stop

Now its time write more interesting Java programs for the EV3. Start Eclipse on your PC. When Eclipse is ready, the EV3 Exercises project you created in the HelloWorld lesson should appear in the project explorer on the left side of the screen.

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. Here is the code:

Exercise: Using Encoders

So far we have been controlling the distance the robot travels with elapsed time. A more accurate way to control movement is with encoders. Encoders attach to or are integrated into the drive motors and count the revolutions of the motor shaft either optically or magnetically. The DcMotor object has support for using encoder counts to control how long the motor will run when turned on. In the example code we will run forward for 5000 encoder counts and stop. Then we will run backwards to zero encoder counts to the starting point.

Exercise: Using a Touch Sensor

Lets look a using sensors by starting with the simple touch sensor. This is simply a button that can be used to signal your code that something has happened by pushing the button. We will add a REV Robotics Touch Sensor to the robot and plug it into digital port 0-1 on the Expansion Hub. We modify our controller phone configuration to identify digital port 1 as a  Rev Touch Sensor and name it touch_sensor. Why did we use port 1 instead of 0? It has to do with the way the ports are wired.

Exercise: Using Servos

Now lets look at using servos to control robot functions. The wheel motors we used in the previous exercises simply run at whatever power level they are set at. Servos are different in that they have a defined range of motion and you control them by setting the location in that range you want the servo to move to. Once in that position, servos resist movement. Servos are typically used for arms and grippers.

Exercise: Joystick Driving - Tank Mode

Now lets look at a teleop example. Here we will use the joysticks on one of the Xbox controllers to drive the robot. We will use tank style driving. In tank mode each joystick controls the motor on one side of the robot. Moving the joystick forward or backward will cause the motor on the corresponding side of the robot to turn in that direction and the power level will be determined by how much the joystick is moved off of center. In this mode, to drive straight you have to move each joystick the same amount.

Exercise: Joystick Driving - Arcade Mode

Lets look at another style of motor control (driving) called arcade. In arcade mode, a single joystick controls both forward/backward motion but also left and right. This allows the robot to be driven with one finger, typically the thumb.

Sample Robot in more Detail

Lets examine the Sample Robot project in more detail.

Looking at Robot.java the first thing we see is the package created for us by the WPILib plugin based on our team number.

Next we see some reference variables defined. These are for a RobotDrive object and a Joystick object. These objects are part of the WPILib API. The RobotDrive object gives you many ways to control the motors in a robot drive system.  The Joystick object gives you access to the inputs (deflection and buttons) available from a joystick.

Programming the RoboRio

RoboRio based robots can be programmed with one of three models or styles of program design. These are:

  • Iterative
  • Command
  • Sample

You can read a discussion of the three models here, but for our lessons we are going to use the Sample (also called Simple) model. It is our belief that this is the easiest model to start with.