# Basic Motor Speed Control Application

This program demonstrates a basic motor control application that adjusts the motor's speed based on user input. It communicates with the motor controller via USB and sets the motor speed (velocity) according to the user's specified value. This allows for simple motor speed control, where the user can input a desired speed, and the motor will rotate accordingly.

## Key Components:

1. **USB Port Detection**:
   1. The program starts by detecting the available USB serial port for communication with the motor. It uses the `serial` module to find connected serial devices.
   2. The program supports multiple platforms (Windows, Linux, and macOS) and automatically identifies the correct port based on the operating system.
2. **Motor Control with SMD Red**:
   1. The motor is controlled through the `smd.red` library, which allows sending commands to the motor via the Red controller.
   2. A `Master` object is used to establish communication with the motor, and the motor is attached using the `Red(ID)` method.
3. **Motor Speed Configuration**:
   1. The program allows the user to input a speed value (in RPM or another suitable unit). The motor's velocity is set to this value using the `m.set_velocity()` method.
   2. The motor's other parameters, such as the revolutions per minute (RPM) and control settings, are preconfigured for optimal operation.
4. **User Input**:
   1. The program prompts the user to input the desired motor speed, and then it sets the motor’s velocity accordingly. The speed is used to control how fast the motor will rotate.
5. **Feedback to User**:
   1. After setting the motor speed, the program confirms the set speed with a printed message: `"The engine rotates at speed {speed}."`

## Example Usage:

When you run the program, it will prompt you to enter the speed:

```
Speed: 150
```

After entering the speed, for example, 150, the motor will rotate at the set speed. The program will then print a message confirming the motor's speed:

```
The engine rotates at speed 150.
```

## Code

```python
from smd.red import *
from serial.tools.list_ports import comports
from platform import system
import math
import os

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)

ID = 0

m.attach(Red(ID))

m.set_shaft_cpr(ID, 6533)
m.set_shaft_rpm(ID, 100)
m.set_operation_mode(ID, OperationMode.Velocity)
m.set_control_parameters_velocity(ID, 30.0, 5.0, 0.0)
m.enable_torque(ID, True)

speed = input("Speed: ")

m.set_velocity(ID,float(speed))

print("The engine rotates at speed "+speed+".")
```


---

# 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-speed-control-application.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.
