Accelerometer on STM32 Systematic Error Static Test 8.4m/s^2 instead 9.8m/s^2 - accelerometer

i connected a three axis accelerometer to a STM32 with a friends.
I runned a static test and the acceleration module was 8,4m/s^2, instead of 9,8m/s^2
I would like to understand the root of the problem :)
What factor can affect this result?
Is possible to apply a linear offset to the three axis to obtain the right value in a static mode?
I know that is no possible to apply a linear offset for the module, but can i work on every single axes?
Is a linear trasformation?
ax (the correct value)= ax'(the value collected by the accelerometer in static condition) + c (the offset value)
Thanks!

Related

Are MATLAB's lsim outputs derivatives or the state vector?

I'm trying to do a simulation of a 2-body mass-spring-damper. I've set up a state-space model that I'm pretty confident in and set an input of a displacement and velocity at the base in just one degree of freedom. Upon getting my outputs, I expected that the output vector would just be the state vector at each time step. However, when plotting the output vector corresponding to displacement for each mass in the vertical direction (the input direction), it looked much more like a velocity (0 at the extrema of the input). The plots are shown below:
When I integrated the top 2 plots, I got the following:
Now, I obviously can just accept the outputs as they are and assume I am right in my understanding. But, I want to be sure. From the documentation page:
lsim(___) also returns the time vector t used for simulation and the
state trajectories x (for state-space models only)
I'm just hoping to find out whether or not I am correct in that the output matrix columns correspond to the history of the state derivatives before I go base an analysis on a bad assumption.
I figured it out. My B-matrix expected [derivative, state,...] but I had them in the opposite order.

Non-Symbolic loss in Keras/TensorFlow

For a university project, I want to train a (simulated) robot to hit a ball given the position and velocity. The first thing to try is policy gradients: I have a parametric trajectory generator. For every training position, I feed the position through my network, send the trajectory to the simulator and get a reward back. I now can use that as the loss, sample the gradient, feed it back and update the weights of my network so that it does better next time.
Therefore, goal is to learn the mapping from position to trajectory weights. When using all-star compute graph libraries like Theano and Tensorflow (or Keras), the I have the problem that I do not know how to actually model that system. I want to have standard fully connected layers first, then the output are my trajectory weights. But how do I actually calculate the loss so that it can use the backprop?
In a custom loss function, I would ignore/not specify the true labels, run the simulator and return the loss it gives. But from what I read, you need to return a Theano/Tensorflow function which is symbolic. My loss is quite complicated, so I do not want to move it from simulator to network. How can I implement that? Problem then is to differentiate that loss, as I might need to sample to get that gradient.
I've had a similar problem some time ago.
There was a loss function which relied heavily on optimized C code and third-party libraries.
Porting this to tensorflow was not possible.
But we still wanted to train a tensorflow graph to create steering signals from the current setup.
Here is an
ipython notebook which explains how to mix numerical and analytical derivatives
https://nbviewer.jupyter.org/gist/lhk/5943fa09922693a0fbbbf8dc9d1b05c0
Here is a more detailed description of the idea behind it:
The training of the graph is an optimization problem, so you will definitely need the derivative of the loss.
The challenge is to mix the analytical derivative in tensorflow and the numerical derivative of your loss.
You need this setup
Input I
output P
Graph G maps I to P, P = G(I)
add a constant of the same shape as P, P = C * G(I)
Loss function L
Training the tensorflow graph works with backpropagation.
For every parameter X in the graph, the following derivative is computed
dL/dX = dL/dP * dP/dX
The second part of that, dP/dX comes for free by just setting up the tensorflow graph. But we still need the derivative of the loss.
Now there's a trick.
We want tensorflow to update X based on the correct gradient dL/dP * dP/dX
but we can't get tensorflow to compute dL/dP, because that's not a tensorflow graph.
we will instead use P~ = P * C,
the derivative of that is dP~ / dX = dP/dX * C
So if we set C to dL/dP, we get the correct gradient.
We simply have to estimate C with a numerical gradient.
This is the algorithm:
set up your graph, multiply the output with a constant C
feed 1 for the constant, compute a forward pass, get the prediction P
compute the loss at P
compute the numerical derivative of P
feed the numerical derivative as C, compute the backward pass, update the parameters

How to numerically differentiate a set of unevenly time spaced quaternions in MATLAB

I have the time evolution (unevenly spaced) of around 1000 quaternions which provides the transformation from an inertial coordinate system to a body fixed. My goal is to obtain the angular velocity of the body, so for that I need the derivative of the quaternion (or at least I think so), and I am having trouble to obtain it.
So far I have tried first to use the gradient function
time_grad = gradient(time);
dquat(1,:) = gradient(quat(1,:)) ./ (time_grad);
dquat(2,:) = gradient(quat(2,:)) ./ (time_grad);
dquat(3,:) = gradient(quat(3,:)) ./ (time_grad);
dquat(4,:) = gradient(quat(4,:)) ./ (time_grad);
I have also tried using a Fornberg algorithm I found online, but I think I am not obtaining good results based on the final outcome (some computations using the angular velocity which should result in something very similar to some information I already have).
These quaternions could be easily converted to Direction Cosine Matrices if you know a better method to calculate angular velocity based on DCMs.

collimation of a ion beam with many charge states

I'm simulating an ion beam with different charge states passing through two pinholes. I'm using random angles, from an external function, to evaluate the velocity components along the axes.
In order to evaluate the particle motion I'm solving the differential equation:
O=[0 0 0]';
f = #(t,ys) [ys(4:6); O];
options=odeset('RelTol',1e-7,'Events',#(t,ys)Event_Stop_Sorgente(t,ys,Pinhole1));
[t,ys] = ode23t(f,tspan,y_sorgente(i,:),options);
and it works fine as long as the beam has no angular spread and the initial condition vector is y_sorgente = [0 0 0 0 0 v_z]. I suppose it works fine with angular spread as well because the trajectories are straight lines, as they are supposed to be.
The external function get some points from a sferic surface and uses their coordinates as direction cosine to get the velocity components from its modulus.
The problem I have is that only few particle with charge state 1+ can pass the pinhole, all the others can not and it doesn't make any sense. I have the same issue even if each particle has the same modulus of velocity. I'm doing something wrong, but I can't understand what, any suggestion?
I can provide the full code if you want to check it.

MATLAB interpretation of Xcorr2

I have two vectors of matching lengths. They are readings from two different sensors (one is from a smartphone and the other is from a wiimote) of the same hand movement. I am trying to find the time offset between them to synchronise the readings for further processing. The readings I get are of the format (Time(ms) Value) for accelerations in the X,Y and Z direction.
For the synchronization, I plotted the cross-correlation function xcorr2() between the two sets. I am getting the same graph (a weird triangle peak and a straight line at the bottom) for Accelerations along the x, y and z directions (which I guess is good) but I don't know how to interpret it. What do the axes in the graph represent?
Can anyone explain to me what xcorr2() means in a qualitative sense. From the correlation function, how do I determine the offset (i.e. how many seconds is sensor1 behind sensor2)?
I concur with the comment made above by Predictor. To align the time series against each-other, I would pick xcorr() without the 2. Consider correlating only the acceleration magnitudes. For example:
a_mag_wii = sqrt(a_x_wii.^2 + a_y_wii.^2 + a_z_wii.^2);
a_mag_phone = sqrt(a_x_phone.^2 + a_y_phone.^2 + a_z_phone.^2);
res = xcorr(a_mag_wiimote, a_mag_smartphone);