This tutorial includes:
What is Motor
used for?
The Motor
class is used to represent the lego ev3 large motor and medium motor. They are represented by the same class and you do not have to specify which you are using. When measuring speed, we use rpm (rotations per minute). This ensures that there is no difference between using a large motor and a medium motor with this class. (except torque and max speed)
How to bind the motor
The bind method
To create a single instance of Motor
Simply do
Ev3Wrap::Motor myMotor = Ev3Wrap::Motor::bind();
This will cause myMotor
to be the first motor that the program can find connected to the ev3.
Connecting 2 or more motors
To create 2 or more motors, you need to specify which output ports they are connected to. These can be ports A-D Simply do
Ev3Wrap::Motor myMotorA = Ev3Wrap::Motor::bind(Ev3Wrap::Motor::OUTPUT_A)
Ev3Wrap::Motor myMotorB = Ev3Wrap::Motor::bind(Ev3Wrap::Motor::OUTPUT_B)
Specifiying which port the sensors / motors should bind to will always speed up your program. However, specifying the wrong port (ie using Ev3Wrap::IrSeeker::INPUT_1
instead of Ev3Wrap::Motor::OUTPUT_A
here will cause a crash or undefined behaviour.)
How to run the motor at a constant speed
We always measure Motor speed at rpm, rotations per minute.
Warning - changing the wheel size can affect the speed that a robot travels, even if the rpm is unchanged
Warning - a large motor may not be able to reach the same rpm as a medium motor
To make a motor spin at 50 rpm:
myMotor.runforever(50);
How to run the motor for a specific amount of time
In this library, we always use milliseconds as the measurement of time. 1 second = 1000 milliseconds To make a motor spin at 50 rpm for 10 seconds
myMotor.runTimed(10000, 50);
Warning - the timed functions in this library default to being non-blocking, meaning they will continue executing the next line of code even if you tell the motor to run for a duration. to wait for the motor to finish, do
myMotor.setBlocking(true)
How to stop the motor
You have 2 choices:
Motor::holdPosition()
holdPosition()
tells the motor to stop and resist any outside forces trying to move it. This is often preferred.Motor::releaseMotor()
releaseMotor()
tells the motor to cut electricity and come to a free stop. It still stops quick, but does a little roll before stopping
To find more information about the Motor
class, click here