r4386 - in developers: . alphaone alphaone/u-blox

alphaone at sita.openmoko.org alphaone at sita.openmoko.org
Tue Apr 22 04:50:50 CEST 2008


Author: alphaone
Date: 2008-04-22 04:50:50 +0200 (Tue, 22 Apr 2008)
New Revision: 4386

Added:
   developers/alphaone/
   developers/alphaone/u-blox/
   developers/alphaone/u-blox/ubx-config.rb
Log:
* Utility to configure UBX GPS receivers


Added: developers/alphaone/u-blox/ubx-config.rb
===================================================================
--- developers/alphaone/u-blox/ubx-config.rb	2008-04-21 14:34:05 UTC (rev 4385)
+++ developers/alphaone/u-blox/ubx-config.rb	2008-04-22 02:50:50 UTC (rev 4386)
@@ -0,0 +1,164 @@
+#!/usr/bin/ruby
+# ubx-config.rb - Utility to configure advanced features of u-blox GPS chipset
+#
+# Copyright 2008 OpenMoko, Inc.
+# Authored by Daniel Willmann <daniel at openmoko.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+gem_original_require 'serialport'
+
+UBX_CLASS = {
+	:NAV => 0x01,
+	:RXM => 0x02,
+	:INF => 0x04,
+	:ACK => 0x05,
+	:CFG => 0x06,
+	:UPD => 0x09,
+	:MON => 0x0a,
+	:AID => 0x0b,
+	:TIM => 0x0d,
+	:USR => 0x40
+}
+
+UBX_ID = {
+	:ACK => 0x01,
+	:NACK => 0x00,
+	:ALM => 0x30,
+	:DATA => 0x10,
+	:EPH => 0x31,
+	:HUI => 0x02,
+	:INI => 0x01,
+	:REQ => 0x00,
+	:ANT => 0x13,
+	:CFG => 0x09
+}
+
+UBX_REVSTR = {
+	[:AID, :ALM, 1] =>
+		["C", [:SVID]],
+	[:AID, :ALM, 8]  =>
+		["VV", [:SVID, :WEEK]],
+	[:AID, :ALM, 40] =>
+		["V"*10, [:SVID, :WEEK, :DWRD0, :DWRD1, :DWRD2, :DWRD3, :DWRD4, :DWRD5, :DWRD6, :DWRD7]]
+}
+
+UBX_FWDSTR = {}
+UBX_REVSTR.each_pair { |key, value|
+	newkey = key[0..1] << value[1].length
+	UBX_FWDSTR[newkey] = value
+}
+
+class Message
+	SYNC1=0xb5
+	SYNC2=0x62
+	attr_accessor :data
+	def initialize(cl, id, data)
+		@cl = cl
+		@id = id
+		@data = data
+	end
+
+	def checksum(str)
+		@ck_A = 0
+		@ck_B = 0
+		str[2..-1].each_byte { |el|
+			@ck_A = @ck_A + el
+			@ck_B = @ck_B + @ck_A
+		}
+		@ck_A = @ck_A&0xff
+		@cl_B = @ck_B&0xff
+		return [@ck_A, @ck_B].pack("CC")
+	end
+
+	def to_a()
+		return [SYNC1, SYNC2, @cl, @id, @data]
+	end
+
+	def to_s()
+		if @data.length > 0
+			(data_fmt, data_names) = UBX_FWDSTR[[@cl, @id, @data.length]]
+			data_packed = @data.pack(data_fmt)
+		else
+			data_packed = ""
+		end
+		result = [SYNC1, SYNC2, UBX_CLASS[@cl], UBX_ID[@id], data_packed.length].pack("CCCCv") + data_packed
+		result << checksum(result)
+	end
+
+	def Message.parse(data)
+		if (data.length < 8)
+			return [nil, 0]
+		end
+		i = data.index([SYNC1, SYNC2].pack("CC"))
+		if i == 0
+			(cl, id, len) = data.unpack("xxCCv")
+			if data.length < len+8
+				return [nil, 0]
+			end
+
+			cl = UBX_CLASS.index(cl)
+			id = UBX_ID.index(id)
+			if len > 0
+				(data_fmt, data_names) = UBX_REVSTR[[cl, id, len]]
+				payload = data[6..len+6].unpack(data_fmt)
+			else
+				payload = []
+			end
+
+			msg = Message.new(cl, id, payload)
+			return [msg, len+8]
+		elsif not i 
+			return [nil, data.length]
+		else
+			return [nil, i]
+		end
+	end
+end
+
+device = SerialPort.new(ARGV[0], baudrate=9600, databits=8, stopbits=1, parity=SerialPort::NONE)
+device.read_timeout=-1
+
+worker = Thread.new() {
+	stream = []
+	while true
+		begin
+			stream << device.getc
+			(msg, len) = Message.parse(stream.map {|i| i.chr}.to_s)
+			if msg
+				puts msg.inspect
+			end
+			stream = stream[len..-1]
+		rescue
+			puts "An error occurred: #{$!}"
+			stream = [ ]
+		end
+	end
+}
+
+sleep(2)
+
+foo = Message.new(:AID, :ALM, [])
+
+while true
+	puts "Submitting Almanach request"
+  device.write(foo.to_s)
+  device.flush()
+	sleep(3)
+	bar = Message.new(:AID, :ALM, [1, 1234, 1, 2, 3, 4, 5, 6, 7, 8])
+	device.write(bar.to_s)
+	device.flush()
+	sleep (2)
+end


Property changes on: developers/alphaone/u-blox/ubx-config.rb
___________________________________________________________________
Name: svn:executable
   + *





More information about the commitlog mailing list