Basic Motor Control Application Using PWM Input

This program demonstrates a fundamental motor control application, where the motor’s behavior is controlled through Pulse Width Modulation (PWM) input specified by the user. The script sets up the motor, establishes communication, and allows the user to input a PWM value to control the motor's rotation speed and direction. PWM is a common method to adjust motor speed in motor control applications.

About Tools and Materials:

SMD Red (Purchase Here)

SMD USB Gateway (Purchase Here)

Arduino Gateway Module (Purchase Here)

BDC Motor (Purchase Here)

Step 1: Hardware & Software Overview

Project Key Components

  1. SMD: The SMD acts as a bridge between the script and the modules. It is responsible for interpreting the commands sent by the script and translating them into actions that read input from the Button Module and actuate the BDC motor, the RGB LED Module and the Buzzer Module.

  2. BDC Motor: The motor is controlled using velocity commands, allowing for smooth acceleration and deceleration.

Project Key Features

  1. USB Port Detection:

    1. The script first identifies and connects to the appropriate USB serial port based on the operating system. It uses the serial module to scan for connected serial devices.

    2. The program is compatible with Windows, Linux, and macOS, using different naming conventions for the USB port depending on the OS.

  2. Motor Initialization and Configuration:

    1. The Master and Red objects from the smd.red library are used to set up the motor and establish communication.

    2. The motor's essential parameters are initialized, such as counts per revolution (set_shaft_cpr) and shaft RPM (set_shaft_rpm).

  3. PWM Input for Motor Control:

    1. After configuring the motor, the program prompts the user to input a PWM value, which will control the motor’s rotation speed and direction.

    2. PWM values can vary, with positive values for one direction and negative values for the opposite. This input is passed to the motor using m.set_duty_cycle(0, -int(pwm)).

    3. By inputting a PWM value, the user directly controls the motor’s duty cycle, affecting both the speed and direction.

  4. User Feedback:

    1. After setting the PWM, the program prints a confirmation message indicating that the motor is running at the specified PWM level.

Step 2: Assemble

Getting Started

  1. Hardware Setup

Project Wiring Diagram

Step 3: Run & Test

Run the Script

• Execute the script to initiate the Basic Motor Control Application Using PWM Input.

• The script will automatically detect the USB port and establish communication with the SMD Master Controller.

• Enter a PWM value (0 to 255) when prompted to adjust the motor speed.

Experience PWM-Based Motor Control

• Observe how the motor speed increases as the PWM value increases.

• Enter zero (0) to stop the motor completely.

• Notice how the PWM signal directly influences the motor speed, providing smooth and efficient control.

Codes

from smd.red import *  # Import the SMD Red motor control library
import math
import os

from serial.tools.list_ports import comports  # Import serial communication tools
from platform import system  # Import system detection module

# Function to detect and return the correct USB port for communication
def USB_Port():
    ports = list(comports())  # Get a list of available serial ports
    usb_names = {
        "Windows": ["USB Serial Port"],  # Windows-specific port names
        "Linux": ["/dev/ttyUSB"],  # Linux-specific port names
        "Darwin": [  # macOS-specific port names
            "/dev/tty.usbserial",
            "/dev/tty.usbmodem",
            "/dev/tty.SLAB_USBtoUART",
            "/dev/tty.wchusbserial",
            "/dev/cu.usbserial",
            "/dev/cu.usbmodem",
            "/dev/cu.SLAB_USBtoUART",
            "/dev/cu.wchusbserial",
        ]
    }
    
    os_name = system()  # Detect the operating system
    if ports:  # If ports are available
        for port, desc, hwid in sorted(ports):  # Iterate through detected ports
            # Check if the port name or description matches the expected USB names
            if any(name in port or name in desc for name in usb_names.get(os_name, [])):
                return port  # Return the detected port
        
        # If no matching port was found, print the available ports
        print("Current ports:")
        for port, desc, hwid in ports:
            print(f"Port: {port}, Description: {desc}, Hardware ID: {hwid}")
    else:
        print("No port found")  # Print message if no ports are detected
    return None  # Return None if no suitable port is found

# Get the detected USB port
port = USB_Port()

# Initialize the motor controller using the detected port
m = Master(port)

# Attach the motor with ID 0
m.attach(Red(0))

# Set motor parameters
m.set_shaft_cpr(0, 6533)  # Set encoder counts per revolution (CPR)
m.set_shaft_rpm(0, 100)  # Set shaft speed in RPM

# Initialize control variables
motor_speed = 0
angle_degrees = 0
current_limit = 100  # Maximum allowable current
current_value = 0
previous_current = 0  # Store the previous current value for monitoring

# Configure motor operation modes and control parameters
m.set_operation_mode(0, OperationMode.Velocity)  # Set velocity control mode
m.set_control_parameters_velocity(0, 30.0, 5.0, 0.0)  # Set PID parameters for velocity control
m.set_control_parameters_position(0, 0.5, 0.0, 20.0)  # Set PID parameters for position control
m.set_control_parameters_torque(0, 3.0, 0.1, 0.0)  # Set PID parameters for torque control

# Enable motor torque to allow movement
m.enable_torque(0, True)

# Switch the motor to PWM control mode
m.set_operation_mode(0, OperationMode.PWM)

# Ask user to enter PWM value
pwm = input("PWM: ")

# Set motor PWM duty cycle (negative value for reverse direction)
m.set_duty_cycle(0, -int(pwm))

# Print confirmation message
print(f"The motor is running with a PWM value of {pwm}.")

Last updated