Simple File Rename script in Python

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

Raspberry Pi Samba Share

My RPi is the brain of my ARK-AV-01 robot. As I currently am in the programming phase, I found myself programming it from the command line. I wanted something easier, with no login/password, so I activated samba with the following samba.conf file:

[global]
workgroup = mygroup
interfaces = wlan
bind interfaces only = yes
server string = Arkadian's ARK-AV-01
security = share
guest account = nobody
log file = /var/log/samba.%m

[Robot]
comment = Robot Control Center
path = /robot/
available = yes
browseable = yes
public = yes
writable = yes
read only = no
guest ok = yes

The above configuration allows you to connect to the /robot/ folder from any windows or linux machine.

I had to also run the following commands

sudo chown nobody /robot

and

sudo chmod 777 /robot -R

to ensure the folder is owned by user nobody and is writable.

then, just restart the samba service:

sudo service samba restart

And now you can mount the folder /robot to your windows or linux machine!
if everything works ok, make sure to add samba to the startup script:

sudo update-rc.d samba defaults

Robot Challenge 007

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!).

Robot Challenge 005

Today I have quite a bit of work to do on this project. By the end of the day, I should have a fully functional remote control vehicle.

Earlier this morning, I finished the joystick. I will be using a Nexus 7 with touchOSC as my joystick and I put together the following layout:

OSC_Control_Panel

Rotating the “Start” icon to “Full” will wake up the robot.

The three toggle switches underneath will simply be switching between RC mode, Auto mode (just avoiding stuff) and Quest mode (which I will be able to finetune a bit better, for our competition).

The navigation buttons on the right will be doing what you expect: North, East, South, West and their combinations. The red button in the middle will be the stop button.

When the robot is ready, I will add additional pages to the remote control to increase the number of options available to the user (e.g. speech, reactions, specific moves, etc).

There is a great OSC Python library (PyOSC) which makes interfacing the Nexus with python very easy. While OSC was created to control music, you can make it do whatever you want in Python, which is really cool.

By the way, while I’ll be using a Nexus 7 tablet, it’s worth mentioning that TouchOSC works on iPhones and iPads too. Excellent app! :)

Robot Challenge 003

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,

Icons, photos & vectors for projects

This is a post that I will edit from time to time, adding more links as I find them.

1. This company has free and commercial icons: dryicons.com. I like the iconika icons (available in red, blue, green, grey – here is a sample in blue: http://dryicons.com/icon/iconika-blue-icons). One thing I really appreciated from this site, is that you get the option to download the icons at the size you need. This saved me a lot of time messing around with image resizing…

2. Fotolia.com is a good site (the only site I have personally paid so far to buy photos and vectors). Not as cheap as it used to be, but still good value, considering the amount of material that’s posted there.

3. Vector Stock sells vectors from various creators: www.vectorstock.com
I like the concept of the hand-drawn icons from this artist: Azzzya. This is a great place to pick up nice designs for presentation backgrounds and office reports.

4. Another interesting site is Wikimedia. You can use whatever you need, but you may have to search a lot to find something you need/like. Definitely worth looking into, if you have the time.