Main Content

pidMotor

Connection to motors in closed-loop PID control on Arduino MKR Motor Carrier or Nano Motor Carrier

Since R2020a

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

Description

This object represents a connection to motors running in closed-loop PID control with the specified motor number on the MKR Motor Carrier or Nano Motor Carrier. You can control the DC motors M1 and M2 in closed-loop PID control using the functions in Object Functions.

Creation

Description

pidObj = pidMotor(mcObj,PIDNumber) creates a connection to the PID motor at the specified port number on the Arduino® MKR Motor Carrier or the Nano Motor Carrier.

example

pidObj = pidMotor(mcObj,PIDNumber,controlmode) creates a connection to the PID motor at the specified port number and the specified control mode on the Arduino MKR Motor Carrier or the Nano Motor Carrier.

example

pidObj = pidMotor(mcObj,PIDNumber,controlmode,ppr) creates a connection to the PID motor at the specified port number with the specified pulses per revolution on the Arduino MKR Motor Carrier or the Nano Motor Carrier.

example

pidObj = pidMotor(mcObj,PIDNumber,controlmode,ppr,[PIDGains]) creates a connection to the PID motor at the specified port number and specified PID gains on the Arduino MKR Motor Carrier or the Nano Motor Carrier.

example

Input Arguments

expand all

Connection to the MKR Motor Carrier or Nano Motor Carrier, specified as an object.

Example: pidObj = pidMotor(mcObj,1) creates a connection to the motor number 1.

Motor number where the PID motor is connected on an MKR Motor Carrier or Nano Motor Carrier, specified as a number. The PIDNumber 1 implies a connection to the motor number 1 (M1) and the encoder 1 whereas the PIDNumber 2 implies a connection to the motor number 2 (M2) and the encoder 2.

Example: pidObj = pidMotor(mcObj,2) creates a connection to the motor number 2.

The mode of control of the PID motor, specified as a character vector. To drive the DC motor at the specified speed, create the connection to the PID motor in speed control mode. To rotate the motor by the specified angle, create the connection to the PID motor in position control mode.

Example: pidObj = pidMotor(mcObj,1,'Position'); creates a connection to the motor number 1 in the position control mode.

Number of pulses per revolution of the rotary encoder, specified as a number.

Example: pidObj = pidMotor(mcObj,1,'Position',3); sets the pulses pulse per revolution to 3.

Proportional (Kp), integral (Ki), and derivative (Kd) gains of the PID motor specified as a 1-by-3 vector. The following are the default values of [Kp,Ki,Kd] in the speed and position control modes:

Boardspeed Control Modeposition Control Mode
Arduino MKR Motor Carrier[2000,200, 0][25,0,3]
Arduino Nano Motor Carrier[5.2,0.3,0.0][0.18, 0.0, 0.01]

Note

Tuning gains can lead to the motor and board getting burnt in case of excess current. Also, the Arduino firmware does not allow setting MaxSpeed to ensure safety.

Example: pidObj = pidMotor(mcObj,1,'Position',3,[25,0,3]); sets the PID gains of the PID motor on the MKR Motor Carrier to 25, 0, and 3.

Data Types: double

Properties

expand all

This property is read-only.

PID motor number corresponding to DC motor number and encoder channel number on an MKR Motor Carrier or Nano Motor Carrier, specified as a number.

Example:

>> pidObj.PIDNumber
ans = 
      1 

Data Types: double

This property is read-only.

The mode of control of the PID motor, specified as a string.

Example:

>> pidObj.ControlMode
ans = 
      'speed' 

Data Types: char

This property is read-only.

Number of pulses per revolution of encoder, specified as a number.

Example:

>> pidObj.PulsesPerRevolution
ans = 
      3 

Data Types: double

Proportional gain of the PID control.

Example:

>> pidObj.Kp
ans = 
       2000

Data Types: double

Integral gain of the PID control.

Example:

>> pidObj.Ki
ans = 
       200

Data Types: double

Derivative gain of the PID control.

Example:

>> pidObj.Kd
ans = 
       0

Data Types: double

This property is read-only.

Maximum acceleration in radians per seconds squared.

Example:

>> pidObj.MaxAcceleration
ans = 
       2094400000 (rad/s2)

Data Types: double

This property is read-only.

Maximum acceleration in revolutions per minute.

Example:

>> pidObj.MaxSpeed
ans = 
       25000 RPM

Data Types: double

Object Functions

Use these object functions to create a connection to the PID motor:

readAngularPositionRead angular position of DC Motor in closed-loop control
readSpeedRead current rotational speed of DC Motor in closed loop control
writeAngularPositionRotate DC motor by specified angle in closed loop position control
writeSpeedWrite speed of PID motor

Examples

collapse all

Create an arduino object with the 'MotorCarrier' library.

arduinoObj = arduino('COM7','Nano33IoT','Libraries','MotorCarrier')
arduinoObj = 
  arduino with properties:

                  Port: 'COM7'
                 Board: 'Nano33IoT'
         AvailablePins: {'D0-D13', 'A0-A7'}
  AvailableDigitalPins: {'D0-D13', 'A0-A7'}
      AvailablePWMPins: {'D2-D6', 'D9-D12', 'A2-A3', 'A5'}
   AvailableAnalogPins: {'A0-A3', 'A6-A7'}
    AvailableI2CBusIDs: [0]
AvailableSerialPortIDs: [1]
             Libraries: {'MotorCarrier'}
Show all properties

Create a connection to the Nano Motor Carrier.

mcObj = motorCarrier(arduinoObj)
mcObj = 
  MotorCarrier with properties:

          SCLPin: 'A5'
          SDAPin: 'A4'
      I2CAddress: 102 ('0x66')

Create a connection to the PID motor.

pidObj = pidMotor(mcObj,1)
pidObj = 
  PIDMotor with properties:

             PIDNumber: 1
           ControlMode:'speed'
   PulsesPerRevolution: 3
                    Kp: 5.200000
                    Ki: 0.300000
                    Kd: 0.000000
       MaxAcceleration: 2094400000 (rad/s^2)
              MaxSpeed: 25000 (RPM)

Create an arduino object with the 'MotorCarrier' library.

arduinoObj = arduino('COM7','Nano33IoT','Libraries','MotorCarrier')
arduinoObj = 
  arduino with properties:

                  Port: 'COM7'
                 Board: 'Nano33IoT'
         AvailablePins: {'D0-D13', 'A0-A7'}
  AvailableDigitalPins: {'D0-D13', 'A0-A7'}
      AvailablePWMPins: {'D2-D6', 'D9-D12', 'A2-A3', 'A5'}
   AvailableAnalogPins: {'A0-A3', 'A6-A7'}
    AvailableI2CBusIDs: [0]
AvailableSerialPortIDs: [1]
             Libraries: {'MotorCarrier'}
Show all properties

Create a connection to the Nano Motor Carrier.

mcObj = motorCarrier(arduinoObj)
mcObj = 
  MotorCarrier with properties:

          SCLPin: 'A5'
          SDAPin: 'A4'
      I2CAddress: 102 ('0x66')

Create a connection to the PID motor in 'Position' control mode.

pidObj = pidMotor(mcObj,1,'position')
pidObj = 
  PIDMotor with properties:

             PIDNumber: 1
           ControlMode:'position'
   PulsesPerRevolution: 3
                    Kp: 0.180000
                    Ki: 0.000000
                    Kd: 0.010000
       MaxAcceleration: 2094400000 (rad/s^2)
              MaxSpeed: 25000 (RPM)

Create an arduino object with the 'MotorCarrier' library.

arduinoObj = arduino('/dev/ttyACM0','Nano33IoT','Libraries','MotorCarrier')
arduinoObj = 
  arduino with properties:

                  Port: '/dev/ttyACM0'
                 Board: 'Nano33IoT'
         AvailablePins: {'D0-D13', 'A0-A7'}
  AvailableDigitalPins: {'D0-D13', 'A0-A7'}
      AvailablePWMPins: {'D2-D6', 'D9-D10'}
   AvailableAnalogPins: {'A0-A3', 'A6-A7'}
    AvailableI2CBusIDs: [0]
AvailableSerialPortIDs: [1]
             Libraries: {'MotorCarrier'}
Show all properties

Create a connection to the Nano Motor Carrier.

mcObj = motorCarrier(arduinoObj)
mcObj = 
  MotorCarrier with properties:

          SCLPin: 'A5'
          SDAPin: 'A4'
      I2CAddress: 102 ('0x66')

Create a connection to the PID motor in 'Position' control mode.

pidObj = pidMotor(mcObj,1,'position',3)
pidObj = 
  PIDMotor with properties:

             PIDNumber: 1
           ControlMode:'position'
   PulsesPerRevolution: 3
                    Kp: 0.180000
                    Ki: 0.000000
                    Kd: 0.010000
       MaxAcceleration: 2094400000 (rad/s^2)
              MaxSpeed: 25000 (RPM)

Create an arduino object with the 'MotorCarrier' library.

arduinoObj = arduino('COM7','Nano33IoT','Libraries','MotorCarrier')
arduinoObj = 
  arduino with properties:

                  Port: 'COM7'
                 Board: 'Nano33IoT'
         AvailablePins: {'D0-D13', 'A0-A7'}
  AvailableDigitalPins: {'D0-D13', 'A0-A7'}
      AvailablePWMPins: {'D2-D6', 'D9-D12', 'A2-A3', 'A5'}
   AvailableAnalogPins: {'A0-A3', 'A6-A7'}
    AvailableI2CBusIDs: [0]
AvailableSerialPortIDs: [1]
             Libraries: {'MotorCarrier'}
Show all properties

Create a connection to the Nano Motor Carrier.

mcObj = motorCarrier(arduinoObj)
mcObj = 
  MotorCarrier with properties:

          SCLPin: 'A5'
          SDAPin: 'A4'
      I2CAddress: 102 ('0x66')

Create a connection to the PID motor in 'Position' control mode.

pidObj = pidMotor(mcObj,1,'position',3,[0.18, 0.0, 0.01])
pidObj = 
  PIDMotor with properties:

             PIDNumber: 1
           ControlMode:'position'
   PulsesPerRevolution: 3
                    Kp: 0.180000
                    Ki: 0.000000
                    Kd: 0.010000
       MaxAcceleration: 2094400000 (rad/s^2)
              MaxSpeed: 25000 (RPM)

Version History

Introduced in R2020a