Here is a test program you can use to test your speed follower controller. Our test program will perform a test similar (but not the same!) to this on your final project. The test program generates a speed "profile" that runs the master motor at different speeds. In the final test, this value will control the speed of the master motor, which you will have to measure to control your slave motor. The test program measures the speed of both motors and accumulates the total absolute relative error as a measure of quality and prints this when done. During the program run, it prints out the speeds of the two motors. You can cut-and-paste this into an Excel spreadsheet graph to visualize the performance of your controller. We are giving you the part of this test program that generates the speed profile. In the "simulation" case, the master motor is virtual - you can just look at a variable to see how fast it is running. You will use this to implement your controller. Note that the master motor speed is ideal. That is, there will not be any noise that your controller will have to follow. You will need to merge your PID controller code with this test program. Your program should be structured as follows: The main loop generates the speed profile - it will be difficult to add anything useful to this loop. (In the final test, this loop will be removed since it will be done by our test program but there's really no reason to use it.) + Timer 0 is used by the Arduino time and delay functions, so we have left these alone. (We do not think Timer 1 is used for anything by Arduino - if you think this is wrong, let us know! Of course, if you use the PWM pins, the timer comes in to play - so pick your PWM output pins carefully!) + The test program sets up Timer 2 to generate regular interrupts. This is used to sample and print the motor speeds and accumulate the absolute differences. You can share this interrupt. + The test program has stubs for the interrupts for the motor - add your code here. You should write your code so that it is easy to switch from simulation mode to test mode. We will have two test stations for you to use. To test your code, you will have to connect your Arduino to our Arduino. This will require 3 wires - these wires will be labeled. Make these connections ***BEFORE*** you plug in the boards. + Ground to Ground + Master Motor Sense + Slave Motor Sense. Some notes - PAY ATTENTION, THIS IS IMPORTANT: 1. Remember that while you are in an interrupt routine, no other interrupts will be processed because interrupts are turned off when the handler is called. This will mean that interrupts may get lost, or even if not lost, you won't know exactly when an interrupt happened. The right way to solve this is to put the "critical section" at the beginning of the handler and then turn on interrupts so that other interrupts can be handled while you are doing whatever else you need to do. You must deal with any data that is shared with other interrupt routines before you turn on interrupts (that's why it is called the critical section). Otherwise, the data may change underneath you, or you may change data before and after another routine sees it, resulting in inconsistencies. The other thing you need to think about is whether your interrupt handler can run so long that the same interrupt will happen again before you are done. If so, your interrupt handler will be called "recursively" (actually reentrantly, since you have no control over when it gets called), which won't work unless your routine is "reentrant", meaning that two or more copies of the same routine can operate at the same time. To avoid any complication, make sure that this cannot happen. Make sure your floating point, divisions and especially SerialPrints don't add up to more time than your interrupt interval can be. You should document this in your comments. (Note that our test program does *not* address this problem for you!) 2. As I have discussed with most of you, there are two ways to measure the speed of the motor. I'll use the analogy of measuring the speed of your car while driving using the mile markers. a) Start counting when the second hand reaches 12, and count how many markers you pass in exactly 10 minutes. b) Start a stopwatch when you pass a mile marker and stop it when you pass the next (or Nth) mile marker. Which is more accurate? Which gives an answer quicker? i.e. quick response time? How might you keep the response time more or less the same by adjusting to the speed of the car? 3. Getting any kind of reasonable speed following performance will be sufficient to get credit for this project. Getting good performance will be worth some small extra credit. 4. When you simulate, the master motor speed with be ideal. That is, it won't have any noise or "wobble" in it. To get good final performance, you might want to add some random noise to the speed profile in your simulation. 5. You probably know that there is an interesting special case when the motor stops. If you handle this case, you will earn some small extra credit. As always, please let us know about any questions you might have.