Spark Motors

Spark Motors are just the type of motors the Romi uses. From now on, we'll refer to them as Sparks. Here they are on the Romi itself:

Creating SparkObjects

This is the constructor of a Spark:

Spark s = new Spark(int motorID)

The motor ID of a Spark is usually written/printed on the motor itself. If you don't know the ID of a Spark, ask a more experienced team member for help.

Like most components, such as controllers, Sparks have IDs to differentiate between them. 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)

set() simply sets the robot's speed. However, the method weirdly achieves this. The parameter, speed. is a percentage of the motor's maximum voltage. So in the example below, set(0.5) will tell the Spark to use 50% of its max voltage. (The Spark's max voltage is around 11V)

If you set speed to a negative value, it will simply make the robot move in the opposite direction, but with that speed.

/* sets the left motor's speed to 50%. */
m_leftMotor.set(0.5);

void setVoltage(double voltage)

setVoltage() is essentially the same as setSpeed(), but it takes slightly different parameters. setVoltage() as the name suggests, lets you directly set the voltage of the Romi. So in the example below, you're telling the Spark to let the motor use 6V.

/* sets the left motor's speed to 50%. */
m_leftMotor.setVoltage(6);

After you set a motor to a speed, the motor will keep that speed unless you explicitly stop it/the code.

Also, make sure you don't exceed the max voltage of your motors. Doing this can cause damage to the motors.


Inverting Spark Motors

One other thing you should know, besides setting motors, is inverting motors. Inverting motors, as the name suggests, inverts their speeds. This means that two motors that are physically facing opposite directions can rotate in the same direction.

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 objects.

//The right motor is now inverted
m_rightMotor.setInverted(true);
//The left motor is now not inverted
m_leftMotor.setInverted(false);

Luckily, the RomiDrivetrain already inverts the m_rightMotor for us, so we don't have to worry about it. However, if we were to create a DriveTrain from scratch we'd need to invert the motor itself.

Last updated