Marco Mojana Github pageMarco Mojana random activities
http://offbytwo.net/
Sat, 09 May 2015 10:44:11 +0200Sat, 09 May 2015 10:44:11 +0200Jekyll v2.4.0How Li Ion Battery Chargers Work<p>In this post we will see how the charging process works for Li-ion batteries and how to choose the appropriate PMIC (Power Management Integrated Circuit) charger. What described below applies also to Li-poly cells.</p>
<h2 id="how-to-charge-a-li-ion-battery">How to charge a Li-ion battery</h2>
<h3 id="charging-parameters">Charging parameters</h3>
<p>The Li-ion batteries cannot be charged by simply connecting them to a voltage source: this will most probably damage them irremediably or even make them explode. Families of PMIC have been specifically designed to protect them during their charging phases. There are essentially two constraints that must be constantly enforced:</p>
<ul>
<li><strong>The maximum current $I_{max}$.</strong> Every battery datasheet reports the suggested maximum charging current. As a rule of thumb you can estimate this parameter by dividing the battery capacity by 2h. Example: if you are dealing with a 300mAh accumulator, a good guess for its $I_{max}$ is 150mA. Some producers provide also a «fast charging current» that is usually set to twice the «standard» value. Using a smaller value will lead to a longer charging time, but at the end the battery will reach a 100% charge. Exceeding the recommended value will damage or destroy the cell.</strong></li>
<li><strong>The maximum voltage $V_{max}$.</strong> This is the maximum potential difference across the two battery terminals. Also in this case you should find the correct value in the datasheet or printed on the component body. This parameter depends strongly on the two chemical elements that constitute the accumulator. For Li-ion it is almost always around 4.2V. Configuring a smaller value will not slow down the charge, but it will interrupt it prematurely, preventing the accumulator to reach the 100% level. Going beyond the correct value will cause malfunctions or the death of the battery.</li>
</ul>
<p>Below you find the block diagram of a generic charger IC.</p>
<p><img src="/images/charger-block-diagram.svg" alt="" /></p>
<p>The main building blocks are:</p>
<ul>
<li><strong>A regulator.</strong> It can operate in linear or switched mode. More details on this element are described in the sections below. Its goal is to reduce the input voltage $U_{in}$ to $U_{out}$. The ratio is imposed by the controller.</li>
<li><strong>A current sensor.</strong> Constantly compares the output current with the threshold $I_{max}$.</li>
<li><strong>A voltage sensor.</strong> It’s not needed to know the exact output voltage value, but only if the level reached can be dangerous for the battery. It is realized with a comparator and a reference voltage source set to the $V_{max}$.</li>
<li><strong>A controller.</strong> It receives the feedbacks from the voltage comparator and the current sensor (dashed lines) and adapts the regulator operating point accordingly.</li>
</ul>
<h3 id="dangers">Dangers</h3>
<p>As all the types of batteries, li-ion do self-discharge with time. When the battery level is really low, its voltage could drop to levels near 1.5V. In this case there is the danger that some shorts could form in the cell, so you always have to check the accumulator tension with a voltmeter before recharging or you have to read the datasheet of the PMIC you are using to ensure that this check is integrated in the chip logic. Nowadays most batteries have an internal protection that irreversibly breaks the cell connections to the outside when the voltage drops below 2.7V.</p>
<h3 id="charging-phases">Charging phases</h3>
<p>Also if the controller operates continuously and linearly, we can observe at least two distinct phases that characterize also the name of the chargers: CI+CV, i.e. constant current and constant voltage. Each producer adopts its own strategy variations: I will try to describe the steps without binding too much on a specific manufacturer implementation choices. The plot below is a simplified trace of the process:</p>
<p><img src="/images/li-ion-charging-phases.svg" alt="" /></p>
<ul>
<li><strong>Trickle charging.</strong> If the battery level is considered very low (below 2.8V), it is charged with a current that is usually an order of magnitude lower than $I_{max}$. If the battery voltage doesn’t go above 2.8V for a preconfigured amount of time, the charger halts.</li>
<li><strong>Constant current.</strong> This behavior can be observed when the battery voltage is below its $V_{max}$, but above the safety guard level of 2.8V. The controller operates the regulator so that a constant current $I_{max}$ is fed into the battery. It corresponds to the part of the plot to the left of the dashed line. In the example $I_{max}$ is set to 100mA. In reality the cell voltage doesn’t increase linearly, but follows a very irregular trend.</li>
<li><strong>Constant voltage.</strong> This is the second phase of the charging process, depicted in the right portion of the plot. The current is reduced so that the battery voltage doesn’t exceed $V_{max}$ (that in the plot is set to 4.2V). The current decreases slowing down the progress. The charge terminates when the current drops to a preconfigured fraction of $I_{max}$.</li>
<li><strong>Trickle charging.</strong> Some manufacturers introduce another trickle charging phase here to maintain an high charge level.</li>
</ul>
<h2 id="types-of-chargers">Types of chargers</h2>
<p>There are essentially two ways of implementing the regulator shown in the block diagram: with a linear component or using the switching technology. In the following sections we will see how they compare.</p>
<h3 id="linear">Linear</h3>
<p>The linear component here is normally a MOSFET. The big advantage is that the component count is reduced to a single element or, for small applications, it can even be integrated in the same chip. The RF interferences are also kept to a minimum, since there is no high frequency activity in this type of regulator. Of course the main drawback is the low efficiency caused by the power dissipation that is equal to:</p>
<script type="math/tex; mode=display">
P = I_{in} V_{in} - I_{out} V_{out}
</script>
<p>since, for a linear regulator, the input and output current are the same:</p>
<script type="math/tex; mode=display">
P = I_{out} \left(V_{in} - V_{out} \right)
</script>
<p>Many regulators have special packages that have extra pads on the bottom side to ensure a proper thermal dissipation. This sometimes creates some difficulties to the hobbyist that is used to the classic soldering iron, as those pin are not directly accessible. Using an heat gun not always solves the problem because the hot air fails to reach the paste between the component and the board. As if that is not enough, when the paste melts, the component will sit on the board, preventing us from inspecting the soldering quality. An insufficient power dissipation will cause the death of the chip or, if it has some sort of protection, a longer charging time.</p>
<p>Another side effect of a reduced efficiency is particularly evident when the supply isn’t provided by a wall adapter, but with a limited power connection as an USB host/hub. In fact, for batteries with capacities higher than 1Ah, the constant-current phase of the charge will be slowed down by the limits imposed by the standard, i.e. $I_{max}$=500mA (because $I_{in} = I_{out}$). </p>
<h3 id="switching">Switching</h3>
<p>Switching regulators rely on an alternating scheme of charge and discharge of reactive components, usually inductors. In the first phase the coil is powered with the available power source, so that a magnetic field is created. In the second, the energy stored is reversed on the output (in our case, the battery). Varying the duration of the two steps, one can achieve different ratios of input/output voltages. The output current is not necessarily equal to the input current, but the relation is:</p>
<script type="math/tex; mode=display">
V_{out} I_{out} \leq \eta V_{in} I_{in}
</script>
<p>where $\eta$ is the regulator efficiency.
This solution requires more external components and this rises the costs and size of the board. The circuit must also be appropriately shielded to avoid releasing too many EMI or it must use components specifically made for this purpose. On the other side, no big heatsinks are usually required. </p>
<p>These configurations reach levels of efficiency as high as 95%. A speedup in the charging process is evident when the supply has limited power and the battery level is very low. For example if the charger is USB powered and the battery has $U$=3V, with a linear regulator the current will be limited to that available from the USB connection (500mA),instead with a switching charger with an efficiency of 90%, it could reach:</p>
<script type="math/tex; mode=display">
I_{out} = \eta \frac{V_{in}}{V_{out}} I_{in} = 0.9 \cdot \frac{5V}{3V} \cdot 500mA = 750mA
</script>
<p>The increase of the maximum available current from 500mA to 750mA reduces the constant current phase duration by 33%.</p>
<h2 id="charging-during-utilization">Charging during utilization</h2>
<p>Often we don’t want to disconnect the battery from the appliance it is powering to charge it. This eliminates the need of a spare accumulator and the downtime for the substitution. For example mobile phones continue to operate seamlessly when the USB cable is plugged in. You could be tempted to use a scheme like the one below: </p>
<p><img src="/images/charger-wrong-wiring.svg" alt="" /></p>
<p>Why is the drawing crossed out? Well, for two good reasons:</p>
<ol>
<li>The current going out the charger must now feed both the battery and the load (represented by $R_{load}$). This means that the current sensor embedded in the regulator cannot measure precisely the current that goes into the cell. The effect is a longer charging time because the load «steals» part of the current meant to flow in the battery. If the cell has $I_{max}$ = 100mA and the load requires 80mA, only 20mA will be delivered to the cell, increasing the constant current phase duration by 400%.</li>
<li>The voltage on the cell equals that on the load. This means that when the battery is heavily discharged, the load supply could be as low as 2.8V. If you are dealing with a logic circuit that needs for example 3.3V to work, you will have to wait until the battery reaches that level to be able to use it.</li>
</ol>
<p>In the following subsections we will see a couple of tricks to mitigate the two problems listed above.</p>
<h3 id="mosfet-and-diode-trick">Mosfet and diode trick</h3>
<p>This is a simple schematic that uses a MOSFET, a Schottky diode and a couple of capacitors to automatically switch the power source between the battery and an external source like a wall adapter. Sometimes the two active components are placed in the same package. This solution works only if the current available from the external power source is always at least the sum of the maximum battery charge current $I_{max}$ and the maximum load current. </p>
<ul>
<li><strong>Autonomous operation.</strong> $R_{pull}$ pulls $U_{in}$ low so that $Q_1$ conduction connects the battery to the load (depicted by $R_{load}$). $D_1$ is inversely polarized, so it’s not conductive.</li>
<li><strong>External power source connected.</strong> When the wall adapter is attached to $U_{in}$, $Q_1$ will isolate the battery from the load, so that the regulator will power only the cell. The load will be powered through $D_1$. A Schottky diode has been chosen to reduce as much as possible the voltage drop.</li>
</ul>
<p><img src="/images/charging-diode-mosfet.svg" alt="" /></p>
<h3 id="ideal-diode">Ideal diode</h3>
<p>Many manufacturers have created a more advanced version of what explained in the previous subsection. Each one adopted its own naming for this technology, but essentially what their solutions achieve is the same. Other than being able to attach and remove an external power source without disrupting the load operation, is also possible to:</p>
<ul>
<li>Set a limit on the incoming current. This is needed if the external energy source must satisfy some standard criteria. For example, all the devices attached to the USB network are required to sink at most 500mA. This poses the problem of how to balance the current on the load and on the battery. The solution in called «load prioritization»: the load gets all the current it needs (but almost 500mA) and if it requires less than 500mA, the remaining amount can be used to charge the accumulator.</li>
<li>Combine the battery and the external source to deliver more current to the load. Going back to the USB example, when the load asks for 600mA, the bus can provide the first 500mA and the remaining 100mA could be drawn from the battery (if possible). This scheme is often called «ideal diode» because this cooperation between the two power sources could be theoretically realized using two ideal diodes (i.e. that have no voltage drop when forward polarized).</li>
</ul>
<h2 id="conclusion">Conclusion</h2>
<p>If you want to find the correct PMIC for your project I suggest you to give a look at the Linear Technology website that has a dedicated section and a simple tool to select the products that match your specs. Despite the company name, they provide both linear and switching chargers. Their datasheet are well-organized and, after reading this post, I hope you will find yourself comfortable with the terms and concepts they contain.</p>
Mon, 02 Mar 2015 00:00:00 +0100
http://offbytwo.net/how-li-ion-battery-chargers-work
http://offbytwo.net/how-li-ion-battery-chargers-workchargerideal diodeli-ionli-polyload prioritizationpmicpower pathCamera Matching<p>Camera matching is the technique that allows to compute the position, rotation and focal length of the camera that shot a given photo. This process has some useful usecases:</p>
<ul>
<li><strong>Recreating a 3D scene starting from a 2D image.</strong> When creating 3D assets of organic objects, the artist uses reference photos to guide his creative process, but this works up to a certain point. Instead, when the meshes are mainly geometric (like most of the buildings), the photo can be superimposed to the scene being created so that the $x$, $y$ and $z$ axes match those of the image, greatly simplifying the process.</li>
<li><strong>Measure relative objects sizes.</strong> This helps for example if we would like to know the dimensions of object that are visible but difficult to reach. Another field of application is the forensic image analysis of photographic evidences.</li>
<li><strong>Create the illusion of a 3D set from a flat landscape.</strong> Instead of creating a complex scene background using a 3D modeling tool, we can obtain a more realistic result using a photo. To avoid making the landscape look flat, we can project it on a simple geometry that roughly reproduces the changing depth of the different picture regions. This technique helps speed up the process of collimating the panorama with the underlying geometry.</li>
</ul>
<p>To see how this is possible, we have to understand some simple concepts. As reference we will use the following image:
<img src="/images/perspective-effect.png" alt="The reference images we will use throughout the exercise." title="The reference image we will use throughout the exercise. Notice the distortions due to the effect of the perspective projection." />
It consists of:</p>
<ul>
<li>A $x$-$y$ plane with a checkerboard pattern oriented following the $x$ and $y$ direction</li>
<li>Two red segments that in the scene are parallel to the $x$ axis.</li>
<li>The green strokes that extend in the $y$ direction</li>
<li>A blue circle at the cube corner. It corresponds to the position of the axes origin the scene</li>
</ul>
<p>A fundamental property of perspective is what is called the “vanishing point”: all the lines that in the 3D scene are parallel to each other, in the photo converge in the same point. Looking at the reference image it’s evident that the two red lines that follow the edges of the checkerboard pattern converge at a (vanishing) point about in the middle of the picture. It’s easy to prove that using linear algebra. Let’s consider all the lines with direction $\vec{d}$: </p>
<script type="math/tex; mode=display">\vec{x}=\vec{x}_0 + \lambda \vec{d}</script>
<p>and a generic projection matrix $C$. If we follow this line ad infinitum, the projection of our position will converge to the point:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
\vec{y} &= \lim_{\lambda \to \infty} C \vec{x} \\
\vec{y} &= \lim_{\lambda \to \infty} C \left( \vec{x}_0 + \lambda \vec{d} \right)
\end{align*}
%]]></script>
<p>Thanks to the fact that we are using homogeneous coordinates, we can scale the vector using any non-zero coefficient (in our case $\frac{1}{\lambda}$) without influencing the result:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
\vec{y}_s &= \lim_{\lambda \to \infty} \frac{1}{\lambda} C \left( \vec{x}_0 + \lambda \vec{d} \right) \\
\vec{y}_s &= \lim_{\lambda \to \infty} C \left( \frac{1}{\lambda}\vec{x}_0 + \vec{d} \right) \\
\vec{y}_s &= C \vec{d}
\end{align*}
%]]></script>
<p>This shows that the point projection converges to a coordinate that depends only from the movement direction.
In the above picture we can arbitrarily choose that the red segments are going in the $x$ direction and we can measure that the coordinates (taken from the optical center, i.e. the middle of the image) at which they converge are $\left( -89, 71 \right)$, so it holds that:</p>
<script type="math/tex; mode=display">
C
\begin{bmatrix}
1 \\
0 \\
0 \\
0
\end{bmatrix}
=
\alpha_1
\begin{bmatrix}
-89 \\
71 \\
1
\end{bmatrix}
</script>
<p>Once again, the $\alpha_1$ term is necessary because we are working with homogeneous coordinates. From basic linear algebra we already know that this means that the first column of the projection matrix must be parallel to $\left[ -89, 71, 1 \right]^T$. We repeat the same procedure for the $y$ axis, that we choose to be parallel to the green segments. We see that prolonging those edges we reach the point $\left( 947, 104 \right)$, so:</p>
<script type="math/tex; mode=display">
C
\begin{bmatrix}
0 \\
1 \\
0 \\
0
\end{bmatrix}
=
\alpha_2
\begin{bmatrix}
947 \\
104 \\
1
\end{bmatrix}
</script>
<p>At this point we have enough data to compute the focal length of the camera. To do that, we have to decompose the projection matrix in a sequence of a translation, a rotation and a projection normal to the $x$-$y$ plane:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
C &=
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & -1/f & 0 \\
\end{bmatrix}
\begin{bmatrix}
R & 0 \\
0 & 1 \\
\end{bmatrix}
\begin{bmatrix}
I & -t \\
0 & 1 \\
\end{bmatrix} \\
C &=
\begin{bmatrix}
1 & 0 & 0 & 0 \\
0 & 1 & 0 & 0 \\
0 & 0 & -1/f & 0 \\
\end{bmatrix}
\begin{bmatrix}
R & -Rt \\
0 & 1 \\
\end{bmatrix} \\
C &=
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & -1/f \\
\end{bmatrix}
\begin{bmatrix}
R & -Rt \\
\end{bmatrix}
\end{align*}
%]]></script>
<p>Substituting $C$ with the values we found leveraging the vanishing points, we come to the following equation:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
\begin{bmatrix}
-89 \alpha_1 & 947 \alpha_2 & \square & \square \\
71 \alpha_1 & 104 \alpha_2 & \square & \square \\
\alpha_1 & \alpha_2 & \square & \square
\end{bmatrix}
&=
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & -1/f \\
\end{bmatrix}
\begin{bmatrix}
R & -Rt \\
\end{bmatrix} \\
\begin{bmatrix}
-89 \alpha_1 & 947 \alpha_2 & \square & \square \\
71 \alpha_1 & 104 \alpha_2 & \square & \square \\
-f \alpha_1 & -f \alpha_2 & \square & \square
\end{bmatrix}
&=
\begin{bmatrix}
R & -Rt \\
\end{bmatrix}
\end{align*}
%]]></script>
<p>As all the rotation matrices, $R$ is orthonormal, so the first two columns of the matrix on the left side must be orthogonal, i.e.:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{align*}
\alpha_1
\begin{bmatrix}
-89 \\
71 \\
-f
\end{bmatrix}
\alpha_2
\begin{bmatrix}
947 \\
104 \\
-f
\end{bmatrix} &= 0 \\
\begin{bmatrix}
-89 \\
71 \\
-f
\end{bmatrix}
\begin{bmatrix}
947 \\
104 \\
-f
\end{bmatrix} &= 0 \\
f^2 &= -
\begin{bmatrix}
-89 \\
71
\end{bmatrix}
\begin{bmatrix}
947 \\
104
\end{bmatrix} \\
f &= 227
\end{align*}
%]]></script>
<p>That is equivalent to a horizontal aperture angle of:</p>
<script type="math/tex; mode=display">
\beta = 2 \arctan \left( \frac{w}{2f} \right) = 120^\circ
</script>
<p>Where $w$ is the image width, in pixel (if we are interested in the vertical field of view, use the image height instead).
Now that we know $f$, we can choose $\alpha_1$ and $\alpha_2$ so that the two columns have unitary norm:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{bmatrix}
-0.297 & 0.954 & \square & \square \\
0.237 & 0.105 & \square & \square \\
-0.925 & -0.279 & \square & \square
\end{bmatrix}
=
\begin{bmatrix}
R & -Rt \\
\end{bmatrix}
%]]></script>
<p>$R$, like any other orthogonal matrix, the third column must be the cross product of the first two:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{bmatrix}
-0.297 & 0.954 & 0.031 & \square \\
0.237 & 0.105 & -0.965 & \square \\
-0.925 & -0.279 & 0.257 & \square
\end{bmatrix}
=
\begin{bmatrix}
R & -Rt \\
\end{bmatrix}
%]]></script>
<p>We can multiply again both sides by the $x$-$y$ projection matrix:</p>
<script type="math/tex; mode=display">% <![CDATA[
\begin{bmatrix}
-0.297 & 0.954 & 0.031 & \square \\
0.237 & 0.105 & -0.966 & \square \\
0.003 & 0.001 & 0.001 & \square
\end{bmatrix}
=
\begin{bmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & -1/f \\
\end{bmatrix}
\begin{bmatrix}
R & -Rt \\
\end{bmatrix}
%]]></script>
<p>To find the correct values for the fourth column, we can ask the user to tell us where the origin of the axes must be projected. For our reference image we could choose the corner of the grey cube that is highlighted by a small blue circle, that is at $\left( 31, -33 \right)$. We can thus remove 2 additional unknowns from the matrix $C$ because:</p>
<script type="math/tex; mode=display">
C
\begin{bmatrix}
0 \\
0 \\
0 \\
1
\end{bmatrix}
= \alpha_3
\begin{bmatrix}
31 \\
-33 \\
1 \\
\end{bmatrix}
</script>
<p>The value of $\alpha_3$ can be set to any value, if we have no data about the size of the elements on the scene. If instead we know for example that the side corner of the cube is 1 meter high and on the image it is 146 pixel long, we can add:</p>
<script type="math/tex; mode=display">
C
\begin{bmatrix}
0 \\
0 \\
1 \\
1
\end{bmatrix}
=
\begin{bmatrix}
0.031 \\
-0.966 \\
0.001 \\
\end{bmatrix}
+ \alpha_3
\begin{bmatrix}
31 \\
-33 \\
1 \\
\end{bmatrix}
</script>
<p>And impose:</p>
<script type="math/tex; mode=display">
\left( 31 - \frac{0.031 + 31 \alpha_3}{0.001 + \alpha_3} \right)^2 + \left( -33 - \frac{-0.966 - 33 \alpha_3}{0.001 + \alpha_3} \right)^2 = 146^2
</script>
<p>The solution $\alpha_3 = 0.005$ let us complete the projection matrix as follows:</p>
<script type="math/tex; mode=display">% <![CDATA[
C =
\begin{bmatrix}
-0.297 & 0.954 & 0.031 & 0.170 \\
0.237 & 0.105 & -0.966 & -0.181 \\
0.003 & 0.001 & 0.001 & 0.005
\end{bmatrix}
%]]></script>
<p>Having the $C$ matrix completely defined allow us to extract all the other useful parameters like the camera position $\left( -1.312,-0.568, -0.571 \right)$ and rotation angles $\left( -104.910^\circ, -1.763^\circ, 107.280^\circ \right)$.
This entire algorithm can be executed using <a href="/downloads/camera-matching.m">this Octave script</a> that I have developed to compute the above values. You can now extend, customize and start using it together with your favorite 3D modeling software. Enjoy!</p>
Sat, 21 Feb 2015 00:00:00 +0100
http://offbytwo.net/camera-matching
http://offbytwo.net/camera-matchingcamera matchinglinear algebraphotogrammetry