(today I had some fun attaching a RC receiver, beat that, IR link:
http://www.youtube.com/watch?v=9Vnl4-xTfhA )
gloomyandy wrote:I think you need to take a step back and...
Explain what it is you are trying to do.
Make a robot that follows GPS waypoints.
gloomyandy wrote:How your robot is constructed.
It's basically a pimped version of the HiTechnic cart.
gloomyandy wrote:How you are trying to run this code (does it run on a pc or directly on the NXT).
Directly on the NXT, together with some other code, which I'll post below.
gloomyandy wrote:Why you are using Mirah and how this fits with leJOS
Because I like it. It just compiles to Java, but does away with the boilerplate.
gloomyandy wrote:What do you expect your robot to do?
Follow waypoints I wrote to a file using another program.
gloomyandy wrote:What does it actually do?
Well, it reads the waypoints, and then drives of in a random direction.
The screen is a little small for debugging.
This is the actual code:
- Code: Select all
import lejos.robotics.localization.PoseProvider
import lejos.robotics.navigation.SteeringPilot
import lejos.robotics.navigation.Navigator
import lejos.nxt.Motor
import lejos.addon.gps.GPS
import lejos.addon.gps.GPSListener
import lejos.robotics.navigation.Pose
import java.lang.Math
import lejos.nxt.comm.Bluetooth
import lejos.nxt.comm.NXTConnection
import java.io.FileInputStream
import java.io.File
import java.io.DataInputStream
import java.io.IOException
import java.io.FileNotFoundException
class GPSPoseProvider
implements PoseProvider
implements GPSListener
def initialize(gps:GPS)
@gps = gps
@pose = Pose.new()
GPS.addListener(GPSListener(self))
end
def getPose
@pose
end
def setPose(p)
@pose = p
end
def sentenceReceived(sen)
heading = @gps.getCompassDegrees
lat = @gps.getLatitude
lon = @gps.getLongitude
y = float(11131949*lat)
x = float(Math.toRadians(6378137.0 * Math.cos(Math.toRadians(lat))))
self.setPose(Pose.new(x,y,heading))
end
end
conn = Bluetooth.connect("Voda GPS", NXTConnection.RAW)
puts "connected!"
stream = conn.openInputStream()
location = GPS.new(stream)
location.setDaemon(false)
p = SteeringPilot.new(8.16, Motor.A, Motor.C, 100, 30, 30)
pp = GPSPoseProvider.new(location)
n = Navigator.new(p, pp)
begin
log = DataInputStream.new(FileInputStream.new(File.new("path.log")))
nil
rescue FileNotFoundException => ex
puts "file not found"
nil
end
while true
begin
x = log.readFloat()
y = log.readFloat()
puts "" + x + ", " + y
n.addWaypoint(x, y)
nil
rescue IOException => ex
puts "end"
break
nil
end
end
n.followPath
and this the code for writing the coordinates:
- Code: Select all
import lejos.nxt.comm.Bluetooth
import lejos.nxt.comm.NXTConnection
import lejos.addon.gps.GPS
import lejos.nxt.LCD
import lejos.addon.gps.GPSListener
import java.io.FileNotFoundException
import java.io.IOException
import java.io.File
import java.io.FileOutputStream
import java.io.DataOutputStream
import lejos.nxt.Button
import java.lang.Math
conn = Bluetooth.connect("Voda GPS", NXTConnection.RAW)
puts "connected!"
stream = conn.openInputStream()
location = GPS.new(stream)
location.setDaemon(false)
begin
log = DataOutputStream.new(FileOutputStream.new(File.new("path.log")))
nil
rescue FileNotFoundException => ex
puts "file not found"
nil
end
while true
b = Button.waitForAnyPress
if b == Button.ID_ENTER
lat = location.getLatitude
lon = location.getLongitude
y = float(11131949*lat)
x = float(Math.toRadians(6378137.0 * Math.cos(Math.toRadians(lat))))
begin
log.writeFloat(x)
log.writeFloat(y)
nil
rescue IOException => ex
puts "file error"
nil
end
else
begin
log.close
nil
rescue IOException => ex
puts "file error"
nil
end
System.exit(0)
end
end