Friday, September 27, 2013

Arduino Yún Naked

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…

If you read the documentation it’s nothing surprising. On the following photo there is also description of the main parts.

More photos…

Note: The connector for external Wi-Fi antenna looks like U.FL but it’s NOT! Actually it’s SWF connector. Unfortunately purpose of this connector is mainly during product testing, so counterpart is far less available than U.FL connector.

Sunday, July 17, 2011

LDmicro and Arduino

Ladder Logic for Arduino.

In this article the way how to run program in ladder logic from LDmicro on Arduino is presented. LDmicro is free SW to write and simulate ladder logic 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.

Little Background

When we select ANSI C Code as Microcontroler type in Settings menu of LDmicro after compile we will get C file. This file contains all logic but it requires header file ladder.h which is missing. So, we have to implement this file. Let say we have input contact called “button” then we have to implement function:

BOOL Read_U_b_Xbutton(void) {
 return digitalRead(10); // button on digital pin 10
}

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 digitalRead or digitalWrite. 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.

For more information read actual generated C file. It contains more information.

Generating ladder.h

If we need just direct control of digital IO pins of Arduino ladder.h 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 ladder.h with all nessesary functions and setup function (correctly calls pinMode for all used digital pin). Script could be used as console application or it’s also available online.

Step by step

Let’s do it step by step. Complete example is available for download.

1. Write code in LDmicro.

2. Select as Microcontroller “ANSI C Code”.

3. Compile it as ladder.cpp (save it as *.cpp file not *.c, otherwise Arduino won’t compile it)

4. Prepare mapping for IO pins of Arduino. Its text file called pinmap.ini. See example for details.

5. Generate ladder.h file by ladder-gen.php.

Tip: Ladder.h generator is available also online. However you can’t use pinmode.ini in online version. So all input and output pins have to have name “d0” for arduino digital pin 0, “d1” for pin 1 and so on.

6. Create Arduino sketch.

#include "ladder.h"

void setup() {
    PlcSetup();
}

void loop() {
    PlcCycle();
    delay(10);
}

See example for better sketch implementation (this one is for illustration and it's not accurate for timers).

7. Compile it by Arduino editor and upload it.

8. That’s all.

Tip: 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.

Conclusion

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.

Sunday, January 2, 2011

Arduino Predefined Constants

There are a lot of #define-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.

Gcc constants

__DATE__and __TIME__

Date and time of compilation.

__FILE__and __LINE__

FILE is full path to current compiled file and LINE is current line number inside this file. However FILE is not *.pde (sketch file), it’s *.cpp. If you don’t know how Arduino build process works you will probably don’t need to use this constants. However it would be useful in libraries.

__func__

Name of current function.

void setup()
{
 Serial.println(__func__); // prints "setup"
}
__COUNTER__

Every time you use this one, it increases its value.

Serial.println(__COUNTER__); // 0
Serial.println(__COUNTER__); // 1
Serial.println(__COUNTER__); // 2

CPU Type and How to Determine Arduino Bord

According to selected target processor, compiler defined constant by processor name.

Constant CPU Board
__AVR_ATmega168__ ATmega 168 Arduino Decimilia and older
__AVR_ATmega328P__ ATmega 328P Arduino Duemilanove and Uno
__AVR_ATmega1280__ ATmega 1280 Arduino Mega
__AVR_ATmega2560__ ATmega 2560 Arduino Mega 2560
__AVR_ATmega32U4__ ATmega 32U4 Arduino Leonardo
__SAM3X8E__ AT91SAM3X8E Arduino Due

So by testing of these constants you can determine board type. For example:

#if not (defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__))
    #error Sorry dude, this program works only on Arduino Mega 
#endif

Another example is in sketch in the end of article.

Arduino Specific Constant

Arduino compiler define these constants

ARDUINO

Version of Arduino SW. So if you use Arduino 0022 you wil get:

Serial.println(ARDUINO); // 22
F_CPU

Frequency of oscillator on the board (in hertz). For example on Arduino Uno you will get:

Serial.println(F_CPU); // 16000000

Try it

You can try example sketch on your board.

Friday, December 31, 2010

Simple way how to use PROGMEM

If you write large Arduino program with a lot of strings, RAM can be huge issue.

When reading something about Arduino Memory organization you might notice that all strings are being put in to RAM. And Arduino has "only" 2kB of the RAM. So if you have a program with lots of these:

Serial.prinln("Some very long string...");
Serial.prinln("Some another very long string...");

RAM runs out very quickly. In my case it was quite talkative telnet server that sometimes unexpectedly crashed because of this issue.

Note: Free RAM could be determined by function ATS_GetFreeMemory() form ArduinoTestSuite.

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 PROGMEM section in Arduino reference. You will probably end with something like this:

#include <avr/pgmspace.h>

const char message[] PROGMEM = "Some very long string...";
char buffer[30];

strcpy_P(buffer, message);
Serial.println(buffer);

And this is a lot of code for one println, isn't it. Ideal would be something like

Serial.prinln(P("Some very long string..."));

And surprise, it's possible. Solution is to declare a macro.

#include <avr/pgmspace.h>
 
char p_buffer[80];
#define P(str) (strcpy_P(p_buffer, PSTR(str)), p_buffer)

That’s all!

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 PSTR from pgmspace.h. PSTR allows you to declare string inline into the program memory without PROGMEM keyword and variables.

In my telnet server, I've saved more then 1kB (more then 50%!). And it only requires to surround all strings with P("...").