Tuesday, May 10, 2011

Arduino Python Blink - II

My previous post is about one way communications to Arduino using Python. Now I want to make it two way.

Here is the code I loaded my Duemilanove with.

void setup() {
Serial.begin(9600);
}

void loop() {
char i=Serial.read();
if(i=='Y'){
digitalWrite(13, HIGH);
Serial.println("At Your Command, The Lights Are On");
} else if (i=='N') {
digitalWrite(13, LOW);
Serial.println("At your Command, The Lights Are Off");
}
}

Just as in my previous post, I accessed Python through the Terminal. I entered the following sequence of commands.

>>> import serial
>>> se=serial.Serial('/dev/tty.usbserial-A9007RxF',9600)

Now we are set to talk to the serial port.

>>> se.write('Y')

The LED lights up.

>>> se.read()
'A'

That is what Python's response to that command is. Arduino's buffer contains, "At Your Command, The Lights Are On". However, se.read() picked only the first character. So now I say to Python,

>>> se.readline()
't Your Command, The Lights Are On\r\n'

The response to se.readline() emptied the buffer completely with the exception of 'A'. 'A' was emptied by the previous command se.read(). There are more exceptions to this. Now I entered the following commands and obtained the following responses as well.

>>> se.write('N')
1
>>> se.write('Y')
1
>>> se.write('N')
1
>>> se.readline()
'At your Command, The Lights Are Off\r\n'
>>> se.readline()
'At your Command, The Lights Are Off\r\n'
>>> se.readline()
'At Your Command, The Lights Are On\r\n'
>>> se.readline()
'At your Command, The Lights Are Off\r\n'
>>>

Every command generates a serial response and that is stored separately from the responses from other commands. Each readline request will fetch then seperately as can be seem above.

So now Arduino and Python can talk to each other.

Monday, May 9, 2011

Arduino Python Blink

Have been working on Arduino for sometime with reference to automating some aspects of my work.
Have been tinkering with Python just for the sake of it.
So now I want to talk to my Arduino using Python.
For the starters this is what I did.

Loaded this code on my Duemilanove.

void setup(){
Serial.begin(9600);
}

void loop(){
char i=Serial.read();
if (i=='Y'){
digitalWrite(13, HIGH);
} else if(i=='N'){
digitalWrite(13, LOW);
}
}

Dowloaded and installed pyserial.
On a mac, the pyserial tar file downloads to Download folder and unzips to the same folder.
From the terminal: cd /Downloads/pyserial-2.5
From the the terminal: sudo python setup.py install
Lo and behold pyserial is installed.
On terminal: python
That takes us to >>> python prompt.

At the python prompt, either the following can be entered and a py file can be created and run straight from the terminal user prompt.

import serial
arduino=serial.Serial('/dev/tty.usbserial-A9007RxF', 9600)
arduino.write('Y') //This should power the 13
arduino.write('N') //This should power down 13


So simple.
And now to bigger projects... PyRobots?

Wednesday, September 10, 2008

Reinstate user account in Mac OS X Leopard

A note for me on how to reinstate a user account in Mac OS X Leopard that I deleted without creating an account dmg but just a saved user home folder.

If you delete the home folder then the chances of recovery is nil.

Once you delete the account with saved home folder, the home folder is saved in ~/Macintosh HD/Users/user (Deleted)

Now, 
sudo mv "user (Deleted)" user

and get back to the system preferences and create an account with the same user name. Leopard will inform you that a folder of that name exists and will offer you a chance to use that folder as home folder for the new user. Accept it. Voila you get back the old user.

Saturday, January 19, 2008

Google database

Ref: research@google
One of data management issues that plagues anyone new to creating, saving, remote accessing and analyzing data is the confusion between what is the difference between microsoft excel and SQL and related such softies. Often times they tend to think that excel is for beginners and SQL is just the same but high end highly programmable sort of spreadsheet`ing'. This is not absolutely untrue but only 1/100 of the truth. This confusion primarily causes issues for scientists who deal with huge datasets, they often just store them in excel spreadsheets. SQL both server and on machine versions (like SQLite) can store the same datasets that the scientists want stored. Sometimes they can even very efficiently serve it and with a little bit of programming can serve specific bits and pieces of particular characteristics (say, data on a certain day or time, data exceeding or below certain values). SQL based databasing can also automatically collect data from user input on web or automated inputs from instruments. Researchers also want the data analyzed numerically (statistically). And they tend to believe that since it cannot be done directly on SQL databases, that such databasing is useless (useful only for businesses) for them and they prematurely decide to adhere to excel. If one had put in some more effort, he or she might notice that there is something called odbc (usually an intimate component of the SQL programming - I call it SQL command line). Google it and you will know that using odbc one can move SQL datasets between batabases and statistical analysis softwares such as R, Stata or even excel.

Now I notice that google is working towards a scientific database management tactic. Read about it at research.google.com. I am am also so sure that they will let us use a SQL like language to manage the datasets so we can move it between their storage space and google spreadsheet or excel or any statistical software. This would be a tremendous boost to researchers around the world. I am excited because now I can place my raw datasets online and code it such that users around the world will see it the way I want it seen. Cool job Google. 

Saturday, August 11, 2007

Waiting for 'Numbers'

Well finally it is out. Numbers from Apple. Well it is also accompanied by praises to Apple from Macworld, CNet etc etc. What I am wondering is are these guys who reviewed in any ways associated with data analysis on a spreadsheet at all. Apple makes a whole lot of stuff that is 'not just a pretty face'. With Numbers though that is what it is - 'Just a pretty face'. I have been using Excel since mid nineties and it is the ultimate in number storage and analysis for common users. Apple should be aware of the dent it is making into the PC world among the academics. There are brilliant softwares like Chemdraw, Papers, R, Jmp, Stata, Filemaker and many many others that are... to repeat again just brilliant. And then there is this 'I am a mac, I am a PC' ad that ridicules a PC when it says it is going to spend time GUI programming or working on spreadsheets. At a certain point of time computers are not just fun packages but a productivity enhancer. Numbers totally knocked me off. As an academic researcher, my view is that it is no use to any of us. We do not want colorful business graphs. Even the savviest of the business gurus often do not realize the importance of error bars. It does not involve a lot of codes to make it happen. In it's urge to make it simple Apple has dropped out a lot of features like managing the color of individual groups of bars in a graph, ability to control the width of the bars manually, ability to outline a bar individually with no fills at all (it seems to be somewhere hidden inside but not obvious). Numbers must have the ability to quickly scramble a chart out in pdf and must also have formatting features very similar to some of the publication quality graphics (no not the wall street journal graph but at least that). May this is just a first step and Apple will get to make it work like a mash up of both filemaker and excel in the days to come. So I am not impressed but it is a start. Right now I am still struggling to sort just a column of numbers in the ascending to order... hmmm...

Thursday, June 21, 2007

"Waiting for the iphone" - The Mac OS Strategy

Well it is a good feeling to know that iphone will be out there on 29th June 2007 a 6.00 pm. I do want to see at the least how it feels to use it, walk with it, drive with it and have fun and do business with the iphone. Did I say "at the least"? At the same time I also know that it is not possible for me to buy one. This impossibility increases several fold when you live in Canada. No one knows who will sell it, who will serve it, locked... unlocked...

There is are a couple of things about iphone. The buzz it has risen. Some call it hype. It is not. Has there been a similar product before? No. Did anyone anticipate the design and functionality earlier? Sort of no. Even if they had, they had done it in association with the Apple brand. It was only a perception that Apple can do some thing like this. So this is one unique and revolutionary product.

The second interesting aspect is the interest in the iphone itself. There is a huge crowd that has said they would 'like' to have it. They did not say that they will buy it. It is just on their wish-list. I believe that progressively there will be large population of iphone users. But it will be gradual. We have to save the nuggets to buy one. This wish-list gang is not restricted to Mac OS users. And Apple has, to the best of my knowledge, the full but immutable Mac's own OS inside of it. This is a grand design that will invariably let everyone (including the MS-OS users) to see what Mac OS is all about.

Here is the prediction. This will turn numerous MS-OS users to Mac OS. There should be a more perceivable iphone influence on the conversion. The damper would be the cost of the iphone, world-wide marker for the iphone (Someone please tell me what is happening in Canada). I am wishing that the cost is lower and unlocked phones are easily available. Then Apple has a cool way of increasing the Mac OS user base.

Saturday, January 20, 2007

MacFUSE

[Update August 26 2007: Over the past few months there is been a cute .dmg file on macfuse link at the 'google's'. Just use it.]

Rejoice, for a Mac can go anywhere !

Macintosh computer users in UWO can access the panther.uwo.ca to mount their folder and edit webpage/html files on public_html using MacFuse (1), the newly released verison of FUSE that was letting the Linux user the same privilages. With respect to panther.uwo.ca, here is how I did it.

Start with a mac that has 10.4 or higher (sic.) installed.

Naaah... do not geekzap yourself with tar files unless you are one of those who live by the terminal. Just download (Ref. 3) the .dmg file. Mount it and run it.

Open the terminal.app and type in sshfs. If the machine complains that no host has been specified your installation was sucessful. Else, request to see if there is a .bashrc file in your ~ folder. No? Create one. And have this statement placed in it.

export PATH=$PATH:/usr/local/bin/

Now restart the terminal and type the command sshfs. Missing host? Great.

Now to mount my uwo folder, I used, this command.

sshfs -oping_diskarb,volname=Panther arajamoh@panther.uwo.ca: ~/pantheros
Wait for fusefx.kext to load successfully. You can have any folder created with the name of your choice created in the ~/ and replace the pantheros in the command. The remote folder will be mounted in this folder. Also have your volname=. This the name that will be shown in the Finder.

To unmount it use the following command in teh terminal,

umount ~/pantheros

Mind the name I use and you can use.

Now we can mount almost any type of remote folders on a mac and work on it as a local folder.

Reference:

Well we all Google, don't we? Still...

1) http://googlemac.blogspot.com/2007/01/taming-mac-os-x-file-systems.html
2) http://code.google.com/p/macfuse/
3) http://google-code-updates.blogspot.com/2007/01/macfuse-fuse-for-mac-os-x.html

PS: Must say that when I first saw kernalthread.com about 3 years back I knew this guy is a genius. Why else would he use a Macintosh?