Wednesday, February 4, 2015

DSRC VSmart

VSmart Testbed from McGill Lab.
https://sites.google.com/site/xichenmcgill/vsmart

Wednesday, February 26, 2014

Vagrant in Arch Linux

I am using vagrant recently and come across some problems in Arch Linux.
Here I share the experience with all of you.

Before installing vagrant, you should at least have the virtualbox installed. If not, following the official instructions.

There are two important steps here, which cannot be ignored:
Step 1.
Step 2.

Install vagrant


Vagrant is not available in Arch Linux official repository for the moment. However, it can be downloaded from AUR.

The repository provides rpm version, which means we cannot install the package directly by using pacman. As suggested by Peter, using AUR helper is a better way to install the package.

Crate or download your vagrant configuration file.

to download and/or start your guest.
vagrant up

To access the server you deployed.
vagrant ssh

NFS

If you use NFS to synchronize the files between host and guest, you need to install some extra packages in your host and guest.

For arch host, the  following packages should be installed.
pacman -S nfs-utils

pacman -S net-tools


To start nfs, the following services should be start

systemctl start rpc-idmapd.service

systemctl start rpc-mountd.service


To start nfs during the bootup.

systemctl enable rpc-idmapd.service

systemctl enable rpc-mountd.service


For arch guest, the following modules should be installed.
apt-get install nfs-common portmap



Wednesday, September 5, 2012

Get back your privilege for your Ubuntu.

Recently, I lost my sudo privilege for linux system, which make me feel panic. I don't want to reinstall the system again. I got no time to do this.
However, I was lucky enough to find a solution.



In fact it is easy. Restart the system and select recovery mode from grub menu.

Next step, just select fsck so that you can mount / as a writeable directory.

When you finish the step above, select enter the system as root.

sudo addgroup your-username sudo

Now you are fine.

Sunday, June 17, 2012

How to use Rssi value to calculate Distance

Before we start to explain the formula. We have to make it clear about db and dbm.
db is just a kind of ratio between two value. db=10*lg(x/y)
so dbm is a ratio between a power value and 1 mWatt.

0dbm=10*lg(x/1mWatt), so x=1mWatt.
10dbm=10*lg(y/1mWatt), so y=10mWatt.


Thus, the difference between 2 two value in dbm is in db.

10dbm-0dbm=10*lg(x/1mWatt)-10*lg(y/1mWatt)=10*lg(x/y)=10*lg(10mWatt/1mWatt)=10db


Now lets look at the Log-distance path loss model:

PL\;=P_{Tx_{dBm}}-P_{Rx_{dBm}}\;=\;PL_0\;+\;10\gamma\;\log_{10} \frac{d}{d_0}\;+\;X_g,     (1)

PTxdbm is the transmitted power in dbm, and PRxdbm is the received power in dbm.
so the path loss PL is in db.

Path loss, in fact, can be calculated in this formula:

L = 10\ n\ \log_{10}(d)+C    (2)

If we add a reference path loss into the formula, we can have:

PL = PL0 - 10*n*lg(d0) + 10*n*lg(d) + C = PL0 + 10*n*lg(d/d0) + C   (3)

(3) is as same as (1).

We now can use the data from experiment to calculate the distance from Rssi.

Note: PTxdbm is the initial power P for sending the message. PRxdbm is the Rssi value.

First, choose a reference distance d0 and corresponding Rssi value. calculate the PL0 = PTxdbm - PRxdbm


Then, use the other data to make more formulas. We can use this formulas to get the value of γ and Xg

Finally, we can get a function between Rssi and d

Rssi(d) =  P - PL0 - 10*γ*lg(d/d0) + Xg

P is the transmitted power.
PL0 is the reference path loss.
d0 is the reference distance.
 γ is the path loss exponent.

reference:
http://en.wikipedia.org/wiki/Log-distance_path_loss_model
http://en.wikipedia.org/wiki/Path_loss#Loss_exponent

Thursday, May 24, 2012

TinyOS-2.1.1 Installation on Ubuntu 12.04 and Hello World

I am working on TinyOS and nesc recently, but installation confused me for a long time.
Finally, I found a way to make it.
Install TinyOS-2.1.1 on Ubuntu 12.04


  1. add TinyOS-2.1.1 to source list.
    You can open the /etc/apt/source.list to edit the file.
    sudo gedit /etc/apt/sources.list

    and add
     deb http://tinyos.stanford.edu/tinyos/dists/ubuntu natty main

    or you can open ubuntu software centre and choose edit, choose software sources, other software, add, put the above sentence in the dialog. 

  2. open your terminal, type in
    sudo apt-get update
  3. install tinyos
     sudo apt-get install tinyos
  4. add the following lines to ~/.bashrc
    source /opt/tinyos-2.1.1/tinyos.sh
    export CLASSPATH=$TOSROOT/support/sdk/java/tinyos.jar:.


After you finish the above steps, you get TinyOS-2.1.1 successfully on your host. However, when you go to  /opt/tinyos-2.1.1/apps/Blink to compile your first program


make telosb


you may get error like this:

mkdir -p build/telosb
compiling BlinkAppC to a telosb binary
ncc -o build/telosb/main.exe -Os -O -mdisable-hwmul -fnesc-separator=__ -Wall -Wshadow -Wnesc-all -target=telosb -fnesc-cfile=build/telosb/app.c -board= -DDEFINED_TOS_AM_GROUP=0×22 -DIDENT_APPNAME=\”BlinkAppC\” -DIDENT_USERNAME=\”liruan\” -DIDENT_HOSTNAME=\”liruan-desktop\” -DIDENT_USERHASH=0x93f2e5efL -DIDENT_TIMESTAMP=0x4e98142bL -DIDENT_UIDHASH=0x40bf25adL BlinkAppC.nc -lm
In file included from /opt/tinyos-2.1.1/tos/platforms/telosa/PlatformLedsC.nc:38,
from /opt/tinyos-2.1.1/tos/system/LedsC.nc:38,
from BlinkAppC.nc:45:
In component `HplMsp430GeneralIOC’:
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:227: syntax error before `;’
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:321: cannot find `P30′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:322: cannot find `P31′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:323: cannot find `P31′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:324: cannot find `P32′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:325: cannot find `P33′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:326: cannot find `P33′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:327: cannot find `P34′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:328: cannot find `P35′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:330: cannot find `P50′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:331: cannot find `P51′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:332: cannot find `P52′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:333: cannot find `P53′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:334: cannot find `P36′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:335: cannot find `P37′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:337: cannot find `P60′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:338: cannot find `P61′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:339: cannot find `P62′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:340: cannot find `P63′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:341: cannot find `P64′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:342: cannot find `P65′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:343: cannot find `P66′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:344: cannot find `P67′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:346: cannot find `P66′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:347: cannot find `P67′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:349: cannot find `P67′
/opt/tinyos-2.1.1/tos/chips/msp430/pins/HplMsp430GeneralIOC.nc:350: cannot find `P57′
In component `PlatformLedsC’:
/opt/tinyos-2.1.1/tos/platforms/telosa/PlatformLedsC.nc:48: cannot find `Port54′
/opt/tinyos-2.1.1/tos/platforms/telosa/PlatformLedsC.nc:51: cannot find `Port55′
/opt/tinyos-2.1.1/tos/platforms/telosa/PlatformLedsC.nc:54: cannot find `Port56′
make: *** [exe0] Error 1


It is the problem of msp430 packages. The 2.1.1 is good to compile mica, but not telos. 
So, we have to remove msp430 packages and install the old packages.


remove msp430

sudo apt-get autoremove --purge msp430-tinyos

sudo apt-get autoremove --purge msp430-gcc-tinyos

download old packages from here:
double click them in the following order:
tinyos-base
binutils-tinyos
gcc-tinyos
libc-tinyos
tinyos
optional-tinyos

or you can use the following commands(all for 32 bits):

sudo dpkg -i msp430-tinyos-base_2.1-20080806_all.deb 
sudo dpkg -i msp430-binutils-tinyos_2.17-20080806_i386.deb
sudo dpkg -i msp430-gcc-tinyos_3.2.3-20080806_i386.deb
sudo dpkg -i msp430-libc-tinyos_20060801cvs-20080806_i386.deb 
sudo dpkg -i msp430-tinyos_2.1-20080806_all.deb
sudo dpkg -i msp430-optional-tinyos_2.1-20090326_all.deb


Now, you can compile the packages successfully.

Well, when you trying to move the program to the chips, you may run the following command:


make telosb reinstall bsl,DEVICE

where DEVICE is the USB Device path provided from motelist command. example: make telosb reinstall bsl,/dev/ttyUSB0
you may still get errors. mainly because of the permission to access the Device. what you can do is to add the user to the group tty


sudo gpasswd -a username tty


and then give the user the permission to access to the Device

sudo chmod 666 /dev/ttyUSB0

if your Device is ttyUSB0

Now you can find your program copy to the chip.

Sunday, October 23, 2011

placement new


1. What is placement new? placement new is a overriding version of operator new which I mentioned in my last tips. The placementnew can make it possible to "new" a object on the existing memory. For example, you have a block of memory in your code, which you want to "new" a object on it. You can use placement new.

char* pool=new char[sizeof(Bob)]; //initialize a block of memory
Bob* bob2=new(pool)Bob(2); //"new" a Bob in the pool
You can either "new" Bob on stack or on heap. The example is on the heap.
In fact, the placement new is the step 2 of new operator which I mentioned in my last tips. It just invokes the constructor. Since you cannot invoke the constructor explicitly, you can use placement operator instead. 

2. The problem now is how to release these memory.
I found some materials on the internet which confused me a lot. So I did a sample program myself.

#include<iostream>
#include<new>
using namespace std;

class Bob{
 int index;
 char buffer[7];
public:
 Bob(int i){
  index=i;
  buffer[0]='I';
  buffer[1]=' ';
  buffer[2]='a';
  buffer[3]='m';
  buffer[4]=' ';
  buffer[5]='0'+(char)index;
  buffer[6]='\0';
  cout<<"Bob"<<index<<" is born in constructor"<<endl;
 }
 void Say(){
  cout<<buffer<<endl;
 }
 ~Bob(){
  cout<<"Bob"<<index<<" died in destructor"<<endl;
 }
};

void test(){
 char* pool=new char[sizeof(Bob)]; //on heap
 Bob bob1(1); //on stack
 bob1.Say();
 Bob* bob2=new(pool)Bob(2);
 bob2->Say();
 Bob* bob3=new(&bob1)Bob(3);
 bob3->Say();
 /*
 delete bob2;
 */
 //instead of delete bob2 you can also do the following
 bob2->~Bob();
 delete[] pool;
 //we usaully do the later because placement is usaully used to
 //manage the memory, which means we can declare a block of
 //memory from heap in advance, than manage it, when the program
 //is done, we delete the memory.

 //the stuff in the stack will be released automatically
 //(including destructor)

}

void main(){
 test();
 char a;
 a=getchar();
}

 So 
(1).if the memory is on the heap, we usually invoke the destructor explicitly and then delete the block of memory. This is probably the only situation that need to invoke destructor explicitly.
(2).if the memory is on the stack, let it go. It will invoke your destructor automatically. 


Summary:
This is a good way to manage your memory. Just declare a block of memory, do what you want on it, then delete it.

operator new and new operator and overloading


1. new and delete are very complex, it is worth to do some research on it.
first thing I want to mention is about operator new and new operator. 
new operator is a keyword in the C++ just like sizeof, you can do nothing on it(you cannot try to change them). In fact, when you write the expression
Bob* bob=new Bob();
 C++ do 3 steps for you.
First, they allocate a block of memory for you(actually, you will find that this step is invoke the function new operator) , then invoke the constructor, finally, they return you a pointer. 

We cannot change the whole operator new, but we can do something for each step.

for first step, you can overload the new operator, this is an operator just like + operator,you can overload this operator.
So you can do this in your code:
void *operator new(size_t size); 
 //this is most basic overloading for new operator, placement new 
//and something else will come soon in next tips
for the second step you can override you constructor.

third step is nothing but return a pointer. 

2.Delete operator also does the similar things. But the destructor is invoked before the delete operator. One thing you should never forget, if you overload the new operator, please never forget your delete operator! because who knows what you did in your new operator.


let's look some codes here to know what happened.

#include<iostream>
using namespace std;
class Bob{
 int a;
 int c;
public:
 Bob(int a_,int c_){
  a=a_;
  c=c_;
  cout<<"Bob constructor"<<endl;
 }

 void *operator new(size_t size){
  void* p=malloc(size);
  cout<<"Bob new"<<endl;
  return p;
 }
 void operator delete(void* ptr){
  free(ptr);
  cout<<"Bob died in delete"<<endl;
 }
 ~Bob(){
  cout<<"Bob is killed in destructor"<<endl;
 }
};

void main(int argc,char* arg[]){
 Bob* bob=new Bob(1,2);
 delete bob;
 char a;
 a=getchar();
}
result:

Bob new
Bob constructor
Bob is killed in destructor
Bob died in delete