Introduction to Motors

Now that you know how to use the LCD display as an output, let's learn how to use the motors. While motors make the robot move, they are also an output (just like the LCD display) and can be programmed to respond to a variety of input. But first, we need to learn the basics of how to make the robot move.

Objectives

  1. Improve understanding of basic Java programming using the IntelliBrain bots.
  2. Learn how to make the robot move forward and backward.
  3. Learn how to make the robot rotate in place and drive in circles.
  4. Use the motors as an input/output by using the thumbwheel as input.

Important Words

  • new
  • constructor
  • instance
  • method
  • signature
  • problem solving
  • critical thinking
  • time


Before you start

Know how to create a project in Java IDE and RoboJDE

Have basic familiarity with the IntelliBrain bot

Complete the Introduction to Input/Output tutorial



Making the Robot Move Forward

We know the robot can move, but how do we make it happen? Let's start building a program that will make the robot move forward. The information which tells the program the details about how to use the motors on the robot are located in two libraries that we need to import. Let's start by writing a new program MoveForward and do that now.


    import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.Motor;  //the first library we need to import to use the motors

	import com.ridgesoft.robotics.ContinuousRotationServo;  //the second library we need to import to use the motors

	

	public class MoveForward {

		 

		public static void main(String[] args){

			

			//this is where your code will go

			

        }

    }

Now that we have our import statements which will allow us to use the motors on the IntelliBrain bot, we need the robot to know that it has two motors. While this seems obvious to us as we can look at the robot and see that it has two motors attached to it, the program needs to be told that it has two motors that it can use (just like with the LCD display). We will also need to tell it a lot of different information about the motor as well. This includes: (1) what port the motor is plugged into, (2) which direction the motor is going to turn, (3) and the maximum speed allowed. This is done in what is called a constructor, which allows us to create variables of certain types and each constructor has a certain form it needs to follow. The online documentation for these constructors from Ridgesoft can be found here //link to API.


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.Motor;

	import com.ridgesoft.robotics.ContinuousRotationServo;

	

	public class MoveForward {

			 

		public static void main(String[] args){

		

			Motor leftMotor = new ContinuousRotationServo(IntelliBrain.getServo(1), false, 10);  //we are creating the left motor here

			Motor rightMotor = new ContinuousRotationServo(IntelliBrain.getServo(2), true, 10);  //we are creating the right motor here

		}

	}

We have now created two motors which the robot can use to move. We have named the left motor, leftMotor, following Java naming convention, told the program that this motor is plugged into port (1), the direction it is turning is false, and the maximum speed allowed is 10. We did the same for the right motor, changing the variable name to rightMotor, the port that it is plugged into (2), and the direction it is turning - true, and maximum speed is also 10.


Note: The two motors on the robot are actually two of the same motor, and in order to have the motors rotate the wheels in the proper direction, one of the motors needs to be going backwards. This is accomplished by setting the rotation of one to true and the rotation of the other to false.

We have motors, now what? How do we make the robot move? We need to let the robot know how fast we want it to move. This is done by using a method defined in the libraries we imported earlier. The .setPower(6) method will use the code from the library to set the speed of the motors to 6. Let's set the power of both of our motors to 6 now.


    import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.Motor;

	import com.ridgesoft.robotics.ContinuousRotationServo;

	

	public class MoveForward {

		 

		public static void main(String[] args){

		

			Motor leftMotor = new ContinuousRotationServo(IntelliBrain.getServo(1), false, 10);

			Motor rightMotor = new ContinuousRotationServo(IntelliBrain.getServo(2), true, 10);

			

			leftMotor.setPower(6);  //setting the power of the left motor using a method from the library

			rightMotor.setPower(6);  //setting the power of the right motor using a method from the library

		}

    }

We're almost done! We've set the power to our motors, but we haven't told it anything about how long it is supposed to be moving forward. Let's do that using a while(true) loop. By using a while(true) loop, we are forcing the program into an infinite loop and it will keep moving forward until you stop it. Be careful and don't let it hit anything!


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.Motor;

	import com.ridgesoft.robotics.ContinuousRotationServo;

	

	public class MoveForward {

		 

		public static void main(String[] args){

		

			Motor leftMotor = new ContinuousRotationServo(IntelliBrain.getServo(1), false, 10);

			Motor rightMotor = new ContinuousRotationServo(IntelliBrain.getServo(2), true, 10);

			

			while(true) {

				leftMotor.setPower(6);

				rightMotor.setPower(6);

			}

		}

	}

Congratulations! You just wrote the program to make the robot move forward!


Summary of what we did: we imported the libraries from Ridgesoft that we needed to be able to use the motors, we then created two motors using the appropriate constructor, next we set the power for both of the motors, and finally we told the robot how long to move forward.



Words to know

new
- a keyword that lets the program know you are creating a new object for it to use
constructor
- a method that creates an object of the specified type, line 9 in the preceding codes for example
instance
- you can create multiple objects of the same type with different names, in the above code we have leftMotor and rightMotor. While both are motors, they are different instances of the same object
method
- a small segment of code that performs a specific task, useful for tasks that will be done repeatedly
signature
- the information a method needs in order to perform its task, it appears in the parentheses after a method. See line X setPower(6), setPower( ) is the methods and the 6 within the parentheses is what power level the motors will be set.
time
- we are familiar with time. In this program, time is used as a stopwatch – the program will run for a designated amount of time, then stop.


More Making the Robot Move Forward

  1. What happens if you change the power of the motors to 2? Try it!
  2. What happens if you change the power of the motors to 10? Try it!
  3. What happens if you make the number for the time variable larger? Smaller?
  4. Can you think of reasons being able to set the speed of the motors would be beneficial?


Making the Robot Move Backward

Above, we worked through the process of how to write the code to make the robot move forward. Now we want to make the robot move backwards. Think about the process we did for the robot moving forward and start writing your program named MoveBackward, putting the parts together step by step just like we did before. The complete code for the MoveBackward program is below, but try and write your code first, and then compare your code to ours!


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.Motor;

	import com.ridgesoft.robotics.ContinuousRotationServo;

	

	public class MoveBackward {

		 

		public static void main(String[] args){

			Motor leftMotor= new ContinuousRotationServo(IntelliBrain.getServo(1), false, 10);

			Motor rightMotor = new ContinuousRotationServo(Intellibrain.getServo(2), true, 10);

			

			while(true){

				leftMotor.setPower(-6);

				rightMotor.setPower(-6);

			}

		}

	}

Did you try and write your code first and then compare it to the code above? How did it compare? The program to have the robot move backwards is virtually identical to the program you wrote to make the robot move forward, with one small exception. Did you catch it? The power level that we set the motors is negative, which causes them to rotate in the opposite direction they do normally, resulting in the robot moving backwards.


Note: The maximum speed allowed for the motor (remember you set this when you created the motor) is a positive integer in the constructor, but the values that can be used range from the negative of the value to the value. For example, we put in the integer 10 so the valid values to set the motor speed range from -10 to 10.

More Making the Robot Move Backward

  1. What was the difference between this code and the first one?
  2. What happens if you change the power of the motors to 2? Try it!
  3. What happens if you change the power of the motors to 10? Try it!
  4. What happens if you make the number for the time variable larger? Smaller?
  5. Can you think of reasons being able to have the robot move backward would be beneficial?

Making the Robot Rotate in Place

You have made the robot move forward and you have made the robot move backward. What about making the robot turn in place? How can we do this? (Hint: remember that the robot has two wheels located across from each other) Think about the process you did for the previous programs and start writing your program called RotateInPlace. The complete code for the RotateInPlace program is below, but try and write your code first, and then compare your code to ours!


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.Motor;

	import com.ridgesoft.robotics.ContinuousRotationServo;

	

	public class RotateInPlace {

	 

		public static void main(String[] args){

			Motor leftMotor= new ContinuousRotationServo(IntelliBrain.getServo(1), false, 10);

			Motor rightMotor = new ContinuousRotationServo(Intellibrain.getServo(2), true, 10);

			

			while(true){

				leftMotor.setPower(6);

				rightMotor.setPower(-6);

			}

		}

	}

How did we get the robot to rotate in place? All we needed to do was set the motors at the same speed, but have one rotating in the opposite direction of the other.


More Making the Robot Rotate in Place

  1. What was the difference between this code and the first two?
  2. What happens if you change the power of the motors to 2? Try it!
  3. What happens if you change the power of the motors to 10? Try it!
  4. Can you make the robot rotate in the opposite direction?
  5. What happens if you make the number for the time variable larger? Smaller?
  6. Can you think of reasons being able to have the robot rotate in palce would be beneficial?

Making the Robot Move in a Circle

Whew! We're almost done with this tutorial! We've learned how to write the program for the robot to move forward, to move backward, and to rotate in place. Your last task will be to have the robot drive in a circle. Follow the same process that you have been and start to write a program named MoveInCircle. Think about what you are trying to do, and keep in mind what you know how to do. Just like before, the complete code for MoveInCircle is located below, but write your code first and see how it compares!


	import com.ridgesoft.intellibrain.IntelliBrain;

	import com.ridgesoft.robotics.Motor;

	import com.ridgesoft.robotics.ContinuousRotationServo;

	

	public class MoveInCircle {

	 

		public static void main(String[] args){

			Motor leftMotor= new ContinuousRotationServo(IntelliBrain.getServo(1), false, 10);

			Motor rightMotor = new ContinuousRotationServo(Intellibrain.getServo(2), true, 10);

			

			while(true){

				leftMotor.setPower(10);

				rightMotor.setPower(5);

			}

		}

	}

What did we need to do to get the robot to move in a circle? All we needed to do was set the power of one motor higher than the other (or the power of one motor lower than the other, depending on how you like to look at things). Why is the robot moving in the direction it going when making the circle? (Hint: Remember there are two motors, and each are moving at a different speed)


Words to know

critical thinking
- one important aspect of programming is critical thinking. It is often very helpful to take a step back and think about the specific problem you are trying to solve, what do you know how to do, what capabilities does the robot have, and then make a solution in a step by step manner.
problem solving
- programming is problem solving. What do you know, what is the robot capable of, and how do you create a solution.

More Making the Robot Move in a Circle


  1. What was the difference between this code and the previous ones?
  2. What happens if you change the power of the motors to 2? Try it!
  3. What happens if you change the power of the motors to 10? Try it!
  4. What happens if you make the number for the time variable larger? Smaller?
  5. Which direction is your robot turning? Can you make it turn the other way?
  6. Can you think of reasons being able to have the robot move in a circle would be beneficial?

You did it! You've completed the tutorial for learning how to program the IntelliBrain bot to move using the motors.