Motor Rotation Based on Turn Input Value
This program demonstrates a simple motor control application using Python. The main objective of the script is to rotate a motor by a specific number of turns, which is determined by the user input. The motor's position is tracked, and it continues to rotate until the desired number of turns is achieved.
About Tools and Materials:
SMD USB Gateway (Purchase Here)
Arduino Gateway Module (Purchase Here)
100 RPM BDC Motor with Encoder (Purchase Here)
Step 1: Hardware & Software Overview
Project Key Components
SMD The SMD acts as a bridge between the script and the modules. It is responsible for interpreting the commands sent by the script and translating them into actions that read input from the Button Module and actuate the BDC motor, the RGB LED Module and the Buzzer Module.
100 RPM BDC Motor with Encoder The 100 RPM BDC Motor with Encoder is used to imitate an engine running. The speed of the motor can be controlled precisely thanks to the built-in encoder.
Project Key Features
Establishes a connection with the SMD Red motor using the Master and Red classes from the smd.red library.
Allows users to specify the number of turns for the motor, using an encoder with a predefined steps-per-turn value.
Continuously monitors the motor’s position and stops it once the desired rotation is achieved.
Sets the motor velocity dynamically, starting with maximum speed and stopping precisely at the target position.
Designed to work efficiently within multi-threaded applications for real-time motor control.
Step 2: Assemble
Getting Started
Hardware Setup
Connect the SMD to the PC or Arduino board using USB Gateway Module or Arduino Gateway Module.
Connect the 100 RPM BDC Motor with Encoder to the motor ports of the SMD using an RJ-45 cable.
USB Connection and Serial Communication:
The program first detects the available USB serial port by checking the connected devices. This is done using the
serial
module and thecomports()
method. The program checks for the USB Serial Port on Windows,/dev/ttyUSB
on Linux, and/dev/tty.usbserial
or/dev/tty.usbmodem
on macOS, depending on the operating system.
Motor Control with SMD Red:
The motor control is handled by the
smd.red
library, which provides an interface to communicate with the motor using the Red controller. TheMaster
andRed
objects are used to establish communication with the motor and send commands to it.
Motor Rotation Logic:
The motor rotation is based on the encoder counts per revolution (CPR). The variable
steps_per_turn
represents the CPR, which is typically a value like6533
for many motors.The function
rotate_motor(turns)
takes the number of turns as input, calculates the corresponding target position (in encoder steps), and then continuously rotates the motor until the target position is reached.The
m.set_velocity()
function is used to set the speed of the motor, and the motor stops once the desired position is achieved.
User Input:
The program prompts the user to input the number of turns they wish the motor to rotate. The
rotate_motor()
function then handles the rotation based on this input.
Threading and Time Control:
The program uses a simple
while
loop withtime.sleep(0.01)
to periodically check the motor’s position. This ensures the motor rotates in a controlled manner, and the loop continues until the target position is reached.
Example Usage:
When the program is executed, it will ask the user to input the number of turns:
After the user inputs the value (e.g., 5), the motor will rotate the equivalent number of turns. The program will print a message once the motor has completed the rotation:
Code
Last updated