Archive for November 2008

A Makefile for NXC code

# A Makefile to compile NXC code using NBC
# Copyright (C) 2008 Gary French
#
# 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.
#

INCLUDEDIR =../include
NBC=/usr/local/bin/nbc
NBC_FLAGS=-I=$(INCLUDEDIR)
#optional compiler optimizations
# NBC_FLAGS=-I=$(INCLUDEDIR) -Z1
# NBC_FLAGS=-I=$(INCLUDEDIR) -Z2

#this can also be explicitly set
# NXC_FILES := TheProgram.nxc
NXC_FILES := $(shell ls -1 -t -F *.nxc  2>/dev/null | grep '\.nxc')
RXE_FILES := $(NXC_FILES:.nxc=.rxe)

BYTECODE_FILES := $(NXC_FILES:.nxc=.bytecode)
LISTING_FILES := $(NXC_FILES:.nxc=.listing)
SYMTABLE_FILES := $(NXC_FILES:.nxc=.symtable)

#the targets can be called individually, or called from the "all" target
#all: compile bytecode listing symtable
all: compile

compile: $(RXE_FILES)
%.rxe: $(NXC_FILES)
	@echo "compiling $<"
	$(NBC) $(NBC_FLAGS) -O=$@ $<
	@echo

bytecode: $(BYTECODE_FILES)
%.bytecode: $(NXC_FILES)
	@echo "generating bytecode for $<"
	$(NBC) $(NBC_FLAGS) -b -nbc=$@ $<
	@echo

listing: $(LISTING_FILES)
%.listing: $(NXC_FILES)
	@echo "generating code listing for $<"
	$(NBC) $(NBC_FLAGS) -b -L=$@ $<
	@echo

symtable: $(SYMTABLE_FILES)
%.symtable: $(NXC_FILES)
	@echo "generating symbol table for $<"
	$(NBC) $(NBC_FLAGS) -b -Y=$@ $<
	@echo

clean:
	@echo "cleaning up..."
	-rm $(RXE_FILES)
	-rm $(BYTECODE_FILES)
	-rm $(LISTING_FILES)
	-rm $(SYMTABLE_FILES)
	@echo

.PHONY: clean bytecode listing symtable

Compiling and Transfering NXC Code to a NXT-Brick Under Fedora Linux

you must be able to communicate with the NXT Brick for any of the following to be applicable. there are a few “how-to”s available, including this post.

I am using the compiler available at Next Byte Codes & Not eXactly C site. This is a simple package package to “install” (simply copying the executable from the tar/gzip to /usr/local/bin did it for me), and is wonderfully documented.

I am using a modified version of a script provided by Jan-Klaas Kollhof:

#!/bin/sh
set -e

_nbcpath=/PATH/TO/NBC/EXECUTABLE/DIRECTORY

_dirname=`dirname $1`
if [ "${_dirname}" == "." ]; then
	_dirname=$(pwd)
fi
_basename=`basename $1 .nxc`

echo "compiling ${_basename}.rxe from ${_basename}.nxc..."
${_nbcpath}/nbc \
	-O="${_dirname}/${_basename}.rxe" \
	-nbc="${_dirname}/${_basename}.nbc" \
		"${_dirname}/${_basename}.nxc"

echo "sending ${_basename}.rxe to brick..."
cd "${_dirname}"
nxt_push "${_basename}.rxe"

as should be apparent, /PATH/TO/NBC/EXECUTABLE/DIRECTORY will need to be altered to suit your environment.

the “-nbc” switch will save the generated “Next Byte Code” to YOUR_CODE_FILE_NAME.nbc. the resulting file is not only interesting to poke around in, but may help when optimizing for size.

currently, I am having some issues with nxt_python, documented here.

nxt_pull, a companion program for nxt_push

while working with nxt_python and a few applications written in NXC, I found I had a need to get files from the NXT Brick with a minimum of fuss. The following python script will “pull” a single file from the NXT Brick. Wildcard behavior is undefined, and is planned for version 0.2.

example usage:

[gary@motoko]# nxt_pull log.txt

code:

#!/usr/bin/env python
#
# nxt_pull:
#   Pull a file to a LEGO Mindstorms NXT brick,
#   based on Douglas P Lau's nxt_filer and nxt_push
#   Copyright (C) 2008 Gary French
#   Version: 0.1
#
# 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.

import sys
import nxt.locator
from nxt.brick import FileFinder, FileReader, FileWriter

def read_file( b, fname ):
	r = FileReader(b, fname)
	r.__enter__()
	try:
		f = open(fname, 'wb')
		print 'Pulling %s ...' % (fname),
		try:
			size = 0
			for data in r:
				f.write(data)
				size += len(data)
		finally:
			f.close()
			print 'read %d bytes' % size
	finally:
		r.__exit__(None, None, None)

if __name__ == '__main__':
	sock = nxt.locator.find_one_brick()
	#sock = nxt.locator.find_one_brick(host='00:16:53:XX:XX:XX')
	if sock:
		b = sock.connect()
		read_file( b, sys.argv[1] )
		sock.close()

Error: “bluetooth.BluetoothError: (115, ‘Operation now in progress’)” while using nxt_python

currently, to avoid a “bluetooth.BluetoothError: (115, ‘Operation now in progress’)” error, I have to:

  1. turn the Brick’s Bluetooth off
  2. turn off the Brick
  3. restart Bluetooth services
  4. turn on the Brick
  5. turn the Brick’s Bluetooth on

it acts as if something is not cleaning up after itself properly.
I’m sure I’ll figure that one out, but I have more pressing concerns at the moment. if anyone has some insight on this issue I’d appreciate the comment.

current workaround:

  • use “hcitool scan” to find the MAC
  • modify nxt_push line 49 from:
    sock = nxt.locator.find_one_brick()

    to:

    sock = nxt.locator.find_one_brick(host='00:16:53:XX:XX:XX')

as one might suspect, this workaround is applicable in any other similar situation.

Setting up Bluetooth communication with a NXT Brick under Fedora linux

it is vital that the following python packages are installed:

to test PyBlueZ, the following can be used (code by Albert Huang <albert@csail.mit.edu>)

#!/usr/bin/env python
import bluetooth
print "performing inquiry..."
nearby_devices = bluetooth.discover_devices(lookup_names = True)
print "found %d devices" % len(nearby_devices)
for name, addr in nearby_devices:
    print "  %s - %s" % (addr, name)

as I do not care for gnome, my default desktop is KDE,
what follows is my procedure for pairing with a NXT Brick.

[root@motoko]# /usr/bin/bluetooth-applet &

a Bluetooth icon will appear in your system tray,

right-click the Bluetooth icon and click “Preferences” from the popup.

  • set the radio button under “Mode of operation” to be “Visible and connectable for other devices”
    (even if it ought to be “connectible”)
  • click “Close”

on the NXT Brick, from the root menu,

  • select “Bluetooth” and press the square button.
  • select “Search” and press the square button.
  • after it has finished searching, select the name that matches your computer and press the square button.
  • select “[1]” and press the square button.
  • wait for it to connect.
  • enter passkey, or press the square button for the “1234″ default.
  • Bluetooth icon will begin to blink, click it and enter the passkey in the resulting dialog.
  • right-click the Bluetooth icon and click “Preferences” from the popup.
    • select the NXT Brick in the “Bonded devices” box.
    • Click “Set Trusted”
    • click “Close”

the connection can now be tested with the following python code:

#!/usr/bin/env python
import nxt.locator
sock = nxt.locator.find_one_brick()
if sock:
	brick = sock.connect()
	name, host, signal_strength, user_flash = brick.get_device_info()
	print 'NXT brick name: %s' % name
	print 'Host address: %s' % host
	print 'Bluetooth signal strength: %s' % signal_strength
	print 'Free user flash: %s' % user_flash
	sock.close()

NXT Puzzlebot

not much yet, but with two development forks we’ll probably have something workable soon.
 
 
as of 11/16/2008:
a few test ideas.


 
 
as of 11/22/2008:
some general refinements, and the software to raise and lower the whatever-the-hell-that-is is working.

 
 
PuzzleBot - 2008-11-28

  • Categories

  • Need Code Written?

  • Need a Coding Job?

  • Tags

  • Archives