XRP Motors
Simple motors attached that move the XRP around.
XRPMotors are the type of motors the XRP uses for its wheels. There are two of them already on the robot, but you can always add more when customizing the robot.
Creating XRPMotor Objects
This is the constructor of an XRPMotor.
XRPMotor xrpMotor = new XRPMotor(int motorID);Like most components, such as controllers, Sparks have IDs to differentiate between them. The motorIDs for devices are built-in to the robot itself, and they are for the different ports on the robot. No two XRP motor objects can have the same ID, or your code will break.
How do you find the IDs of Devices?
On the XRP, there are various motor ports on its controller board. Next to each of the ports are printed text, such as "Servo1" or "Motor0". The numbers next to these words are the IDs for the device, if you decide to connect your device to these. The other devices, such as the rangefinder, accelerometer and gyroscope do not have any extra ports for them, and therefore do not have any extra details.
In the XRP template, both motors were already created, and their IDs were filled out. Here is some code for creating said motors:
// The XRP has the left and right motors set to
// PWM channels 0 and 1 respectively
private final XRPMotor m_leftMotor = new XRPMotor(0);
private final XRPMotor m_rightMotor = new XRPMotor(1);Now that you can access the motors through your code, you need to make them move. You can do this with two simple methods.
Setting the Motors' Speeds
set()
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 XRPMotor's max voltage is around 11V, so this will make the motor use 6.5V.
If you set speed to a negative value, it will simply make the robot move in the opposite direction, but with that same voltage applied to it.
setVoltage()
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 XRPMotor. So in the example below, you're telling the Spark to let the motor use 6V.
So, you might be wondering which one should you use? setVoltage() generally works nicer because you know exactly how much voltage you are sending to the motor, unlike with set().
Inverting XRP 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. Luckily, in XRPDrivetrain, the right motor is already inverted so we don't have to do this. Just keep this in mind, just in case.
Last updated