Tuesday, August 30, 2016

boost shared_memory_object

shm_open is in librt. So compile the program as

 g++ -o test test.cpp -lrt   


Note that "-lrt" needs to be put after -o.

Tuesday, February 9, 2016

How do accelerometer and gyroscope work - an example with MPU6050 and raspberry pi

MPU6050 provides an affordable option for providing accelerometer and gyroscope data for DIY electronics.

1) how does an electronic accelerometer work? The common one is based on piezoelectric effect: this type of material generates a voltage when being applied pressure. Imagine a small mass in the middle of the device. According to Newton's second law F= m a, if there is a finite acceleration a, it will press on the contact piezoelectric material and generate an electric signal. The acceleration is a vector, which has x, y, z components. If you suddenly move the device, generating an acceleration, this will be picked up by accelerometer. But remember, if the device is moving at a constant velocity, this does not have a finite acceleration.  Naturally, there is gravity, or acceleration is g. This will be always picked up by the accelerometer. So, accelerometer measures accelerations (in unit of meter/second^2 or g).

2) how to interpret the MPU6050 accelerometer data? MPU6050 provides acceleration data in x, y, z directions (0x3b, 3d, 3f). Each data has two bytes, or range from [-32768, +32767], which can be calibrated in units of +- 2, 4, 8, 16g.
Let's say the default setting 2g, if MPU6050 is laying flat on a surface, a_x=a_y=0. a_z = g. You should get a reading of a_x=a_y=0, a_z = 16384.

3) how to use the accelerometer data to identify the orientation of the device? Let's start with xy in horizontal plane. The simplest case is to tilt an angle theta along x-axis (x remains horizontal, yz are tilted). Here, the gravity is no longer points to z direction, but certain direction in yz plane. A little trigonometry, a_z = g cos(theta), a_y= g sin(theta), and therefore theta = atan(a_y/a_z).  So we can use a_y and a_z reading to determine the tilt angle from vertical direction. A generic case is to tilt along an arbitrary axis in xy plane. Still, a_z = g cos(theta), sqrt(a_x^2 + a_y^2) = g sin(theta), we have theta = atan(sqrt(a_x^2+a_y^2)/a_z).
In real applications, say an accelerometer in a cell phone or Pad, the chip plane is aligned with LCD monitor (z is perpendicular to LCD), assume x is pointing up in landscape. We can use the following criterion to decide the orientation:
a_x > 0.5g AND |a_y|<0.4g AND |a_z| < 0.4g: pointing up
a_x < -0.5g AND |a_y|<0.4g AND |a_z| < 0.4g: pointing down
a_y < -0.5g AND |a_x|<0 .4g AND |a_z| < 0.4g: left portrait
a_y > 0.5g AND |a_x|<0 .4g AND |a_z|<0.4g portrait right
4) how to determine yaw, pitch, roll? Let me assume roll is an angle to rotate along the airplane direction from head to tail (x-axis), pitch is an angle to rotate along the wing-wing direction(y-axis), yaw is to rotate along the z-axis perpendicular to the airplane. Strictly speaking, pitch, roll, yaw are impossible to determine from accelerometer data due to the order of these rotations are performed. To simplify, let's just do one rotation. For example, for pitch (theta) , if y-axis is horizontal, pitch = atan(a_x/a_z). If we add a roll (phi), a_x = g sin(theta) (still), a_z = g cos(theta) cos(phi), a_y = g cos(theta) sin(phi). So, we estimate pitch(theta) = atan(a_x/ sqrt(a_y^2+a_z^2)).  roll (phi) in this case is atan(a_y/a_z).  Let's say, if we do roll phi first, a_z = g cos(phi), a_y = g sin(phi), and do a pitch theta, a_z=gcos(phi) cos(theta), a_x = g cos(phi)sin(theta). The pitch theta = atan(a_y/a_z) and roll phi = atan(a_y/sqrt(a_x^2+a_y^2). So people normally use atan(a_y/sqrt(a_x^2+a_z^2)) to estimate roll and ... for pitch. It is just an estimation and only applies to small angle. Also, yaw is not really captured by accelerometer. Instead, we use magnetometer(compass) or gyroscope.

5) how does an electronic gyroscope work? MPU6050 uses a MEMS gyroscope. It uses the Coriolis effect, i.e. F= -2 m Omega x V, where F omega V are vectors. The idea is to drive the mass moving along one direction (say x), if there is an angular velocity omega along z direction, a force is generated along y direction, which again can be picked by piezoelectric device.  

6) how to interpret gyroscopic data? So, gyroscope measures the angular velocity, or omega, in unit of degree/second or rad/second). It is also a vector, whose xyz components are the rate change of roll,pitch,yaw angles. The latter are obtained through proper integration. MPU6050 again uses two bytes for each component, or range from [-32768, +32767]. It can be programmed to scale as +-250, 500, 1000, 2000 degrees per second. For the default setting 250 deg/s, 32767/250 ~ 131, and for a reading g_x, the angular speed is g_x*250/32767 or gx/131 deg/s. For example, if the device rotates once in 2 seconds, or 180 deg/s, the reading is 180*131~23600.

7) how to combine accelerometer and gyroscopic data to get a better angle information? Accelerometer has a problem with noisy small-amplitude signals. MEMS gyro has the problem with drifting. A better solution is to combine them
angle = percentage x angle_by_gyro + (1-percentage) x angle_by_accel.
Notice yaw cannot be obtained by accelerometer, but can be done by compass. Also, certain filters can be used to filter out the noisy signals. So, consider to buy an additional magnetometer, or IMU with accel+gyro+compass.

8) programming, there are many excellent resources, such as http://www.invensense.com/products/motion-tracking/6-axis/mpu-6050/ (check the register maps), i2cdevlib.

Monday, May 26, 2014

Rocks cluster cannot ssh to nodes with root account

If you accidentally deleted /root/.ssh directory, the automatic ssh key generator doesn't produce the right permission for all files. This causes a problem to ssh to nodes. In rocks cluster, the root account nodes have a different password (no one knows what it is exactly, since it is never generated or used).

You have to check the right permission :
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
chmod 644 ~/.ssh/authorized_keys
chmod 644 ~/.ssh/known_hosts

Wednesday, June 12, 2013

Linux find and remove

find . -type f -name "FILE-TO-FIND" -exec rm -f {} \;

Monday, April 15, 2013

Install Sun Grid Engine on ubuntu

http://webappl.blogspot.com/2011/05/install-sun-grid-engine-sge-on-ubuntu.html

Monday, March 18, 2013

remove old linux kernel in ubuntu

dpkg -l linux-* | awk '/^ii/{ print $2}' | grep -v -e `uname -r | cut -f1,2 -d"-"` | grep -e [0-9] | xargs sudo apt-get -y remove

Thursday, May 31, 2012

To make Emacs recognize cuda (.cu) files in c++ mode

Much simpler to just put following in your .emacs file: (add-to-list 'auto-mode-alist '("\\.cu$" . c++-mode)) Or alternatively, on a per .cu file basis, you could say just put following in the first line of your .cu files: // -*- c++ -*-

Wednesday, August 19, 2009

Disable the annoying bell sound in bash

In bash, it's common to take the advantage of the "Tab" key helping you to auto-complete the command. Somehow, it will create annoying bell-sound when there are many matches and ...

So, how to disable it. Edit your ~/.inputrc, put something like
set bell-style none
Restart bash, no sound any more!

This applies to bash in both Linux and Mac OS X.

Wednesday, July 8, 2009

Emacs common commands

Search and Replace:
M-x then replace-string RET your_search_string RET your_replace_string

Wednesday, December 17, 2008

Ganglia tricks

Ganglia is a powerful monitoring tool for clusters. Some tricks:

1) remove dead node
try to restart gmond and gmetad.
in /etc/gmond.cong, try to set host_dmax=3600 (sec) in globals.

2) two clusters in one subnetwork
for gmetad to collect data from corresponding cluster (not the other), the trick is to set the TCP/IP port for these two clusters to be different. For instance, the default port is 8649. You can set the second one as 8648. Then, in the server with web frontend, edit gmetad.conf, and put something
data_source "Cluster1" c1_headnode:8649
data_source "Cluster2" c2_headnode:8648
to show all info in one web interface.

Wednesday, December 3, 2008

Create software RAID with mdadm

Here, I will briefly list the steps to create a software RAID5 with mdadm.

1) use "fdisk -l" to check the available hard drives. In my case, I will allocate 4 drives: /dev/sdb, to /dev/sde.
2) to create partitions on raid drives, "fdisk /dev/sdb"; "n->p->1" to create a new partition, /dev/sdb1, "t" to change partition type, use code "fd" to set it as Linux raid autodetect. Do the same for the rest drives.
3) to create the raid "mdadm --create /dev/md0 --chunk=64 --level=raid5 --raid-devices=4 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1". And it will report that /dev/md0 is created and started. (note that it's md0, 0 as number zero, not letter o).
4) "cat /proc/mdstat" and wait for the reshaping process to complete. It will take hours, so don't wait, come back later. After this is done, continue the following.
5) to format the new device "mkfs.ext3 /dev/md0". It will also takes a little while to complete.
6) create a mount point and mount /dev/md0 there. You can also edit /etc/fstab to mount it automatically next time.
7) "mdadm --detail --scan > /etc/mdadm.conf" to save the raid configuration,
8) use "mdadm --detail /dev/md0" to check raid status. also "smartctl --all -d ata /dev/sdb" to check the SMART status of each hard drive.

Emacs problem in Cygwin after rebase

In Cygwin, in order for grace (xmgr) to work, you need to rebase (google it for the guide). But after rebasing, emacs stops working: it hangs there and costs all the CPU. The solution is to reinstall libncurses7. Yeehooo....!

Tuesday, November 18, 2008

Insert driver to Linux initrd.img

Recently, I tried to install Rocks Cluster to some new computers. The problem is that newly introduced motherboards contain gigabit ethernet controllers whose drivers are not included in Linux installer. This gives a lot of troubles in installing the node.

Rocks documentation suggests re-compiling the kernel roll by putting in the device drivers. Somehow, the compiling process works but the installation has problems in finding in libs.

I found an easy way to insert the drivers, i.e., to modify the initrd.img.
The procedures are, for example,

1) in /tmp, mkdir /tmp/initdproject to create a work directory.
2) gunzip < /tftpboot/pxelinux/initrd.img > /tmp/initrdproject/initrd.img.unzipped
3) cd /tmp/initrdproject; mkdir initrd
4) cd initrd; cpio -idv < ../initrd.img.unzipped
5) mkdir /tmp/initrdproject/modules
6) cd /tmp/initrdproject/initrd/modules
7) gunzip < modules.cgz | (cd /tmp/initrdproject/modules && cpio -idv)
8) cp your driver, in my case r8168.ko: cp /somedir/r8168.ko /tmp/initrdproject/modules/2.6.18-53.1.14.el5/x86_64
9) verify your r8168.ko has the right attrib, if not, chmod 744 r8168.ko
10) cd /tmp/initrdproject/modules; find 2.6.18-53.1.14.el5 | cpio -ov -H crc | gzip > modules.cgz
11) copy back modules.cgz to replace the old one: cp /tmp/initrdproject/modules.cgz /tmp/initrdproject/initrd/modules
12)cd /tmp/initrdproject/initrd/modules, change module configuration files as following steps
13) edit module-info, add
r8168
eth
"RTL8168/8111 Gigabit Ethernet"
14) edit modules.alias, add
alias pci:v000010ECd00008168sv*sd*bc*sc*i* r8168
15) edit pci.ide, find 10ec Realtek ...., and add
8168 RTL8168/8111 Gigabit Ethernet
16) ok, pack things back now.
cd /tmp/initrdproject/initrd
find . | cpio --create --format='newc' > ../newinitrd
cd ..
gzip <> initrd.img
17) copy initrd.img back to replace the origin initrd.img (don't forget to backup).

Monday, October 13, 2008

Linux: update drivers

For a updated driver to be corrected recognized in the next boot:

To save the current ramdiscimage and make a new one:
> mv /boot/initrd.img-`uname -r` /boot/initrd.img-`uname -r`.old
> mkinitramfs -o /boot/initrd.img-`uname -r`

Thursday, October 2, 2008

RAID configuration

I will introduce the different RAID configurations commonly used.

1) RAID 0: simply connect several drives together as one big drive.
Good things:
* cost no CPU, so the onboard(motherboard) RAID controller or soft RAID is adequate.
* simple configuration
* sometimes, for big files, the read/write speed is promoted as you are writing to several disks at the same time. In this sense, don't put two drives on a single IDE cable (master and slave) for IDE hard drives.
Bad thing:
* No redundancy. One drive goes bad, all things are lost. You are double, triple .... chances of losing data.

2) RAID 1: simply mirror of two drives. This is a way to protect your data while maybe costs a little machine efficiency.

3) RAID 1+0 (which is also called RAID 10): use several disks as RAID0 and the same number of mirror disks ( RAID 1). Taking the advantage of both speed and safety. But not efficient.

4) RAID 5: RAID 5 is the most popular RAID configuration to take the both advantage of date redundancy (by additional parity data) and the read/write speed by accessing many disks at the same time. A RAID 5 uses block-level striping with parity data distributed across all member disks.
* You will need at least three drives for RAID 5.
* You had better buy a decent RAID controller card (with an own CPU) rather than cheap controllers( 99% of motherboard controllers) because there are a lot of calculations which may instead cost your own CPU.

5) JBOD: JBOD stands for Just a Bunch Of Disks (Just a Box Of Drives). Some RAID controllers use JBOD to refer to configuring drives without RAID features. Each drive shows up separately in the OS. Concatenation or Spanning of disks is not one of the numbered RAID levels, but it is a popular method for combining multiple physical disk drives into a single virtual disk. It provides no data redundancy. As the name implies, disks are merely concatenated together, end to beginning, so they appear to be a single large disk. The good thing compared with RAID 0 is that you can combine different volumes of physical drives (120G, 300G ...) into a single logical drive. This is similar to Logical Volume Manager(LVM) in Linux.

6) Linux soft RAID compared with RAID controller.
It's almost the same unless RAID 5 will cost a little bit of CPU.


FAQ:
1) how to build a RAID 1 for an existing drive?
You will loose the data in existing disk. The proper procedure a) better back up the data first(actually recommended to all raid changes)
b) create the array first with the new drive, c) copy data back d) add the old drive as the missing drive .
in linux, use commands as
mdadm --create /dev/md0 --level 1 --raid-devices=2 missing /dev/sdb2
mdadm /dev/md0 -a /dev/sda2

2) add a new drive to RAID 5 (grow).
IN general, possible. But be sure to check the RAID controller manual to see whether it's supported. If supported, you need to add the drive to the existing RAID in the configuration menu(BIOS).

For soft-RAID, you can skip and boot Linux directly.

The general procedure is 1) to add the drive to the RAID array 2) to reshape the data distributed.

In Linux, use commands like
mdadm --add /dev/md1 /dev/sdx1
mdadm --grow /dev/md1 --raid-devices=n (n = new total numbers of physical drives).
Then wait hours for it to reshape.

3) recover the hard drive failure in RAID 5. If one drive fails, you still have chances to fix it. Normally it's a procedure by replacing the failed drive and rebuilding the data from other drivers. But be sure to read the hardware/software manual first.
In linux a) use # smartctl -cv /dev/hdx to check the failed drive, 2) shutdown the computer and replace
the failed drive. 3) reboot (very slow), partition the new drive as type fd (that is in hexadecimal, aka 0xfd), "Linux raid autodetect". 4) /sbin/mdadm /dev/md0 -a /dev/sdx1.






What is SELinux (Security-Enhanced Linux) ?

What is SELinux (Security-Enhanced Linux) ?
Wiki: Security-Enhanced Linux (SELinux) is a Linux feature that provides a variety of security policies, including U.S. Department of Defense style mandatory access controls, through the use of Linux Security Modules (LSM) in the Linux kernel. It is not a Linux distribution, but rather a set of modifications that can be applied to Unix-like operating systems, such as Linux and BSD. Its architecture strives to streamline the volume of software charged with security policy enforcement, which is closely aligned with the Trusted Computer System Evaluation Criteria (TCSEC, referred to as Orange Book) requirement for trusted computing base (TCB) minimization (applicable to evaluation classes B3 and A1) but is quite unrelated to the least privilege requirement (B2, B3, A1) as is often claimed.[citation needed] The germinal concepts underlying SELinux can be traced to several earlier projects by the U.S. National Security Agency.

More resources:
WIKI

I will take the example of Fedora 9 as an example to explain the settings for SELinux. More to come ....

SELinux and Intel Fortran/C compilers

I have met several problems with SELinux when using Intel Fortran/C compilers on EMT64 systems. For examples, one with the libifcore.so.5 conflict : http://physicscomputing.blogspot.com/2008/04/intel-fortran-compiler-selinux-and.html.

A recent problem is the program stopped after running for a couple of hours, only reporting page errors in /var/log/messages. The system sometimes crashed. After I disabled the selinux, everything is fine.

SELinux has restricted certain access to shared libraries. You can configure it to allow the access to shared libraries. In Fedora Core, you can bring up "system-config-security". If you feel that the computation stability is of the first priority or your computer is well protected(such as nodes in a cluster), you can simply disable SELinux.

Friday, September 26, 2008

Bargain workstations available: MSI introduced a 8DIMM motherboard for Core 2 Quad/Dual systems

MSI launches "P45-8D Memory Lover" motherboard: the world's first motherboard equipped with 8 memory slots. Intel P45 chipsets support DDR2 memories up to 16GB and MSI is the first to make 16GB possible. It is already available on market for around $160 dollars. This provides the cheapest option for workstations. Reason: Core 2 Quad CPUs are cheaper than Xeons; unbuffered non-ECC memories are cheaper than registered ECC memories for servers.

Estimate the cost for a workstation with 16GB memory:
1) get this motherboard for $160
2) CPU, recommand the 1333MHZ core 2 Quad Q9550 ($300) or Q9650($500). 1666Mhz is still very expensive right now.
3) P-45 only supports DDR2 to 16GB while 8GB max for DDR3. Currently, DDR2-1066 only has 1GB/DIMM avaiable. We have to pick DDR2-800 8x2GB: around $300.
4) hard drive: depends, let bugget $150 for a 1TB SATAII drive.
5) a cheap graphic card and computer case around $100.

The total cost is around $1000. Wowwwww

Something to correct: the 8 DIMM slots are actually 4 for DDR2 (up to 16GB) and another 4 for DDR3 (up to 8GB). There are limited types of 4GB/DIMM non-Ecc unbuffered memory available: for instance OCZ 4x4GB PC2-6400 DDR2800, CL 5-6-6-18, for $350.  But I expect more manufacturers will come into this line.  This 4GB chip was useless because Intel chipsets only support up to 8GB mem while nVidia's chipsets (claiming to support up to 16GB) are not popular relatively . But since the introduction of P45 chipset generations, 4GB chips are born! 




Thursday, July 17, 2008

Ultimate pocket gadget: Iphone, Windows Mobile, or Nokia Tablet

Apple has released its 3G versions of Iphone: you probably are already tired of hearing this word, iphone. But yes, I believe this is the one. Oh gee, why only apple makes things right?

OK, I have owned a blackberry, Nokia internet tablet, ATT 8525 Windows Mobile, but I still want an iphone. Here's the story.

What do you want to do with a pocket electronic gadget?
1) communication: to make phone calls, internet chat, messaging, email
2) basic internet surfing, such as checking news headline, weather, traffic, googling
3) maps, GPS is a plus
4) multimedia, such as watching videos,listening to music
5) personal organizer, such as addressbook, calendre, to-do list
6) gaming
7) E-book reader

Let one first review all these products.
1) Blackberry: The advantages of blackberry phones are push-emails and full-qwerty keyboard. But these features have been and will be included in other phones. It allows third-party applications in Java. Unless there is a big development in Symbian OS, I doubt that blackberry phones won't be able to compete with iphone, windows mobile phones in high-end applications for pocket computers. However, blackberry indeed has decent phones, very clear voice and very helpful voice command features. It will remain a good choice for cellular phones with message support. 
2) Windows mobile. I used a Cingular 8525(HTC Hermes). I don't like this bulky phone. Maybe Samsung's models are better. Well, this is Microsoft's Windows. So, almost all feature can be implemented. At the same time, like Windows VS MacOS,  or Zune VS IPOD, don't expect everything to work perfectly. I especially don't like the fact that almost any usable programs in windows mobile is not free. 
3) Nokia internet tablet. I owe a Nokia 770. There are great improvements along this product line to include camera, GPS, Qwerty-keyboard. Big thing, it is a Linux computer! So, you can have all the wonderful and free programs running in Linux. I indeed enjoyed the touchscreen, and a bigger screen( good enough to read ebooks and pdf files).  But this is not a cellular phone (although you can use the phone feature through internet, such as skype, google talk, gizmoproject). I believe Linux in the long run has advantages over Symbian. Nokia, a big fan for Symbian has converted their top intelligent phones to Linux.   

4) Google has created all nice web applications for mobile internet devices. All it need is a platform. It's no surprise that Google will introduce it's own Cellular phone version. Anyway, when you can have Wireless Internet connections everywhere, who will need a cellular phone? 

5) Iphone, almost perfect! While applications are important, they are not all. Imagine there are so many MP3 players in the market, why does IPOD win the major share? I am a big fan for MacOS due to its simple operation and a lot of free applications. MacOS is indeed a powerful Linux + a nice Windows user interface. 

My perfect pocket PC will be
1) having a decent computer system, such as CPU, memory, flash-card support. 
2) running Linux or MacOS, with lots of free application available.
3) big LCD screen while foldable to fit in my pockets. 
4) support external devices, such as printer, monitor, keyboard via USB, bluetooth, ...
5) good touchscreen features, handwriting recognition. I love Apple's two-finger control. Hey, most people has ten fingers, and a mouth, and body movements. Can we use some of these too?

Wednesday, July 16, 2008

Connecting SATA disk array

Let's say, you want an external enclosure for sata drvies and hook up to your computer. Here's the solution.

1) use sata multilane: check this guide. Basically, you can connect four sata drives to four sata ports in your computer with a single sata multilane cable(bridge).

2) use sata port multiplier, check this guide Theoretically, it is possible to hook up 15 sata drives to one sata port on your computer(motherboard or PCI sata adaptor) with port multiplier. Yet, they have to share 3Gbps band width. Actually, commercial SATA disk arrays use port multiplier. Check addnonics as an example: it has internal/external 5x1 port multiplier which allows 5 sata drives on one sata/eSATA port.

3) commercial enclosures can also provide interfaces such as Fiber Channel, SCSI, SAS and Infiniband. Check out some commercial products, such as Sans Digital.

FAQ:
1) how to get an eSATA port?
You can use a SATA to eSATA converter or get an eSATA PCI adaptor.