Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Nov 2, 2013

Drone programming

I spent a little time learning how to program a Quadcoptor or Hexacoptor UAVs. We had a small workshop today at GMU. I just started learning how to fly/move/land etc a quadcoptor UAV (Parrot AR Drone 2.0). Quick summary to get things started:
  • Install node.js (build on javacript runtime to make network application)
    • Proceed to installing it with the following steps in terminal
    • ./configure
    •  make
    • sudo make install
  • Download ar-drone library (An implementation of the networking protocols used by the Parrot AR Drone 2.0.)
    • You can do it by another software npm (Node Packaged Modules) which allows user to get node.ja application via command line. Install it first (if you don't have it), then get the ar-drone library using npm:
    • sudo apt-get install npm
    • npm install ar-drone
  • Now create a simple program e.g., test_drone.js which will allow the ar-drone to takeoff and stay hovering for some seconds then land down on earth.
    • var arDrone = require('ar-drone');
      var client  = arDrone.createClient();
      client.takeoff();

      client.after(8000, function() {
          this.stop();
          this.land();
         });
  • Run the javascript from command prompt
    • node test_drone.js
  • Congratulations your drone is flying. You can continue building (see the tutorial) on the program to do some more fun stuffs such as receiving the video output from the drone and make an amazing video of your campus. Here (taken from Quadcoptor and Hexacoptor UAVs) is one that is done in our George Mason University, Fairfax campus.
  • Other reference

Oct 11, 2013

OpenCV: a sample program

A simple program compilation on Ubuntu 12.04. There is an annoying issue where the placement of libraries has to happen before the object needing them. Correct format.
  • g++:
    • g++ `pkg-config --cflags opencv` test.cpp `pkg-config --libs opencv` -o test
  • Makefile:
    •  executable: test.o
          g++ -o executable test.o `pkg-config --libs opencv`
         
      test.o:
          g++ `pkg-config --cflags opencv` -c test.cpp

Nov 10, 2012

Coding smarter

1. Avoiding if else by max of min:
    e.g., if (x-CONST_1 < 0) y = CONST_2 else y = x-CONST_1
    => max(1,x-CONST_1)
   condition  CONST_2 > x-CONST_1

   e.g., if (x+CONST_1 > CONST_3) y = CONST_4 else y = x+CONST_1
    => min(CONST_4,x+CONST_1)
   condition  CONST_4 < x+CONST_1

2. 




Feb 3, 2012

Matlab parsing a string by a delimiter

Reference: regexp
inputTxt=textscan(fid,'%s',1,'delimiter','\n');
input=inputTxt{1};
str = cellstr(input)
disp(input);
sElem = regexp(str, ' ', 'split');
splitedElem = sElem{1};

Jan 26, 2012

Java from command prompt in windows :@

THIS FREAKING PATH VARIABLE UPDATE IS SO ANNOYING. KILL YOU FOOL.
Additional note is that, if java -version, javac -version do not give the same version then you have problem. You should also change/add path to the ../jre/bin path into the PATH variable as well.

Jan 24, 2012

Matlab updating the path directory

Append new directory:
p = path;
path(p, '/Users/hello/Desktop/directory/);

Remove a directory from the path:

rmpath('/Users/hello/Desktop/directory/);


Reference

Dec 2, 2011

(Static vs Dynamic) binding in Java

dynamic-binding-vs-static-binding
Polymorphism via inheritance or interfaces are done by dynamic binding. They are bound during the run time.

late-binding-vs-static-binding
static methods, private methods, member variable initializations are done by static binding. They are done at compile time.
Compiler decides the member variable value depending on the type of reference that has been used when resolving member variable value with the same name from super and sub class.

Polymorphism via Inheritance and Interface

Polymorphism: So it means entity that can have many forms. More specifically, a reference variable that can refer to different types of objects at different points of time. The specific method called through a polymorphic reference can change from one invocation to any subsequent invocations.
Java allows two kinds of polymorphism reference:

Polymorphism through inheritance:
For example think of a class hierarchy Object -> Animal -> Mammal -> Tiger
Animal objAnimal;
Mammal objMammal;
Tiger tigerObj = new Tiger("Bengal Tiger");
objAnimal = tigerObj; // Valid. objAnimal.move() will invoke the move method of Tiger class
objMammal = tigerObj // Valid. objMammal.move() will invoke the move method of Tiger class
objAnimal = new Tiger("Siberian Tiger"); // Valid. objAnimal.move() invoke the move() of Tiger class
Object obj = new Tiger("Thai Tiger"); // Valid. obj.move() will invoke the move method of Tiger class

At any point of time objAnimal can be used polymorphically, because it can refer to Animal object, Mammal object, or Tiger object. Particular version of move method that will be used depends on what kind of reference the objAnimal holds at a particular moment. Usually the children classes over-rides the method definitions. So each of the children in the Animal hierarchy has an different definition of move(). Depending on the reference class it is currently pointing to, a particular implementation of the move will be invoked.

Even if Mammal's move method is abstract it can invoke any of its children's move() (for example Tiger, Lion, Dolphin can have different implementation of move methods. Mammal reference can be used to instantiate any of these three objects and can be used to invoke move method at different points of time.)

Tiger tigerObj2 = objAnimal. //INVALID

Tiger tigerObj2 = (Tiger)objAnimal. //Valid.
But tigerObj may not have all the properties now because it is down-casted from its ancestor. It is less useful and tend to create problems.


Polymorphism through interface:
Interface can also have hierarchy. Child interface inherits all the methods/constants of its parents. No visibility issue since all methods and constant members are public.
An interface can be used to refer to any object of any class that implements it.

public interface Singer{
 public void sing();
 public void scream();
}

Singer singer = new ClassicalMusician(); // Valid
ClassicalMusician class implements the Singer interface. So singer reference can be used to refer to an object of ClassicalMusician.
singer.sing() //Valid
singer = new Cuckoo(); //Valid assuming Cuckoo class implements Singer interface.
singer.scream(); //Valid

Polymorphism via interface is different from polymorphism via inheritance in a sense that here the type of the object that the reference points to at the moment of invocation determines what code will be executed.

Unlike polymorphism via inheritance, we are allowed to invoke only those methods that are defined in the interface (in example more specifically the sing() and scream() methods only). If singer currently refers to an object of Cuckoo class, and singer invokes a singer.buildNest(), method from the Cuckoo, it will produce a compiler error.

However, if we have the knowledge in a particular situation that such invocation is valid, we can cast the object into the appropriate reference so that the compiler will accept it:
(Cuckoo)singer).buildNest(); //Valid. singer is converted to an object of Cuckoo then the buildNest() is invoked.

An interface name can be used as the type of a method parameter. In such situation, any object of any class that implements the interface can be passed into the method.
public void operaSinging(Singer obj) {
   obj.sing();
}

Then we can pass instance of ClassicalSinger or an instance of Cuckoo to the operaSinging() method as a parameter.


Dec 1, 2011

Compiling source files: C/C++ vs Java

Each time I switch between C/C++ and Java, I tend to forget the commands to compile the source files. I should make a post that makes sure I reminds (at least I don't forget) the difference between the two.

C/C++:
If I am trying to compile a file called main.cpp and it uses other file such as helperA.c and helperB.c; I would be using the command:
>>g++ -o main.o helperA.c helperB.c main.cpp

This will create an executable called main.o. I can run this executable using:
>>./main.o

Additional note is that C/C++ uses command line arguments in pointer to character array. An integer argc counts the number of arguments. For example,
int main(int argc, char* argv[]) {...
If I run the program using three arguments then argc will be 4 (3 arguments in addition to the name of the program in argv[0]). The first argv[0] will hold the name of the executable. The three arguments that I passed will be stored in argv[1], argv[2], and argv[3] respectively.

Java:
If I am trying to compile a file called Main.java and it uses other file such as HelperA.java and HelperB.java; I would be using the command:
>>javac -d bin -sourcepath src src/Main.java

Here I am assuming that I have the source files inside the directory src. To indicate that I used -sourcepath option. The compiled Main.class, HelperA.class, and Helper.B files will be created inside the folder bin. And I indicated that using the -d option.

Given the above compiled files, I will get inside the bin folder using the cd command. I can run this executable using:
>>java Main

As opposed to the C/C++ command line argument, Java uses only an array of String type. For example,
public static void main(String[] argv) {...
If I run the program using three arguments, then I can access those arguments inside the main by
argv[0], argv[1], and argv[2] respectively. Index in "String[] argv" starts from 0 as opposed to C/C++, which starts at 1 (index 0 there stores the executable name).

Nov 17, 2011

Junit test in eclipse

You can run a bunch of tests to verify the correctness of the implemented methods in a java class using Junit test. In order to do that, you have to write some junit test code in your program. Here goes the steps:
  • step 1: Download the junit jar file (e.g., you can download this one.)
  • step 2: Add the junit in your project path. (e.g., I already shown in another post how to that.)
  • step 3: Follow the example here to get an idea.
  • step 4: To gain more experience, try this exercise (from the class that I am TAing).

Adding jar files in eclipse

Working with external libraries is a bit hassle when developing project using IDE. This was so annoying which eventually led me to quit the use of IDE. May be I was naive back then to cope up with the additional hassle. Anyway, here goes the stepwise instruction to add external jar files into your project in eclipse.

Sep 1, 2011

Java resources

Java resources
5 things you did't know about ...
New to java technology

Java using eclipse in windows

I run java program in Mac environment from the command prompt. When I started working on java using eclipse in windows environment I encountered with an error: unbound classpath container: JRE system library in windows
It is because the JRE library was not added to the project.
Solution:
Step 1: Go to the "New" drop-down menu
Step 2: Chose "Java Project" option
Step 3: A pop-up window will come. In the "JRE" subwindow, select the option "Use an execution environment JRE".
Step 4: If it is not configured, click on the "Configure JREs".
Step 5: Another window will pop up. Show the path of the jre (e.g., C:\Program Files\Java\JDK_3.6.1\jre\)
Step 6: Complete the remaining step of the project creation (Project name, hit the finish button)
Beginners java program using eclipse

Aug 22, 2011

Adding image in table

\begin{table}[ht]

\caption{A table arranging images}
\centering
\begin{tabular}{cc}
\includegraphics[scale=1]{graphic1}&\includegraphics[scale=1]{graphic2}\\
\newline
\includegraphics[scale=1]{graphic3}&\includegraphics[scale=1]{graphic4}\\
\end{tabular}
\label{tab:gt}

\end{table}
Reference

Equation in multiple lines: Latex eqnarray

\begin{eqnarray}
CCI_{n} &=& PLA*w_{1} + PFT*w_{2} + MCD*w_{3} + \nonumber \\
&& {} + SDCD*w_{4} + MHA*w_{5} + \nonumber \\
&& {} + SDHA*w_{6} + MS*w_{7} + SDS*w_{8}
\label{results_CCI_equation}
\end{eqnarray} \\ \\

Reference: Latex Cookbook

Game engines

Unity engine   Unreal engine Godot engine