Spark Motors
Spark Motors are just the type of motors the Romi uses. After this, we'll refer to them as Sparks. Here are them on the Romi itself:
Creating Spark
Objects
Spark
ObjectsThis is the constructor of a Spark:
Spark s = new Spark(int motorID)
Each motor has its specific ID to differentiate between each other. Usually, their IDs are written on them (if they aren't, ask an experienced programmer for help). No two motors should have the same ID, or your motors won't work properly.
In the Romi template, both motors were already created, and their IDs were filled out. Here is some code for creating said motors:
// The Romi has the left and right motors set to
// PWM channels 0 and 1 respectively
private final Spark m_leftMotor = new Spark(0);
private final Spark m_rightMotor = new Spark(1);
Now that you can access the motors through your code (yay!), you need to make them move. It's a lot easier than you think.
void set(double speed)
void set(double speed)
As you can probably tell, set() is a method that sets the motor's speed to whatever value speed
is. Speed can only be between -1.0 and 1.0. Anything more or less would probably not work.
/* sets the left motor's speed to 50%. */
m_leftMotor.set(0.5);
Inverting Spark
Motors
Spark
MotorsOne other thing you should know besides setting motors is inverting motors. Inverting motors allows you to make 2 opposite-facing motors move in the same direction. One example of where you would need this is on the Romi because the 2 motors are facing opposite directions, so if we make both the motors move at 100% speed, the Romi won't go straight, but instead spin because one motor is pushing the opposite way as the other motor.
Inverting a motor is also a method in the motor object like the .set();
method. Here is an example with the m_rightMotor
and the m_leftMotor
object.
//The right motor is now inverted
m_rightMotor.setInverted(true);
//The left motor is now not inverted
m_leftMotor.setInverted(false);
Challenge: The moveRobot()
Method
moveRobot()
MethodNow that you learned about how to set motors, at the bottom of the RomiDrivetrain
class, make a helper method called moveRobot()
.
Conditions:
Once you have what you believe to be your answer, compare your code with the answer code below.
Last updated