- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import javaclient3.PlayerClient;
- import javaclient3.PlayerDevice;
- import javaclient3.Position2DInterface;
- import javaclient3.RangerInterface;
- import javaclient3.SonarInterface;
- import javaclient3.structures.PlayerConstants;
- public class SafeGTo {
- static boolean done=false;
- @SuppressWarnings("deprecation")
- public static void main(String[] args) {
- ArrayList<ArrayList<Double>> points =new ArrayList<ArrayList<Double>>();
- PlayerClient pc;
- pc = new PlayerClient("lewis.rit.edu",6665);
- try{
- FileInputStream fstream = new FileInputStream(args[0]);
- DataInputStream in = new DataInputStream(fstream);
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String strLine;
- int n=0;
- ArrayList<Double> temp= new ArrayList<Double>();
- while ((strLine = br.readLine()) != null) {
- for(int i=0;i<strLine.split(" ").length;i++){
- temp.add(Double.valueOf(strLine.split(" ")[i]));
- }
- System.out.println(temp.toString());
- ArrayList<Double> element = new ArrayList<Double>();
- element.addAll(temp);
- points.add(element);
- temp.clear();
- n++;
- }
- in.close();
- }catch (Exception e){//Catch exception if any
- System.err.println("Error: " + e.getMessage());
- e.printStackTrace();}
- Position2DInterface pos = pc.requestInterfacePosition2D(0,PlayerConstants.PLAYER_OPEN_MODE);
- pos.setMotorPower(1);
- SonarInterface ranger =pc.requestInterfaceSonar(0,PlayerConstants.PLAYER_OPEN_MODE);
- while(pc.requestInterfaceSonar(0,PlayerConstants.PLAYER_OPEN_MODE)==null){
- ranger = pc.requestInterfaceSonar(0,PlayerConstants.PLAYER_OPEN_MODE);
- }
- double turnrate = 0;
- double fwd = .0;
- double speeds[]={};
- float[] ranges={};
- pc.readAll();
- for(int n=0;n<points.size();n++){
- System.out.println("point X: " + points.get(n).get(0) + " point Y: " + points.get(n).get(1));
- done=false;
- while(!done){
- double sumx = 0;
- double sumy = 0;
- double angle;
- double oXY[]={0.0,0.0};
- double x;
- double y;
- double goalvect[]={0.0,0.0};
- double objvect[]={0.0,0.0};
- ArrayList<Double> finalvect=new ArrayList<Double>();
- pc.readAll();
- //!pos.isDataReady()
- if (!ranger.isDataReady() || !pos.isDataReady()){
- continue;
- }
- ranges = ranger.getData().getRanges();
- for(int i=0;i<=7;i++){
- if(ranges[i]> .7){
- continue;
- }else{
- System.out.println("I have detected an object!");
- angle =get_range_angle(i,ranges);
- oXY=get_XY(ranges[i],angle);
- x=(oXY[0]/(Math.pow(oXY[0], 2))+(Math.pow(oXY[1], 2)));
- y=(oXY[1]/(Math.pow(oXY[0], 2))+(Math.pow(oXY[1], 2)));
- sumx+=x;
- sumy+=y;
- }
- }
- objvect[0]=sumx;
- objvect[1]=sumy;
- double d=Math.sqrt(Math.pow(points.get(n).get(0),2)+Math.pow(points.get(n).get(1), 2));
- goalvect[0]=(points.get(n).get(0)-pos.getX())/d;
- goalvect[1]=(points.get(n).get(1)-pos.getY())/d;
- double newangle=Math.atan2(goalvect[1],goalvect[0])-pos.getYaw();
- goalvect[0]=Math.cos(newangle)*1;
- goalvect[1]=Math.sin(newangle)*1;
- //System.out.println("goal x local " +goalvect[0] + "goal y local" + goalvect[1]);
- finalvect.add(0,(.7*goalvect[0])-(.2*objvect[0]));
- finalvect.add(1,(.7*goalvect[1])-(.2*objvect[1]));
- ArrayList<Double> goal=new ArrayList<Double>();
- goal=points.get(n);
- speeds=goTo(finalvect,pos,turnrate,fwd,goal);
- turnrate=speeds[0];
- fwd=speeds[1];
- pos.setSpeed(fwd,turnrate);
- }
- }
- }
- private static double[] get_XY(double d, double angle) {
- double xy[]={0.0,0.0};
- xy[0]=d*Math.cos(angle);
- xy[1]=d*Math.sin(angle);
- return xy;
- }
- private static double get_range_angle(int i, float[] ranges) {
- double angle = 90-(180/8)*i;
- return (angle*Math.PI/180);
- }
- public static double [] goTo(ArrayList<Double> points,Position2DInterface pos,double curturn,double curfwd, ArrayList<Double> goal){
- double speeds [] = {0.0,0.0};
- double EPSILON = 0.01;
- //double distance=Math.sqrt((points.get(0)-pos.getX())+(points.get(1)-pos.getY()));
- double angle=Math.atan2(points.get(1),points.get(0));
- //System.out.println("The Angle before: "+Math.abs(angle));
- double diff=angle-pos.getYaw();
- if(angle>Math.PI){
- angle-=2*Math.PI;
- }
- else if(angle<-Math.PI){
- angle+=2*Math.PI;
- }
- double angle_deg=angle*180/Math.PI;
- double yaw_deg=pos.getYaw()*180/Math.PI;
- double yaw_ang_sub=(angle_deg-yaw_deg);
- System.out.println("yaw_ang_sub " + yaw_ang_sub);
- //System.out.println("The Angle: "+Math.abs(angle));
- if(angle>0){
- //System.out.println("I feel the need to turn right! "+" the Yaw " + pos.getYaw() + " angle-pos " + (angle-pos.getYaw()));
- curturn=.2;
- }else if(angle<0){
- //System.out.println("I feel the need to turn left! " + " the Yaw " + pos.getYaw() + " angle-pos " + (angle-pos.getYaw()));
- curturn=-.2;
- }
- if(Math.sqrt(((goal.get(0)-pos.getX())+(goal.get(1)-pos.getY()))/10)<0.1 ){
- //System.out.println("gets here");
- curfwd=0 ;
- curturn=0;
- speeds[0]=curturn;
- speeds[1]=curfwd;
- done=true;
- }else{
- curfwd=.2;
- speeds[0]=curturn;
- speeds[1]=curfwd;
- }
- speeds[0]=curturn;
- speeds[1]=curfwd;
- return speeds;
- }
- }