Acrome-SMD Docs
All Acrome ProductsReferencesBlogCase StudiesContact Us
  • ACROME SMD
  • Electronics
    • 🔴SMD Red
      • 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 – 75 lbs
    • 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
      • Basic Brushed DC motor Applications
    • Education Kit
    • Motion Kit
      • Differential Robot Projects
  • 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
      • Mouse Cursor Tracker Motion Robot
      • Waypoint tracker robot
      • Braitenberg Robot
      • Line-Follower Robot
      • Object Tracking Robot
      • Teleoperation Robot
      • Obstacle Avoidance Robot
      • ESP32 Wireless Controlled Mobile Robot
  • AI
    • Groq Chatbot-Controlled Robot
  • 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
  • Key Components:
  • Example Usage:
  • Code:
  1. SMD Kits
  2. Starter Kit
  3. Basic Brushed DC motor Applications

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.

Key Components:

  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.

Example Usage:

When you run the program, it will prompt you to enter a PWM value:

PWM: 50

After you input 50, the motor will start rotating at the specified PWM level. The program will then display the following message:

The motor is running with a PWM value of 50.

Code:

from smd.red import *
import math
import os

from serial.tools.list_ports import comports
from platform import system

def USB_Port():
	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()
	if ports:
		for port, desc, hwid in sorted(ports):
			if any(name in port or name in desc for name in usb_names.get(os_name, [])):
				return port
		print("Current ports:")
		for port, desc, hwid in ports:
			print(f"Port: {port}, Description: {desc}, Hardware ID: {hwid}")
	else:
		print("No port found")
	return None
	
port = USB_Port()
m = Master(port)
m.attach(Red(0))

m.set_shaft_cpr(0,6533)
m.set_shaft_rpm(0,100)

motor_speed = 0
angle_degrees=0
current_limit = 100
current_value = 0
previous_current = 0

m.set_operation_mode(0, OperationMode.Velocity)
m.set_control_parameters_velocity(0, 30.0, 5.0, 0.0)
m.set_control_parameters_position(0, 0.5, 0.0, 20.0)
m.set_control_parameters_torque(0, 3.0, 0.1, 0.0)
m.enable_torque(0, True)

m.set_operation_mode(0, OperationMode.PWM)
pwm = input("PWM:")
m.set_duty_cycle(0, -int(pwm))

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

Last updated 5 months ago