Technology Careers Contact
Documentation Community Download Support

Controlled-motor module type

This module type allows to control a motor with a reduction and a sensor (usually called motor-reducer or speed-reducer). This module computes PID for speed, position and motion planning.

You can find basic information about PID control here: An introduction to PID control with DC motor, and a code example to tune your PID at the end of this page.

Its type has access to all common capabilities.

Modules’s type settings:

Warning: This module doesn't save any of the following parameters, they must be set each time your module reboots.

Before using your controlled motor module, you have to setup the resolution, motor reduction and eventually the wheel size, if you plan to use translation modes. To check the configuration, just make a complete turn on the motor shaft with your hand and check if the rotation position value is OK.

Both PID’s values have to be set accordingly to the motor-reducer plugged to the board. Each different motor-reducer will have different PID’s values for position and speed control, and you have to define them by yourself. The default values [0, 0, 0] won’t have any effect on the motor, and must be changed if you plan to use any position or speed control mode. To setup your PID please refer to the example at the end of this page.

Warning: PID for position and speed must be set in your code as an initialization before starting to use your module with position or speed control.

Now that everything is configured, you can enable the control modes you want to use. You can use position and speed mode simultaneously. Power mode is only usable alone. The controlled motor is now ready to use, you can disable compliance to start moving the motor.


Functions

Function name and parametersActionComment
setToZero(self)Resets current position of the motor to 0You can use it to initialize the position of the motor
control(self)Displays module type graphical interfaceOnly available using Jupyter notebook

Variables

Motor settings

Variable nameActionType
positionPidSets position PID used for rotation position mode and translation position moderead / write: [float P, float I, float D]
speedPidSets speed PID used for rotation speed mode and translation speed moderead / write: [float P, float I, float D]
encoder_resDefines the motor sensor resolution, in steps by rotation.
This module considers that the sensor is placed before the reduction. If it is not your case, just setup a reduction ratio of 1.
read / write: float
reductionDefines the motor reduction.
Set this value to 1 if your sensor measures after the reduction.
read / write: float
wheel_sizeDefines wheel size used for translation mode, in mm.read / write: float

Motor control modes

Variable nameActionType
compliant- True: disables the motor driver, you can use it to move the motor by hand.
- False: Enables the motor driver.
read / write: Boolean (True or False)
power_modeEnables/Disables the power control mode.
Disables any other control mode if enabled.
read / write: Boolean (True or False)
rot_position_modeEnables/Disables the motor rotation position control mode.
Disables power mode and translation position mode if enabled.
Doesn't work if no position PID is configured.
read / write: Boolean (True or False)
rot_speed_modeEnables/Disables the motor rotation speed control mode.
Disables power mode and translation speed mode if enabled.
Doesn't work if no speed PID configured.
read / write: Boolean (True or False)
trans_position_modeEnables/Disables the motor translation position control mode.
Disables power mode and rotation position mode if enabled.
Doesn't work if no position PID configured.
read / write: Boolean (True or False)
trans_speed_modeEnables/Disables the motor translation speed control mode.
Disables power mode and rotation speed mode if enabled.
Doesn't work if no speed PID configured.
read / write: Boolean (True or False)

Motor sensing

Variable nameActionType
rot_positionReads rotation position in °
Reading it auto-Enables actualization.
read only: float
rot_positionStarts/Stops rotation position measurement actualizationwrite only: Boolean (True or False)
rot_speedReads rotation speed in °/s
Reading it auto-Enables actualization.
read only: float
rot_speedStarts/Stops rotation speed measurement actualizationwrite only: Boolean (True or False)
trans_positionReads translation position in mm
Reading it auto-Enables actualization.
read only: float
trans_positionStarts/Stops translation position measurement actualizationwrite only: Boolean (True or False)
trans_speedReads translation speed in mm/s
Reading it auto-enables actualization.
read only: float
trans_speedStarts/Stops translation speed measurement actualizationwrite only: Boolean (True or False)
currentReads the current consumption in A
Reading it auto-enables actualization.
read only: float
currentStarts/Stops current measurement actualizationwrite only: Boolean (True or False)

Motor commands

Variable nameActionType
power_ratioSets the power quantity send to the motor between -100% and 100%.read / write: float
target_rot_positionSets the target rotation position to reach in °.read / write: float
target_rot_speedSets the target rotation speed to reach in °/s.read / write: float
target_trans_positionSets the target translation position to reach in mm.read / write: float
target_trans_speedSets the target translation speed to reach in mm/s.read / write: float

ROS topics

Topic nameMessage type
/mod/variables/positionPid/readgeometry_msgs/msg/Vector3
/mod/variables/positionPid/writegeometry_msgs/msg/Vector3
/mod/variables/speedPid/readgeometry_msgs/msg/Vector3
/mod/variables/speedPid/writegeometry_msgs/msg/Vector3
/mod/variables/encoder_res/readstd_msgs/msg/Float32
/mod/variables/encoder_res/writestd_msgs/msg/Float32
/mod/variables/reduction/readstd_msgs/msg/Float32
/mod/variables/reduction/writestd_msgs/msg/Bool
/mod/variables/wheel_size/readstd_msgs/msg/Float32
/mod/variables/wheel_size/writestd_msgs/msg/Float32
/mod/variables/compliant/readstd_msgs/msg/Bool
/mod/variables/compliant/writestd_msgs/msg/Bool
/mod/variables/power_mode/readstd_msgs/msg/Bool
/mod/variables/power_mode/writestd_msgs/msg/Bool
/mod/variables/rot_position_mode/readstd_msgs/msg/Bool
/mod/variables/rot_position_mode/writestd_msgs/msg/Bool
/mod/variables/rot_speed_mode/readstd_msgs/msg/Bool
/mod/variables/rot_speed_mode/writestd_msgs/msg/Bool
/mod/variables/trans_position_mode/readstd_msgs/msg/Bool
/mod/variables/trans_position_mode/writestd_msgs/msg/Bool
/mod/variables/trans_speed_mode/readstd_msgs/msg/Bool
/mod/variables/trans_speed_mode/writestd_msgs/msg/Bool
/mod/variables/rot_position/readstd_msgs/msg/Float32
/mod/variables/rot_position/writestd_msgs/msg/Bool
/mod/variables/rot_speed/readstd_msgs/msg/Float32
/mod/variables/rot_speed/writestd_msgs/msg/Bool
/mod/variables/trans_position/readstd_msgs/msg/Float32
/mod/variables/trans_position/writestd_msgs/msg/Bool
/mod/variables/trans_speed/readstd_msgs/msg/Float32
/mod/variables/trans_speed/writestd_msgs/msg/Bool
/mod/variables/current/readstd_msgs/msg/Float32
/mod/variables/current/writestd_msgs/msg/Bool
/mod/variables/power_ratio/readstd_msgs/msg/Float32
/mod/variables/power_ratio/writestd_msgs/msg/Float32
/mod/variables/target_rot_position/readstd_msgs/msg/Float32
/mod/variables/target_rot_position/writestd_msgs/msg/Float32
/mod/variables/target_rot_speed/readstd_msgs/msg/Float32
/mod/variables/target_rot_speed/writestd_msgs/msg/Float32
/mod/variables/target_trans_position/readstd_msgs/msg/Float32
/mod/variables/target_trans_position/writestd_msgs/msg/Float32
/mod/variables/target_trans_speed/readstd_msgs/msg/Float32
/mod/variables/target_trans_speed/writestd_msgs/msg/Float32

Example code

PID Setting example code

The PID values allow your motor to stick to the target command as fast as possible. The quality of a set of PID values depends on time to reach the target position and position precision. Tuning a PID is something difficult and takes a lot of practice. It's really important to have simple ways to evaluate PID values impact on your motor before starting to tune these values. Here is the code we use at Luos to tune a PID by ourself. Use it with Jupyter notebook to get your plot instantly.

The main code:

%matplotlib inline
from pyluos import Device
from IPython.display import clear_output
import time
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt

# 1. Connect your Luos network (here using an USB module for example)
r = Device('/dev/cu.usbserial-DN2AAOVK')
r.modules

# 2. Select the module of your network you need to configure
module = r.controlled_moto

# 3. Setup module basic settings
module.encoder_res = 48
module.reduction = 26.851

def run_speed_test(velocity_target):
    module.rot_position = False
    module.rot_speed = True
    module.rot_position_mode = False
    module.rot_speed_mode = True
    module.target_rot_speed = 0.0
    module.compliant = False
    target = []
    real = []
    test_time_vector = []
    test_start_time = time.time()
    target.append(module.target_rot_speed)
    real.append(module.rot_speed)
    test_time = time.time()
    test_time_vector.append(0.0)
    while (test_time < test_start_time + 0.5):
        target.append(module.target_rot_speed)
        real.append(module.rot_speed)
        test_time_vector.append(test_time - test_start_time)
        test_time = time.time()
    module.target_rot_speed = velocity_target
    while (test_time < test_start_time + 2.5):
        target.append(module.target_rot_speed)
        real.append(module.rot_speed)
        test_time_vector.append(test_time - test_start_time)
        test_time = time.time()
    module.compliant = True
    plot_test(test_time_vector, target, real)

def run_pos_test(pos_target):
    module.rot_speed = False
    module.rot_position = True
    module.rot_speed_mode = False
    module.rot_position_mode = True
    module.target_rot_position = 0.0
    module.compliant = False
    target = []
    real = []
    test_time_vector = []
    test_start_time = time.time()
    target.append(module.target_rot_position)
    real.append(module.rot_position)
    test_time = time.time()
    test_time_vector.append(0.0)
    while (test_time < test_start_time + 1):
        target.append(module.target_rot_position)
        real.append(module.rot_position)
        test_time_vector.append(test_time - test_start_time)
        test_time = time.time()
    module.target_rot_position = pos_target
    while (test_time < test_start_time + 2.5):
        target.append(module.target_rot_position)
        real.append(module.rot_position)
        test_time_vector.append(test_time - test_start_time)
        test_time = time.time()
    module.compliant = True
    plot_test(test_time_vector, target, real)

def plot_test(test_time_vector, target, real):
    fig = plt.figure()
    ax = plt.subplot(111)
    ax.plot(test_time_vector,target,'r')
    ax.plot(test_time_vector,real,'b')
    plt.show()
    plt.close(fig)

Now, you are ready to tune the PID values for position and speed control modes. To do that, you have to try values to get best the result possible. In order to succeed, we advise you to do it step by step:

  1. Set P, I, and D values to 0.
  2. Increase the P value until you have a small oscillation around the target.
  3. Increase the D value until you have a fast and stable position.
  4. Increase with really small figures the I value to improve the motor precision.

The code you can use to tune your speed PID:

# Speed PID settings
module.speedPid = [0.1,0.1,0] # speed PID [P, I, D]
run_speed_test(100.0)

The code you can use to tune your position PID:

# Position PID settings
module.positionPid = [4.0,0.02,100] # position PID [P, I, D]
run_pos_test(100.0)

Example command from ROS topics

By publishing on 3 topics you will take control over the controlled motor named controlled_moto to a velocity command of 90 deg/s:

# Launch the broker. Note: warnings will be displayed, please ignore them
ros2 launch luos_interface broker.launch.py

# Start the command in velocity mode
ros2 topic pub -1 /controlled_moto/variables/rot_speed_mode/write std_msgs/msg/Bool data:\ true\ 
ros2 topic pub -1 /controlled_moto/variables/target_rot_speed/write std_msgs/msg/Float32 data:\ 90.0\ 
ros2 topic pub -1 /controlled_moto/variables/compliant/write std_msgs/msg/Bool data:\ false\ 

Then publish true to the /controlled_moto/variables/compliant/write topic to stop the driver.


Follow Watch Star