diff --git a/MATLAB/momentOfInertia/getAverageMomentOfInertia.m b/MATLAB/momentOfInertia/getAverageMomentOfInertia.m new file mode 100644 index 0000000000000000000000000000000000000000..a55ab871e4ebf791079cd70fa578404b71dbcb47 --- /dev/null +++ b/MATLAB/momentOfInertia/getAverageMomentOfInertia.m @@ -0,0 +1,40 @@ +function averageMOI = ... + getAverageMomentOfInertia( directoryLocation, massUsed_grams_array ) + %Given a directory of data labeled "Test_1", "Test_2", etc, will find + %the moment of inertia for each data set, using the mass used in grams + %at the corresponding index of the massUsed_grams_array. And from + %finding all of the moments of inertia, in this way, this function will + %return the average. + + %First find the number of tests we need to run (will be equal to the + %length of the massUsed_grams_array) + numRuns = length(massUsed_grams_array); + + %Create a sum variable, to which we will add our calculated MOI values: + sumOfMOIs = 0; + + %For each of the runs, calculate the moment of inertia + for i = 1:numRuns + + %Create the file name for the current run, in string: + currFileName = ['Test_', num2str(i), '.csv']; + + %Create the full file path for the current run: + currFullFilePath = [directoryLocation, '\', currFileName]; + + %Get the current mass used in grams + massUsed_grams = massUsed_grams_array(i); + + %Get the moment of inertia for this run + curr_MOI = getMomentOfInertia(currFullFilePath, massUsed_grams); + + %Add this run's moment of inertia value calculated to the sum + sumOfMOIs = sumOfMOIs + curr_MOI; + + end %for i = 1:numRuns + + %Get the average moment of inertia value, by dividing by the total + %number of runs + averageMOI = sumOfMOIs / numRuns; + +end diff --git a/MATLAB/momentOfInertia/getMomentOfInertia.m b/MATLAB/momentOfInertia/getMomentOfInertia.m new file mode 100644 index 0000000000000000000000000000000000000000..8b8dfd156e9018cbe152192a9337d95d301bb485 --- /dev/null +++ b/MATLAB/momentOfInertia/getMomentOfInertia.m @@ -0,0 +1,96 @@ +function momentOfInertia = getMomentOfInertia( fullFilePath, massUsed_grams ) +%This function will calculate a single moment of inertia value, given a +%file location to a csv file, along with the mass value used on the mass +%handle as a torque. (Note, there's no need to have to consider the mass of +%the handle, which is 5.0 grams, since this function takes care of that) + + %fileLocation = 'C:\Users\Tara\Desktop\Project Documents\Current Project Documents\EE 491\Data\Physics Department Measurements\Test\'; + %fileName = 'Test_1.csv'; + + %Mass used in grams + %mass_grams = 200.1; + massHandle_grams = 5.0; + totalMass_grams = massUsed_grams + massHandle_grams; + totalMass_kg = totalMass_grams / 1000.0; + + %Define a cut-off start angle in radians + angleStart_cutoff = 0.1; + + %Radius of the knob where the string was pulling from (in meters) + radius = 0.04445; %(4.445 centimeters - or 1.75 inches) + + %Acceleration constant due to gravity (in meters per second squared) + g = 9.80665; + + %Define a cut-off "padding" to stop taking data before reaching the end of + %the data recorded in the file + %For example, if dataEndPadding is 10, we will "cut-off" our data when we + %get to 10 data points from the very end of the data run + dataEndPadding = 5; + + %Define columns of the table + timeCol = 1; + angleCol = 2; + angularVelCol = 3; + %angularAccelCol = 4; + + %Create full file path + %fullFilePath = [fileLocation, fileName]; + + %Bring in Data + dataTable = readtable(fullFilePath); + + %Extract the time, angle (in radians), and angular velocity (in radians per + %second) arrays from the data table + timeArray = dataTable{:, timeCol}; + angleArray = dataTable{:, angleCol}; + angularVelArray = dataTable{:, angularVelCol}; + + %Find the first positive angle value + i_start = find(angleArray > angleStart_cutoff, 1); + + %Get the start time and start angular position, based on this start index + time_start = timeArray(i_start); + angle_start = angleArray(i_start); + + %Find the initial angular velocity, which is the corresponding angular + %velocity value at this start index (in radians per second) + w_o = angularVelArray(i_start); + + %Determine the end time of our data collection, so first get the length of + %the time array + timeArray_len = length(timeArray); + + %Get the last index based on the length of the data and the "padding" + %amount defined at the beginning + i_end = timeArray_len - dataEndPadding; + + %Get the end time and end angular position in radians + time_end = timeArray(i_end); + angle_end = angleArray(i_end); + + %Get the difference in time and angular position (in radians) + angle_diff = angle_end - angle_start; + time_diff = time_end - time_start; + + %Using this expression, where alpha is the constant angular acceleration: + % theta_diff = w_o*t_diff + (1/2)*alpha*(t_diff^2) + %We can solve for alpha to get + % (theta_diff - w_o*t_diff) = (1/2)*alpha*(t_diff^2) + % 2*(theta_diff - w_o*t_diff) / (t_diff^2) = alpha + + %Thus the angular acceleration in radians per second-squared, denoted by + %alpha can be found like this: + angularAccel = 2*(angle_diff - w_o*time_diff) / (time_diff * time_diff); + + %This constant angular acceleration should be due to the torque caused by + %the gravitational force caused by our falling mass (m*g*r) + torque_gravity = totalMass_kg * g * radius; + + %The moment of inertia would be this torque divided by the constant angular + %acceleration value (in kilograms meters-squared) + momentOfInertia = torque_gravity / angularAccel; + + +end + diff --git a/MATLAB/momentOfInertia/getMomentOfInertiaFinalData.m b/MATLAB/momentOfInertia/getMomentOfInertiaFinalData.m new file mode 100644 index 0000000000000000000000000000000000000000..c3667674d5d3acdd782631822a05cb4fb3013d67 --- /dev/null +++ b/MATLAB/momentOfInertia/getMomentOfInertiaFinalData.m @@ -0,0 +1,44 @@ +%Create the massesUsed_grams_array, which represents the masses we used for +%each run, which we keep the same for the runs done for each of the +%testing (yaw, pitch, roll, and their calibration tests as well) +massesUsed_grams = [200.1, 300.1, 100.1]; +numTrialsPerMass = 5; +massUsed_grams_array = [ ones(1, numTrialsPerMass) * massesUsed_grams(1), ... + ones(1, numTrialsPerMass) * massesUsed_grams(2), ... + ones(1, numTrialsPerMass) * massesUsed_grams(3)]; + +%Set the directory locations for the pitch, roll, and yaw data and their +%calibration runs +topLevelDir = 'C:\Users\Tara\Desktop\Project Documents\Current Project Documents\EE 491\Data\Physics Department Measurements\Moment of Inertia Data'; +directoryLocation_yawCalib = [topLevelDir , '\Calibration Testing Yaw']; +directoryLocation_pitchCalib = [topLevelDir, '\Calibration Testing Pitch and Roll']; +directoryLocation_rollCalib = [topLevelDir, '\Calibration Testing Pitch and Roll']; +directoryLocation_yawData = [topLevelDir, '\Yaw Data']; +directoryLocation_pitchData = [topLevelDir, '\Pitch Data']; +directoryLocation_rollData = [topLevelDir, '\Roll Data']; + +%Get the average moment of inertia values for the pitch, roll, and yaw data +%as well as their calibration funs +averageMOI_yawCalib = getAverageMomentOfInertia( ... + directoryLocation_yawCalib, massUsed_grams_array ); + +averageMOI_pitchCalib = getAverageMomentOfInertia( ... + directoryLocation_pitchCalib, massUsed_grams_array ); + +averageMOI_rollCalib = getAverageMomentOfInertia( ... + directoryLocation_rollCalib, massUsed_grams_array ); + +averageMOI_yawData = getAverageMomentOfInertia( ... + directoryLocation_yawData, massUsed_grams_array ); + +averageMOI_pitchData = getAverageMomentOfInertia( ... + directoryLocation_pitchData, massUsed_grams_array ); + +averageMOI_rollData = getAverageMomentOfInertia( ... + directoryLocation_rollData, massUsed_grams_array ); + +%Now determine the quadcopter's average moment of inertia about the pitch, +%roll, and yaw axes of rotation +quadYaw = averageMOI_yawData - averageMOI_yawCalib +quadPitch = averageMOI_pitchData - averageMOI_pitchCalib +quadRoll = averageMOI_rollData - averageMOI_rollCalib \ No newline at end of file