Distance Auto Stop

This project showcases a dynamic motor control system utilizing the ACROME SMD platform. The project integrates hardware, real-time processing, and dynamic scaling to achieve precision control. The goal is to create a motorized system where the speed decreases as the motor approaches an object and stops completely when it gets very close. This is achieved using distance measurements and velocity interpolation.

About Tools and Materials:

SMD Red (Purchase Here)

SMD USB Gateway (Purchase Here)

Arduino Gateway Module (Purchase Here)

100 RPM BDC Motor with Encoder (Purchase Here)

Ultrasonic Distance Sensor Module (Purchase Here)

Step 1: Hardware & Software Overview

Project Key Components

  1. ACROME SMD Red Platform The ACROME SMD platform serves as the control hub, interfacing with the motor and reading sensor data.

  2. Ultrasonic Distance Sensor A distance sensor is used to measure the object's position in real-time.

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

Key Features

  1. Distance-Based Motor Control The motor dynamically adjusts its speed based on proximity.

  2. Smooth Interpolation The velocity decreases gradually instead of abrupt stops, ensuring smoother operation.

  3. Real-Time Feedback The system logs current distance and velocity values for monitoring.

Step 2: Assemble

Getting Started

  1. Hardware Setup

Project Wiring Diagram

Step 3: Run & Test

  1. Run the Script

    • Run the script on your computer. This will establish communication with the SMD and initiate control of the Ultrasonic Distance Sensor.

from smd.red import *
from serial.tools.list_ports import comports
from platform import system


# Serial Communication Settings
baudrate = 115200           # Baud rate for communication
module_id = 0               # ID of the SMD module
distance_sensor_id = 1      # ID of the distance sensor module
motor_id = 0                # ID of the motor module


# Distance Thresholds and Motor Speed Settings
middle_distance = 20        # Medium distance threshold (cm)
near_distance = 5           # Close distance threshold (cm)
max_speed = 100             # Maximum motor speed


def detect_usb_port():
    """
    Scans and identifies a compatible USB port for the current operating system.

    Returns:
        str: The detected USB port or None if no suitable port is found.
    """
    ports = list(comports())

    usb_names = {
        "Windows": ["USB Serial Port"],
        "Linux": ["/dev/ttyUSB"],
        "Darwin": [
            "/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()
    print(f"Operating System: {os_name}")

    if ports:
        for port in ports:
            if any(name in port.device or name in port.description for name in usb_names.get(os_name, [])):
                print(f"USB device detected on port: {port.device}")
                return port.device
        print("No suitable USB device found. Available ports:")
        for port in ports:
            print(f"Port: {port.device}, Description: {port.description}, HWID: {port.hwid}")
    else:
        print("No ports detected!")
    return None


# Initialize the USB port and SMD module
SerialPort = detect_usb_port()
if SerialPort is None:
    print("No suitable USB port found.")
    exit(1)

master = Master(SerialPort, baudrate)
master.attach(Red(module_id))


# Motor Configuration
master.set_shaft_cpr(motor_id, 6533)  # Set encoder counts per revolution
master.set_shaft_rpm(motor_id, 100)   # Set maximum RPM
master.enable_torque(motor_id, 1)     # Enable motor torque


# Function for Speed Interpolation
def interpolate_speed(distance):
    """
    Calculates the motor speed based on distance using linear interpolation.

    Args:
        distance (float): The measured distance from the sensor.

    Returns:
        float: The calculated speed.
    """
    if distance < near_distance:
        return 0  # Stop the motor
    elif near_distance <= distance < middle_distance:
        return max(10, max_speed * (distance - near_distance) / (middle_distance - near_distance))  # Smooth acceleration
    else:
        return max_speed  # Full speed


# Main Control Loop
while True:
    distance = master.get_distance(module_id, distance_sensor_id)

    if distance is not None:
        speed = interpolate_speed(distance)
        master.set_duty_cycle(motor_id, speed)

        if speed == 0:
            print("Motor Stopped")
        elif speed == max_speed:
            print("Motor Running at Max Speed")
        else:
            print(f"Motor Running at Speed: {speed}")

Conclusion

This project highlights the ACROME SMD Red platform's versatility in creating advanced motor control systems. By combining real-time distance sensing with velocity interpolation, it provides a robust solution for a range of applications.

Last updated