tag:blogger.com,1999:blog-63106261980042987642024-03-14T00:51:31.180-07:00Electronics for DogsAdamhttp://www.blogger.com/profile/15873550616150845689noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-6310626198004298764.post-47828155025163034772014-12-28T16:14:00.000-08:002014-12-28T16:31:41.154-08:00Debugger for Arduino (Visual Micro)<div class="separator" style="clear: both; float: right; margin-left: 15px;"><img border="0" src="http://1.bp.blogspot.com/-c_MEV18ldS8/VKCesGJL0LI/AAAAAAAAEeg/fa7Y-b1Fdx0/s1600/VMLogo_64x64.png" /></div>
<p>
<a href="http://www.visualmicro.com/">Visual Micro</a> is very an interesting extension for Microsoft Visual Studio (it works also in Atmel Studio). It allows you to program Arduino directly from Visual Studio environment – that’s cool, but that’s not all!
</p>
<p>
Visual Studio is incomparable with Arduino IDE in terms of users experience. It gives you all kinds of goodies like IntelliSense, syntax highliting, code navigation, git/svn/whatever integration and so on. But wait for it… it gives you also a debugger!
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-8mb152tAeTs/VKCXxSV8CyI/AAAAAAAAEd0/uwbk0U2WxhM/s1600/vm-vs.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://4.bp.blogspot.com/-8mb152tAeTs/VKCXxSV8CyI/AAAAAAAAEd0/uwbk0U2WxhM/s550/vm-vs.png" /></a></div>
<p>
Debugging AVR processor requires dedicated external HW debugger, which is pretty expensive gadget. So Visual Micro is not true debugger but still, fairly awesome.
</p>
<h4>How it works?</h4>
<p>
Visual Micro utilize technique called <a href="http://en.wikipedia.org/wiki/Instrumentation_(computer_programming)">Instrumentation</a>.
</p>
<p>
We can take this code as an example:
</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="http://4.bp.blogspot.com/-f-lFixDqS-c/VKCYIMuDCEI/AAAAAAAAEd8/Szok4qZvS3c/s1600/vm-code.png" /></div>
<p>
When you start debugging in VS, Visual Micro takes your code and inserts small pieces of its own code into it. First is at the beginig of <code style="font-size: 90%">setup()</code> and next to everywhere where breakpoints are.
<p>
Result is saved to temporary folder, so anybody can look at it. Path to it is usually something like <code style="font-size: 90%">c:\Users\{UserName}\AppData\Local\VMicro\Arduino\Builds\</code>
</p>
<p>
Result of this process for the example shown above looks like this:
</p>
<script src="https://gist.github.com/ah01/1ddb5b17df0507759145.js"></script>
<p>
If you open it in normal Arduino IDE, Serial Monitor gives you something like this:
</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="http://1.bp.blogspot.com/-mKbI0TwePaw/VKCYnS-MDLI/AAAAAAAAEeE/4wO2WRSK9a8/s1600/vm-serial-monitor.png" /></div>
<p>
The magic happens when Visual Micro takes this cryptical lines and visualize it nicely in Visual Studio.
</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="http://4.bp.blogspot.com/-kUdfUGgUzgM/VKCZNhrT11I/AAAAAAAAEeQ/Mx81dYePohs/s1600/vm-expression.png" /></div>
<p>
And that’s what you are paying for. Visual Micro with Debugger is not for free. The instrumentation is fairly simple, but the integration into Visual Studio is more difficult part. Price starts at $29. But it’s worth it.
</p>Adamhttp://www.blogger.com/profile/15873550616150845689noreply@blogger.com7tag:blogger.com,1999:blog-6310626198004298764.post-90618797363947575272013-09-27T09:14:00.000-07:002013-09-27T09:14:42.510-07:00Arduino Yún Naked<p>
<div class="separator" style="clear: both; text-align: center;">
<img border="0" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;" src="http://4.bp.blogspot.com/-exjCbHcCtgA/UkWmLICeEuI/AAAAAAAAERw/lDzI3D1U9Zs/s1600/yun.png" /></div>
The “linux” part of Arduino Yún is mostly hidden under shielding. I was wondering what’s under it.
Luckily shielding is mounted only by two solder joins. So nothing that soldering iron and soldering wick can't handle…
</p>
<p>
If you read <a href="http://arduino.cc/en/Main/ArduinoBoardYun">the documentation</a> it’s nothing surprising. On the following photo there is also description of the main parts.
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://4.bp.blogspot.com/-KvE0YhIyy7g/UkWgxCWYNDI/AAAAAAAAERc/eqsRmLMXWnw/s1600/notes.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;">
<img border="0" height="424" src="http://4.bp.blogspot.com/-KvE0YhIyy7g/UkWgxCWYNDI/AAAAAAAAERc/eqsRmLMXWnw/s580/notes.jpg" width="580" />
</a>
</div>
<p>
<strong><a href="http://www.flickr.com/photos/horcicaa/sets/72157635949563483">More photos…</a></strong>
</p>
<p>
<small><em>Note:</em> The connector for external Wi-Fi antenna looks like U.FL but it’s NOT! Actually it’s <a href="http://www.digikey.com/us/en/ph/Murata/SWF_Connector.html">SWF connector</a>. Unfortunately purpose of this connector is mainly during product testing, so counterpart is far less available than U.FL connector.</small>
</p>Adamhttp://www.blogger.com/profile/15873550616150845689noreply@blogger.com1tag:blogger.com,1999:blog-6310626198004298764.post-31747648842392169122011-07-17T15:47:00.000-07:002012-05-05T06:03:02.350-07:00LDmicro and Arduino<p>
<em>Ladder Logic for Arduino.</em>
</p>
<p>
In this article the way how to run program in ladder logic from <a href="http://www.cq.cx/ladder.pl">LDmicro</a> on <a href="http://arduino.cc">Arduino</a> is presented. LDmicro is free SW to write and simulate <a href="http://en.wikipedia.org/wiki/Ladder_logic">ladder logic</a> programs. When is ladder logic program is successfully simulated it could be compiled for AVR or PIC processor. However ATmega used in Arduino is not supported. Fortunately LDmicro can “compile” code into ANSI C. So, let’s take a look how we can run this code on Arduino.
</p>
<div class="separator" style="clear: both; text-align: center; margin: 10px 0;">
<img border="0" src="http://2.bp.blogspot.com/-onm-qW4Dv2Q/TiNiGf_3zzI/AAAAAAAADOs/lM86SYsuK2o/s480/ldmicro.png" />
</div>
<h4>Little Background</h4>
<p>
When we select <em>ANSI C Code</em> as <em>Microcontroler type</em> in Settings menu of LDmicro after compile we will get C file. This file contains all logic but it requires header file <code>ladder.h</code> which is missing. So, we have to implement this file. Let say we have input contact called “<em>button</em>” then we have to implement function:
</p>
<pre><span style="color: #CC6600;">BOOL</span> Read_U_b_Xbutton(<span style="color: #CC6600;">void</span>) {
<span style="color: #CC6600;">return</span> <span style="color: #CC6600;">digitalRead</span>(10); <span style="color: #7E7E7E;">// button on digital pin 10</span>
}</pre>
<p>
And similar to others input and output pins. Actually we have to write the layer between leader logic program and real HW of Arduino. And it could be more than simple call of <code>digitalRead</code> or <code>digitalWrite</code>. For example we could control servos, read data from proximity sensor and so on. Ability to write this layer is very powerful. Ladder logic could be used for core logic of application and C only for HW layer.
</p>
<p>
For more information read actual generated C file. It contains more information.
</p>
<h4>Generating ladder.h</h4>
<p>
If we need just direct control of digital IO pins of Arduino <code>ladder.h</code> could be easily generated for us. I wrote PHP script that can do this job. It requires generated code and mapping between variable name we use in ladder logic and pin numbers of Arduino. It will generate <code>ladder.h</code> with all nessesary functions and setup function (correctly calls <code>pinMode</code> for all used digital pin). Script could be used as <a href="https://github.com/ah01/LDmicro2arduino">console application</a> or it’s also <a href="http://adam.horcica.cz/tools/ladder-gen/">available online</a>.
</p>
<h4>Step by step</h4>
<p>
Let’s do it step by step. <a href="https://github.com/ah01/LDmicro2arduino/tree/master/ldexample"><strong>Complete example</strong></a> is available for download.
</p>
<p>
<strong>1.</strong> Write code in LDmicro.
</p>
<p>
<strong>2.</strong> Select as Microcontroller “<em>ANSI C Code</em>”.
</p>
<p>
<strong>3.</strong> Compile it as <code>ladder.cpp</code> (save it as <code>*.cpp</code> file not <code>*.c</code>, otherwise Arduino won’t compile it)
</p>
<p>
<strong>4.</strong> Prepare mapping for IO pins of Arduino. Its text file called <code>pinmap.ini</code>. See example for details.
</p>
<p>
<strong>5.</strong> Generate <code>ladder.h</code> file by <em>ladder-gen.php</em>.
</p>
<div class="note">
<i>Tip:</i> <strong>Ladder.h generator is available also <a href="http://adam.horcica.cz/tools/ladder-gen/">online</a></strong>. However you can’t use pinmode.ini in online version. So all input and output pins have to have name “<code>d0</code>” for arduino digital pin 0, “<code>d1</code>” for pin 1 and so on.
</div>
<p>
<strong>6.</strong> Create Arduino sketch.
</p>
<pre>#include <span style="color: #006699;">"ladder.h"</span>
<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>setup</b></span>() {
PlcSetup();
}
<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>loop</b></span>() {
PlcCycle();
<span style="color: #CC6600;">delay</span>(10);
}</pre>
<p>
See example for better sketch implementation (this one is for illustration and it's not accurate for timers).
</p>
<p>
<strong>7.</strong> Compile it by Arduino editor and upload it.
</p>
<p>
<strong>8.</strong> That’s all.
</p>
<div class="note">
<i>Tip:</i> Now if you enable “External editor” setting of Arduino editor then you
need just hit “F5“ in LDmicro and then push “Upload “ in Arduino editor to run program.
</div>
<h4>Conclusion</h4>
<p>
I hope presented method will help you start with ladder logic for Arduino.
If you have any suggestion or find a bug in script, let me know.
</p>
<style>
pre{padding: 8px 5px; margin: 1em 0;background-color: #C7DBE1; font-size: 90%}
.note{background-color: #FFE582;color: #005580; margin: 1em 0; padding: 8px 5px; font-size: 90%}
.note em{font-weight: bold;}
</style>Adamhttp://www.blogger.com/profile/15873550616150845689noreply@blogger.com73tag:blogger.com,1999:blog-6310626198004298764.post-74803288139753341492011-01-02T07:03:00.000-08:002013-01-07T14:13:55.008-08:00Arduino Predefined Constants<p>
There are a lot of <strong><code>#define</code></strong>-d constants in Arduino.
Most of them are from avr-gcc compiler but some are also Arduino specific. Let’s take a look at some interesting ones.
</p>
<h4>Gcc constants</h4>
<h5><code>__DATE__</code>and <code>__TIME__</code></h5>
<p>
Date and time of compilation.
</p>
<h5><code>__FILE__</code>and <code>__LINE__</code></h5>
<p>
FILE is full path to current compiled file and LINE is current line number inside this file. However FILE is not <em>*.pde</em> (sketch file), it’s <em>*.cpp</em>. If you don’t know how Arduino <a href="http://arduino.cc/en/Hacking/BuildProcess">build process</a> works you will probably don’t need to use this constants. However it would be useful in libraries.
</p>
<h5><code>__func__</code></h5>
<p>
Name of current function.
</p>
<pre>
<span style="color: #CC6600;">void</span> <span style="color: #CC6600;"><b>setup</b></span>()
{
<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(__func__); <span style="color: #7E7E7E;">// prints "setup"</span>
}
</pre>
<h5><code>__COUNTER__ </code></h5>
<p>
Every time you use this one, it increases its value.
</p>
<pre>
<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(__COUNTER__); <span style="color: #7E7E7E;">// 0</span>
<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(__COUNTER__); <span style="color: #7E7E7E;">// 1</span>
<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(__COUNTER__); <span style="color: #7E7E7E;">// 2</span>
</pre>
<br>
<h4>CPU Type and How to Determine Arduino Bord</h4>
<p>
According to selected target processor, compiler defined constant by processor name.
</p>
<table class="tab">
<thead>
<tr>
<th>Constant</th>
<th>CPU</th>
<th>Board</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>__AVR_ATmega168__</code></td>
<td>ATmega 168</td>
<td>Arduino <a href="http://arduino.cc/en/Main/ArduinoBoardDiecimila">Decimilia</a> and older</td>
</tr>
<tr>
<td><code>__AVR_ATmega328P__</code></td>
<td>ATmega 328P</td>
<td>Arduino <a href="http://arduino.cc/en/Main/ArduinoBoardDuemilanove">Duemilanove</a> and <a href="http://arduino.cc/en/Main/ArduinoBoardUno">Uno</a></td>
</tr>
<tr>
<td><code>__AVR_ATmega1280__</code></td>
<td>ATmega 1280</td>
<td><a href="http://arduino.cc/en/Main/ArduinoBoardMega">Arduino Mega</a></td>
</tr>
<tr>
<td><code>__AVR_ATmega2560__</code></td>
<td>ATmega 2560</td>
<td><a href="http://arduino.cc/en/Main/ArduinoBoardMega2560">Arduino Mega 2560</a></td>
</tr>
<tr>
<td><code>__AVR_ATmega32U4__</code></td>
<td>ATmega 32U4</td>
<td><a href="http://arduino.cc/en/Main/ArduinoBoardLeonardo">Arduino Leonardo</a></td>
</tr>
<tr>
<td><code>__SAM3X8E__</code></td>
<td>AT91SAM3X8E</td>
<td><a href="http://arduino.cc/en/Main/ArduinoBoardDue">Arduino Due</a></td>
</tr>
</tbody>
</table>
<p>
So by testing of these constants you can determine board type. For example:
</p>
<pre>
<span style="color: #B8001F;">#if</span> not (defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__))
<span style="color: #B8001F;">#error</span> Sorry dude, this program works only on Arduino Mega
<span style="color: #B8001F;">#endif</span>
</pre>
<p>
Another example is in sketch in the end of article.
</p>
<h4>Arduino Specific Constant</h4>
<p>
Arduino compiler define these constants
</p>
<h5><code>ARDUINO </code></h5>
<p>
Version of Arduino SW. So if you use Arduino 0022 you wil get:
</p>
<pre>
<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(ARDUINO); <span style="color: #7E7E7E;">// 22</span>
</pre>
<h5><code>F_CPU</code></h5>
<p>
Frequency of oscillator on the board (in hertz). For example on Arduino Uno you will get:
</p>
<pre>
<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(F_CPU); <span style="color: #7E7E7E;">// 16000000</span>
</pre>
<br>
<h4>Try it</h4>
<p>
You can try <strong><a href="https://gist.github.com/762576">example sketch</a></strong> on your board.
</p>
<style>
pre{padding: 8px 5px; margin: 1em 0;background-color: #C7DBE1; font-size: 85%}
.note{background-color: #FFE582;color: #005580; margin: 1em 0; padding: 8px 5px; font-size: 80%}
.note em{font-weight: bold;}
h5{margin: 1em 0 .1em 0;}
h5 code{font-size:105%; font-weight: bold;}
table.tab {width: 100%; border-collapse: collapse; border: solid #C7DBE1 1px;}
table.tab td, table.tab th {padding: 4px;border: solid #C7DBE1 1px;}
table.tab th { font-size: 80%; background-color:#E9F1F3; }
</style>Adamhttp://www.blogger.com/profile/15873550616150845689noreply@blogger.com6tag:blogger.com,1999:blog-6310626198004298764.post-49158157409194281222010-12-31T03:33:00.000-08:002010-12-31T10:38:41.301-08:00Simple way how to use PROGMEM<p>
<i>If you write large Arduino program with a lot of strings, RAM can be huge issue.</i>
</p>
<p>
When reading something about Arduino <a href="http://www.arduino.cc/playground/Learning/Memory">Memory organization</a> you might notice that all strings are being put in to RAM. And <abbr title="Arduino based on ATmega 328">Arduino</abbr> has "only" 2kB of the RAM. So if you have a program with lots of these:
</p>
<pre><span style="color: #CC6600;"><b>Serial</b></span>.prinln(<span style="color: #006699;">"Some very long string..."</span>);
<span style="color: #CC6600;"><b>Serial</b></span>.prinln(<span style="color: #006699;">"Some another very long string..."</span>);</pre>
<p>
RAM runs out very quickly. In my case it was quite talkative telnet server that sometimes unexpectedly crashed because of this issue.
</p>
<p class="note">
<em>Note:</em> Free RAM could be determined by function <code>ATS_GetFreeMemory()</code> form <abbr title="Library available from Arduino 0022">ArduinoTestSuite<abbr>.
</p>
<p>
The solution is to put strings into program memory (Arduino has 32kB of program memory). However that is not as simple as it sounds. Details can be found in <a href="http://www.arduino.cc/en/Reference/PROGMEM">PROGMEM</a> section in Arduino reference. You will probably end with something like this:
</p>
<pre>#include <avr/pgmspace.h>
<span style="color: #CC6600;">const char</span> message[] <span style="color: #CC6600;">PROGMEM</span> = <span style="color: #006699;">"Some very long string..."</span>;
<span style="color: #CC6600;">char</span> buffer[30];
strcpy_P(buffer, message);
<span style="color: #CC6600;"><b>Serial</b></span>.<span style="color: #CC6600;">println</span>(buffer);</pre>
<p>
And this is a lot of code for one <code>println</code>, isn't it. Ideal would be something like
</p>
<pre><span style="color: #CC6600;"><b>Serial</b></span>.prinln(<strong>P(</strong><span style="color: #006699;">"Some very long string..."</span><strong>)</strong>);</pre>
<p>
And surprise, it's possible. Solution is to declare a macro.
</p>
<pre>#include <avr/pgmspace.h>
<span style="color: #CC6600;">char</span> p_buffer[80];
<strong>#define</strong> <strong>P</strong>(str) (strcpy_P(p_buffer, PSTR(str)), p_buffer)</pre>
<p>
That’s all!
</p>
<p>
Principle is simple. String is stored in the program memory and processor copies it to the buffer and returns this buffer instead of the string. Key is macro <a href="http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html#ga05ca900ebf7cd121be73c654d9ccb3eb">PSTR</a> from <a href="http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html">pgmspace.h</a>. PSTR allows you to declare string inline into the program memory without PROGMEM keyword and variables.
</p>
<p>
In my telnet server, I've saved more then 1kB (more then 50%!). And it only requires to surround all strings with <code>P("...")</code>.
</p>
<style>pre{padding: 8px 5px; margin: 1em 0;background-color: #C7DBE1; font-size: 90%}
.note{background-color: #FFE582;color: #005580; margin: 1em 0; padding: 8px 5px; font-size: 80%}
.note em{font-weight: bold;}</style>Adamhttp://www.blogger.com/profile/15873550616150845689noreply@blogger.com5