Acrome-SMD Docs
All Acrome ProductsReferencesBlogCase StudiesContact Us
  • ACROME SMD
  • Electronics
    • 🔴SMD Red
      • Coding Guide
      • Robot with Raspberry Pi Setup Guide
      • Robot with Arduino Setup Guide
      • Troubleshooting Guide
    • 🔵SMD Blue
    • 🟢SMD Green
    • Gateway Modules
      • Arduino Gateway Module
      • USB Gateway Module
    • Electrical Motors
      • Brushed DC Motors (BDC)
      • Stepper DC Motors (SDC)
      • Brushless DC Motor (BLDC)
      • Linear Actuator with Feedback
    • Add-on Modules
      • Ambient Light Sensor Module
      • Button Module
      • Buzzer Module
      • IMU Module
      • Joystick Module
      • Potentiometer Module
      • Reflectance Sensor Module
      • RGB LED Module
      • Servo Module
      • Ultrasonic Distance Sensor Module
  • SMD Kits
    • Starter Kit
      • What You Can Build
    • Education Kit
      • What You Can Build
    • Motion Kit
      • What You Can Build
  • Software
    • Libraries
      • Python Library
      • Arduino Library
      • Java Library
      • Matlab Library
    • SMD UI
    • SMD Blockly
      • Introducing Customized Blockly Blocks
  • SMD Applications
    • Basics
      • Blink
      • Action - Reaction
      • Autonomous Lighting
      • Smart Doorbell
      • Security System
      • Distance Buzzer Warning
      • Distance Auto Stop
      • Smart Light Control
    • Interactive
      • Automatic Trash Bin
      • Radar
      • Chrome Dino Game Player
      • Play Chrome Dino Game With Joystick
      • Snake Game With Joystick
      • Pan-Tilt with Joystick Module
      • Joystick Mouse Control
      • Rev Up the Engine
      • Motor Rotation Based on Turn Input Value
      • Basic Motor Speed Control Application
      • Basic Motor Control Application Using PWM Input
      • Basic Motor Position Control Application
      • Basic Motor Torque Control Application
      • Motor Rotation Based on Joystick Counting
    • Robotics
      • Differential Robot Projects
      • Mouse Cursor Tracker Motion Robot
      • Waypoint tracker robot
      • Braitenberg Robot
      • Line-Follower Robot
      • Teleoperation Robot
      • Obstacle Avoidance Robot
      • ESP32 Wireless Controlled Mobile Robot
  • AI
    • Object Tracking Robot
    • Groq Chatbot-Controlled Robot
  • ROS
    • Teleoperation Robot with ROS
  • Mechanics
    • Building Set
      • Plates
        • 2x2 Plate Package
        • 2x3 120° Plate Package
        • 3x3 Plate Package
        • 3x5 Plate Package
        • 3x9 Plate Package
        • 11x19 Plate
        • 9x19 Plate
        • 5x19 Plate
        • 3x19 Plate
        • 9x11 Plate
        • 5x13 Plate
      • Joints
        • 60° Joint Package
        • 90° Joint Package
        • 120° Joint Package
        • Slot Joint M2 Package
        • Slot Joint M3 Package
        • U Joint Package
      • Mounts
        • Add-on Mount Package
        • Motor L Mount Package
        • Pan-Tilt Package
      • Wheels
        • Ball Wheel Package
        • Caster Wheel Package
        • Wheel Package
      • Cables
        • Power Cable 10 cm Package
        • Power Cable 20 cm Package
        • Power Cable 35 cm Package
        • RJ-11 Cable 7.5 cm Package
        • RJ-11 Cable 20 cm Package
        • RJ-11 Cable 35 cm Package
        • RJ-45 Cable 7.5 cm Package
        • RJ-45 Cable 20 cm Package
        • RJ-45 Cable 35 cm Package
      • Fasteners
        • M2x5 Allen Hex Screw Package
        • M3x6 Allen Hex Screw Package
        • M3x8 Allen Hex Screw Package
        • M3 Hex Nut Package
  • Help
    • Manual
    • Shops
    • Reach Us
Powered by GitBook
On this page
  • Step 1: Hardware & Software Overview
  • Step 2: Assemble
  • Step 3: Run & Test
  • Codes
  1. SMD Applications
  2. Interactive

Basic Motor Position Control Application

In this application, you will learn how to control a DC motor’s exact position in degrees using the Position Mode of the SMD Red module. You will enter a target angle between 0 and 360, and the motor will rotate precisely to that point using encoder feedback.

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 Red: Controls the brushed DC motor and reads encoder feedback for accurate position control.

  2. BDC Motor: Converts electrical energy to mechanical rotation and provides position feedback via encoder signals.

Project Key Features

  1. Precise angle-based control: Move the motor shaft to an exact angle between 0° and 360°.

  2. Real-time encoder feedback: Monitor actual shaft position with high-resolution encoder data.

  3. Tunable PID position control: Customize responsiveness and stability using: set_control_parameters_position

  4. Encoder-integrated control loop: Closed-loop control ensures accurate positioning over time.

Step 2: Assemble

Getting Started

Hardware Setup

  • Connect the SMD to the PC or Arduino board using USB Gateway Module or Arduino Gateway Module.

  • Connect the 100 RPM BDC Motor with Encoder to the motor ports of the SMD Red.

  • Make sure that the SMD is powered and all connections are correct.

Project Wiring Diagram

Step 3: Run & Test

Run the Script

After launching the script, you will be prompted to enter a desired angle between 0 and 360 degrees. The motor will rotate to the target position using encoder feedback and stop.

Codes

from smd.red import *  # Import SMD Red control library
from serial.tools.list_ports import comports  # For listing available serial ports
from platform import system  # To detect the operating system
import time  # For sleep/delay
import math  # For mathematical operations

# Automatically detect and return the correct USB port connected to SMD Red
def USB_Port():
    ports = list(comports())
    usb_names = {
        "Windows": ["USB Serial Port"],
        "Linux": ["/dev/ttyUSB"],
        "Darwin": ["/dev/cu."]  # macOS ports usually start with /dev/cu.
    }
    os_name = system()
    for port, desc, _ in ports:
        if any(name in port or name in desc for name in usb_names.get(os_name, [])):
            return port
    return None

def main():
    port = USB_Port()
    if not port:
        print("No port found.")  # If no port is found, exit the program
        return

    master = Master(port)  # Create a Master object to control SMD Red
    motor_id = 1  # ID of the connected motor
    CPR = 6533  # Encoder Counts Per Revolution

    # Attach the motor and configure its basic parameters
    master.attach(Red(motor_id))
    master.set_shaft_cpr(motor_id, CPR)  # Set encoder resolution
    master.set_shaft_rpm(motor_id, 100)  # Set nominal RPM
    master.set_control_parameters_position(motor_id, 0.5, 0.0, 20.0)  # Set PID gains for position control
    master.set_operation_mode(motor_id, OperationMode.Position)  # Enable position control mode
    master.enable_torque(motor_id, True)  # Enable torque so the motor can move

    while True:
        try:
            # Get target angle from user input and keep it in [0, 360) range
            angle = float(input("Enter target angle (0-360°): ")) % 360

            # Convert angle in degrees to encoder counts
            target = angle * (CPR / 360)

            # Send position command to the motor
            master.set_position(motor_id, target)

            # Wait for movement to complete
            time.sleep(0.5)

            # Read actual encoder position
            current = master.get_position(motor_id)

            # Convert encoder counts back to degrees
            actual = current * (360 / CPR)

            # Print both target and actual angles
            print(f"Target: {angle:.2f}°, Actual: {actual:.2f}°\n")

        except KeyboardInterrupt:
            # On user interrupt (Ctrl+C), disable torque and stop the motor
            master.enable_torque(motor_id, False)
            break

# Entry point of the script
if __name__ == "__main__":
    main()
#include <Acrome-SMD.h>
#define BAUDRATE 115200         // Serial communication speed
#define CPR 6533                // Counts per revolution of the encoder
#define ID 1                    // ID of the SMD Red module

Red master(ID, Serial, BAUDRATE);  // Create SMD Red object

void setup() {
  Serial.begin(115200);             // Start serial monitor
  master.begin();                   // Initialize communication with SMD Red
  master.torqueEnable(1);          // Enable motor torque
  master.setOperationMode(PositionControl);  // Set operation mode to Position Control
}

void loop() {
  // Read joystick X and Y values from module 1
  int joystickX = master.getJoystickX(1);
  int joystickY = master.getJoystickY(1);

  // If joystick is moved beyond dead zone
  if (abs(joystickX) > 10 || abs(joystickY) > 10) {
    // Calculate angle based on joystick direction
    float angle = atan2(joystickY / 100.0, joystickX / 100.0);
    float angleDegrees = fmod(degrees(angle) + 360.0, 360.0);  // Normalize angle to 0–360°
    
    // Convert angle in degrees to encoder position (CPR)
    int position = angleDegrees * (CPR / 360.0);

    master.setpoint(1, position);  // Send position command to motor

    // Debug output
    Serial.print("Target Angle: "); Serial.println(angleDegrees);
    Serial.print("Target CPR: "); Serial.println(position);
  }

  delay(100);  // Small delay for stability
}
PreviousBasic Motor Control Application Using PWM InputNextBasic Motor Torque Control Application

Last updated 1 month ago