I'm completely new to this forum so I don't know whether there is a FAQ-thread about this or if this is the wrong forum to post this issue.
Anyway. I've just started writing some simple libraries for Arduino, and my next project is way more complex than the morse-example in the Arduino.cc-playground so to speak.
So I figured I'll need some kind of IDE for writing the libraries (C++-IDE) and I was just wondering if there is any "standard" IDE that you guys are using for this purpose?
Msquare:
Library code is just the same C++ like the rest - I just use the standard IDE. You can have two IDEs open - one for the library one for test sketch.
Ok, yeah that's one solution. But I would like to have a possibility to debug (stepping) my library/sketch in order to see what values a node in a linked list contains for instance...
One way to make a lib is to make a sketch with all functions / classes included. If it is functional extract the library part.
I use Notepad++ as it has good syntax highlighting, has tabs and can display 2 files side by side.
Linked lists are probably a bad idea because dynamic memory allocation is pretty flaky.
IIRC malloc has been repaired but still it is not preferred as it makes "swiss cheese" of your memory.
However you could instead of freeing nodes, add the "deleted" ones to a pool of nodes for reuse.
Other strategy is to allocate all nodes needed in advance, but then an array might be more memory efficient.
victorzki:
But I would like to have a possibility to debug (stepping) my library/sketch in order to see what values a node in a linked list contains for instance...
Ahhh.. that is a completly different fish. If you want to debug using "single step" or "examine variable" in the Ardino you need some hardware support for that. Never needed that. Thinking about the problem, adding a few Serial.print gets you very far.
I think he wants to test it stand alone, not burned into the arduino. Currently the IDE does not allow for that. He will probably need the standard gcc toolchain to create a command line test program.
(As far as linked lists, I think a better system is a static array of structs. This way you are guaranteed to have them there when you need them, and if you find out you did not allocate enough you can handle it gracefully. You also save all the bytes you need to allocate for the linking pointers.)
I'll wait for the OP to explain. If it is outside the Arduino, he needs an emulator, including something to emulate the IO.
(hijack warning)Dynamic allocation of memory is not the same as using linked lists - the elements (and pointers) can be preallocated. (And that means one could use shorter index number rather then a full memorypointer, as you point out)
Dynamic memory allocation only makes sense if
1: You release allocated memory before the program is finished, so it can be reused by another section/module.
2: You work on a multi program/multi user system, where you do not want to take a (large) of the common memory resource unless your program actually uses it.
In the Arduino there is never a program/user switch and the program (sorry, "sketch") never finishes.
Most linked list code usually uses malloc() and free() when creating the nodes. And if you have an array of nodes, you know where they are, there is no need for pointers to previous and next. These pointers will use up valuable RAM space on the arduino!
One way to make a lib is to make a sketch with all functions / classes included. If it is functional extract the library part.
Hi Rob.. I like that idea! Can you point to an example or two where you've done that?? Or email me one?? I'd like to add a "How-To Libraries" part on the ArduinoInfo.Info WIKI HERE: after I feel I'm more competent.
My wife and I have been discussing writing a section about "The Engineering Design Process" and "Stepwise Refinement", and this is on that conceptual path...
The methods of the class were separate functions first with several Serial.prints in it. These were used to print the raw data, check does it make sense etc.
Then conversion functions were added to make Celsius from the raw data and check the CRC.
Then hard coded values like the data-pin became variables/parameters.
After that I wrapped it all in a class with all methods public as that makes it easy to test.
Then thinking started about what the public interface of the class should look like methods and members and constructor.
Took some time to smooth the internal behaviour,
Publish on the playground.
first make it work,
get the interfaces right,
then optimize.
In step 2 and 3 constant testing took care I did not fall back (too often) to step 1.
Then I created a library for the DHT22. I took the DHT11 lib as a base and added a few lines to make it work. I never published it as I saw that there were so much commonalities between these two libs that screamed to be merged. That was the start of the DHTlib which support both - Arduino Playground - DHTLib -
For this the interface was slightly changed to reflect both sensors. Then maintenance started and some versions appeared.
Note this class does not wrap a DHT sensor, it is merely an object that can read many without keeping state.
It would be a nice exercise to write a real DHT base class with derived the DHT11 and DHT22. Such classes could hold state like lastTemperature, lastHumidity, and hold a timestamp internally to enforce the 2 seconds delay between readings. If one asks the temperature or humidity too fast one just would get the prev one.
I will try this out with a library I was thinking of for pan-tilt and ultrasonic sensor. I would LIKE the library to run a pan (at least) and (maybe) Tilt mechanism with 1 or 2 servos.
Maybe This One .It would have an ultrasonic distance sensor.
I would like it to return a data structure of horizontal angle VS distance of objects. Maybe the stepsize would be 10 degrees or so. So it would give a "picture" of the surrounding objects.
Maybe there would be an option to do this for various vertical orientations, or maybe return a 2 dimensional array. Then a robot could decide what to do based on that data.
This is based on a project a high school student did in an Arduino workshop Mary Alice and I did. He had a servo scanning an ultrasonic sensor, and then a circle of LEDs around a vertical strip of LEDs. As the sensor scanned, it displayed the direction and the "closeness". He called it "A Lookout". I had done a little with this idea a year ago and I was amazed when he came up with the whole idea...
I have to retread my Library head.. It gets a little leaky some days
// interface 1: detailed
Class radar(pin servo1, pin servo2);
void setPanTilt(int, int);
void setPan(int);
void setTilt(int);
int getPan();
int getTilt();
unsigned long ping();
radar.setPan(100);
while (radar.getPan() != 100);
distance = radar.ping();
build in a compass too
void setNorth(); // set pan direction that is north
string getDirection(); // degrees ...
...
//interface 2: Blocking single call interface
distance = radar.ping(pan, tilt); // not to difficult to implement when it may block the rest of the program... might be part of the interface 1:
// interface 3: nonBlocking async interface
radar.ping(pan, tilt); // start the ping
...
if (radar.ready()) // when the right pan/tilt found
{
distance = radar.distance; // read the distance field
}
Well you guys explained my problem better than I did, what I wanted was to write and test it in a stand alone environment. Although I don't need to emulate the I/O since my primary goal is to write somewhat of a generic linked list which don't need to care about I/O at all.
So yes, what I need is another IDE with a standard GCC toolchain as someone said (?). So what I was wondering was if there is some free IDE that you guys were using.
Again - I'm sorry for the bad explanation. Thanks for all the answers and tips and tricks!
How about I send you the hardware samples and we have a software contest??
I like the offer, but I am too busy in a project that consumes almost all my "Arduino time",
still I like the idea of a radar class so here a quick partial brain dump RADAR class with several TODO's
// disclaimer code not tried/compiled
Think you can fill in the gaps
Regards,
Rob
radar.h
//
// FILE: radar.h
// AUTHOR: Rob Tillaart
// VERSION: see RADAR_LIB_VERSION
// PURPOSE:
// URL:
//
// Released to the public domain
//
#ifndef Radar_h
#define Radar_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#include "SoftwareSerial.h"
#else
#include "WProgram.h"
#include "NewSoftSerial.h"
#endif
#define RADAR_LIB_VERSION "0.1.00"
#define PAN_PER_SEC 20 // TODO determine emperically
#define TILT_PER_SEC 10 // TODO determine emperically
class RADAR
{
public:
RADAR(int, int);
void setPan(int pan);
int getPan();
void setTilt(int tilt);
int getTilt();
void setHomePosition(int pan, int tilt);
void home();
bool ready();
unsigned long ping();
unsigned long ping(int pan, int tilt);
private:
int _pinPan;
int _pinTilt;
int _prevPan;
int _pan;
int _homePan;
unsigned long _lastPanTime
int _prevTilt;
int _tilt;
int _homeTilt;
unsigned long _lastTiltTime
};
#endif
// -- END OF FILE --
radar.cpp
//
// FILE: radar.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE:
// URL:
//
// Released to the public domain
//
#include "radar.h"
////////////////////////////////////////////////////////////
//
// CONSTRUCTOR
//
RADAR::RADAR(int pinPan, int pinTilt)
{
_pinPan = pinPan;
_pinTilt = pinTilt;
}
// PUBLIC
void RADAR::setPan(int pan)
{
_prevPan = _pan; // remember last postion // TODO should in fact be getPan() to get current position.
_pan = pan;
digitalWrite(_pinPan, pan);
_lastPanTime = millis();
}
int RADAR::getPan()
{
// ESTIMATE current pan on time it takes to go from _prevPan to _pan
// and the time since setPan ==> millis() - _lastPanTime
//
// TODO CHECK MATH
unsigned long duration = millis() - lastPanTime;
int pan = 0;
if (duration >= abs(_pan - _prevPan) * PAN_PER_SEC/1000) pan = _pan;
else if (_pan > _prevpan) pan = _pan - duration * PAN_PER_SEC/1000;
else pan = _pan - duration * PAN_PER_SEC/1000;
return pan;
}
void RADAR::setTilt(int tilt)
{
_prevTilt = _tilt;
_tilt = tilt;
digitalWrite(_pinTilt, tilt);
_lastTiltTime = millis();
}
int RADAR::getTilt()
{
// TODO see pan
return 0;
}
// TODO extend this to an array of 10 memory positions
void setHomePosition(int pan, int tilt)
{
_homePan = pan;
_homeTilt = tilt;
}
void home()
{
setPan(_homePan);
setTilt(_homeTilt);
}
bool RADAR::ready()
{
return ((getPan() == _pan) && (getTilt == _tilt));
}
unsigned long RADAR::ping()
{
// TODO ping code here - playground or teckel's improved ping)))
return 0;
}
unsigned long RADAR::ping(int pan, int tilt)
{
setPan(pan);
setTilt(tilt);
while (!ready());
return ping();
}
// PRIVATE
// TODO distill private parts (getPan and getTilt share a lot
// -- END OF FILE --