Week 10: Output Devices

For output devices I want to program the motor board that I've been working with. My motor has rotary encoder which I read values from in input devices week.

Programming the board

I used the Ardiuno IDE to program my board. Since I'm using the PWM pins, the speed of the motor is dependent on how often during a cycle the signal is "HIGH" for. Sending an analogWrite(0, 255) sends a analog signal to pin 0 that is HIGH for 100% of the time. Decreasing the value decreases the amount of the time that the signal is high for, making the overall speed of the motor lower. To stop the motor, I need to send a HIGH signal to both motor pins. While programming, I noticed that sending both LOW and LOW would cause the motor to stall and I would need to do a powercycle. Reading the datasheet, I realized that the motor bridge I was using (A4953) enters standby mode when it recieves two low inputs. I had to make sure that my code never sent two analogWrite(_, 0) outputs.

With the simple Arduino program that I wrote in embedded programming week, I got the motor to spin at a steady speed.

PID Control

To achieve, good speed control, I want to implement a PID controller. PID control is a feedback-loop system that uses a proportional, integration, and derivative constant that constantly changes the ouput depending on error. Using PID control, I can control the motor to run at a constant speed and alter the signal sent out depending on the error between the actual and expected speed. I can calculate the error by using the encoder to determine the working speed of the motor. This allows me to correct for differences in power supply, force exerted on the motor, etc.

I coded up a simple PID controller in the Arduino IDE. I determined the error by finding the difference between the desired output and the input read in by the rotary encoder.

I was having trouble reading the speed off the encoder so I simplified to code to try to maintain a position using the PID controller. I played around with the k coefficients until I found good values to maintain a steady system. By changing the setPoint, I was able to change the position that the motor settled upon.