RPI Step Motor controller - Viktor Mihalovic
Project Overview:
The primary goal of the RPI Step Motor Controller project is to automate the process of controlling the height of the Oree R-t model laser arm for laser marking in production. This automation involves automatically scanning a product's serial number, retrieving information from a database, and adjusting the height of the laser arm accordingly. This project was focused on part to manage height of laser arm
Hardware Setup:
- Raspberry Pi Model 4 4GB
- Oree R-t model laser
- Step Motor: 57CM22X
- Microstep Drive: DM542
- Controller with display: MC-24MR-4MT-500-FX-A
- Bottom and top sensors: TL-Q5MC1-Z
Raspberry Pi Contact Board:
To interface the Raspberry Pi with the Microstep Drive DM542 and incorporate buttons and LEDs for control and feedback, a custom contact board was created. The contact board consists of the following components:
- Transistors
- Resistors
- Buttons
- LEDs
- Wires
This contact board facilitates the control and monitoring of the stepper motor and provides visual feedback through LEDs. It is responsible for translating signals between the Raspberry Pi GPIO pins and the connected components, enabling seamless integration into the overall system. Chalange was to change input signals to 3.3V and output signals to 5V. For this purpose I need to use transistors and other components.
Software Setup:
Programming Language: Python Libraries/Frameworks: RPi.GPIO, time
Testing and Validation:
Extensive testing was conducted to ensure the accuracy and reliability of the height adjustment process. Commands were implemented to adjust the height for different locations, and the system demonstrated precise control, achieving adjustments accurate to within a tenth of a millimeter.
Code Explanation:
import RPi.GPIO as GPIO #Import necessary libraries
import time
GPIO.setmode(GPIO.BCM) #Set GPIO mode
class Arm:
def __init__(self):
# Initialize GPIO pins and variables
self.settings()
self.sensor_down = False
self.sensor_up = False
self.buttonUp = False
self.buttonDown = False
self.actHeight = -1
self.check_read_sensors()
self.numberOfSteps = 0
# Function to initialize GPIO settings
def settings(self):
# Define GPIO pins for motor control, sensors, and buttons
self.motor_direction_pin = 27
self.motor_step_pin = 22
self.sensor_down_pin = 23
self.buttonUpPin = 25
self.buttonDownPin = 6
self.sensor_up_pin = 24
self.delay = 0.000003 # Adjust this delay for motor speed
self.pulses_per_rev = 5000 # Set pulses per revolution
# Setup GPIO pins
GPIO.setup(self.motor_direction_pin, GPIO.OUT)
GPIO.setup(self.motor_step_pin, GPIO.OUT)
GPIO.setup(self.sensor_down_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(self.sensor_up_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(self.buttonUpPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(self.buttonDownPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Function to perform a single step
def step_once(self):
self.numberOfSteps += 1
GPIO.output(self.motor_step_pin, True)
time.sleep(self.delay)
GPIO.output(self.motor_step_pin, False)
time.sleep(self.delay)
# Function to step the motor forward
def step_forward(self):
GPIO.output(self.motor_direction_pin, True)
self.step_once()
self.actHeight += 0.0008
# Function to step the motor in reverse
def step_reverse(self):
GPIO.output(self.motor_direction_pin, False)
self.step_once()
self.actHeight -= 0.0008
# Function to stop the motor
def stopMotor(self):
GPIO.output(self.motor_step_pin, False)
# Function to read sensor states
def check_read_sensors(self):
self.sensor_down = not GPIO.input(self.sensor_down_pin)
self.sensor_up = not GPIO.input(self.sensor_up_pin)
self.buttonUp = not GPIO.input(self.buttonUpPin)
self.buttonDown = not GPIO.input(self.buttonDownPin)
# Main function to start height adjustment
def start(self):
while True:
self.check_read_sensors()
if self.sensor_down and self.buttonDown:
self.stopMotor()
print('Hit bottom')
continue
if self.sensor_up and self.buttonUp:
self.stopMotor()
print('Hit top')
continue
if self.buttonUp and self.buttonDown:
self.stopMotor()
print('Both Sensors')
break
if self.buttonUp:
self.step_forward()
print('Forward')
continue
if self.buttonDown:
self.step_reverse()
print('Backward')
continue
self.stopMotor()
GPIO.cleanup()
# Function to find the bottom position
def findBottom(self):
while not self.sensor_down:
self.check_read_sensors()
self.step_reverse()
self.actHeight = 143
self.numberOfSteps = 0
# Function to move to a specified height
def goto(self, height):
self.check_read_sensors()
if self.actHeight <= height:
while round(self.actHeight,2) != round(height,2) and not self.sensor_up:
self.step_forward()
self.check_read_sensors()
else:
while round(self.actHeight,2) != round(height,2) and not self.sensor_down:
self.step_reverse()
self.check_read_sensors()
#Main function
if __name__ == "__main__":
print("Program has started")
a = Arm()
a.findBottom()
print('I am at 0 cm')
time.sleep(4)
print('Going up to 480 mm')
a.goto(480)
print('I am at 480 mm')
time.sleep(4)
print('Going up to 200 mm')
a.goto(200)
print('I am at 200 mm')
time.sleep(4)
print('Going up to 0 mm')
a.goto(0)
print('Stopped at 143 mm due to height limit')
GPIO.cleanup()
Video:
Conclusion:
The RPI Step Motor Controller project successfully achieves the goal of automating the process of controlling the height of the Oree R-t model laser arm. With further improvements and enhancements, it can effectively contribute to the automation of the laser marking process in production.
Sources: