EOS 2  1.1.0
Einfache Objektbasierte Sprache
Car.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.ev3;
2 
3 import java.io.DataInputStream;
4 import java.io.DataOutputStream;
5 import java.io.IOException;
6 import java.net.InetAddress;
7 import java.net.Socket;
8 import java.net.UnknownHostException;
9 
10 import de.lathanda.eos.base.event.CleanupListener;
11 import de.lathanda.eos.ev3.exception.ConnectionLostException;
12 import de.lathanda.eos.ev3.exception.DeviceNotFoundException;
13 
14 public class Car implements CleanupListener {
15  private Socket ev3;
16  private String addr;
17  private DataInputStream in;
18  private DataOutputStream out;
19 
20  public Car() {
21  addr = "";
22  }
23 
24  public void connect(String address) {
25  InetAddress ip;
26  this.addr = address;
27  try {
28  ip = InetAddress.getByName(address);
29  } catch (UnknownHostException e) {
30  throw new DeviceNotFoundException(address);
31  }
32  try {
33  ev3 = new Socket(ip, 8777);
34  in = new DataInputStream(ev3.getInputStream());
35  out = new DataOutputStream(ev3.getOutputStream());
36  } catch (IOException e) {
37  throw new DeviceNotFoundException(address);
38  }
39  }
40 
41  public void turnLeft(int degree) {
42  Command cmd = new Command(Command.TURN_LEFT, degree);
43  try {
44  cmd.send(out);
45  } catch (IOException io) {
46  throw new ConnectionLostException();
47  }
48  }
49 
50  public void turnRight(int degree) {
51  Command cmd = new Command(Command.TURN_RIGHT, degree);
52  try {
53  cmd.send(out);
54  } catch (IOException io) {
55  throw new ConnectionLostException();
56  }
57  }
58 
59  public void driveForward(int distance) {
60  Command cmd = new Command(Command.MOVE_FORWARD, distance);
61  try {
62  cmd.send(out);
63  } catch (IOException io) {
64  throw new ConnectionLostException();
65  }
66  }
67 
68  public void driveBackward(int distance) {
69  Command cmd = new Command(Command.MOVE_BACKWARD, distance);
70  try {
71  cmd.send(out);
72  } catch (Exception io) {
73  throw new ConnectionLostException();
74  }
75  }
76 
77  public void honk() {
78  Command cmd = new Command(Command.HONK);
79  try {
80  cmd.send(out);
81  } catch (Exception io) {
82  throw new ConnectionLostException();
83  }
84  }
85 
86  public int readDistanceSensor() {
88  try {
89  cmd.send(out);
90  Command answer = Command.receive(in);
91  return answer.getData(0);
92  } catch (Exception io) {
93  throw new ConnectionLostException();
94  }
95  }
96  public int readAngleSensor() {
98  try {
99  cmd.send(out);
100  Command answer = Command.receive(in);
101  return answer.getData(0);
102  } catch (Exception io) {
103  throw new ConnectionLostException();
104  }
105  }
106 
107  @Override
108  public String toString() {
109  return addr;
110  }
111 
112  @Override
113  public void terminate() {
114  if (out != null) {
115  Command cmd = new Command(Command.DISCONNECT);
116  try {
117  cmd.send(out);
118  } catch (Throwable t) {
119  // if we can't send a shutdown message we can't do anything
120  // useful anymore
121  }
122  try {
123  ev3.close();
124  } catch (IOException e) {
125  }
126  out = null;
127  in = null;
128  ev3 = null;
129  }
130 
131  }
132 }
void turnRight(int degree)
Definition: Car.java:50
void connect(String address)
Definition: Car.java:24
int readDistanceSensor()
Definition: Car.java:86
void turnLeft(int degree)
Definition: Car.java:41
void driveForward(int distance)
Definition: Car.java:59
void driveBackward(int distance)
Definition: Car.java:68
static final int TURN_RIGHT
Definition: Command.java:9
static final int TURN_LEFT
Definition: Command.java:8
static final int DISCONNECT
Definition: Command.java:13
void send(DataOutputStream out)
Definition: Command.java:34
static final int HONK
Definition: Command.java:12
static final int REQUEST_ANGLE
Definition: Command.java:16
static Command receive(DataInputStream in)
Definition: Command.java:25
static final int REQUEST_DISTANCE
Definition: Command.java:14
static final int MOVE_FORWARD
Definition: Command.java:10
static final int MOVE_BACKWARD
Definition: Command.java:11
int getData(int index)
Definition: Command.java:44
Impressum