# Software

We are implementing our control on an Arduino Duemilanove microcontroller. Because our main deliverable is development and analysis of an algorithm, the hardware we use to implement it is important only in it's ease of use, and similarity in function to the hardware on the CubeSat. The Arduino is a controller that we are familiar with, so the startup time was very small. Additionally, it has built in Analog to Digital converters, allowing us to measure temperature much more easily. In addition, the code can be written in C, which is the code used on the CubeSat. The built-in Arduino functions we use will be well annotated for future translation into pure C.

## PWM Output

__What is PWM?__ To control temperature, we will be using Pulse Width Modulation, or PWM. With PWM, one can use rapid digital pulses that simulate an analog signal. The value of the simulated analog voltage depends on the "duty cycle" or the ratio of the pulse duration to the period of the signal. It is expressed as a percentage. An illustration of pulses with varying duty cycles is shown in Figure 1.

__Coding for PWM:__ Arduino has a built-in function to output PWM, however we had to choose an output frequency that would work with the circuits we were designing. We chose the highest output possible, 62.5kHz. Our only job is to find the voltage we want, and map our desired 0-5V to 0-255 bits, which is the parameter taken by the PWM output function.

## Voltage Finder

__Code Design:__ Figure 2 suggests the most efficient voltages to use for specific Delta Ts. The qualitative conclusion is that cooling gradually is the most efficient method. However, to implement this accurately, we recorded the given data points. For testing, we recorded the temperatures in a two-dimensional array. When we measure our Delta T, we find the closest Delta T with a data point, and apply that voltage. In the future, we plan to either create a piecewise function of straight lines between data points, or, ideally, to create a curve that fits all the data points. Before we go to this trouble, we will test the accuracy of the graph.

__Proof of Concept:__ To understand this better, we have designed a test for the efficiencies of three different methods, being 1) constant 7 volt voltage, 2) PWM signal, with duty cycle set by the Voltage Graph, and 3) Smoothed PWM signal, with duty cycle and effective voltage set by the Voltage Graph. Efficiency will be measured as Kelvins per Joule over the course of a 50 Kelvin temperature drop. This 50 Kelvin drop should allow us to experience a range of temperature differences over the cooler, to take advantage of any of the benefits of changing the voltage. This test will determine the accuracy of the Voltage Graph, how beneficial it is, whether PWM needs to be smoothed to achieve the added efficiency.

__Finding the REAL Voltage Graph:__ It may turn out that, while using gradual cooling is more efficient, the given graph is not quantitatively applicable to our cooler. This is to be expected, because, while TECs are often physically similar, they are not identical, and the datasheet with this graph is not for our exact cooler. If we find discrepancies, this brings up two issues. For one, we'd like to find the correct information for our cooler, which will be a delicate process, requiring complex code and several iterations, performed in a vacuum. We probably cannot ask for a high degree of accuracy, but can surely find something more efficient than the data for a different cooler. The other problem that will arise if we find significant differences is applying our results to the Marlow cooler, used on the CubeSat. We do not have access to this particular cooler, so whatever we design must be modular. We may have to settle for a rough estimate on our cooler, to prove the concept, and then do more detailed research/experiments with the CubeSats cooler when we can access it.

## Controller

__What is PID?:__ To find the voltage that we want to apply, we calculate three terms: proportional, derivative, and integral. Each term is a function of the error, or previous errors, multiplied by a constant, or "gain". The terms are then summed and applied to the system. For proportional, we take the error between our goal temperature and the actual temperature, and multiply by the gain. It is the simplest one: the applied voltage increases linearly with error.

The next, more complicated term we introduced was integral control. Integral helps find the ideal temperature for steady state in systems with constant resistance, by adding up the previous error. For example, in our system proportional control cannot stay at steady state, or it will apply a control signal of zero, and start heating up. However, with integral control, the controller will add up the previous errors to find an error that keeps the temperature at the goal. If it integrates too much error, it will overshoot, and start subtracting. Integral control is notorious for oscillation, but with the right amount, it oscillates minimally, and is very good at reaching steady state.

The final term is derivative control. This applies a signal that is proportional to the rate of change of the temperature. In doing so, it prevents oscillations, as well as disturbances. Derivative control is what you use when you react to someone suddenly shoving you.

__Why PID?__ We chose to use PID for a number of reasons. After researching control theory briefly, we determined that PID control fit our requirements very well. Those requirements being: fast response time, minimal overshoot, and minimal fluctuations at steady state. It appears that if we can achieve a steady state within +/- .1 Kelvin, it will be with PID.

__Specifics of our Controller:__ One of the nice things about PID is that we have only three parameters to worry about, the three gains to determine the magnitude of each effect. The gains are obviously not set in stone. To see this: imagine if you measured temperature in degrees Fahrenheit, found an error of 10 degrees and applied that in millivolts. But what if our system uses volts? The general behavior will be the same: output will increase proportionally with error, but our gain should be a factor of 1,000 lower to get the same effect!

There is no obvious translation between temperature and voltage to use without a complex simulation that takes into account all of the elements of the physical system. For this reason, we've started with a rough estimate for the proportional gain: max output (8 volts) should occur when the error is about 5 Kelvin, and begin slowing down from there. We'll call +/- 5 Kelvin the control cutoff. Any error beyond this will generate the same effect: 8 Volts output. So, to start, we multiply our error by 1.6 Volts per Kelvin. Research did give us a good starting point for the relationship between the three constants. In general, integral gain should be about one tenth of proportional, and derivative gain only a tenth of that. So we have a good starting point for constants, and experiments, along with research into temperature control can help us adjust those to ideal levels. There are also some practical considerations about our system that will help guide our adjustments. For example, our derivative term may need to be higher than most, since fluctuations are likely to be slow, as well as the corrective response to those fluctuations.

We also have to address when to start the control. For most of the cooling process, we are not concerned with fluctuations and overshoot. This lets us set the voltage based purely on efficiency, as discussed above. Eventually, control becomes important to consider, and we have to sacrifice efficiency. Since anything greater than the control cutoff makes no difference to the control algorithm, this is a good point to change from efficiency mode to control mode. Experimentally, we may determine that we must start control sooner, or that we are allowed to start later.

Coming soon: Adventures with Integral Windup and Weird Logical Fixes!

## Efficiency Analysis

A large part of our project is analyzing different methods of cooling and finding the most efficient one for the CubeSat.__Finding clear and relevant metrics:__ The standards for efficiency are not immediately obvious. Power efficiency is more important than total energy output when the CubeSat is in the sun, however, minimizing total energy loss is important when the CubeSat is in the Earth's shadow. For now, we are recording both metrics, but we will soon come up with a standard rubric that may be more complex than total energy/max power output. Another idea would be to set a maximum energy usage bar, and find a power plan that stays under that energy usage while minimizing power. This comes down to a balance of time spent cooling versus power input to the cooler. It is very likely that maximizing power efficiency will also maximize energy efficiency, as the most efficient voltage seems to be close to the lowest voltage that can cool the TEC.

__Calculating Total Joules:__ As a proof of concept, we've started measuring the total energy output for different cooling methods.