# Micro-Satellite Systems Design, Integration, and Flight
## 4 Attitude Control System (ACS) Development
### Battery Voltage Offset Correction  
We noticed that the soft iron offsets decreased as the CubeSat's battery voltage went down. The battery voltage could vary from 4.2 to 3.7 volts over the charge cycle of the battery. The ACS would automatically shut down to save power if the battery went below 3.7 V, so we did not consider lower voltages. The change in voltage from a full to an empty battery did not affect the magnetic field much. The differences were 1-2.5 times the resolution error of the IMU magnetometer. We first thought of a linear subtraction because it was fast and easy. But we knew that the PWM and offset correlation was not linear, so this method seemed too simple. We did not need the extra precision, but we calculated a correction coefficient instead. We collected data by connecting the CubeSat to a variable power supply and changing the voltage from 4.2 to 3.7 volts by 0.1 volts each time. We did this for each of the three coils, and the changes in offsets were measured along all three coordinate axes by the IMU. The PWM value of each coil was set to 255. We did nine experiments in total. The average change in the offsets from 4.2 to 3.7 volts was recorded for all nine cases. The value was then normalized. The normalization was performed to find the fraction of change in the offsets compared to full battery conditions. Since the experiments were performed at a PWM value of 255, normalizing against the 4.2 volt 255 PWM value would give the percentage of change in the offsets at 3.7 volts. This value shows the proportional difference in the magnetic offsets between a full battery and 3.7 volts. We programmed a linear gradient that could use any input voltage and correct the change in offsets accordingly. Subtracting the value from one would show the factor by which  
Figure 48: (a) Y coil 3-degree polyfit IMU offsets. (b) Z coil 3-degree polyfit IMU offsets. the polyfit would have to flatten, the correlation coefficient (\(C_{Correlation}\) ). See Equation (12) below. Here \(offset(PWM(255)\,battVolts)\) represents an interpolated instantaneous offset (based on battery voltage), and \(offset(PWM(255)\,battVolts))\) represents the offset at full battery (which is used to normalize). \[C_{Correlation}\ =1-\frac{offset(PWM(255),\ \ 4.2\ \mathrm{V}\ )-offset(PWM(255)\,battVolts)}{offset(PWM(255),\ \ 4.2\ \mathrm{V}\ )} \tag{12}\]  
This equation uses the \"offset\" function to achieve the magnetic field offsets at the conditions in the parenthesis. The variable battVolts is the current battery voltage. Using Equation (12), all nine correlation coefficients were calculated. The resulting coefficients were then multiplied by each of the corresponding nine polyfits based on the PWM values (explained in the previous section). This method was accurate both experimentally and theoretically. If the battery is full, the correlation coefficient is 1, and nothing changes. As the battery drains, the coefficient is adjusted according to the difference in the offsets measured experimentally. If the battery voltage was zero, each of the polyfits would be zero, as expected. The polyfit approach is therefore much more reliable and robust than a simple linear subtraction. To test the soft iron solution, offsets were predicted using the model with random PWM inputs. The IMU offsets matched the predictions. Therefore, the battery model satisfied requirement 6.1 in Table 5.  
Hard iron offsets are the effects that the electronics on the CubeSat have on the magnetometer readings. These are static magnetic fields that exist whenever the satellite is on. To find the hard iron offsets, a procedure from Adafruit was followed ([PERSON], Adafruit, 1 June 2020). A detailed procedure can be found in the references. To find the hard iron offsets, the IMU had to be on. The IMU was started using a sample Arduino script from the Adafruit sensor library. Running the script turned on both the gyroscope and the accelerometer. With both, the IMU could know how it was being moved relative to the gravitational field.