Golf Club- Code1



For this project, both MatLab and the Arduino software was utilized. The code for each and a brief description are given below.

Arduino Code

There were three primary functions for the Arduino in this project.

1.) Analyze readings from Laser System

2.) Operate a Finite State Machine (FSM)

3.) Analye ADXL-335 Accelerometer

For the most part, the analysis processes are rather simple. The Arduino is a fundamentally user friendly microcontroller when it comes to using the digital- and analog- read functions.

An example of the analogRead used for the ADXL-335 is listed below:

        x_base = analogRead(xpin);
        y_base = analogRead(ypin);
        z_base = analogRead(zpin);

As can be seen, all that needs to be done to assign three variables to the analog values of the three axis is use one simple function command.

The digitalRead function operates in the same fashion:

      if (digitalRead(Tee_Laser) == LOW);
      state = 3;

Now, the FSM aspect of the code is slightly more complex. Progressing from one state to another based on specified input, I was able to create a system which went through 8 stages to optimally operate the system:

  1.) Zero the Accelerometer (User holds club still in Tee_Laser beam to stabilize)
  2.) Set base values
  3.) Wait for club to leave the Tee_Laser path
  4.) Begin recording the readings from the accelerometer
  5.) When club crosses Tee_Laser, begin a timer
  6.) Continue recording readings
  7.) Release_Laser is tripped, swing is complete, stop recordings
  8.) Calculate speed, print speed

The full Arduino code used can be found here!

An important fact to note is that I utilized a C-style printf function, which is not native to the Arduino. The reason for this was that it is able to print multiple integers in a string at a much faster rate than the typical Serial.print function of the Arduino. Many thanks go out to Mark Carrol for help on this part.

MatLab Code

The MatLab portion of this project was divided up into two parts: Data.m & createGolf_Fit.m. The Data.m portion is extremely short as all it does is call the Excel Spreadsheet in which the Arduino data was manually saved to and assigns the columns to array variables. Once this is done, it sends these arrays as arguments to the createGolf_Fit.m function file.

     mat_x = xlsread('Golf_Club', 'C:C');
     mat_y = xlsread('Golf_Club', 'D:D');
     mat_z = xlsread('Golf_Club', 'E:E');
     mat_HIT = xlsread ('Golf_Club', 'B:B');
     mat_time = xlsread('Golf_Club', 'A:A');

     createGolf_Fit(mat_time, mat_x, mat_y, mat_z, mat_HIT);

As can be seen, the Excel Spreadsheet must be save as Golf_Club in the MatLab directory.

createGolf_Fit.m is a file which takes the arrays and plots them. On top of this, though, I wanted to generate function line curves which could then be processed and analyzed using MatLab's integrating tools. The entirety of this code can be found here! An important item to note in regards to this code is that it is generated by MatLab itself. One of the useful features of MatLab is that if you use a tool, such as the custom fit tool bar (cftool in MatLab prompt), and run a sequence of commands manually, at the end of your session, you can ask MatLab to create a function file that automatically does whatever it was that you did in that session. I utilized this feature for this project here. While I did make some changes to the code MatLab produced in order to make it more presentable and flexible to different data ranges, it is still nearly entirely MatLab's, so this should be noted.