Main Content

motor

Connection to motor

Add-On Required: This feature requires the MATLAB Support Package for LEGO MINDSTORMS EV3 Hardware add-on.

Description

This object represents a connection to an EV3 Large or Medium Motor (item numbers 45502 or 45503). To control the speed and direction of the motor, assign a value to the Speed property of the motor object. To start, stop, and measure the rotation of a motor, use this object with the functions listed in Object Functions.

Creation

Description

mymotor = motor(myev3,outputport) creates a connection to a motor that is attached to an EV3 output port by the OutputPort (see Properties), and returns an object handle for that connection. You can use this connection with the functions listed in Object Functions to start, stop, and measure the rotation of a motor.

example

Input Arguments

expand all

Connection to EV3 brick, specified as a string that represents the object created using legoev3.

Example: myev3

Data Types: char

Output Arguments

expand all

Connection to a motor, returned as an object handle.

Properties

expand all

This property is read-only.

Letter of EV3 output port that the motor uses, returned as a string.

Example: 'A'

Data Types: char

The speed and direction of the motor, specified as a double.

To control the speed and direction, assign a value to this property. Input values from -100 to 100 (full reverse to full forward). You can change this value while the motor is running or stopped.

Example: 50

Data Types: double

Object Functions

startStart motor
stopStop motor
readRotationRead rotation from motor
resetRotationReset rotation count to zero

Examples

collapse all

Control a motor and measure wheel rotation.

Create a connection to the EV3 brick called myev3.

myev3 = legoev3
myev3 = 

  legoev3 with properties:

      FirmwareVersion: 'V1.03E'
           HardwareID: []
            IPAddress: []
    CommunicationType: 'USB'
         BatteryLevel: 100
     ConnectedSensors: {'touch'  'gyro'  'color'  'sonic'}

The sensor appears in the list of connected sensors.

Create a connection to a motor. Specify the letter of the EV3 output port that connects to the motor.

mymotor = motor(myev3,'A')
mymotor = 

  motor with properties:

    OutputPort: 'A'
         Speed: 0

By default, the speed is zero.

Configure the motor to reverse at half speed.

mymotor.Speed = -50
mymotor = 

  motor with properties:

    OutputPort: 'A'
         Speed: -50

Start the motor.

start(mymotor)

Change the direction and speed of the motor.

mymotor.Speed = 70
mymotor = 

  motor with properties:

    OutputPort: 'A'
         Speed: 70

Stop the motor.

stop(mymotor)

Read the rotation value, in degrees, of the motor.

rotation = readRotation(mymotor)
ans =

       -1489

Reset the rotation value of the motor to zero.

resetRotation(mymotor)

Read the rotation again.

readRotation(mymotor)
ans =

       0