# 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:

```python
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}.")
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.acrome.net/smd-kits/starter-kit/what-you-can-build/basic-motor-control-application-using-pwm-input.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
