DS1307 RTC chip from Adafruit

RaspberryPi is a great budget computer. To keep the cost of the unit down, there is no real time clock. This is usually taken care of by the software: the operating system gets the accurate date-time automatically from one of the time servers.
When a RaspberryPi is kept offline but accurate time keeping is needed, an RTC is required. I needed one for Theodora's music box, as I wanted it to play a Happy Birthday tune on her birthday. To do this, I added an RTC module from Adafruit.

I got this unit from Oomlout.co.uk. It's a simple kit, which only takes a few minutes to solder together. Read the adafruit instructions carefully. Note that the resistors are not required for the RPi.

Using the unit in linux is surprisingly straight forward. You can basically read and write the time to this unit from the command line.

The battery should last for at least 10 years!

 

Robot Challenge 008

As promised, here is a video I’ve put together of the RC mode.

Robot Challenge 008 from Arkadian.Eu on Vimeo.

Also as promised, here is the Python code which, when run on a Raspberry Pi, it receives OSC signals from my iPad, interprets them and gets the Arduino board to perform the required action.

This is very rough, and doesn’t not include the code for clockwise and anticlockwise rotation (as I made the addition directly on the raspberry and it’s now out of battery). Clearly, this requires the simpleOSC and pyFirmata libraries to be installed. Most of the code you see here is just a quick mashup, based on examples from the SimpleOSC and pyFirmata libraries. For me the key is that it works! :)

import OSC, time, threading, os, serial
 
import pyfirmata
from pyfirmata import *
from boards import BOARDS
 
class ArduinoMega(Board):
	"""
	A board that will set itself up as an Arduino Mega.
	"""
	def __init__(self, *args, **kwargs):
		args = list(args)
		args.append(BOARDS['arduino_mega'])
		super(ArduinoMega, self).__init__(*args, **kwargs)
 
	def __str__(self):
		return 'Arduino Mega %s on %s' % (self.name, self.sp.port)
 
# Adjust that the port match your system:
PORT = '\\.\COM4' 		#On Windows
#PORT = '/dev/ttyACM0' 	#On RPi
 
# Creates a new board 
board = ArduinoMega(PORT)
 
# ##################################################################
def engine(path, tags, args, source):
	print path, "@TAGS", tags, "@ARGS", args, "@SOURCE", source
	file = "Car_Start_2.mp3"
	print "starting engine"
	#os.startfile(file) # 
 
# ##################################################################
def rcMode(path, tags, args, source):
	print path, "@TAGS", tags, "@ARGS", args, "@SOURCE", source
	print "Switching to RC mode"
	#os.startfile(file)
# ##################################################################
def autoMode(path, tags, args, source):
	print path, "@TAGS", tags, "@ARGS", args, "@SOURCE", source
	print "Switching to AUTO mode"
	#os.startfile(file)
# ##################################################################
def questMode(path, tags, args, source):
	print path, "@TAGS", tags, "@ARGS", args, "@SOURCE", source
	print "Switching to QUEST mode"
	#os.startfile(file)	
 
# LIGHTS SECTION
# ##################################################################
PIN2 = board.get_pin('d:2:p')
def lightR(path, tags, args, source):
	PIN2.write(1 - args[0])
	print path, "@TAGS", tags, "@ARGS", args, "@SOURCE", source	
# ##################################################################
# ##################################################################
PIN3 = board.get_pin('d:3:p')
def lightG(path, tags, args, source):
	PIN3.write(1- args[0])
	print path, "@TAGS", tags, "@ARGS", args, "@SOURCE", source	
# ##################################################################
# ##################################################################
PIN4 = board.get_pin('d:4:p')
def lightB(path, tags, args, source):
	PIN4.write(1-args[0])
	print path, "@TAGS", tags, "@ARGS", args, "@SOURCE", source	
# ##################################################################
# ##################################################################
PIN13 = board.get_pin('d:13:p')
def lightFloor(path, tags, args, source):
	PIN13.write(1 - args[0])
	print path, "@TAGS", tags, "@ARGS", args, "@SOURCE", source	
# ##################################################################
 
 
# MOTOR SECTION
# ##################################################################
PINA = board.get_pin('d:8:s')
PINB = board.get_pin('d:9:s')
PINC = board.get_pin('d:10:s')
PIND = board.get_pin('d:11:s')
 
def motor(aa,bb,cc,dd):
	print aa, bb, cc, dd
 
	if aa==1: PINA.write(20)
	if aa==-1: PINA.write(138)
	if aa==0: PINA.write(91)
 
	if bb==1: PINB.write(20)
	if bb==-1: PINB.write(138)
	if bb==0: PINB.write(91)
 
 
	if cc==1: PINC.write(20)
	if cc==-1: PINC.write(138)
	if cc==0: PINC.write(91)
 
	if dd==1: PIND.write(20)
	if dd==-1: PIND.write(138)
	if dd==0: PIND.write(91)
 
def xmotor(path, tags, args, source):
	direction = path.split("/")[2]
	print "PIN", path.split("/")[2], "@TAGS", 
	print tags, "@ARGS", args, "@SOURCE", source	
 
	if args[0]==0: 
		motor(0,0,0,0)
	else:
		if direction == "nn" 	: motor(1,0,-1,0)
		if direction == "ne"	: motor(1,1,-1,-1)
		if direction == "ee"	: motor(0,1,0,-1)
		if direction == "se"	: motor(-1,1,1,-1)
		if direction == "ss"	: motor(-1,0,1,0)
		if direction == "sw"	: motor(-1,-1,1,1)
		if direction == "ww"	: motor(0,-1,0,1)
		if direction == "nw"	: motor(1,-1,-1,1)
		if direction == "freeze": motor(0,0,0,0)
# ##################################################################	
# ##################################################################
 
def user_callback(path, tags, args, source):
	user = ''.join(path.split("/"))
	print "cb-->",  path, "@TAGS", tags, 
	print "@ARGS", args, "@SOURCE", source
 
def quit_callback(path, tags, args, source):
	#pass
	# don't do this at home (or it'll quit blender)
	global run
	run = False
 
# tupple with ip, port
receive_address = "192.168.0.47", 7110  
 
# OSC Server. there are three different types of server. 
#s = OSC.OSCServer(receive_address) # basic
s = OSC.ThreadingOSCServer(receive_address) # threading
##s = OSC.ForkingOSCServer(receive_address) # forking
 
s.addDefaultHandlers()
 
s.addMsgHandler( "/1/start", engine)
 
s.addMsgHandler( "/1/rc", rcMode) 	
s.addMsgHandler( "/1/auto", autoMode)
s.addMsgHandler( "/1/quest", questMode)
 
s.addMsgHandler( "/1/nn", xmotor)
s.addMsgHandler( "/1/ne", xmotor)
s.addMsgHandler( "/1/ee", xmotor)
s.addMsgHandler( "/1/se", xmotor)
s.addMsgHandler( "/1/ss", xmotor)
s.addMsgHandler( "/1/sw", xmotor)
s.addMsgHandler( "/1/ww", xmotor)
s.addMsgHandler( "/1/nw", xmotor)
s.addMsgHandler( "/1/freeze", xmotor)
 
s.addMsgHandler( "/2/red", lightR)
s.addMsgHandler( "/2/green", lightG)
s.addMsgHandler( "/2/blue", lightB)
 
s.addMsgHandler( "/2/grey", lightFloor)
 
s.addMsgHandler( "/ping", user_callback )
s.addMsgHandler( "/quit", quit_callback )
 
# define a message-handler function for the server to call.
def printing_handler(addr, tags, stuff, source):
	print "---"
	print "received new osc msg from %s" % OSC.getUrlStr(source)
	print "with addr : %s" % addr
	print "typetags %s" % tags
	print "data %s" % stuff
	print "---"
 
s.addMsgHandler("/print", printing_handler) # adding our function
 
 
# just checking which handlers we have added
print "Registered Callback-functions are :"
for addr in s.getOSCAddressSpace(): print addr
 
# Start OSCServer
print "\nStarting OSCServer. Use ctrl-C to quit."
st = threading.Thread( target = s.serve_forever )
st.start()
 
try :
	while 1 : pass
 
except KeyboardInterrupt :
	print "\nClosing OSCServer."
	s.close()
	print "Waiting for Server-thread to finish"
	st.join() ##!!!
	print "Done"

Lines 150-173 are linked to my touchOSC template. For instance “nn” stands for North, “ne” for North East etc…
The TouchOSC editor allows you to give your buttons friendly names. The created template itself, while suffixed with a “.touchosc”, is simply a zipped xml file (in case you want to really fine-tune it!). The editor does a great job – pity it doesn’t work with Android…

Lines 116-124 control the wheels. Basically, 1 is clockwise, -1 is acti-clockwise and 0 is break. The list in motor(1,0,-1,0) includes a value for each one of my four motors. The two rotation functions missing here, simply have motor(1,1,1,1) and motor(-1,-1,-1,-1).

The engine function (line 28) will be playing an mp3 with the sound of a car engine starting, when the virtual engine key is turned. It works fine on Windows and it should be quite easy to get it to work on RPi too.

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 006

Not as productive as I hoped this weekend. I managed to complete the cable harness of the motors and blue leds (which was rather boring but important for the project) and tried out a new look (as I’m moving away from the use of reel cans as my chassis).

Here is the cable harness, before the insulation tape got applied:cable_harness.

The advantage of this harness is that all the motor cables are neatly hidden away and only 4 cables appear on the top surface (one for each motor), plus one for the blue LEDs and the ground cable.

Regarding the new look – I got a few patterns printed on photographic paper and converted them to the side motor panels with a bit of cutting and scoring:
cat_style_corners

Here is how Arkadian-AV1 looks like at the moment (this is still a temporary look):

latest_arkadian-AV1

The image shows an old Freeduino I have for testing. The final robot will be using an Arduino Mega for controlling the motors, reading the sensors and lighting the lights, but all the “thinking” will be done by the Raspberry Pi. rpi_ardmega

Here is a little video, where each motor goes forward for a sec, backwards for a sec, then all the leds are turned on and then the same sequence is repeated on another motor.

Robot Challenge 006 from Arkadian.Eu on Vimeo.

Robot Challenge 004

The chassis is now more or less operational. Here is a video of it being controlled by an arduino.

Robot Challenge 004 from Arkadian.Eu on Vimeo.

In this example, all wheels get the same movement instructions, but, as the weight of the battery is falling mostly on one wheel, the rotation is not perfect.

In any case, this will be fixed when I have the final balanced chassis and of course the compass module connected and the Raspberry Pi module correcting the movement.

Next week, I’ll focus on the second part of the chassis. I will use film reels from PEC:

35mm_2000ft

I’ll have one for the motor section at the bottom reel, a battery section, a brain (RPi & Arduino) section and a sensor reel at the top. The logitech sphere camera will be sitting at the top, giving it a “Cyclop” look.

I’m also planning on adding a few blue LEDs at the bottom reel, which should give the robot a nice under-body glow.

Vex Motors on Arduino

Vex motors (with the Vex Motor Controller 29) are essentially servos. To use them on an Arduino as motors, all you need to do is figure out the “angles” that give you the right speed.

The problem is that all motors are slightly different. To find the right angles for my motors, I simply modified the “knob” example on the Arduino Aplication (1.0.1) to this:

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
#include <Servo.h> 
 
// create servo object to control a servo 
Servo myservo;  
 
// analog pin used to connect the potentiometer
int potpin = 0;  
 
// variable to read the value from the analog pin 
int val; 
 
// vex motor value
int vex;   
 
void setup() 
{ 
  myservo.attach(11); 
  Serial.begin(9600);
} 
 
void loop() 
{ 
  val = analogRead(potpin);            
  vex = map(val, 0, 1023, 0, 179);
 
  myservo.write(vex); 
  Serial.println(String(val) + "  VEX:" + String(vex)); 
  delay(15); 
}

By adjusting the potentiometer a number of “VEX” angle values are generated, from 0 to 180. Make sure that you have the serial monitor up!

My motor was working at the following angles:
20 degrees: move forward
138 degrees: move backward
91 degrees: stop motor

based on the above findings, we can then test the VEX motor by using the following arduino sketch:

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
#include <Servo.h>
Servo Motor; 
 
const int MF = 20;  // angle that moves motor forward
const int MB = 138; // angle that moves motor backward
const int MS = 91; // angle that stops the motor
 
void setup()
{
 // The white pin of the VEX motor controller 29
 // connects to pin 11 (or any other PWM pin)
 Motor.attach(11);
}
 
void loop() 
{
  Motor.write(MF); // move forward
  delay(1000);     
 
  Motor.write(MS); // stop motor
  delay(1000);  
 
  Motor.write(MB); // move backward
  delay(1000);
 
  Motor.write(MS); // stop motor
  delay(1000);   
}