2.017 Deign of Electromechanical Robotic Sytem, Fall 2009 Lab 4: Motor Control Aigned: 10/5/09 1 Overview So far we have learnt how to ue the Arduino to acquire variou type of ignal from enor uch a the GPS receiver, temperature enor, potentiometer, photo reitor, puh button, Reed witche, etc. We will now turn our attention to actuator control, which i a critical part of an electromechanical robotic ytem. With both ening and actuator control capabilitie, the robotic ytem can actively interact with the environment that it i in. The goal of thi lab i to learn how to control a DC motor uing the Arduino microcontroller board and the Motor Shield. We will ue the reading from a rotary encoder attached to the back of the motor a our feedback ignal. You will then learn how to interpret the encoder ignal, deign a controller for the motor, and drive the motor to a et-point or to follow a pre-defined profile. After that we will experiment with an RC ervo by commanding the ervo to go to a et poition. We will pend the lat half hour of the lab for project dicuion. 2 DC Motor Experiment One Maxon F2140.937 DC bruhed ervo motor, one Arduino motor hield with encoder interface circuit, and one external power upply will be given to each group. Connect up the above component to your Arduino board and PC according to the photo hown in Figure 1. Download the file Lab4file.zip from http://web.mit.edu/hchin/public/2.017/ and upzip to your lab4 folder. Unzip ServoTimer1-fixedv13.zip and AFMotor_18-2-09.zip. Put the above two unzipped folder in your C:\...\Arduino\hardware\librarie folder. The ret of the file are decribed below: o DC_Motor_Encoder.pdf Reference document on DC motor, PWM, and encoder. o Maxon_motor_pec.pdf DC motor, encoder and gearhead data heet. o 94_pc6_dataheet_0.pdf Decoder circuit board document. o LS7183_LS7184.pdf Decoder IC chip document. o MotorControlEncoderTemplate3.pde Arduino template code for DC motor control. o Servo1.pde Arduino template code for ervo motor control. Read the above document. 2.017 Fall 2009 1
Figure 1. DC motor control lab etup. 2.1 Proceing Encoder Signal The encoder ignal are proceed by the decoder circuit provided with your motor. It help count the quadrature output and give a decoded ignal to the Arduino. We've choen thi etup becaue the Arduino ha only two interrupt pin available, one of which i ued by the motor hield. Thi make it difficult to keep an accurate record of the two channel of the quadrature input. The dedicated decoder chip tranlate the quadrature output into a ingle clock channel and a direction channel (ee Figure 2). The clock channel changing tatu indicate that a ingle tick for the encoder in the direction indicated by the direction output. Now the Arduino need only interrupt on one ignal, read the direction on a tandard digital pin intead of an interrupt, and update it count accordingly. Figure 2. PC6-C-X decoder circuit timing diagram. The circuit we ue i et up to be the X4 mode. 2.017 Fall 2009 2
Ue an Ocillocope to meaure the raw encoder ignal from either encoder channel. Meaure the clock channel from the decoder circuit. How many encoder count per revolution of the motor haft? Ue the provided pec heet to find the anwer. For the purpoe of thi lab, you can imply ue the encoder0po variable to give you the current encoder poition and allow the provided interrupt function to keep it updated. Capture encoder data by turning the motor by hand: o Upload the MotorControlEncoderTemplate3.pde ketch to Arduino. o The Arduino program continually print time and the current encoder poition. You can capture thi data with the RealTerm oftware. o Run RealTerm. In the Diplay tab, et Diplay a to the econd Acii, then under the Port tab, et the correct COM Port and the baud rate to match up with the one declared in the Arduino program, then click the change button with the green check mark. You hould now be able to ee reaonable data coming from the Arduino. o You can alo end command to the Arduino if you have et the Arduino program up to receive erial command. o To capture data, go to the capture tab. Specify the output location, then begin capturing data of interet by clicking Start Overwrite. When finihed, click Stop Capture. Now you have a text file you can import into Matlab and graph. Be ure that the firt and lat line are complete to avoid uploading partial data. Delete thee line if neceary. Uing the captured data, plot encoder poition v. time. Alo write code to calculate the derivative of the poition, and plot velocity v. time. Find the calibration factor between angle in radian and encoder count by manually turning the motor. I the number the ame a the one you found baed on the information on the pec heet? 2.2 Implementing Cloed-Loop Poition Control Now implement your poition controller on the Arduino. The command to drive the DC motor in the Arduino code i called etspeed() which can be et between 0 and 255, where 0 correpond to no voltage, 255 correpond to full voltage, and the duty cycle (the percent of the time the voltage i et to high) varie linearly in between. Start with a proportional controller and add a derivative term to make a PD controller. See how well your controller work by manually changing the et point. You can alo try uing the provided function generator which give you a low quare wave and a ine wave. Capture your controller' performance and make a plot in Matlab once you've found effective gain. A plot of the root locu ha been provided in Figure 3 to help you think about your controller deign. 2.017 Fall 2009 3
Figure 3. A root locu plot of an open-loop PD controlled motor. Your tak i to adjut the controller gain until the repone of the cloed-loop ytem i reaonably fat and well damped. 2.3 Higher Performance from the Control Sytem Thoe of you who have taken a more advanced cla in control ytem may wonder why we are uing PD control, rather than PID or Lead-Lag/Lag-Lead control. We can, in fact, ue PID control, but there are a few real-world conideration that need to be addreed. For extra credit, try to make a PID controller that follow the quare and the ine wave provided in the code. Remember that a PID compenator i a filter of the following form: G ( ) = K c p K + i K d + K = d 2 + K + K p i It ha two zero and one pole. For the moment, we will not think about thee a eparate gain, but a zero location: G ( z )( 1 2 c ( ) = K o We will aume that the plant i jut two pole at the origin, o the forward tranfer function of the ytem take the form: G( ) = G c ( ) G p ( ) = K o z ) ( z 1 )( z2) 3 2.017 Fall 2009 4
Ue the root locu tool (i.e., iotool) in Matlab to pick value of z 1 and z 2 that produce a reaonable root locu for thi open-loop tranfer function. The performance of the controller will be dominated by the location of the cloed-loop pole/pole pair cloet to the imaginary axi. Once you have choen thee value, ue the equation above to olve for the gain value. Once you have uccefully implemented thi PID controller and have it tracking the reference ine wave, connect it up to a quare wave reference. You will notice that the controller exhibit a huge amount of overhoot. Thi phenomenon i not predicted by the linear model of our ytem. It i due to the fact that the motor repone i rate limited. When a reference i given that the motor cannot follow, the integral error will increae very quickly. Once you are back on the reference, the integral error effectively act a a diturbance, cauing overhoot. Control deigner refer to thi problem a integrator wind-up. It can be olved either by pre-filtering the reference ignal by rate limiting and moothing o that the controller only receive input that it can follow, or by placing aturation limit on the integrator o that it can't wind up too far. Experiment with variou way to reduce wind-up. You may want to try etting aturation limit on the integrator firt. Print out one graph each howing the cloed-loop repone of the PID controlled ytem with the quare and the ine wave reference input repectively. I it better than the PD control and why? 2.4 Velocity Control Some application require motor peed control intead of controlling it poition. If you have time, deign a imple PI controller to control the peed of the motor by modifying the template code. 3 Controlling a Servo RC hobby ervo are the eaiet way to et up for motor control. They have a 3-pin 0.1" female header connection with +5V, ground and ignal input. The motor hield imply bring out the 16bit PWM output line to 2 3-pin header o that it i eay to plug in a ervo and tart ending command ignal to it. Typically an RC ervo can be poitioned from 0 to 180 degree. Inide the ervo there i a DC motor connected to a potentiometer. PWM ignal ent to the ervo are tranlated into poition command by the feedback circuitry inide the ervo. When the ervo i commanded to rotate, the motor i powered until the potentiometer reache the value correponding to the commanded poition. RC ervo are often ued in mall-cale robotic application due to their affordability, reliability, and implicity of control by microproceor. The ervo ha three wire: ground (uually black), power (red) and control (white). However the ervo we have in the kit ha a different color cheme which i wired a brown (negative), red (poitive) and orange (ignal). 2.017 Fall 2009 5
The ervo will move baed on the pule ent over the control wire, which et the angle of the actuator arm. The ervo expect a pule every 20 m in order to gain correct information about the angle. The width of the ervo pule dictate the range of the ervo' angular motion. The PWM pin of the ervo connector on the Arduino motor hield are etup to provide the required duty cycle to drive a typical ervo. Attach one of the haft attachment to the ervo. Connect the ervo to SER1 male connector on the motor hield. Make ure the brown wire i connected to ground pin. Open and upload the Servo1 ketch. Open the Serial Monitor and try ending different haft angle to the ervo. Modify the code o that you can ue a potentiometer to control the haft angle. 4 Project Dicuion Propoal feedback. 5 Deliverable Anwer all the quetion above. Plot. Show the teaching taff your lab notebook. 2.017 Fall 2009 6