DC (Direct Current) motors are essential components in many mechatronic systems. They convert electrical energy into mechanical energy, providing the motion needed in various applications, from simple toys to complex industrial robots. Let's explore the basic principles of DC motors and how we control them.
Controlling the speed of a DC motor is crucial in many applications. One of the most efficient methods for speed control is Pulse Width Modulation.
PWM is a technique where we rapidly switch the power to the motor on and off. The speed of the motor is determined by the ratio of "on" time to "off" time, called the duty cycle.
A higher duty cycle (more "on" time) results in a faster motor speed.
A lower duty cycle (less "on" time) results in a slower motor speed.
The power to the motor is switched on and off very quickly (typically hundreds or thousands of times per second).
The motor's inertia smooths out these pulses, resulting in steady rotation.
By adjusting the width of the "on" pulses (hence "Pulse Width" Modulation), we can control the average power delivered to the motor, and thus its speed.
PWM allows for very precise speed control and is energy efficient, as it minimizes power loss in the control circuitry.
Controlling the direction of a DC motor is another crucial aspect in mechatronics. This is typically achieved using an H-bridge circuit.
An H-bridge is an electronic circuit that allows you to apply voltage across a load (in this case, a motor) in either direction.
It's called an H-bridge because its schematic diagram resembles the letter "H".
The H-bridge consists of four switches (usually transistors) arranged in an "H" configuration.
The motor sits in the middle of the "H".
By closing different pairs of switches, you can change the direction of current flow through the motor.
Forward Motion: Close the top-left and bottom-right switches. Current flows from left to right through the motor.
Reverse Motion: Close the top-right and bottom-left switches. Current flows from right to left through the motor.
Brake: Close either both top switches or both bottom switches. This shorts the motor terminals, providing a braking effect.
Coast: Open all switches. The motor is disconnected and will coast to a stop.
H-bridges are often integrated into motor driver chips or modules, simplifying the control of DC motors in mechatronic systems.
Ready to put your DC motor control skills to the test? These challenges will help you gain precise control over your Maqueen robot's movements. Remember, in robotics, accuracy is key!
Your mission, should you choose to accept it, is to make your Maqueen robot travel exact distances.
Setup:
Mark the following distances on the floor:
500mm (50cm)
1000mm (1m)
2000mm (2m)
Place your Maqueen at the starting point.
Task:
Program your Maqueen to travel each of these distances accurately. Here's how to approach this:
Start with a constant speed of 128 (half of the maximum 255).
Experiment with different durations to find out how long the Maqueen needs to run to cover each distance.
Record your findings and see if you can identify a pattern.
Example Code Structure:
from microbit import *
from maqueenplusv2 import *
init_maqueen()
def drive_for_duration(duration_ms, speed=128):
motors(speed, FORWARD, speed, FORWARD)
sleep(duration_ms)
stop()
while True:
if button_a.is_pressed():
sleep(500) # Allows you to move out of the way
drive_for_duration(2000) # # Drive for 2 seconds
Extension
Can you create a drive_distance function that takes a distance in millimeters as an argument?
def drive_distance(distance_mm, speed=128):
# Your code here
pass
# Example usage:
drive_distance(500) # Drive 500mm
Things to Consider:
What's the relationship between duration and distance?
Does the speed affect this relationship? Try different speeds and see!
Now that you can move in straight lines, let's add some turns!
Task:
Program your Maqueen to turn exact angles. Start with 90 degrees, then try 45 degrees and 180 degrees.
Approach:
Use the motors() function to turn the robot. Remember, to turn left, the right motor should move forward and the left motor backward (or vice versa for turning right).
Start with a constant speed (like 128) and experiment with different durations.
Use the stop() function after each turn for more precise control.
Example Code Structure:
def turn_for_duration(duration_ms, direction='left', speed=128):
if direction == 'left':
motors(speed, BACKWARD, speed, FORWARD)
else: # right turn
motors(speed, FORWARD, speed, BACKWARD)
sleep(duration_ms)
stop()
# Example usage:
turn_for_duration(500, 'left') # Turn left for 0.5 seconds
Extension:
Can you create a turn_angle function that takes an angle in degrees as an argument?
def turn_angle(angle_degrees, direction='left', speed=128):
# Your code here
pass
# Example usage:
turn_angle(90, 'right') # Turn 90 degrees to the right
Things to Consider:
How does the turning radius of your Maqueen affect its rotation?
Is there a relationship between the angle of turn and the duration of motor activation?
Does the speed affect the accuracy of your turns?
Remember how we programmed the Maqueen to move in a square earlier? Now let's see if we can do it with some accuracy.
Combine your new drive_distance and turn_angle functions to make your Maqueen drive in a perfect square!
Example Code Structure:
def drive_square(side_length_mm):
for _ in range(4):
drive_distance(side_length_mm)
turn_angle(90, 'left')
# Example usage:
drive_square(500) # Drive a 500mm square