Python

Simple File Rename script in Python

Posted by arkadian on January 23, 2013
Python, examples / Comments Off
import os
indir = 'c:\\datafilepath\\'

for filename in os.listdir(indir):
	if filename.startswith("xyz_"):
		newname = filename.replace("xyz_","abc_")
		try:
			os.rename(indir + filename, indir + newname)
			print "[NEW]", filename, "-->", newname

		except:
			print "[OLD]", filename, "-->", newname

Tags: , ,

Robot Challenge 007

Posted by admin on November 11, 2012
Arduino, Linux, Projects, Python, examples, robotics / Comments Off

I had a productive couple of days. Arkadian AV-1 is now operational as a remote control vehicle.

Arduino
I’m using the StandardFirmata library on the Arduino Mega 2560, with no modifications whatsoever. It was by far the easiest way to get RaspberryPi to control the 4 motors, receive feedback from the 4 sensors (on order from Sparkfun, should be here soon) and have the ability to switch on and off the lights on the robot.

RaspberryPi
The RaspberryPi is loaded with Ladyada’s RPi distro, as it had the latest python library and a few other things that I’m planning on using later on. I have added the PyFirmata library (to control the Arduino) and the OSC library (as described previously) to receive instructions from the OSC pad.

TouchOSC
I have been using TouchOSC to create the remote control interface. My original plan was to use the Nexus tablet to control the robot. The problem is that TouchOSC is a bit behind when it comes to Android. The app is available to buy, but you cannot upload your own templates, which makes it pretty useless. I’ll be using an iPad for now, but I hope they get the android version sorted soon.

Here is my latest layout (first page):
OSC_Control_Panel_004

The two buttons under the “Quest”, are for clockwise and anti-clockwise rotation.

Vehicle is very responsive and drives really well. Orientation gets a bit tricky for the human brain if the rotation buttons are not used, but for the “Quest Mode” this will really be ideal.

I’ll post the code, photos and a video tomorrow.

Next week, I’ll finish off the sensors (proximity and compass), tidy up the cables and put everything in a more permanent position. My code needs a bit of tidying up too; it’s really a mess right now (but, hey, it works!).

Tags: , , , ,

Robot Challenge 003

Posted by admin on October 28, 2012
Arduino, Projects, Python, examples, robotics / Comments Off

Today I worked on a few necessary but rather boring parts of the construction.

I got the Sparkfun compass module from Proto-Pic and spent a few hours setting it up and calibrating it.

I used the HMC5883L Arduino library example to generate the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <Wire.h>
#include <HMC5883L.h>
 
HMC5883L compass;
int error = 0;
 
void setup()
{
  Serial.begin(9600);
  Wire.begin(); // Start the I2C interface.
  compass = HMC5883L(); // Construct a new HMC5883 compass.
  error = compass.SetScale(1); // Set the scale of the compass. Was 1.3 in the original
  error = compass.SetMeasurementMode(Measurement_Continuous); 
}
 
// Our main program loop.
void loop()
{
  MagnetometerRaw raw = compass.ReadRawAxis();
  float heading = atan2(raw.YAxis, raw.XAxis);
 
  if(heading < 0)
    heading += 2*PI;
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
 
  int headingDegrees = int(heading * 180/M_PI); 
  Serial.println("@|"+ String(headingDegrees) + "|#");
 
}

I wasn’t getting the readings I wanted though. I was hoping to get 0 for North, 90 for East, 180 for South and 270 for West, with an offset or something.This is what I was getting: N=330, E=90, S=200 and W=265, which meant that the distances N-E, E-S, S-W, W-N were not 90 and were not equal. Not sure why, but for my project it doesn’t make much difference, given I’ll be using a Raspberry Pi for the brain.

I wrote the following python script for calibrating the arduino results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import serial
from time import sleep
 
def xmap(OldValue, OldMin, OldMax, NewMin, NewMax):
	t =  ((OldValue - OldMin) * (NewMax - NewMin))
	t = t / (OldMax - OldMin) + NewMin
	return t
 
 
ser = serial.Serial(2, 9600, timeout=0)
 
data = ""
while True:
	try:
		data = data + ser.read(9999)
		if data.find("@")>-1 and data.find("#")>-1: 
			data = data.strip(' \t\n\r')
			x = data.split("@")[1]
			x = x.split("#")[0]
			x = x.split("|")[1]
			x = (float(x)/10 + 3)%36
 
			if 0<=x<12: y = xmap(x,0,12,0,90)
			if 12<=x<23: y = xmap(x,12,23,90,180)
			if 23<=x<29.4: y = xmap(x,23,29.4,180,270)
			if 29.4<=x<36: y = xmap(x,29.4,36,270,360)
 
			print int(round(y,0))
			data = ""
	except: pass
 
ser.close()

The above script actually gives very acceptable results. If you use it, just ensure you feed the xmap function the information you observe in your compass.

In the final project, the compass module will be detecting the rotation of the robot and report it to the main python script. if the robot has rotated by mistake then the main python script will make the necessary adjustments.

On the battery front: I received the battery I ordered from VEX. Originally I was thinking of using sealed led acid batteries, but I didn’t want to use high voltage on the motors. Tomorrow I’ll time the battery to see how long it lasts when it powers 2 motors together.

I think I’m now ready to build the body of the robot and setup the main Python script on the Raspberry Pi for the RC mode. When that’s all done, I will add the proximity sensors and the autonomous mode on the main Python script.

The plan is also to buy an Arduino Mega, as I will need 8 digital ports for the motors, 2 ports for the compass,

Tags: , ,

openWRT device from Omnima UK

Posted by arkadian on August 29, 2011
Components & Materials, Linux / Comments Off

29 Aug 2011
Omnima UK sell a tiny little board for less than £40, that runs on openWRT Linux and has ethernet, WiFi and a USB port. It seems to be a great base for simple projects, when we need more power than one arduino.

Link to product

I’ve ordered one to try something I have in mind.

Btw, at the openWRT site, there is a VM available. I downloaded and tried it yesterday, but I had problems connecting USB peripherals…. Never mind. I have to say I loved the web interface and the precompiled binaries and the fact you can ran python on it!

11 Sep 2011
I’ve now received the device. It runs Fonera (have a look at some screenshots and more info here). Easy to access through a web interface. I tried to load the latest Fonera software and it didn’t quite work; now the device won’t boot. I’m able to log in to it using telnet and a serial cable, but I haven’t been able to load the old bin file yet… I’ll keep you posted…

Tags: , ,

Take Snapshots using Python

Posted by arkadian on August 13, 2011
Python, Uncategorized, examples / Comments Off
1
2
3
import ImageGrab
img = ImageGrab.grab()
img.save('arkadian.png','PNG')

A quick and easy way to take a snapshot of your current window.
You can always use JPG, but the quality is better in PNG.

Works in Windows and requires PIL.

Tags: ,

Merging pdf files in Python with pyPDF

Posted by admin on July 05, 2011
Python, examples / Comments Off

Here is a very simple python script that marges two pdf files, using the pyPDF library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os.path
import pyPdf
 
pdfOne = "C:\\a.pdf"
pdfTwo = "C:\\b.pdf"
 
merged = "C:\\c.pdf"
 
if os.path.exists(pdfOne) and os.path.exists(pdfTwo): 
 
	output = pyPdf.PdfFileWriter()
 
	pdfOne = pyPdf.PdfFileReader(open(pdfOne, "rb"))
	for page in range(pdfOne.getNumPages()):
		output.addPage(pdfOne.getPage(page))
 
	pdfTwo = pyPdf.PdfFileReader(open(pdfTwo, "rb"))
	for page in range(pdfTwo.getNumPages()):
		output.addPage(pdfTwo.getPage(page))
 
	outputStream = open(merged, "wb")
	output.write(outputStream)
	outputStream.close()

Tags: , ,