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:
USB Port Detection:
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.The program is compatible with Windows, Linux, and macOS, using different naming conventions for the USB port depending on the OS.
Motor Initialization and Configuration:
The
Master
andRed
objects from thesmd.red
library are used to set up the motor and establish communication.The motor's essential parameters are initialized, such as counts per revolution (
set_shaft_cpr
) and shaft RPM (set_shaft_rpm
).
PWM Input for Motor Control:
After configuring the motor, the program prompts the user to input a PWM value, which will control the motor’s rotation speed and direction.
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))
.By inputting a PWM value, the user directly controls the motor’s duty cycle, affecting both the speed and direction.
User Feedback:
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:
After you input 50, the motor will start rotating at the specified PWM level. The program will then display the following message:
Code:
Last updated