Motor Control As mechanical engineers, one of the most important things that we typically want to do with mechatronic systems is control motion. DC motors are frequently a convenient actuator for this task, because they come in a wide variety of sizes and are relatively simple to control. In this lab we will look a few methods at controlling motor position of a DC motor. To be clear, when we say we want to control the position of a DC motor, we mean that we want to set a target angle for the motor shaft in our code, and have the motor move to that target. This sounds like a simple task, but by the end of this lab you will see that there are many details that need to be right for motor position control to work well. We’ve actually used an example of motor control before, specifically the hobby servos from lab 1. They have a potentiometer attached to the servo horn which feeds the angular position back to a motor controller. This creates a closed-loop system which attempts to maintain a set position based on the commands you send from the Arduino. Optical Encoders Before we can control the position of a motor, we need to have some type of feedback about the position of the motor. One of the most common ways to measure the angular position of a motor is to use an optical encoder as discussed in class. The output pattern of an optical encoder is known as quadrature and it can be used to sense both position and direction. An example of a quadrature waveform can be seen below. Converting optical encoder signals into useable position information can be a fairly difficult task. One of the biggest challenges is that it can be very processing intensive for a microprocessor to watch for changes on the encoder lines, and keep track of all of the arithmetic associated with the changing values, especially if the encoder has a high resolution. There are chips, such as the HTCL-2222 that assist with this task, however, they can also be somewhat cumbersome to use. For this lab, we will get around this problem by using a motor with a relatively low resolution encoder and using the Arduino’s interrupts to look for changes on the encoder output pins. The table below summarizes the connections for the Lego motors: Lego Motor Connections Color Function Black Motor A White Motor B Green Encoder +5V Red Encoder GND Yellow Blue Encoder A Encoder B In order to help simplify your code, only use an interrupt on one of the encoder pins. Place the following line of code in void setup() to use an interrupt. attachInterrupt(0, increment, CHANGE); This code watches digital pin 2 (which Arduino calls interrupt 0) for any change and calls the function increment() (which you will have to write) when the pin changes state. You can change the name of the function if you want to. Your first task is to write the interrupt function which will add or subtract from a counter that holds the number of degrees that your motor has turned. Hint: declare a global variable to hold the value of this counter, and think about how the states of the two encoder lines relate to changes in your counter. Also, remember that the encoder on the Lego motors has a resolution of one degree. As always, remember to declare any digital pins you are using as inputs or outputs. Write a simple piece of code that prints the value of this encoder to the serial monitor as you turn the motor by hand. Proportional Control As the name implies, a proportional controller produces an output which is proportional to the error between the present position of the motor and the target position. As mentioned above, you will measure the position of the motor using the built-in encoder of the Lego motor. You will use a motor driver to produce a voltage across the terminals of the motor as we have done in previous labs. In equation form a proportional controller looks as follows: 𝑉𝑜𝑙𝑡𝑎𝑔𝑒 = (𝑡𝑎𝑟𝑔𝑒𝑡 𝑑𝑒𝑔 𝑝𝑜𝑠𝑖𝑡𝑖𝑜𝑛 − 𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑑𝑒𝑔 𝑝𝑜𝑠𝑖𝑡𝑖𝑜𝑛) ∗ 𝑔𝑎𝑖𝑛 The gain in this equation is crucial to the performance of the control system as a whole, which you will see throughout this lab. The serial monitor is the easiest way to get the data from your system. You will use two different motor drivers for this lab which appear at first to perform the same function, but we will see that they produce different behavior. One will be a high-performance motor driver from Pololu and the other will be the L293D. The bulk of your code will be the same for these two drivers, but write a separate function for each driver to simplify your code. Only use the 5V supply from your breadboard. Run your code with gains of 0.05 , 0.25 , and 0.5 for each motor driver. Copy the data (time & motor position) for each gain from the serial monitor to either MATLAB or Excel. Create 3 Position vs Time plots, one for each gain and both motor drivers on the same plot. Report: Include your code for proportional control. Your code can run any of the gain cases described above. What happens to the motor position in your code every time you restart your program? Describe the effect of changing the proportional gain of your system. Compare the response of the Pololu driver with the response of the L293D.