Director & GPS HOWTO
DRIVERS
Windows: you might need to install one
Mac OS X: you need to install the one that came with the device or with the USB to serial converter.
On OS X any serial device needs a driver from the device manufacturer, because
there no generic driver.
INSTALL THE SERIAL XTRA
Director can process GPS input by way of the serial Xtra. The Xtra extends Director's
functionality.It can be used to access all kinds of micro controllers
You can download
the free trial version at http://www.physicalbits.com
(you need the full version for making executables/projectors but if you just
want to mess around then the free version is enough)
Like all Xtras, this one needs to go to your Xtras folder.
WORKING WITH THE SERIAL XTRA
1. Testing if the serial Xtra works:enter the lingo "showxlib" command
into Directors message window. It should now show up like this:
2. Setting up the movie.
Make a new Director movie.
Doubleclick Frame 5 in the script row
In order to make the script run continously in a loop we need to tell it to stay looping on this frame.
on exitFrame me
go to the frame
end
Then we need to make the script do something. In this case, it will call a handler
called "doStuff" that is contained in a seperate script. Here we are
just making a reference to that script that will then call that script.
3. More setting up....
Go to the "Window" menu and click on "script" in the window that opens, enter
the following lines:
-----------------------------------
-----------------------------------
on startmovie
serialinit
end
--------------------------------------
-- When the movie is being stopped this handler calls the "serialclose"
-- handler that will then close the serial connection
--------------------------------------
on stopmovie
serialclose
end
--------------------------------------
-- This is the "do stuff routine"...
--------------------------------------
on dostuff
readserial
end
--------------------------------------
-- This initializes the serial port when the movie starts
--------------------------------------
on serialinit
end
-----------------------------------------
-- These are the things that are automatically done when the movie is stopped
-----------------------------------------
on serialclose
-- call from stopmovie!
set gSerialPort to 0
closexlib
put "Closed"
end
--------------------------------------
--This is where the serial port is read our main working area...
--------------------------------------
on readserial
--- in theory you should do this but i'm not sure if it can drop data
flushinputbuffer (gSerialPort)
exit
end
--------------------------------------
--------------------------------------
So the main scripframework is now set. We now want to query the serial port for data... this is how we do it:
Fist we must define a global variable. This global variable is put at the very
beginning of the script.
This means that it is accessible for all other handlers in the script and not
just from within a handler.
So we put:
global gSerialPort
Then we modify the serialinit part of the script as follows:
--------------------------------------
-- This initializes the serial port when the movie starts
--------------------------------------
on serialinit
put new(xtra "serialXtra", "/dev/cu.usbserial0") into gserialPort
setProtocol(gSerialPort, 9600, "n", 8, 1)
result=gSerialPort.OpenPort("/dev/cu.usbserial0")
if result <> "Ok" then
put "ERROR: GPS not connected
-- " & result
else
put "GPS connected."
end if
end
-----------------------------------------
If using Windows put:
-----------------------------------
on serialinit
if the platform contains "Macintosh" then
openxlib "SerialXtra"
else
openxlib "SerialXtra.x32"
end if
put new( xtra "SerialXtra", ("modem") ) into
gSerialPort
put gSerialPort
-- you may need to do this to force the port to match your device...
but has worked without before...
SetProtocol (gSerialPort, 9600, "n", 8, 0)
end
4. This was just the serial Xtra initialization. Now we actually try to get the output from the serial Xtra
on readserial
rawinput = ReadString (gSerialPort)
put rawinput
flushinputbuffer (gSerialPort)
exit
end
5.This generates data---- but the data is somewhat messed up because the serial port is queried too often. So we want to query it only once a second. Director handles time in "ticks".
The command "the ticks" stands for the current time. So if you are assigning "the ticks" to a variable, it's like looking on your watch-
so we add a global variable to gSerialPort
global gSerialPort, gSerialCheck
Under "on startmovie" we say that gSerialCheck is "a quick glance on the watch".
on startmovie
-- ticks says what time it is
gSerialCheck=the ticks
serialinit
end
In the dostuff handler we need to write, that we want "readserial"
called up every second. So if the time is 60 ticks (one second) than the previous
time we looked at the watch, the serial port gets read.
After that we need to
change the gSerialCheck variable to include the current time, so that a second
later the script will call "readserial again".
on dostuff
if the ticks > gSerialCheck+60 then
readserial
gSerialCheck = the ticks
end if
end
To pick apart the input we use the character commands in director so "on
readserial" then looks like this:
--------------------------------------
--------------------------------------
on readserial
rawinput = ReadString (gSerialPort)
start = offset ("_", rawinput)
if start = 0 then
input = rawinput
put "satellite found..."
-- figure out Latitude and Longitude
start2=offset("G",input)
delete input.char[start2..input.length]
-- delete all characters after the "@"-sign including
the "@"-sign as well as delete all character preceding
the "N"-sign
start3 = offset ("N",input)
start4 = offset ("@", input)
delete input.char[start4..start3]
--divide the 0000W000 string to get the minutes
lat = input
longi = input
start5 = offset ("W", input)
delete lat.char[start5..lat.length]
delete longi.char[1..start5]
put lat &" "& longi
-- Optional: the formula for calculating decimals out of degrees/minutes/seconds
-- Decimal Degrees = Degrees + minutes/60 + seconds/3600
set the height of sprite 1 to 100
set the width of sprite 1 to 200
set the height of sprite 2 to 100
set the width of sprite 2 to 200
else
delete rawinput.char[start..rawinput.length]
put rawinput
put "satellite not found.offset ==" &
start
end if
flushinputbuffer (gSerialPort)
exit
end
----