Motor Rotation Based on Joystick Counting
This Python code demonstrates a joystick-controlled motor rotation system that enables a user to count joystick inputs and control the motor's rotations accordingly. The application uses a USB-connected motor controller to read joystick inputs and perform precise motor movements based on the input count. Below is a step-by-step explanation of the code.
Required Libraries and Modules
import serial
from smd.red import Master, Red
from threading import Thread
import time
from serial.tools.list_ports import comports
from platform import systemserial: Handles USB serial communication with the motor controller.smd.red: Provides classes (MasterandRed) for interacting with the motor controller and motor.threading.Thread: Enables multi-threading, allowing joystick input to be handled in parallel with other operations.time: Used for timing and delays.serial.tools.list_ports: Lists available USB ports on the system.platform.system: Detects the operating system (Windows, Linux, or macOS) for platform-specific USB handling.
USB Port Detection Function
def USB_Port():
if system() == "Windows":
ports = list(comports())
if ports:
for port, desc, hwid in sorted(ports):
if 'USB Serial Port' in desc:
return port
return None
elif system() == "Linux":
ports = list(serial.tools.list_ports.comports())
if ports:
for port, desc, hwid in sorted(ports):
if '/dev/ttyUSB' in port:
return port
elif system() == "Darwin": # macOS
ports = list(serial.tools.list_ports.comports())
if ports:
for port, desc, hwid in sorted(ports):
if '/dev/tty.usbserial' in port or '/dev/tty.usbmodem' in port:
return port
return NoneThis function detects the USB port where the motor controller is connected:
Windows: Searches for ports labeled
USB Serial Port.Linux: Looks for ports starting with
/dev/ttyUSB.macOS: Searches for ports containing
/dev/tty.usbserialor/dev/tty.usbmodem.
If no suitable port is found, the function returns
None.
Motor Initialization
USB_Port(): Detects the USB port for the motor controller.Master(port): Establishes communication with the motor controller on the detected port.m.attach(Red(0)): Attaches a motor with ID0.
Motor Parameter Configuration
set_shaft_rpm(0,100): Sets the motor's maximum speed to 6533 RPM.set_shaft_cpr(0,6533): Defines the encoder resolution as 100 counts per revolution (CPR).set_control_parameters_velocity(0, 10, 1, 0): Configures PID velocity control parameters.set_operation_mode(0, 2): Sets the motor's operation mode to velocity control.
Counting Mechanism and Motor Rotation
The code implements two key functionalities:
Counting Mode: The user can increment a counter by pressing and releasing the joystick button.
Motor Rotation: The motor rotates a specified number of turns based on the counter value.
Motor Rotation Function
Calculate Target Position: The number of steps required for the specified turns is calculated.
Control Motor Movement: The motor rotates until the current position reaches the target position.
Stop the Motor: The motor's velocity is set to zero once the target position is achieved.
Joystick Control Function
Button Press Detection: Reads the joystick button state from the motor controller.
Entering Counting Mode: Holding the button for 5 seconds activates counting mode.
Incrementing the Counter: In counting mode, pressing and releasing the button increases the counter.
Triggering Motor Rotation: Holding the button for another 5 seconds rotates the motor according to the counter value.
Application Workflow
Joystick Monitoring: The
joystick_controlfunction continuously monitors joystick input in a separate thread.Counting Mode Activation: The user can activate counting mode by holding the joystick button for 5 seconds.
Increment Counter: In counting mode, each press-and-release increments the counter.
Motor Rotation: Holding the button for 5 seconds in counting mode rotates the motor by the specified number of turns.
Full Code:
Last updated