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

ISO Date/Time in java

took me a while to figure this one out, you’d think getting a standarized date/time would be a bit simpler…

put the following in a class

private final static SimpleDateFormat dateFormat;
static {
    //as per ISO 8601
    dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    //make sure the formatter requires an exact string
    dateFormat.setLenient(false);
    //set "timezone" to Universal Time Coordinates (GMT adjusted by leap seconds)
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
}

and the following elsewhere in the class:

//to create a string
String nowString = dateFormat.format(new Date());
//to parse a proper string to a date
Date nowDate = dateFormat.parse(nowString);

Java ActionEvents and ActionListeners

an ActionListener is an interface for receiving action events, a class that should process action events should implement this interface, then be registered with a component.

an ActionListener can be registered with any object capable of it, typically this is an instance of an AbstactButton class like a JButton or JMenuItem.
Continue reading ‘Java ActionEvents and ActionListeners’ »

concatenate (join together) several *.pdf files

pdf is a document format. there are occasions where I find it easier to work with one large document rather than several single documents (most recently, while working with several spec sheets for electronic components), the following command will join two or more *.pdfs into a single document using ghostscript.

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf INPUT01.pdf INPUT02.pdf ...

-dBATCH - exit after last file
-dNOPAUSE - no pause after page
-q - “quiet”, fewer messages
-sDEVICE=<devname> - select device, in this case the pdf writer
-sOutputFile=<file> - select output file

the following were not, but could be, included:
-g<width>x<height> - page size in pixels
-r<res> - pixels/inch resolution

Animated Flag

an example of the cloth and wind effects available in Blender 2.45

Blender just keeps getting better…

Pointers and References

declaring the variables:

	int A;
	int *pA;
	int B;
	int *pB;

at this point, A is a container for an integer, and B is a container for a pointer to an integer.

in the body of the code, a “*” can be seen as “the value pointed to by”, and “&” can be seen as “the address of”, as in (using the above delarations):

	A = 100; //directly sets the value of A
	pA = &A; //now pA contains the address of A
	pB = pA; //now pB contains the value of pA, which is the address of A
	B = *pB; //now B contains the value that is pointed to by pB

now both A and Bcontain “100″, and both pA and pB contain the location of A.

 

	cout << "A:  " << A << endl;    //displays value in A
	cout << "B:  " << B << endl;    //displays value in B
	cout << "pA: " << pA << endl;   //displays address in pA
	cout << "pB: " << pB << endl;   //displays address in pB
	cout << "*pA: " << *pA << endl; //displays value pointed to by pA
	cout << "*pB: " << *pB << endl; //displays value pointed to by pB
	cout << endl;

would show something like the following (note that [ADDRESS] would actually be a memory location):

A:  100
B:  100
pA: 0x[ADDRESS]
pB: 0x[ADDRESS]
*pA: 100
*pB: 100

 
at this point, setting the value held by A could be done in any of the following ways:

	A = 200;
	*pA = 200;
	*pB = 200;

*pB can be used to set the value of A because pB contains the address of A.

note that B contains the same value as is in A, but is now independant of it. in the above example, A was set, then pA was set to it’s address, then pB was set to the contents of pA, then B was set the the value pointed to by pB.

converting *.pdf to an image series

pdf is a document format. on the occasion where it is easier to reference an image rather than the full document, the following command will convert the pdf.

convert -density 96x96 -trim INPUTFILE.pdf OUTPUTFILE.jpg

-density is the image density in pixels per square inch
-trim will trim the image edges

the output will be in the form of OUTPUTFILE-#.jpg, where # is an increasing “page” number.
this command will also work for OUTPUTFILE.png, OUTPUTFILE.gif, et. al..

also, imagemagick uses ghostscript to convert, so the quality of the final images will only be as good as ghostscript can provide.

Atmel AVR Device List as of 2008-04-15

Converting *.flac to *.mp3

flac is an audio format (Free Lossless Audio Codec) that is excellent for archiving, but few if any portable players will be able to play it, so a method to convert to mp3 is shown below.

convert .flac to .mp3

flac -cd "FILENAME.flac" | lame -h - "FILENAME.mp3"

flac: -c is output to console, -d is decode
lame: -h is shorthand for “-q 2″ and -q is quality (in this case a high quality), “-” is input from console

same but for all .flac files in a directory

for FILE in *.flac; do $(flac -cd "$FILE" | lame -h - "${FILE%.flac}.mp3"); done

same options in a bash for loop, the ${FILE%.flac} strips “.flac” from the name

Notes:
id3 options for lame:
–tt <title> –ta <artist> –tl <album> –ty <year> –tc <comment> –tn <track> –tg <genre>
will automatically add an id3v1 or id3v2 as needed (choice is based on the size of the fields),
“–add-id3v2″ “–id3v2-only” “–id3v1-only” can be used to modify choice.

more complicated operations are beyond the scope of this article, but I’d suggest using K3B.

AVR Toolchain - Build 2008-04-15

Contains all the tools for developing on the AVR under Linux.

This includes:
AVR-gcc - a full-featured ANSI C compiler.
AVR-Binutils - includes tools capable of linking and managing archives, including handling object code, libraries, profile data, and symbol names.
AVR-Libc - this package provides a subset of the standard C library for Atmel AVR 8-bit RISC microcontrollers.
AVRDUDE - an open source utility to download/upload/manipulate the ROM and EEPROM contents of AVR microcontrollers using the in-system programmer (ISP).
AVR-gdb - a debugger, GDB lets you to see what is going on ‘inside’ another program while it executes.
ddd - this works with command-line debuggers such as GDB to give a graphical front-end.
Simulavr - A simulator for the Atmel AVR family of microcontrollers.
 

Known MCU names:
avr1 avr2 avr3 avr4 avr5 avr6 at90s1200 attiny11 attiny12 attiny15 attiny28 at90s2313 at90s2323 at90s2333 at90s2343 attiny22 attiny26 at90s4433 at90s4414 at90s4434 at90s8515 at90s8535 at90c8534 at86rf401 attiny13 attiny2313 attiny261 attiny461 attiny861 attiny24 attiny44 attiny84 attiny25 attiny45 attiny85 attiny43u attiny48 attiny88 atmega103 at43usb320 at43usb355 at76c711 at90usb82 at90usb162 atmega48 atmega48p atmega8 atmega88 atmega88p atmega8515 atmega8535 atmega8hva at90pwm1 at90pwm2 at90pwm2b at90pwm3 at90pwm3b atmega16 atmega161 atmega162 atmega163 atmega164p atmega165 atmega165p atmega168 atmega168p atmega169 atmega169p atmega32 atmega323 atmega324p atmega325 atmega325p atmega328p atmega329 atmega329p atmega3250 atmega3250p atmega3290 atmega3290p atmega32hvb atmega406 atmega64 atmega640 atmega644 atmega644p atmega128 atmega1280 atmega1281 atmega1284p atmega645 atmega649 atmega6450 atmega6490 atmega16hva at90can32 at90can64 at90can128 at90pwm216 at90pwm316 at90usb646 at90usb647 at90usb1286 at90usb1287 at94k atmega2560 atmega2561
 

core package:
binutils-2.18
gcc-4.2.2
avr-libc 1.6.2
avrdude 5.5


AVR-Toolchain - Core - 2008-04-15 Build Binary Package


AVR-Toolchain - Core - 2008-04-15 Build Binary Package - No Docs

debug package:
ddd-3.3.11
gdb-6.8
simulavr-0.1.2.5


AVR-Toolchain - Debug - 2008-04-15 Build Binary Package


AVR-Toolchain - Debug - 2008-04-15 Build Binary Package - No Docs

 

  • Categories

  • Need Code Written?

  • Need a Coding Job?

  • Tags

  • Archives