Although I'm a MechE, I'm really interested in software and electronics. To fuel this interest, I got an
Arduino a couple of months ago. I got busy with school and wasn't able to do any cool projects with it besides make LEDs blink. Anyway I decided to try to control a motor like a servo.
I researched how to control motors and I learned that I needed an h-bridge to control the direction of the motor and something to provide feedback of the motors position. For feedback of the position I decided on potentiometers since I didn't really care if I had continuous rotation. I searched a little longer for information on and schematics for h-bridges until I had a basic understanding of their operation and a had a
schematic that I could make from RadioShack parts.
I used different but similar transistors to what was shown on the schematic. It didn't explode the first time I powered it up! It preformed exactly how it was supposed to. With motor control out of the way, I turned to some way of connecting the motor shaft to the potentiometer. While rummaging through my parts bin I found a KNEX motor and discovers that the pot shaft fit perfectly in the hole for the KNEX shaft. I quickly built a stand for it out of KNEX. I wired the feedback pot, h-bridge inputs and controller pot to my arduino and went to work on the coding.
My first control algorithm was incredible simple and I'm surprised that I thought it would work. It went like this in pseudo code:
take reading of input pot
take reading of output pot
if output less than input, move towards input
if output equals input, stop
if output greater than input, move towards input in opposite direction
That caused major oscillations, as the motor would shoot pass the input and then switch directions and shoot past again. I researched a little more and I found
PID control. I read a couple post on the Arduino forums and looked at some code. I found it to be much easier than I thought and implemented it into my code.
In pseudo code, here is what I added:
take reading of input pot
take reading of output pot
error = input – output
sum of error += error * delta time
error change = (error – last error) / delta time
motor speed = Kp*error + Ki*sum of error + Kd*error change
The motor speed variable gets translated to a PWM signal that gets sent to the motor to control the speed. The Kp, Ki, Kd are gain constants. They tune the output to get the desired response from the system.
With this new code position control was achieved! There were some slight oscillations but they eventually died out. Adjusting the gains would help with those.
To visual the positions and the oscillations better I wrote a Processing program that displayed the angles of the input and output as lines. I also added serial communication between my computer and the Arduino so that I could change the gains on the fly and see how they effected the motor response. Finally! It's time for the video.
Overall it worked very well. If I moved the control pot fast there was some lag between that and the motor position because of the large reduction in the KNEX motor. This is another project that I wanted to iterate and make better by having a more direct connection between the motor and the feedback but I haven't gotten around to doing yet.