#+TITLE: Flexible Joints - Test Bench
:DRAWER:
#+LANGUAGE: en
#+EMAIL: dehaeze.thomas@gmail.com
#+AUTHOR: Dehaeze Thomas

#+HTML_LINK_HOME: ../index.html
#+HTML_LINK_UP:   ../index.html

#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="https://research.tdehaeze.xyz/css/style.css"/>
#+HTML_HEAD: <script type="text/javascript" src="https://research.tdehaeze.xyz/js/script.js"></script>

#+BIND: org-latex-image-default-option "scale=1"
#+BIND: org-latex-image-default-width ""

#+LaTeX_CLASS: scrreprt
#+LaTeX_CLASS_OPTIONS: [a4paper, 10pt, DIV=12, parskip=full]
#+LaTeX_HEADER_EXTRA: \input{preamble.tex}

#+PROPERTY: header-args:matlab  :session *MATLAB*
#+PROPERTY: header-args:matlab+ :comments org
#+PROPERTY: header-args:matlab+ :exports both
#+PROPERTY: header-args:matlab+ :results none
#+PROPERTY: header-args:matlab+ :eval no-export
#+PROPERTY: header-args:matlab+ :noweb yes
#+PROPERTY: header-args:matlab+ :mkdirp yes
#+PROPERTY: header-args:matlab+ :output-dir figs

#+PROPERTY: header-args:latex  :headers '("\\usepackage{tikz}" "\\usepackage{import}" "\\import{$HOME/Cloud/tikz/org/}{config.tex}")
#+PROPERTY: header-args:latex+ :imagemagick t :fit yes
#+PROPERTY: header-args:latex+ :iminoptions -scale 100% -density 150
#+PROPERTY: header-args:latex+ :imoutoptions -quality 100
#+PROPERTY: header-args:latex+ :results file raw replace
#+PROPERTY: header-args:latex+ :buffer no
#+PROPERTY: header-args:latex+ :tangle no
#+PROPERTY: header-args:latex+ :eval no-export
#+PROPERTY: header-args:latex+ :exports results
#+PROPERTY: header-args:latex+ :mkdirp yes
#+PROPERTY: header-args:latex+ :output-dir figs
#+PROPERTY: header-args:latex+ :post pdf2svg(file=*this*, ext="png")
:END:

#+begin_export html
  <hr>
  <p>This report is also available as a <a href="./test-bench-flexible-joints.pdf">pdf</a>.</p>
  <hr>
#+end_export

* Introduction                                                        :ignore:
In this document, we present a test-bench that has been developed in order to measure the bending stiffness of flexible joints.

It is structured as follow:
- Section [[sec:flexible_joints]]: the geometry of the flexible joints and the expected stiffness and stroke are presented
- Section [[sec:test_bench_desc]]: the measurement bench is presented
- Section [[sec:error_budget]]: an error budget is performed in order to estimate the accuracy of the measured stiffness
- Section [[sec:first_measurements]]: first measurements are performed
- Section [[sec:bending_stiffness_meas]]: the bending stiffness of the flexible joints are measured

* Flexible Joints
<<sec:flexible_joints>>

The flexible joints that are going to be measured in this document have been design to be used with a Nano-Hexapod (Figure [[fig:nano_hexapod]]).

#+name: fig:nano_hexapod
#+caption: CAD view of the Nano-Hexapod containing the flexible joints
#+attr_latex: :width 0.7\linewidth
[[file:figs/nano_hexapod.png]]

Ideally, these flexible joints would behave as perfect ball joints, that is to say:
- no bending and torsional stiffnesses
- infinite shear and axial stiffnesses
- un-limited bending and torsional stroke
- no friction, no backlash


The real characteristics of the flexible joints will influence the dynamics of the Nano-Hexapod.
Using a multi-body dynamical model of the nano-hexapod, the specifications in term of stiffness and stroke of the flexible joints have been determined and summarized in Table [[tab:flexible_joints_specs]].

#+name: tab:flexible_joints_specs
#+caption: Specifications for the flexible joints and estimated characteristics from the Finite Element Model
#+attr_latex: :environment tabularx :width 0.5\linewidth :align Xcc
#+attr_latex: :center t :booktabs t :float t
|                   | *Specification* | *FEM* |
|-------------------+-----------------+-------|
| Axial Stiffness   | > 100 [N/um]    |    94 |
| Shear Stiffness   | > 1 [N/um]      |    13 |
| Bending Stiffness | < 100 [Nm/rad]  |     5 |
| Torsion Stiffness | < 500 [Nm/rad]  |   260 |
| Bending Stroke    | > 1 [mrad]      |  24.5 |
| Torsion Stroke    | > 5 [urad]      |       |

Then, the classical geometry of a flexible ball joint shown in Figure [[fig:flexible_joint_fem_geometry]] has been optimized in order to meet the requirements.
This has been done using a Finite Element Software and the obtained joint's characteristics are summarized in Table [[tab:flexible_joints_specs]].

#+name: fig:flexible_joint_fem_geometry
#+caption: Flexible part of the Joint used for FEM - CAD view
#+attr_latex: :width 0.5\linewidth
[[file:figs/flexible_joint_fem_geometry.png]]

The obtained geometry are defined in the [[file:doc/flex_joints.pdf][drawings of the flexible joints]].
The material is a special kind of stainless steel called "F16PH"..

* Measurement Test Bench - Bending Stiffness
:PROPERTIES:
:header-args:matlab+: :tangle ./matlab/bench_dimensioning.m
:END:
<<sec:test_bench_desc>>
** Introduction                                                      :ignore:
The most important characteristic of the flexible joint that we want to measure is its bending stiffness $k_{R_x} \approx k_{R_y}$.

To do so, we have to apply a torque $T_x$ on the flexible joint and measure its angular deflection $\theta_x$.
The stiffness is then
\begin{equation}
k_{R_x} = \frac{T_x}{\theta_x}
\end{equation}

As it is quite difficult to apply a pure torque, a force will be applied instead.
The application point of the force should far enough from the flexible part such that the obtained bending is much larger than the displacement in shear.

The working principle of the bench is schematically shown in Figure [[fig:test_bench_principle]].
One part of the flexible joint is fixed. On the mobile part, a force $F_x$ is applied which is equivalent to a torque applied on the flexible joint center.
The induced rotation is measured with a displacement sensor $d_x$.

#+name: fig:test_bench_principle
#+caption: Test Bench - working principle
[[file:figs/test_bench_principle.png]]


This test-bench will be used to have a first approximation of the bending stiffnesss and stroke of the flexible joints.
Another test-bench, better engineered will be used to measure the flexible joint's characteristics with better accuracy.

** Matlab Init                                              :noexport:ignore:
#+begin_src matlab :tangle no :exports none :results silent :noweb yes :var current_dir=(file-name-directory buffer-file-name)
<<matlab-dir>>
#+end_src

#+begin_src matlab :exports none :results silent :noweb yes
<<matlab-init>>
#+end_src

** Flexible joint Geometry
The flexible joint used for the Nano-Hexapod is shown in Figure [[fig:flexible_joint_geometry]].
Its bending stiffness is foreseen to be $k_{R_y}\approx 5\,\frac{Nm}{rad}$ and its stroke $\theta_{y,\text{max}}\approx 25\,mrad$.

#+name: fig:flexible_joint_geometry
#+caption: Geometry of the flexible joint
[[file:figs/flexible_joint_geometry.png]]

The height between the flexible point (center of the joint) and the point where external forces are applied is $h = 20\,mm$.

Let's define the parameters on Matlab.
#+begin_src matlab
kRx = 5; % Bending Stiffness [Nm/rad]
Rxmax = 25e-3; % Bending Stroke [rad]
h = 20e-3; % Height [m]
#+end_src

** Required external applied force

The bending $\theta_y$ of the flexible joint due to the force $F_x$ is:
\begin{equation}
  \theta_y = \frac{M_y}{k_{R_y}} = \frac{F_x h}{k_{R_y}}
\end{equation}

Therefore, the applied force to test the full range of the flexible joint is:
\begin{equation}
  F_{x,\text{max}} = \frac{k_{R_y} \theta_{y,\text{max}}}{h}
\end{equation}

#+begin_src matlab
Fxmax = kRx*Rxmax/h; % Force to induce maximum stroke [N]
#+end_src

And we obtain:
#+begin_src matlab :results value raw replace :exports results
sprintf('\\begin{equation} F_{x,max} = %.1f\\, [N] \\end{equation}', Fxmax)
#+end_src

#+RESULTS:
\begin{equation} F_{x,max} = 6.2\, [N] \end{equation}

The measurement range of the force sensor should then be higher than $6.2\,N$.

** Required actuator stroke and sensors range

The flexible joint is designed to allow a bending motion of $\pm 25\,mrad$.
The corresponding stroke at the location of the force sensor is:
\[ d_{x,\text{max}} = h \tan(R_{x,\text{max}}) \]

#+begin_src matlab
dxmax = h*tan(Rxmax);
#+end_src

#+begin_src matlab :results value raw replace :exports results
sprintf('\\begin{equation} d_{max} = %.1f\\, [mm] \\end{equation}', 1e3*dxmax)
#+end_src

#+RESULTS:
\begin{equation} d_{max} = 0.5\, [mm] \end{equation}

In order to test the full range of the flexible joint, the stroke of the translation stage used to move the force sensor should be higher than $0.5\,mm$.
Similarly, the measurement range of the displacement sensor should also be higher than $0.5\,mm$.

** Test Bench

A CAD view of the measurement bench is shown in Figure [[fig:test_bench_flex_overview]].

#+begin_note
Here are the different elements used in this bench:
- *Translation Stage*: [[file:doc/V-408-Datasheet.pdf][V-408]]
- *Load Cells*: [[file:doc/A700000007147087.pdf][FC2231-0000-0010-L]]
- *Encoder*: [[file:doc/L-9517-9448-05-B_Data_sheet_RESOLUTE_BiSS_en.pdf][Renishaw Resolute 1nm]]
#+end_note

Both the measured force and displacement are acquired at the same time using a Speedgoat machine.

#+name: fig:test_bench_flex_overview
#+caption: Schematic of the test bench to measure the bending stiffness of the flexible joints
#+attr_latex: :width 0.8\linewidth
[[file:figs/test_bench_flex_overview.png]]

A side view of the bench with the important quantities are shown in Figure [[fig:test_bench_flex_side]].

#+name: fig:test_bench_flex_side
#+caption: Schematic of the test bench to measure the bending stiffness of the flexible joints
#+attr_latex: :width 0.25\linewidth
#+attr_html: :width 300px
[[file:figs/test_bench_flex_side.png]]

* Error budget
:PROPERTIES:
:header-args:matlab+: :tangle ./matlab/error_budget.m
:END:
<<sec:error_budget>>
** Introduction                                                      :ignore:

Many things can impact the accuracy of the measured bending stiffness such as:
- Errors in the force and displacement measurement
- Shear effects
- Deflection of the Force sensor
- Errors in the geometry of the bench

In this section, we wish to estimate the attainable accuracy with the current bench, and identified the limiting factors.


** Matlab Init                                             :noexport:ignore:
#+begin_src matlab :tangle no :exports none :results silent :noweb yes :var current_dir=(file-name-directory buffer-file-name)
<<matlab-dir>>
#+end_src

#+begin_src matlab :exports none :results silent :noweb yes
<<matlab-init>>
#+end_src

** Finite Element Model
From the Finite Element Model, the stiffness and stroke of the flexible joint have been computed and summarized in Tables [[tab:axial_shear_characteristics]] and [[tab:bending_torsion_characteristics]].

#+begin_src matlab :exports none
%% Stiffness
ka = 94e6; % Axial Stiffness [N/m]
ks = 13e6; % Shear Stiffness [N/m]
kb = 5;    % Bending Stiffness [Nm/rad]
kt = 260;  % Torsional Stiffness [Nm/rad]

%% Maximum force
Fa = 469;    % Axial Force before yield [N]
Fs = 242;    % Shear Force before yield [N]
Fb = 0.118;  % Bending Force before yield [Nm]
Ft = 1.508; % Torsional Force before yield [Nm]

%% Compute the corresponding stroke
Xa = Fa/ka; % Axial Stroke before yield [m]
Xs = Fs/ks; % Shear Stroke before yield [m]
Xb = Fb/kb; % Bending Stroke before yield [rad]
Xt = Ft/kt; % Torsional Stroke before yield [rad]
#+end_src

#+begin_src matlab :exports results :results value table replace :tangle no :post addhdr(*this*)
data2orgtable([1e-6*ka, Fa, 1e6*Xa; 1e-6*ks, Fs, 1e6*Xs], {'Axial', 'Shear'}, {'Stiffness [N/um]', 'Max Force [N]', 'Stroke [um]'}, ' %.0f ');
#+end_src

#+name: tab:axial_shear_characteristics
#+caption: Axial/Shear characteristics
#+attr_latex: :environment tabularx :width 0.6\linewidth :align Xccc
#+attr_latex: :center t :booktabs t :float t
#+RESULTS:
|       | Stiffness [N/um] | Max Force [N] | Stroke [um] |
|-------+------------------+---------------+-------------|
| Axial |               94 |           469 |           5 |
| Shear |               13 |           242 |          19 |

#+begin_src matlab :exports results :results value table replace :tangle no :post addhdr(*this*)
data2orgtable([kb, 1e3*Fb, 1e3*Xb; kt, 1e3*Ft, 1e3*Xt], {'Bending', 'Torsional'}, {'Stiffness [Nm/rad]', 'Max Torque [Nmm]', 'Stroke [mrad]'}, ' %.0f ');
#+end_src

#+name: tab:bending_torsion_characteristics
#+caption: Bending/Torsion characteristics
#+attr_latex: :environment tabularx :width 0.7\linewidth :align Xccc
#+attr_latex: :center t :booktabs t :float t
#+RESULTS:
|           | Stiffness [Nm/rad] | Max Torque [Nmm] | Stroke [mrad] |
|-----------+--------------------+------------------+---------------|
| Bending   |                  5 |              118 |            24 |
| Torsional |                260 |             1508 |             6 |

** Setup

The setup is schematically represented in Figure [[fig:test_bench_flex_side_bis]].

The force is applied on top of the flexible joint with a distance $h$ with the joint's center.
The displacement of the flexible joint is also measured at the same height.

The height between the joint's center and the force application point is:
#+begin_src matlab
h = 25e-3; % Height [m]
#+end_src

#+name: fig:test_bench_flex_side_bis
#+caption: Schematic of the test bench to measure the bending stiffness of the flexible joints
#+attr_latex: :width 0.25\linewidth
#+attr_html: :width 300px
[[file:figs/test_bench_flex_side.png]]

** Effect of Bending
The torque applied is:
\begin{equation}
  M_y = F_x \cdot h
\end{equation}

The flexible joint is experiencing a rotation $\theta_y$ due to the torque $M_y$:
\begin{equation}
  \theta_y = \frac{M_y}{k_{R_y}} = \frac{F_x \cdot h}{k_{R_y}}
\end{equation}

This rotation is then measured by the displacement sensor.
The measured displacement is:
\begin{equation}
  D_b = h \tan(\theta_y) = h \tan\left( \frac{F_x \cdot h}{k_{R_y}} \right) \label{eq:bending_stiffness_formula}
\end{equation}

** Computation of the bending stiffness
From equation eqref:eq:bending_stiffness_formula, we can compute the bending stiffness:
\begin{equation}
  k_{R_y} = \frac{F_x \cdot h}{\tan^{-1}\left( \frac{D_b}{h} \right)}
\end{equation}

For small displacement, we have
\begin{equation}
  \boxed{k_{R_y} \approx h^2 \frac{F_x}{d_x}}
\end{equation}

And therefore, to precisely measure $k_{R_y}$, we need to:
- precisely measure the motion $d_x$
- precisely measure the applied force $F_x$
- precisely now the height of the force application point $h$

** Estimation error due to force and displacement sensors accuracy
The maximum error on the measured displacement with the encoder is 40 nm.
This quite negligible compared to the measurement range of 0.5 mm.

The accuracy of the force sensor is around 1% and therefore, we should expect to have an accuracy on the measured stiffness of at most 1%.

** Estimation error due to Shear
The effect of Shear on the measured displacement is simply:
\begin{equation}
  D_s = \frac{F_x}{k_s}
\end{equation}

The measured displacement will be the effect of shear + effect of bending
\begin{equation}
  d_x = D_b + D_s = h \tan\left( \frac{F_x \cdot h}{k_{R_y}} \right) + \frac{F_x}{k_s} \approx F_x \left( \frac{h^2}{k_{R_y}} + \frac{1}{k_s} \right)
\end{equation}

The estimated bending stiffness $k_{\text{est}}$ will then be:
\begin{equation}
  k_{\text{est}} = h^2 \frac{F_x}{d_x} \approx k_{R_y} \frac{1}{1 + \frac{k_{R_y}}{k_s h^2}}
\end{equation}

#+begin_src matlab :results value replace :exports results :tangle no
sprintf('The measurement error due to Shear is %.1f %%', 100*abs(1-1/(1 + kb/(ks*h^2))))
#+end_src

#+RESULTS:
: The measurement error due to Shear is 0.1 %

** Estimation error due to force sensor compression
The measured displacement is not done directly at the joint's location.
The force sensor compression will then induce an error on the joint's stiffness.

The force sensor stiffness $k_F$ is estimated to be around:
#+begin_src matlab
kF = 50/0.05e-3; % [N/m]
#+end_src

#+begin_src matlab :results value replace :exports results
sprintf('k_F = %.1e [N/m]', kF)
#+end_src

#+RESULTS:
: k_F = 1.0e+06 [N/m]

The measured displacement will be the sum of the displacement induced by the bending and by the compression of the force sensor:
\begin{equation}
  d_x = D_b + \frac{F_x}{k_F} = h \tan\left( \frac{F_x \cdot h}{k_{R_y}} \right) + \frac{F_x}{k_F} \approx F_x \left( \frac{h^2}{k_{R_y}} + \frac{1}{k_F} \right)
\end{equation}

The estimated bending stiffness $k_{\text{est}}$ will then be:
\begin{equation}
  k_{\text{est}} = h^2 \frac{F_x}{d_x} \approx k_{R_y} \frac{1}{1 + \frac{k_{R_y}}{k_F h^2}}
\end{equation}

#+begin_src matlab :results value replace :exports results :tangle no
sprintf('The measurement error due to height estimation errors is %.1f %%', 100*abs(1-1/(1 + kb/(kF*h^2))))
#+end_src

#+RESULTS:
: The measurement error due to height estimation errors is 0.8 %

** Estimation error due to height estimation error
Let's consider an error in the estimation of the height from the application of the force to the joint's center:
\begin{equation}
h_{\text{est}} = h (1 + \epsilon)
\end{equation}

The computed bending stiffness will be:
\begin{equation}
  k_\text{est} \approx h_{\text{est}}^2 \frac{F_x}{d_x}
\end{equation}

And the stiffness estimation error is:
\begin{equation}
\frac{k_{\text{est}}}{k_{R_y}} = (1 + \epsilon)^2
\end{equation}

#+begin_src matlab
h_err = 0.2e-3; % Height estimation error [m]
#+end_src

#+begin_src matlab :results value replace :exports results :tangle no
sprintf('The measurement error due to height estimation errors of %.1f [mm] is %.1f %%', 1e3*h_err, 100*abs(1-(1 + h_err/h)^2))
#+end_src

#+RESULTS:
: The measurement error due to height estimation errors of 0.2 [mm] is 1.6 %

** Conclusion
Based on the above analysis, we should expect no better than few percent of accuracy using the current test-bench.
This is well enough for a first estimation of the bending stiffness of the flexible joints.

Another measurement bench allowing better accuracy will be developed.

* First Measurements
<<sec:first_measurements>>
** Introduction                                                      :ignore:


- Section [[sec:test_meas_probe]]:
- Section [[sec:meas_probe_stiffness]]:

** Agreement between the probe and the encoder
:PROPERTIES:
:header-args:matlab+: :tangle ./matlab/probe_vs_encoder.m
:END:
<<sec:test_meas_probe>>
*** Introduction                                                    :ignore:

- *Load Cells*: [[file:doc/A700000007147087.pdf][FC2231-0000-0010-L]] (and  [[file:doc/FRE_DS_XFL212R_FR_A3.pdf][XFL212R]])
- *Encoder*: [[file:doc/L-9517-9448-05-B_Data_sheet_RESOLUTE_BiSS_en.pdf][Renishaw Resolute 1nm]]
- *Displacement Probe*: [[file:doc/Millimar--3723046--BA--C1208-C1216-C1240--FR--2016-11-08.pdf][Millimar C1216 electronics]] and [[file:doc/tmp3m0cvmue_7888038c-cdc8-48d8-a837-35de02760685.pdf][Millimar 1318 probe]]

*** Setup                                                           :ignore:
The measurement setup is made such that the probe measured the translation table displacement.
It should then measure the same displacement as the encoder.
Using this setup, we should be able to compare the probe and the encoder.

*** Matlab Init                                            :noexport:ignore:
#+begin_src matlab :tangle no :exports none :results silent :noweb yes :var current_dir=(file-name-directory buffer-file-name)
<<matlab-dir>>
#+end_src

#+begin_src matlab :exports none :results silent :noweb yes
<<matlab-init>>
#+end_src

#+begin_src matlab :tangle no
addpath('./matlab/mat/');
addpath('./matlab/');
#+end_src

#+begin_src matlab :eval no
addpath('./mat/');
#+end_src

*** Results                                                         :ignore:
Let's load the measurements.
#+begin_src matlab
load('meas_probe_against_encoder.mat', 't', 'd', 'dp', 'F')
#+end_src

#+begin_src matlab :exports none
%% Sampling time [s]
Ts = (t(end) - t(1))/(length(t)-1);

%% Remove first second
t = t(ceil(1/Ts):end);
d = -d(ceil(1/Ts):end);
dp = -dp(ceil(1/Ts):end);
F = F(ceil(1/Ts):end);
#+end_src

The time domain measured displacement by the probe and by the encoder is shown in Figure [[fig:comp_encoder_probe_time]].

#+begin_src matlab :exports none
%% Time Domain plots
figure;
hold on;
plot(t, d, 'DisplayName', 'Encoder');
plot(t, dp, 'DisplayName', 'Probe');
hold off;
xlabel('Time [s]'); ylabel('Displacement [m]');
#+end_src

#+begin_src matlab :tangle no :exports results :results file replace
exportFig('figs/comp_encoder_probe_time.pdf', 'width', 'wide', 'height', 'normal');
#+end_src

#+name: fig:comp_encoder_probe_time
#+caption: Time domain measurement
#+RESULTS:
[[file:figs/comp_encoder_probe_time.png]]

If we zoom, we see that there is some delay between the encoder and the probe (Figure [[fig:comp_encoder_probe_time_zoom]]).

#+begin_src matlab :exports none
%% Zoom
figure;
hold on;
plot(t, d, 'DisplayName', 'Encoder');
plot(t, dp, 'DisplayName', 'Probe');
hold off;
xlabel('Time [s]'); ylabel('Displacement [m]');
xlim([7.7, 7.9])
#+end_src

#+begin_src matlab :tangle no :exports results :results file replace
exportFig('figs/comp_encoder_probe_time_zoom.pdf', 'width', 'wide', 'height', 'normal');
#+end_src

#+name: fig:comp_encoder_probe_time_zoom
#+caption: Time domain measurement (Zoom)
#+RESULTS:
[[file:figs/comp_encoder_probe_time_zoom.png]]

This delay is estimated using the =finddelay= command.

#+begin_src matlab :results value replace :exports results :tangle no
sprintf('The time delay is approximately %.1f [ms]', 1e3*Ts*finddelay(d, dp))
#+end_src

#+RESULTS:
: The time delay is approximately 15.8 [ms]

The measured mismatch between the encoder and the probe with and without compensating for the time delay are shown in Figure [[fig:comp_encoder_probe_mismatch]].

#+begin_src matlab :exports none
figure;
hold on;
plot(t, d-dp, 'DisplayName', 'Raw Mismatch');
plot(t(1:end-finddelay(d, dp)), d(1:end-finddelay(d, dp))-dp(finddelay(d, dp)+1:end), 'DisplayName', 'Removed Delay');
hold off;
xlabel('Time [s]'); ylabel('Measurement Missmatch [m]');
#+end_src

#+begin_src matlab :tangle no :exports results :results file replace
exportFig('figs/comp_encoder_probe_mismatch.pdf', 'width', 'wide', 'height', 'normal');
#+end_src

#+name: fig:comp_encoder_probe_mismatch
#+caption: Measurement mismatch, with and without delay compensation
#+RESULTS:
[[file:figs/comp_encoder_probe_mismatch.png]]

Finally, the displacement of the probe is shown as a function of the displacement of the encoder and a linear fit is made (Figure [[fig:comp_encoder_probe_linear_fit]]).

#+begin_src matlab :exports none
figure;
hold on;
plot(1e3*d, 1e3*dp, 'DisplayName', 'Raw data');
plot(1e3*d, 1e3*d*(d\dp), 'DisplayName', sprintf('Linear fit: $\\alpha = %.5f$', (d\dp)));
hold on;
xlabel('Encoder [mm]'); ylabel('Probe [mm]');
legend('location', 'southeast')
#+end_src

#+begin_src matlab :tangle no :exports results :results file replace
exportFig('figs/comp_encoder_probe_linear_fit.pdf', 'width', 'normal', 'height', 'normal');
#+end_src

#+name: fig:comp_encoder_probe_linear_fit
#+caption: Measured displacement by the probe as a function of the measured displacement by the encoder
#+RESULTS:
[[file:figs/comp_encoder_probe_linear_fit.png]]

#+begin_important
From the measurement, it is shown that the probe is well calibrated.
However, there is some time delay of tens of milliseconds that could induce some measurement errors.
#+end_important

** Measurement of the Millimar 1318 probe stiffness
:PROPERTIES:
:header-args:matlab+: :tangle ./matlab/probe_stiffness.m
:END:
<<sec:meas_probe_stiffness>>
*** Introduction                                                    :ignore:

#+begin_note
- *Translation Stage*: [[file:doc/V-408-Datasheet.pdf][V-408]]
- *Load Cell*: [[file:doc/A700000007147087.pdf][FC2231-0000-0010-L]]
- *Encoder*: [[file:doc/L-9517-9448-05-B_Data_sheet_RESOLUTE_BiSS_en.pdf][Renishaw Resolute 1nm]]
- *Displacement Probe*: [[file:doc/Millimar--3723046--BA--C1208-C1216-C1240--FR--2016-11-08.pdf][Millimar C1216 electronics]] and [[file:doc/tmp3m0cvmue_7888038c-cdc8-48d8-a837-35de02760685.pdf][Millimar 1318 probe]]
#+end_note

#+name: fig:setup_mahr_stiff_meas_side
#+caption: Setup - Side View
#+attr_latex: :width \linewidth
[[file:figs/setup_mahr_stiff_meas_side.jpg]]

#+name: fig:setup_mahr_stiff_meas_top
#+caption: Setup - Top View
#+attr_latex: :width \linewidth
[[file:figs/setup_mahr_stiff_meas_top.jpg]]

*** Matlab Init                                            :noexport:ignore:
#+begin_src matlab :tangle no :exports none :results silent :noweb yes :var current_dir=(file-name-directory buffer-file-name)
<<matlab-dir>>
#+end_src

#+begin_src matlab :exports none :results silent :noweb yes
<<matlab-init>>
#+end_src

#+begin_src matlab :tangle no
addpath('./matlab/mat/');
addpath('./matlab/');
#+end_src

#+begin_src matlab :eval no
addpath('./mat/');
#+end_src

*** Results                                                         :ignore:
Let's load the measurement results.
#+begin_src matlab
load('meas_stiff_probe.mat', 't', 'd', 'dp', 'F')
#+end_src

#+begin_src matlab :exports none
%% Sampling time [s]
Ts = (t(end) - t(1))/(length(t)-1);

%% Remove first second
t = t(ceil(1/Ts):end);
d = d(ceil(1/Ts):end);
dp = dp(ceil(1/Ts):end);
F = F(ceil(1/Ts):end);


%% Remove Offset
t = t - t(1);
F = F - mean(F(1:ceil(1/Ts)));
#+end_src

The time domain measured force and displacement are shown in Figure [[fig:mahr_time_domain]].

#+begin_src matlab :exports none
%% Time Domain plots
figure;
tiledlayout(2, 1, 'TileSpacing', 'None', 'Padding', 'None');

ax1 = nexttile;
plot(t, F);
ylabel('Force [N]'); set(gca, 'XTickLabel',[]);

ax2 = nexttile;
plot(t, d);
xlabel('Time [s]'); ylabel('Displacement [m]');
#+end_src

#+begin_src matlab :tangle no :exports results :results file replace
exportFig('figs/mahr_time_domain.pdf', 'width', 'wide', 'height', 'tall');
#+end_src

#+name: fig:mahr_time_domain
#+caption: Time domain measurements
#+RESULTS:
[[file:figs/mahr_time_domain.png]]


Now we can estimate the stiffness with a linear fit.

#+begin_src matlab :results value replace :exports results :tangle no
sprintf('Stiffness is %.3f [N/mm]', abs(1e-3*(d\F)))
#+end_src

This is very close to the 0.04 [N/mm] written in the [[file:doc/tmp3m0cvmue_7888038c-cdc8-48d8-a837-35de02760685.pdf][Millimar 1318 probe datasheet]].

And compare the linear fit with the raw measurement data (Figure [[fig:mahr_stiffness_f_d_plot]]).

#+begin_src matlab :exports none
figure;
hold on;
plot(F, d, 'DisplayName', 'Raw data');
plot(F, F/(d\F), 'DisplayName', 'Linear fit');
hold off;
xlabel('Measured Force [N]');
ylabel('Measured Displacement [m]');
legend('location', 'southeast');
#+end_src

#+begin_src matlab :tangle no :exports results :results file replace
exportFig('figs/mahr_stiffness_f_d_plot.pdf', 'width', 'wide', 'height', 'normal');
#+end_src

#+name: fig:mahr_stiffness_f_d_plot
#+caption: Measured displacement as a function of the measured force. Raw data and linear fit
#+RESULTS:
[[file:figs/mahr_stiffness_f_d_plot.png]]

#+begin_summary
The Millimar 1318 probe has a stiffness of $\approx 0.04\,[N/mm]$.
#+end_summary

* Bending Stiffness Measurement
<<sec:bending_stiffness_meas>>
** Matlab Init                                              :noexport:ignore:
#+begin_src matlab :tangle no :exports none :results silent :noweb yes :var current_dir=(file-name-directory buffer-file-name)
<<matlab-dir>>
#+end_src

#+begin_src matlab :exports none :results silent :noweb yes
<<matlab-init>>
#+end_src

#+begin_src matlab :tangle no
addpath('./matlab/mat/');
addpath('./matlab/');
#+end_src

#+begin_src matlab :eval no
addpath('./mat/');
#+end_src

** Results
#+begin_src matlab
load('meas_stiff_flex_12_x.mat', 't', 'd', 'F');
#+end_src

#+begin_src matlab
d = d(t > 5.35 & t < 14.0);
F = F(t > 5.35 & t < 14.0);
t = t(t > 5.35 & t < 14.0);

d = d - d(1);
F = F - F(1);
t = t - t(1);
#+end_src

#+begin_src matlab :exports none
%% Time Domain plots
figure;
tiledlayout(2, 1, 'TileSpacing', 'None', 'Padding', 'None');

ax1 = nexttile;
plot(t, F);
ylabel('Force [N]'); set(gca, 'XTickLabel',[]);

ax2 = nexttile;
plot(t, d);
xlabel('Time [s]'); ylabel('Displacement [m]');
#+end_src

#+begin_src matlab :exports none
figure;
hold on;
plot(F, d, 'DisplayName', 'Raw data');
% plot(F, F/(d\F), 'DisplayName', 'Linear fit');
hold off;
xlabel('Measured Force [N]');
ylabel('Measured Displacement [m]');
legend('location', 'southeast');
#+end_src

#+begin_src matlab
ta = [0.05, 1.13];
tb = [5.75, 7.2];

d_l = [d(t > ta(1) & t < ta(2)); d(t > tb(1) & t < tb(2))];
F_l = [F(t > ta(1) & t < ta(2)); F(t > tb(1) & t < tb(2))];

d_l = d_l - d_l(1);
F_l = F_l - F_l(1);
#+end_src

#+begin_src matlab :exports none
figure;
hold on;
plot(F_l, d_l, '.', 'DisplayName', 'Raw data');
plot(F_l, F_l/(d_l\F_l), 'DisplayName', 'Linear fit');
hold off;
xlabel('Measured Force [N]');
ylabel('Measured Displacement [m]');
legend('location', 'southeast');
#+end_src

#+begin_src matlab :results value replace :exports results :tangle no
h = 25e-3;
sprintf('Stiffness is %.3f [Nm/rad]', abs(h^2*(d_l\F_l)))
#+end_src

#+RESULTS:
: Stiffness is 5.579 [Nm/rad]

** Results - Y
#+begin_src matlab
load('meas_stiff_flex_12_y.mat', 't', 'd', 'F');
#+end_src

#+begin_src matlab
%% Automatic Zero of the force
F = F - mean(F(t > 0.9 & t < 1.1));

%% Start measurement at t = 1.0 s
d = d(t > 1.0);
F = F(t > 1.0);
t = t(t > 1.0); t = t - t(1);
#+end_src

#+begin_src matlab :exports none
%% Time Domain plots
figure;
tiledlayout(2, 1, 'TileSpacing', 'None', 'Padding', 'None');

ax1 = nexttile;
plot(t, F);
ylabel('Force [N]'); set(gca, 'XTickLabel',[]);

ax2 = nexttile;
plot(t, d);
hold off;
xlabel('Time [s]'); ylabel('Displacement [m]');

linkaxes([ax1,ax2],'x');
#+end_src

#+begin_src matlab
t_l = [5.58, 6.75]; % Time of flexible joint's linear region
t_s = [6.9, 7.24]; % Time of stop's linear region
#+end_src

#+begin_src matlab :exports none
%% Linear Fit
h = 25e-3;

F_l = F(t > t_l(1) & t < t_l(2));
d_l = d(t > t_l(1) & t < t_l(2));

F_s = F(t > t_s(1) & t < t_s(2));
d_s = d(t > t_s(1) & t < t_s(2));

fit_l = polyfit(F_l, d_l, 1);
fit_s = polyfit(F_s, d_s, 1);

%% Reset displacement based on fit
d = d - fit_l(2);
fit_s(2) = fit_s(2) - fit_l(2);
fit_l(2) = 0;

%% Estimated Stroke
F_max = fit_s(2)/(fit_l(1) - fit_s(1));
d_max = fit_l(1)*F_max;
#+end_src

#+begin_src matlab :exports none
figure;
hold on;
% plot(F_l, d_l, 'k.', 'DisplayName', 'Raw Data');
% plot(F_s, d_s, 'k.', 'HandleVisibility', 'off');
plot(F, d, 'k.', 'DisplayName', 'Raw data');
set(gca,'ColorOrderIndex',1)
plot(F_l, fit_l(1)*F_l + fit_l(2), '--', 'DisplayName', sprintf('$k_{R_x} = %.1f [Nm/rad]$', (h)^2/fit_l(1)));
plot(F_s, fit_s(1)*F_s + fit_s(2), '--', 'DisplayName', sprintf('$k_s = %.1f [Nm/rad]$', (h)^2/fit_s(1)));
plot([0.8*F_max, 1.2*F_max], [d_max, d_max], '--', 'DisplayName', sprintf('$R_{x,max} = %.1f [mrad]$', 1e3*atan2(d_max,h)));
hold off;
xlabel('Measured Force [N]');
ylabel('Measured Displacement [m]');
legend('location', 'southeast');
ylim([-1e-4,inf])
#+end_src

#+begin_src matlab :results value replace :exports results :tangle no
sprintf('Bending Stiffness is %.1f [Nm/rad]', (h)^2/fit_l(1))
#+end_src

#+RESULTS:
: Bending Stiffness is 5.5 [Nm/rad]

#+begin_src matlab :results value replace :exports results :tangle no
sprintf('Bending Stroke is %.1f [mrad]', 1e3*atan2(d_max,h))
#+end_src

#+RESULTS:
: Bending Stroke is 17.9 [mrad]

** Results - X
#+begin_src matlab
load('meas_stiff_flex_12_x.mat', 't', 'd', 'F');
#+end_src

#+begin_src matlab
%% Automatic Zero of the force
F = F - mean(F(t > 0.9 & t < 1.1));

%% Start measurement at t = 1.0 s
d = d(t > 1.0);
F = F(t > 1.0);
t = t(t > 1.0); t = t - t(1);
#+end_src

#+begin_src matlab :exports none
%% Time Domain plots
figure;
tiledlayout(2, 1, 'TileSpacing', 'None', 'Padding', 'None');

ax1 = nexttile;
plot(t, F);
ylabel('Force [N]'); set(gca, 'XTickLabel',[]);

ax2 = nexttile;
plot(t, d);
hold off;
xlabel('Time [s]'); ylabel('Displacement [m]');

linkaxes([ax1,ax2],'x');
#+end_src

#+begin_src matlab
t_l = [4.365, 5.47]; % Time of flexible joint's linear region
t_s = [5.65, 6.09]; % Time of stop's linear region
#+end_src

#+begin_src matlab :exports none
%% Linear Fit
h = 25e-3;

F_l = F(t > t_l(1) & t < t_l(2));
d_l = d(t > t_l(1) & t < t_l(2));

F_s = F(t > t_s(1) & t < t_s(2));
d_s = d(t > t_s(1) & t < t_s(2));

fit_l = polyfit(F_l, d_l, 1);
fit_s = polyfit(F_s, d_s, 1);

%% Reset displacement based on fit
d = d - fit_l(2);
fit_s(2) = fit_s(2) - fit_l(2);
fit_l(2) = 0;

%% Estimated Stroke
F_max = fit_s(2)/(fit_l(1) - fit_s(1));
d_max = fit_l(1)*F_max;
#+end_src

#+begin_src matlab :exports none
figure;
hold on;
plot(F, d, 'k.', 'DisplayName', 'Raw data');
set(gca,'ColorOrderIndex',1)
plot(F_l, fit_l(1)*F_l + fit_l(2), '-', 'DisplayName', sprintf('$k_{R_x} = %.1f [Nm/rad]$', (h)^2/fit_l(1)));
plot(F_s, fit_s(1)*F_s + fit_s(2), '-', 'DisplayName', sprintf('$k_s = %.1f [Nm/rad]$', (h)^2/fit_s(1)));
plot([0.8*F_max, 1.2*F_max], [d_max, d_max], '-', 'DisplayName', sprintf('$R_{x,max} = %.1f [mrad]$', 1e3*atan2(d_max,h)));
hold off;
xlabel('Measured Force [N]');
ylabel('Measured Displacement [m]');
legend('location', 'southeast');
ylim([-1e-4,inf])
#+end_src

#+begin_src matlab :results value replace :exports results :tangle no
sprintf('Bending Stiffness is %.1f [Nm/rad]', (h)^2/fit_l(1))
#+end_src

#+RESULTS:
: Bending Stiffness is 5.7 [Nm/rad]

#+begin_src matlab :results value replace :exports results :tangle no
sprintf('Bending Stroke is %.1f [mrad]', 1e3*atan2(d_max,h))
#+end_src

#+RESULTS:
: Bending Stroke is 17.9 [mrad]

** Results - XY
#+begin_src matlab
load('meas_stiff_flex_12_xy.mat', 't', 'd', 'F');
#+end_src

#+begin_src matlab
%% Automatic Zero of the force
F = F - mean(F(t > 0.9 & t < 1.1));

%% Start measurement at t = 1.0 s
d = d(t > 1.0);
F = F(t > 1.0);
t = t(t > 1.0); t = t - t(1);
#+end_src

#+begin_src matlab :exports none
%% Time Domain plots
figure;
tiledlayout(2, 1, 'TileSpacing', 'None', 'Padding', 'None');

ax1 = nexttile;
plot(t, F);
ylabel('Force [N]'); set(gca, 'XTickLabel',[]);

ax2 = nexttile;
plot(t, d);
hold off;
xlabel('Time [s]'); ylabel('Displacement [m]');

linkaxes([ax1,ax2],'x');
#+end_src

#+begin_src matlab
t_l = [4.99, 6.5]; % Time of flexible joint's linear region
t_s = [6.8, 7.10]; % Time of stop's linear region
#+end_src

#+begin_src matlab :exports none
%% Linear Fit
h = 25e-3;

F_l = F(t > t_l(1) & t < t_l(2));
d_l = d(t > t_l(1) & t < t_l(2));

F_s = F(t > t_s(1) & t < t_s(2));
d_s = d(t > t_s(1) & t < t_s(2));

fit_l = polyfit(F_l, d_l, 1);
fit_s = polyfit(F_s, d_s, 1);

%% Reset displacement based on fit
d = d - fit_l(2);
fit_s(2) = fit_s(2) - fit_l(2);
fit_l(2) = 0;

%% Estimated Stroke
F_max = fit_s(2)/(fit_l(1) - fit_s(1));
d_max = fit_l(1)*F_max;
#+end_src

#+begin_src matlab :exports none
figure;
hold on;
plot(F, d, 'k.', 'DisplayName', 'Raw data');
set(gca,'ColorOrderIndex',1)
plot(F_l, fit_l(1)*F_l + fit_l(2), '-', 'DisplayName', sprintf('$k_{R_x} = %.1f [Nm/rad]$', (h)^2/fit_l(1)));
plot(F_s, fit_s(1)*F_s + fit_s(2), '-', 'DisplayName', sprintf('$k_s = %.1f [Nm/rad]$', (h)^2/fit_s(1)));
plot([0.8*F_max, 1.2*F_max], [d_max, d_max], '-', 'DisplayName', sprintf('$R_{x,max} = %.1f [mrad]$', 1e3*atan2(d_max,h)));
hold off;
xlabel('Measured Force [N]');
ylabel('Measured Displacement [m]');
legend('location', 'southeast');
ylim([-1e-4,inf])
#+end_src

#+begin_src matlab :results value replace :exports results :tangle no
sprintf('Bending Stiffness is %.1f [Nm/rad]', (h)^2/fit_l(1))
#+end_src

#+RESULTS:
: Bending Stiffness is 5.6 [Nm/rad]

#+begin_src matlab :results value replace :exports results :tangle no
sprintf('Bending Stroke is %.1f [mrad]', 1e3*atan2(d_max,h))
#+end_src

#+RESULTS:
: Bending Stroke is 23.1 [mrad]
