Monday, April 29, 2013
Sunday, April 28, 2013
2D/3D vector constructor C++
http://stackoverflow.com/questions/7686745/3d-vector-constructor-c
vec = vector<vector<vector<T>>> (boardSize, vector<vector<T>>(boardSize, vector<T>(boardSize)));
2D:
vector<vector<T>> vec; vec = vector<vector<T>> (boardSize, vector<T>(boardSize));
Passing Objects by Reference and By Pointer
http://people.cs.vt.edu/~kafura/cs2704/reference.html
??
??
window.Display(box1); // uses by reference window.Display(box2); // uses by pointer window.Display(&box3); // uses by pointer window.Display(*box4); // uses by referen
http://stackoverflow.com/questions/410593/pass-by-reference-value-in-c
Fortran data type
http://en.wikibooks.org/wiki/Fortran/complex_types
If you want double-precision complex numbers, you're pretty much stuck with specifying a precision, and hoping that the compiler likes that format. Here's where portability comes in: If, for instance, the machine has floating point types of lengths 4, 8, and 16, and I specify a complex length of 8, 16, or 32, I'm likely to get a pair of floating point numbers at the size I expect. If, however, I specify a length of some other value, for instance, 10, then I'm likely to get a pair of 4s or 8s, depending on what the compiler likes to do. So, in general, to get the values as double-precision, I'd code:
COMPLEX*16 myVariable, anotherVariable, anArray(2,3)
Saturday, April 27, 2013
Two dimensional iteration over 1D vector C++
vector<T> vec1d
typename vector<T>::iterator col;
for (row = vec1d.begin(); row!=vec1d.end(); row++){
for (col = vec1d.begin(); col!=vec1d.end(); col++){
// do stuff
}
}
iterating through 2d "vector" c++
http://stackoverflow.com/questions/1784573/iterator-for-2d-vector
Multi Dimensional Array
Although your question is not very clear, I'm going to assume you mean a 2D vector to mean a vector of vectors:
vector< vector<int> > vvi;
Then you need to use two iterators to traverse it, the first the iterator of the "rows", the second the iterators of the "columns" in that "row":
//assuming you have a "2D" vector vvi (vector of vector of int's)
vector< vector<int> >::iterator row;
vector<int>::iterator col;
for (row = vvi.begin(); row != vvi.end(); row++) {
for (col = row->begin(); col != row->end(); col++) {
// do stuff ...
}
}
Multi Dimensional Array
The more STL savvy coder will opt for a vector-of-a-vector approach to MD arrays, rather than Nested New:
Not a lot to say about this one. Like Nested New it's incompatible with other MD array flavors. If you need to pass this array to a function, none of the above functions will work.
Also, like Nested New, it's inefficent in that is requires additional memory and has double indirection.
On the plus side, you don't need cleanup code, and the allocation code is not as ugly (but still pretty ugly!)
|
|
Not a lot to say about this one. Like Nested New it's incompatible with other MD array flavors. If you need to pass this array to a function, none of the above functions will work.
Also, like Nested New, it's inefficent in that is requires additional memory and has double indirection.
On the plus side, you don't need cleanup code, and the allocation code is not as ugly (but still pretty ugly!)
Friday, April 26, 2013
NI Labview - cRio 0 Arduino setup
google Code project:
https://code.google.com/p/labviewduino/
Official Support Webpage:
http://sine.ni.com/nips/cds/view/p/lang/en/nid/209835
Setting up the cRIO (IP, and formatting)
Creating and Running a LabVIEW Project
http://www.youtube.com/watch?v=7GbpOIK9rmg
Arduino Labview interface
http://www.youtube.com/watch?v=n3AwL-_UCS4
Support webpage
https://decibel.ni.com/content/groups/labview-interface-for-arduino
Detailed program tutorial:
http://www.youtube.com/watch?v=RGRhIQneO6w
University of Central Florida
MMAE (Department) Robotic Course Webpage
http://www2.mmae.ucf.edu/wiki/MMAE_Graduate_Robotics
System integration solution
https://decibel.ni.com/ content/servlet/JiveServlet/ downloadBody/18322-102-1- 34229/EML%206808_demo5.pptx
Interface download for Labview 2012:
https://decibel.ni.com/content/thread/13798
https://code.google.com/p/labviewduino/
Official Support Webpage:
http://sine.ni.com/nips/cds/view/p/lang/en/nid/209835
Setting up the cRIO (IP, and formatting)
Creating and Running a LabVIEW Project
http://www.youtube.com/watch?v=7GbpOIK9rmg
Arduino Labview interface
http://www.youtube.com/watch?v=n3AwL-_UCS4
Support webpage
https://decibel.ni.com/content/groups/labview-interface-for-arduino
Detailed program tutorial:
http://www.youtube.com/watch?v=RGRhIQneO6w
University of Central Florida
MMAE (Department) Robotic Course Webpage
http://www2.mmae.ucf.edu/wiki/MMAE_Graduate_Robotics
System integration solution
https://decibel.ni.com/
Interface download for Labview 2012:
https://decibel.ni.com/content/thread/13798
Thursday, April 25, 2013
derivation of a template class
template<typename T>
class B {
public:
void f() { }
};
template<typename T>
class D : public B<T> {
public:
void g()
{
f(); // compiler gives an error here
}
};
http://www.velocityreviews.com/forums/t291114-derivation-of-a-template-class.html
class B {
public:
void f() { }
};
template<typename T>
class D : public B<T> {
public:
void g()
{
f(); // compiler gives an error here
}
};
http://www.velocityreviews.com/forums/t291114-derivation-of-a-template-class.html
Nested templates with dependent scope
typedef typename ptrModel<std::vector<Data> >::Type Type;
http://stackoverflow.com/questions/3311633/nested-templates-with-dependent-scope
http://stackoverflow.com/questions/11275444/c-template-typename-iterator
Wednesday, April 24, 2013
C static variable and global variable
A static variable provides a function with "memory" without introducing a global variable that might be accidentally accessed and corrupted by other functions.
In these languages, the term "static variable" has two meanings which are easy to confuse:
- A variable with the same lifetime as the program, as described above (language-independent); or
- (C-family-specific) A variable declared with storage class
static
.
As well as specifying static lifetime, declaring a variable as
static
can have other effects depending on where the declaration occurs:- Static global variable: a variable declared as
static
at the top level of a source file (outside any function definitions) is only visible throughout that file ("file scope", also known as "internal linkage"). - Static local variables: variables declared as
static
inside a function are statically allocated, thus keep their memory cell throughout all program execution, while having the same scope of visibility as automatic local variables, meaning remain local to the function. Hence whatever values the function puts into its static local variables during one call will still be present when the function is called again. - static member variables: in C++, member variables declared as
static
inside class definitions are class variables (shared between all class instances, as opposed to instance variables).
Sunday, April 21, 2013
Saturday, April 20, 2013
Circular buffer and Sliding Sindow Step Detection Implementation
Circular Buffer/Cyclic Buffer/ring buffer
Single fixed size buffer, end to end:
Easily to buffering data streams
FIFO, implementation strategy for a queue
http://en.wikipedia.org/wiki/Circular_buffer
Difficulties
Full/Empty Distinction
Library
http://websvn.hylands.org/filedetails.php?repname=Projects&path=%2Fcommon%2FCBUF.h&rev=0&sc=0%3E
Single fixed size buffer, end to end:
Easily to buffering data streams
FIFO, implementation strategy for a queue
http://en.wikipedia.org/wiki/Circular_buffer
Difficulties
Full/Empty Distinction
Library
http://websvn.hylands.org/filedetails.php?repname=Projects&path=%2Fcommon%2FCBUF.h&rev=0&sc=0%3E
c++ terminate called without an active exception producer/consumer
std::thread - “terminate called without an active exception”, don't want to 'join' it
30.3.1.3 thread destructor [thread.thread.destr]~thread();If joinable() then terminate(), otherwise no effects. [ Note: Either implicitly detaching or joining ajoinable() thread in its destructor could result in difficult to debug correctness (for detach) or performance (for join) bugs encountered only when an exception is raised. Thus the programmer must ensure that the destructor is never executed while the thread is still joinable. — end note ]
What this means is that you should not let threads go out of scope without first calling either
join()
ordetatch()
.Concept of producer/consumer
http://stackoverflow.com/questions/7381757/c-terminate-called-without-an-active-exception-producer-consumer
Thursday, April 18, 2013
Display Number of Processors on Linux
http://www.howtogeek.com/howto/ubuntu/display-number-of-processors-on-linux/
cat /proc/cpuinfo | grep processor | wc -l
The command just looks in the /proc/cpuinfo file, pulls out the number of lines containing the word “processor” and passes them into wc (word count), which returns a count of the CPUs in the system.
Here’s what it returned on my remote server:
[root@root]# cat /proc/cpuinfo | grep processor | wc -l4
Wednesday, April 17, 2013
Tuesday, April 16, 2013
using valgrind to check memory leak
http://cs.ecs.baylor.edu/~donahoo/tools/valgrind/
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./test
Saturday, April 13, 2013
Unused parameter
You can cast function parameters to void to avoid "unused parameter" compile warnings.
Assuming your question was asked given that everything already does compile, I would say code in an order that makes sense to you. Hint: think about incremental testability. The more pieces of functionality you can test along the way, the less time you will spend debugging.
iterator find(const Data& item) const { (void) item; return iterator(nullptr); }
Assuming your question was asked given that everything already does compile, I would say code in an order that makes sense to you. Hint: think about incremental testability. The more pieces of functionality you can test along the way, the less time you will spend debugging.
nullptr Vs. NULL
1. We encourage you to use nullptr as we have C++11, the latest version;
2. Whenever a method receives a pointer as a parameter, it can check whether it is equal to nullptr and do the appropriate thing. This is part of good defensive programming. Don't assume other parts of your program are acting as they should.
2) Are we going to assume that null data will never be inserted in the insert method in BST.hpp?
3. Note that we will not test for this corner case, as we didn't specify that you needed to handle the case in the writeup.
(This is a general rule, we won't hold you responsible for details left unspecified)
However, testing this corner case could be helpful during code development, much as cassert can be useful
The reason you should use nullptr instead of NULL: http://www.devx.com/cplus/10MinuteSolution/35167
2. Whenever a method receives a pointer as a parameter, it can check whether it is equal to nullptr and do the appropriate thing. This is part of good defensive programming. Don't assume other parts of your program are acting as they should.
2) Are we going to assume that null data will never be inserted in the insert method in BST.hpp?
3. Note that we will not test for this corner case, as we didn't specify that you needed to handle the case in the writeup.
(This is a general rule, we won't hold you responsible for details left unspecified)
However, testing this corner case could be helpful during code development, much as cassert can be useful
The reason you should use nullptr instead of NULL: http://www.devx.com/cplus/10MinuteSolution/35167
nullptr: A Type-safe and Clear-Cut Null Pointer
Whether you're using NULL or 0, your C++ apps are subjected to bugs and maintenance problems that are caused by the ambiguity of 0. Find out how to use the nullptr keyword instead of 0 to indicate a null pointer value.
Thursday, April 11, 2013
Tuesday, April 9, 2013
unrecognized command line option “-std=c++11” with g++
http://stackoverflow.com/questions/14674597/cc1plus-error-unrecognized-command-line-option-std-c11-with-g
In versions pre-G++ 4.7, you'll have to use
http://stackoverflow.com/questions/11616539/g-nullptr-was-not-declared-in-this-scope
In versions pre-G++ 4.7, you'll have to use
-std=c++0x
, for more recent versions you can use -std=c++11
.http://stackoverflow.com/questions/11616539/g-nullptr-was-not-declared-in-this-scope
Undefined reference to operator new
Possible solution under Linux
You probably need to link with the C++ support runtime library. This happens automatically when you invoke g++. On linux, this is achieved by adding -lstdc++ flag to the linker. You have to figure out how to do the same on your platform.
You probably need to link with the C++ support runtime library. This happens automatically when you invoke g++. On linux, this is achieved by adding -lstdc++ flag to the linker. You have to figure out how to do the same on your platform.
‘cout’ was not declared in this scope
‘cout’ was not declared in this scope
if you forget to include iostream or to use the std namespace.
WRONG
#include <iostream> // no using namespace std; int main(void) { cout << "A"; return 0; }
ALSO WRONG
using namespace std; // no #include iostream int main(void) { cout << "A"; return 0; }
RIGHT
#include <iostream> // one of two using namespace std; // two of two, yay! int main(void) { cout << "A"; return 0; }
Sunday, April 7, 2013
VHDL: Converting from an INTEGER type to a STD_LOGIC_VECTOR
http://electronics.stackexchange.com/questions/4482/vhdl-converting-from-an-integer-type-to-a-std-logic-vector
use ieee.numeric_std.all;
...
my_slv <= std_logic_vector(to_unsigned(my_int, my_slv'length));
Saturday, April 6, 2013
Multiple declaration of unsigned include via multiple use clauses; none are made directly visible
USE ieee.numeric_std.all;
USE ieee.std_logic_arith.ALL;
Xilinx ISE VHDL to Symbol
http://www.xilinx.com/itp/xilinx10/isehelp/pp_p_process_create_schematic_symbol.htm
Schematic overview
http://www.xilinx.com/itp/xilinx10/isehelp/ise_c_schematic_overview.htm
Related discussion: Conversion of VHDL code to symbol
http://forums.xilinx.com/t5/Design-Entry/Conversion-of-VHDL-code-to-symbol/td-p/29535
ISE quick starting guide
http://cseweb.ucsd.edu/classes/wi11/cse141L/Media/Quick-Tutorial-11.pdf
Schematic overview
http://www.xilinx.com/itp/xilinx10/isehelp/ise_c_schematic_overview.htm
Related discussion: Conversion of VHDL code to symbol
http://forums.xilinx.com/t5/Design-Entry/Conversion-of-VHDL-code-to-symbol/td-p/29535
ISE quick starting guide
http://cseweb.ucsd.edu/classes/wi11/cse141L/Media/Quick-Tutorial-11.pdf
Friday, April 5, 2013
ADXL 345 Application Notes
http://www.analog.com/static/imported-files/application_notes/AN-1063.pdf
Quick Start Guide:
http://www.analog.com/static/imported-files/application_notes/AN-1077.pdf
Data format 16bit (13 bit) /bit
C Programming Sample
http://mbed.org/cookbook/ADXL345-Accelerometer
Quick Start Guide:
http://www.analog.com/static/imported-files/application_notes/AN-1077.pdf
Data format 16bit (13 bit) /bit
C Programming Sample
http://mbed.org/cookbook/ADXL345-Accelerometer
Wednesday, April 3, 2013
NI cRIO setup
General Tutorial of NI cRIO Hardware-Software setup
http://www.ni.com/gettingstarted/setuphardware/compactrio/
Make sure correct version of LabView
http://joule.ni.com/nidu/cds/view/p/id/3769/lang/en
DriverSet Collection Page
http://search.ni.com/nisearch/app/main/p/bot/no/ap/tech/lang/en/pg/1/sn/ssnav:ndr/q/ni-rio/
Version List
ftp://ftp.ni.com/pub/devzone/tut/crio_software_versions.htm
Driver List
http://www.ni.com/nisearch/app/main/p/bot/no/ap/tech/lang/en/pg/1/sn/catnav:du,n8:4.9547.9548,ssnav:ndr/
cRIO crossover cable setup
http://forums.ni.com/t5/LabVIEW/How-to-connect-cRIO-with-Computer-via-cross-cable/td-p/1080146
http://www.ni.com/gettingstarted/setuphardware/compactrio/
Make sure correct version of LabView
http://joule.ni.com/nidu/cds/view/p/id/3769/lang/en
DriverSet Collection Page
http://search.ni.com/nisearch/app/main/p/bot/no/ap/tech/lang/en/pg/1/sn/ssnav:ndr/q/ni-rio/
Version List
ftp://ftp.ni.com/pub/devzone/tut/crio_software_versions.htm
Driver List
http://www.ni.com/nisearch/app/main/p/bot/no/ap/tech/lang/en/pg/1/sn/catnav:du,n8:4.9547.9548,ssnav:ndr/
cRIO crossover cable setup
http://forums.ni.com/t5/LabVIEW/How-to-connect-cRIO-with-Computer-via-cross-cable/td-p/1080146
Monday, April 1, 2013
Daily reading; watchdog Timer and AVR/Arduino
Typically, single-stage watchdog timers are used to simply restart the computer, whereas multistage watchdog timers will sequentially trigger a series of corrective actions, with the final stage triggering a computer restart.
In embedded systems and control systems, watchdog timers are often used to activate fail-safe circuitry. When activated, the fail-safe circuitry forces all control outputs to safe states (e.g., turns off motors, heaters, and high-voltages) to prevent injuries and equipment damage while the fault persists. In a two-stage watchdog, the first timer is often used to activate fail-safe outputs and start the second timer stage; the second stage will reset the computer if the fault cannot be corrected before the timer elapses.
Watchdog timers are sometimes used to trigger the recording of system state information—which may be useful during fault recovery[3]—or debug information (which may be useful for determining the cause of the fault) onto a persistentmedium. In such cases, a second timer—which is started when the first timer elapses—is typically used to reset the computer later, after allowing sufficient time for data recording to complete. This allows time for the information to be saved, but ensures that the computer will be reset even if the recording process fails.
http://arduino.cc/forum/index.php?topic=128717.0
External Hardware WatchDog:
http://www.practicalarduino.com/news/id/471
The official MEGA bootloader does not.
I'm not sure about Leonardo or Due.
http://www.nongnu.org/avr-libc/user-manual/group__avr__watchdog.html
stackexchange Arduino Watch Dog Example
http://electronics.stackexchange.com/questions/18/watch-dog-timer-arduino
http://arduino.cc/forum/index.php?PHPSESSID=c322bf19e2be8fb4daedb3e931a0c8f0&topic=128717.0
Blocking service serial.print in WDT_ISR?
In embedded systems and control systems, watchdog timers are often used to activate fail-safe circuitry. When activated, the fail-safe circuitry forces all control outputs to safe states (e.g., turns off motors, heaters, and high-voltages) to prevent injuries and equipment damage while the fault persists. In a two-stage watchdog, the first timer is often used to activate fail-safe outputs and start the second timer stage; the second stage will reset the computer if the fault cannot be corrected before the timer elapses.
Watchdog timers are sometimes used to trigger the recording of system state information—which may be useful during fault recovery[3]—or debug information (which may be useful for determining the cause of the fault) onto a persistentmedium. In such cases, a second timer—which is started when the first timer elapses—is typically used to reset the computer later, after allowing sufficient time for data recording to complete. This allows time for the information to be saved, but ensures that the computer will be reset even if the recording process fails.
http://arduino.cc/forum/index.php?topic=128717.0
External Hardware WatchDog:
http://www.practicalarduino.com/news/id/471
#include <avr/wdt.h>
void setup ()
{
Serial.begin (115200);
Serial.println ("Restarted.");
wdt_enable (WDTO_1S); // reset after one second, if no "pat the dog" received
} // end of setup
void loop ()
{
Serial.println ("Entered loop ...");
wdt_reset (); // give me another second to do stuff (pat the dog)
while (true) ; // oops, went into a loop
} // end of loop
The more severe issue is, that the bootloader does not de-activate the watchdog upon reset, so that one can end up with endless resets.
the official ATmega328 bootloader (optiboot) deactivates the watchdog on reset.The official MEGA bootloader does not.
I'm not sure about Leonardo or Due.
AVR Watch Dog Macro
stackexchange Arduino Watch Dog Example
http://electronics.stackexchange.com/questions/18/watch-dog-timer-arduino
Watch dog AVR Library
Bootloader Selectionhttp://arduino.cc/forum/index.php?PHPSESSID=c322bf19e2be8fb4daedb3e931a0c8f0&topic=128717.0
A arguable solution?
http://www.jasoncavett.com/2011/03/accessing-watchdog-timer-on-arduino-uno.htmlBlocking service serial.print in WDT_ISR?
Subscribe to:
Posts (Atom)