Monday 10 December 2012

Measuring position from Servo using Potentiometer [Voltage divider]


The majority of servo would use potentiometer to measure the position of the servo internally.
So to measure the position of the servo we just need to tap into the voltage divider. One wire to the three largest centre pin and we are done.

Warning always check the maximum voltage of the feedback before hooking the feedback wire to your micro controller. If you are running a 6V servo, plugging the feedback wire into your micro controller will smoke it.

Side Note
Most device when rated at 6V will out put 7V,

Tuesday 4 December 2012

Helicopter Flight Stabilizer Part 1

Electrical Layout Cong's TRex500 Flight Stabilizer


I love flying my helicopter but certain aspect of controlling the helicopter annoyed me.
  1. Moving up rapidly the helicopter twisted left
  2. Moving down rapidly the helicopter twisted to the right
  3. When the wind pressure change, the helicopter bob ups and downs.
  4. To keep the helicopter stable I need to constantly corrected it.
  5. When the voltage are low the helicopter tail move like an axe.
  6. After changing the wings, some servo would require a realignment.
  7. The helicopter should stay still in mid air when there are no input (remote control runs out of battery)
In the coming week I'll attempt to rectify the above issue by rebuilding all the electrical components of the standard helicopter (Head Lock Gryo, Flybarless Module, Radio Receiver). It's going to be rough journey but I'll learn a lot about electronics.

The propose of this post are to help me keep tract of the project and help me remember the plans.
I have a reasonably busy schedule of working, forex, salsa and data collection for forex. In the next few post I'll go into details about each of the components and why they help toward the goals.

Current Task:
Purchase the missing components from Adafruit when  High Side Voltage/Current measurement  is available.

Friday 30 November 2012

Master Master Replication: Unintentional shutdown

This morning I got a rude awakening, a staff call me and told me one of the database server is down. Since I'm not on site, I got her to check if the server was on. The server was dead quite some, instantly I think of power failure. I so got her to turn on the machine beep beep and it fire up like a hurricane.

 It was silly of me to think of power failure, we got a UPS and three power supply for the database server. Once the machine turn on I did some checking and there was nothing wrong with the server at 11:49:03 suddenly all logs just stop.

The only explanation is some one turn off the power via the UPS or unplug all three power supply. I'll investigate when I get to the office.

The most disturbing event is when I check the mysql log and saw the following:
Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave
The message to me means
MySQL server wasn't shutdown properly and your our relay log are corrupted. Please flush your relay cache.

I did a few google search and I saw a really disturbing solution. The solution are to skip all the activity since the issue arise, you never want to do this especially if your replication are SQL base.

The proper solution are to reset the relay log and start replication.

  1. run `stop slave;`
  2. in console run `cat /var/lib/mysql/master.info`
  3. note down the follow master log file, master log position, username and password.
    The list below show the format of the files
    1. dont know dont care
    2. master log file
    3. master log position
    4. slave username
    5. slave password
    6. run `show slave status \G` to check if you master log file and master log position are correct
    7. run `reset slave;`
    8. run `CHANGE MASTER TO MASTER_USER="your user name", MASTER_PASSWORD = "password" , MASTER_LOG_FILE = "the master log file", MASTER_LOG_POS = "the master log position";`
    9. run `start slave;`
    10. run `show slave status \G` everything should work now

If you have a master-master... configuration, the server replicating to the above server will failed. 
With the follow error
Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from impossible position'
To fix simply change the master log file to the master log file and change the master log position to 4.
The good news is when the above happen the master log file is incremented by 1 and the first position of a log file is always 4.

Good Luck

Wednesday 12 October 2011

MySql Replication - Master Master - Producing Conflict

Conflict is when the Master database are not in sync.
MySQL Master Master have a cool conflict. When the latency between two server is high you could get a case where two database
could be modifying the same row and instead of clobbering each other. The data from the database swaped instead.

 
Server A            Server B
row 1: c            row 1: c
change row 1 = a    change row 1 = b
High Latency
get change from b   get change from a
set row 1 = b       set row 1 = a 
row 1: b            row 1: a

Trying it your Self

sql1.example.com
create database test;
use test; 
CREATE TABLE animals (
    id MEDIUMINT NOT NULL AUTO_INCREMENT,
    name CHAR(30) NOT NULL,
    PRIMARY KEY (id)
) ENGINE=MyISAM;
insert into animals set name = "chicken";

Now both database will have one animal in the animals table.
id:1, name:chicken

sql1.example.com
stop slave

sql2.example.com
stop slave

sql1.example.com
update animals set name = "dog" where id=1;

sql2.example.com
update animals set name = "cat" where id=1;

sql1.example.com
start slave

sql2.example.com
start slave


Now the bug kick in and we get a dog in sql2 and a cat in sql1


We can also produce conflicting by simultaneously insert a record in each server with conflicting primary/unique key. 

MySql Replication - Master Master - From Scratch

Installing MySQL Replication
sql1.example.com (ubuntu), sql2.example.com (ubuntu)
aptitude install mysql-server mysql-client

Configuring MySQL
sql1.example.com: my.conf
server-id                       = 1
log-slave-updates
replicate-same-server-id        = 0
auto_increment_increment        = 5
auto_increment_offset           = 1
report-host                     = sql2.example.com
master-host                     = sql2.example.com
master-user                     = username
master-password                 = password
innodb_flush_log_at_trx_commit  = 1
sync_binlog                     = 1
skip-slave-start                = true
binlog-format                   = ROW


log-bin = /var/log/mysql/bin.log
log-bin-index = /var/log/mysql/log-bin.index
log-error = /var/log/mysql/error.log

relay-log = /var/log/mysql/relay.log
relay-log-info-file = /var/log/mysql/relay-log.info
relay-log-index = /var/log/mysql/relay-log.index
 
#comment out
#bind-address 127.0.0.1

sql2.example.com: my.conf
server-id                       = 2
log-slave-updates
replicate-same-server-id        = 0
auto_increment_increment        = 5
auto_increment_offset           = 2
report-host                     = sql1.example.com
master-host                     = sql1.example.com
master-user                     = username
master-password                 = password
innodb_flush_log_at_trx_commit  = 1
sync_binlog                     = 1
skip-slave-start                = true
binlog-format                   = ROW


log-bin = /var/log/mysql/bin.log
log-bin-index = /var/log/mysql/log-bin.index
log-error = /var/log/mysql/error.log

relay-log = /var/log/mysql/relay.log
relay-log-info-file = /var/log/mysql/relay-log.info
relay-log-index = /var/log/mysql/relay-log.index
 
#comment out
#bind-address 127.0.0.1

Restarting MySQL
For newer ubuntu 9.04++, use the following it faster
sql1.example.com , sql2.example.com
restart mysql

On older machine use the following

sql1.example.com , sql2.example.com
/etc/init.d/mysql restart

Creating the replication account
sql1.example.com
mysql -u root -p
CREATE USER 'username'@'sql2.example.com' 
IDENTIFIED BY 'password';
   GRANT ALL PRIVILEGES ON *.* 
TO 'username'@'sql2.example.com' 
WITH GRAND OPTION;
FLUSH PRIVILEGES;

sql2.example.com
CREATE USER 'username'@'sql1.example.com' 
IDENTIFIED BY 'password';
   GRANT ALL PRIVILEGES ON *.* 
TO 'username'@'sql1.example.com' 
WITH GRAND OPTION;
FLUSH PRIVILEGES;

Starting Replication
sql1.example.com, sql2.example.com
mysql -u username -ppassword -e "start slave"

Finish

Thursday 9 December 2010

Ubuntu 10.10, 64Bit, Blue Tooth.

If your blue tooth is not working, here is a solution for you :D, fingers cross and hope it work. I have been searching the web ever since I got my brand new blue tooth keyboard over a year ago. Today I lucky stumble up on www.bluez.org and they just release a new bluez that claim to fix regression. Unfortunately ubuntu don't have it in the repository yet so I have to build it. Excited, I try it out and it works for me, hopefully it work for you too.

The solution is not perfect, but I have restarted my computer a few time and my blue tooth keyboard still worked nicely.

We are going to compile the latest bluez 4.81, with out the regression that have dread the ubuntu blue tooth since version 9.4(that when I got my beautiful keyboard)

Installing some dependency
sudo aptitude install build-essential bison zlib1g-dev zlib1g

Visit http://www.gtk.org/download-linux.html for the latest glib
wget http://ftp.gnome.org/pub/gnome/sources/glib/2.26/glib-2.26.1.tar.gz
cd /glib-2.26.1/
./configure
make 
sudo make install

Visit http://www.bluez.org for the latest drivers
wget http://www.kernel.org/pub/linux/bluetooth/bluez-4.81.tar.gz
tar -xvf bluez*.tar.gz
cd bluez-4.81
./configure
make
sudo make install
Restart your computer and it should work beautifully.

At this point I was feeling pretty happy so I play around abit and screw it up :D
If you are still experiencing problem try the following:
Check hciconfig in console
hciconfig 
hci0: Type: BR/EDR  Bus: USB
 BD Address: 00:1F:81:00:02:50  ACL MTU: 1021:4  SCO MTU: 180:1
 UP RUNNING PSCAN 
 RX bytes:80028 acl:4395 sco:0 events:52 errors:0
 TX bytes:617 acl:12 sco:0 commands:28 errors:0

If you see "UP RUNNING PSCAN", then you can start adding new devices and start using your blue tooth.

Otherwise do the following:
Bring down the interface
sudo hciconfig hci0 down

Bring up the interface
sudo hciconfig hci0 up
Bring the interface up can cause some problems.
If the interface keep timing out pull the blue tooth out and plug it back in.
If hciconfig complain about rfkill do the following
sudo rfkill unblock bluetooth
Go back and check

Hopefully ubuntu team integrate the new bluez fix as soon as possible.

Good Luck.

Wednesday 8 December 2010

Forex MetaTrader 5 HCC to CVS

Meta Trader 5, store history data in HCC files.
The quickest ways to get all the history data is to run a Strategy Tester on the entire history.

History files are store at MetaTrader 5\Bases\MetaQuotes-Demo\history\.

Don't exactly know what's the entire structural, but know enough to extract what we want, the history data.

Data Structural


4 byte, seperator , little endian encoding 18385028, hex(84 88 18 01)
4 byte, time, int divisible by 60
8 byte, double open
8 byte, double high
8 byte, double low
8 byte, double close
1 byte, char | small int spread
1 byte, char | small int tick volume

The data repeat over and over for each day.

I made one in c++, it ugle :D,
Just switch to ubuntu again, lost all my IDE :(
SRC: http://example.citium.net/forex/HccReader.cpp
BIN: http://example.citium.net/forex/HccReader