virtuelles_java-teleskop
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
virtuelles_java-teleskop [2019/06/24 11:49] – torsten.roehl | virtuelles_java-teleskop [2020/11/22 16:19] (aktuell) – Externe Bearbeitung 127.0.0.1 | ||
---|---|---|---|
Zeile 12: | Zeile 12: | ||
<WRAP center round info 95%> | <WRAP center round info 95%> | ||
- | Um mit Java auf den Arduino über die serielle Schnittstelle zugreifen zu können, verwenden wir die jSSC (Java Simple Serial Connector) API. Sie besteht aus nur einer jar-Datei und wird auch von der Arduino-IDE selbst verwendet. In Eclipse wird sie über Eigenschaften (Java Build Path - Add External JAR' | + | Um mit Java auf die serielle Schnittstelle zugreifen zu können, verwenden wir die jSSC (Java Simple Serial Connector) API. Sie besteht aus nur einer jar-Datei und wird auch von der Arduino-IDE selbst verwendet. In Eclipse wird sie über Eigenschaften (Java Build Path - Add External JAR' |
https:// | https:// | ||
Zeile 31: | Zeile 31: | ||
==== Installation und Inbetriebnahme ==== | ==== Installation und Inbetriebnahme ==== | ||
+ | - Eclipseprojekt erzeugen und die Datei (unten) **SimulatorTeleskop** einfügen | ||
+ | - jssc-2.0.0.jar als Externe Library dem Projekt hinzufügen | ||
+ | - Projekt mit Eclipse übersetzen (kann nicht fehlerfrei gestartet werden, da die Schnittstelle "/ | ||
+ | - Virtuelle serielle Schnittstelle erzeugen | ||
+ | - Rechte setzen | ||
+ | - Testen | ||
+ | == Details == | ||
+ | Die folgenden beiden befehle erfordern Rootrechte. Außerdem müssen | ||
- | 1. Virtuelle serielle | + | Die virtuelle |
socat -d -d PTY, | socat -d -d PTY, | ||
+ | | ||
+ | Anschließend werden zum Testen alle Rechte vergeben | ||
+ | # chmod 777 / | ||
- | 2.Rechte setzen | + | Überprüfen Sie (Konsole ls-Befehl) ob alles richtig gemacht wurde 8-) |
- | + | ||
- | # chmod 777 / | + | |
- | 3. Testen | + | == Test 1: Konsole == |
- | | + | Ein erste Test erfolgt mit einer User-Konsole, indem folgender Befehl an das Gerät gesendet wird, zuvor muss das Programm (z.B. über Eclipse gestartet werden). |
- | echo ": | + | echo ": |
| | ||
- | Simulator.java sollte Nachricht anzeigen! | + | Auf der Konsole (Eclipse) sollten jetzt die Anzahl der gelesenen Bytes angezeigt werden. Damit funktioniert die Kommunikation über die virtuelle serielle Schnittstelle. |
- | ii) Stellarium | + | - Schnittstelle einrichten (socat, |
+ | - Java // | ||
+ | - In einer Userkonsole //echo ": | ||
- | lx200 Teleskop einrichten mit / | ||
- | Die auf diese Weise angelegte | + | == Test 2: Stellarium == |
- | | + | |
+ | Der zweite Test ist spannender, da wir mit Stellarium kommunizieren. | ||
+ | - LX200 Teleskop einrichten mit / | ||
+ | - Schnittstelle | ||
+ | | ||
+ | - LX200 Teleskop in Stellarium verbinden | ||
+ | ===== Quellcode Projekt A ===== | ||
- | < | + | Das Protokoll kann hier eingesehen werden {{ : |
- | package test; | + | |
- | import com.fazecast.jSerialComm.SerialPort; | + | Das Javaprogramm implementiert nur wenige Kommandos des Protokolls. Nachdem Stellarium sich mit dem " |
- | public class Test01 | + | Wenn mit Stellarium geschwenkt werden soll, d.h. ein bestimmtes Objekt angefahren werden soll, werden drei Kommandos geschickt. Die ersten beiden Kommandos übermitteln die Zielkoordinaten, |
+ | <code java> | ||
+ | |||
+ | /** LX200 Protocol | ||
+ | * @author torsten.roehl@fsg-preetz.org | ||
+ | * simple demo to communicate with a indi-client! | ||
+ | */ | ||
+ | |||
+ | import java.util.Timer; | ||
+ | import java.util.TimerTask; | ||
+ | |||
+ | import jssc.SerialPort; | ||
+ | import jssc.SerialPortException; | ||
+ | |||
+ | public class SimulatorTeleskop { | ||
+ | |||
+ | static String currentRA = " | ||
+ | static String currentDE = " | ||
+ | |||
+ | static String targetRA = currentRA; | ||
+ | static String targetDE = currentDE; | ||
+ | |||
+ | static String dev = "/ | ||
+ | |||
+ | public enum Protocol // some serial lx200 commands | ||
+ | { | ||
+ | CMD_GR, CMD_GD, CMD_QS, CMD_SD, CMD_MS, CMD_UNKNOWN | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) throws InterruptedException { | ||
+ | |||
+ | SerialPort serialPort = new SerialPort(dev); | ||
+ | try { | ||
+ | serialPort.openPort(); | ||
+ | serialPort.setParams(9600, | ||
+ | System.out.println(" | ||
+ | Thread.sleep(4000); | ||
+ | |||
+ | } catch (SerialPortException e1) { | ||
+ | e1.printStackTrace(); | ||
+ | } | ||
- | public static void main(String[] args) { | ||
- | SerialPort comPort = SerialPort.getCommPorts()[2]; | ||
- | comPort.openPort(); | ||
- | comPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, | ||
try { | try { | ||
while (true) { | while (true) { | ||
- | byte[] readBuffer = new byte[1024]; | ||
- | int numRead = comPort.readBytes(readBuffer, | ||
- | if (numRead > 0) { | + | String msg = decode(serialPort); |
- | plot(numRead, readBuffer); | + | Protocol cmd = protocol(msg); |
- | if(getRADEC(numRead,readBuffer)){ | + | switch |
- | System.out.println("received radec messgage"); | + | case CMD_GR: |
- | writeRADEC(comPort); | + | writeMessage(serialPort, currentRA); |
- | } | + | break; |
- | if (isGetRA(numRead, readBuffer)) { | + | case CMD_GD: |
- | System.out.println("received ra messgage"); | + | writeMessage(serialPort, |
- | writeRa(comPort); | + | break; |
- | } | + | case CMD_QS: |
- | if (isGetDec(numRead, readBuffer)) { | + | targetRA = msg.substring(msg.indexOf(' ' |
- | System.out.println(" | + | writeMessage(serialPort, |
- | writeDec(comPort); | + | break; |
- | } | + | case CMD_SD: |
- | // | + | currentDE = msg.substring(msg.indexOf(' |
+ | targetDE = currentDE; | ||
+ | writeMessage(serialPort, " | ||
+ | break; | ||
+ | case CMD_MS: | ||
+ | writeMessage(serialPort, | ||
+ | simulateMove(); | ||
+ | break; | ||
+ | case CMD_UNKNOWN: | ||
+ | if (!msg.startsWith(" | ||
+ | System.out.println(" | ||
+ | break; | ||
} | } | ||
+ | |||
Thread.sleep(100); | Thread.sleep(100); | ||
Zeile 98: | Zeile 158: | ||
e.printStackTrace(); | e.printStackTrace(); | ||
} | } | ||
- | comPort.closePort(); | ||
- | } | + | try { |
- | + | serialPort.closePort(); | |
- | static void plot(int numRead, byte[] readBuffer) | + | } catch (SerialPortException e) { |
- | System.out.println("Read " + numRead + " bytes." | + | e.printStackTrace(); |
- | + | ||
- | for (int i = 0; i < numRead; i++) { | + | |
- | char c = (char) readBuffer[i]; | + | |
- | System.out.print(c); | + | |
} | } | ||
- | System.out.println("" | + | |
} | } | ||
- | static boolean getRADEC(int num, byte[] readBuffer) { | ||
- | char c[] = new char[num]; | ||
- | if (readBuffer.length < 1) | ||
- | return false; | ||
- | for (int i = 0; i < num; i++) { | + | private static Protocol protocol(String msg) { |
- | c[i] = (char) readBuffer[i]; | + | |
- | } | + | if (msg.startsWith("#: |
+ | return Protocol.CMD_GR; | ||
+ | if (msg.startsWith("#: | ||
+ | return Protocol.CMD_GD; | ||
+ | if (msg.startsWith("#: | ||
+ | return Protocol.CMD_QS; | ||
+ | if (msg.startsWith(": | ||
+ | return Protocol.CMD_SD; | ||
+ | if (msg.startsWith(": | ||
+ | return Protocol.CMD_MS; | ||
- | if (c[0] == ' | + | return |
- | return | + | |
- | } | + | |
- | + | ||
- | return false; | + | |
} | } | ||
- | |||
- | static void writeRADEC(SerialPort p) { | ||
- | String | + | static |
- | byte[] buffer = new byte[18]; | + | try { |
- | byte[] byte_str = str.getBytes(); | + | // step 1: read bytes from port |
- | for (int i = 0; i < 18; i++) | + | byte[] buffer = serialPort.readBytes(); |
- | buffer[i] = byte_str[i]; | + | // step 2: verify |
+ | if (buffer | ||
+ | return " | ||
- | // | + | int numRead |
- | // buffer[10] = 0x0A; | + | if (buffer.length <= 0) |
+ | return "zero length" | ||
- | p.writeBytes(buffer, | + | // step 3: convert buffer to string |
- | } | + | String res = "" |
- | + | for (int i = 0; i < numRead; i++) { | |
- | + | byte b = buffer[i]; | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | static boolean isGetRA(int num, byte[] readBuffer) { | + | |
- | char c[] = new char[num]; | + | |
- | if (readBuffer.length < 4) | + | |
- | return false; | + | |
- | for (int i = 0; i < num; i++) { | + | // fixme: problem with Star * |
- | c[i] = (char) | + | char |
+ | if (b == -33) | ||
+ | c = ' | ||
+ | res += c; | ||
- | } | + | } |
+ | return res; | ||
- | if (c[0] == '#' | + | } catch (SerialPortException e) { |
- | return true; | + | e.printStackTrace(); |
} | } | ||
- | return | + | return |
} | } | ||
- | static | + | static |
- | char c[] = new char[num]; | + | try { |
- | if (readBuffer.length < 4) | + | byte[] buffer |
- | return false; | + | p.writeBytes(buffer); |
+ | } catch (SerialPortException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | } | ||
- | for (int i = 0; i < num; i++) { | + | /** |
- | c[i] = (char) readBuffer[i]; | + | * helper method convert a ra-string to a number |
+ | */ | ||
+ | static | ||
+ | String tmp = str.substring(0, str.length() - 1); | ||
+ | String lsTmp[] = tmp.split(":" | ||
+ | int res = 3600 * Integer.valueOf(lsTmp[0]) + 60 * Integer.valueOf(lsTmp[1]) + Integer.valueOf(lsTmp[2]); | ||
+ | return res; | ||
+ | } | ||
- | } | + | /** |
+ | * helper method to convert a number into ra-string | ||
+ | */ | ||
+ | static String number2string(double number) { | ||
- | if (c[0] == '#' | + | int h = (int) (number - (number % 3600)) / 3600; |
- | return true; | + | int rest = (int) (number - h * 3600); |
- | } | + | int m = (int) (rest - (rest % 60)) / 60; |
+ | rest = rest - m * 60; | ||
+ | int s = rest; | ||
- | return | + | String strH = String.format(" |
+ | String strM = String.format(" | ||
+ | String strS = String.format(" | ||
+ | return | ||
} | } | ||
- | static void writeRa(SerialPort p) { | + | /** |
+ | * dummy method to simulate kind of telescope motion - using ten steps only | ||
+ | * in ra-axis | ||
+ | */ | ||
+ | static void simulateMove() { | ||
+ | TimerTask repeatedTask = new TimerTask() { | ||
- | String str = " | + | int run = 0; |
- | byte[] buffer | + | double stepwidth |
- | byte[] byte_str = str.getBytes(); | + | int iTo = 0; |
- | for (int i = 0; i < 9; i++) | + | int iFrom = 0; |
- | buffer[i] | + | |
- | // | + | public void run() { |
- | // | + | System.out.println(run + " ...move simulation "); |
- | p.writeBytes(buffer, | + | // step 0: calculate stepwidth once |
- | } | + | if |
+ | iFrom = string2int(currentRA); | ||
+ | iTo = string2int(targetRA); | ||
+ | stepwidth = (iTo - iFrom) / 10.0; | ||
+ | } | ||
+ | // step 1: adjust currentRA with stepwidth | ||
+ | if (run > 0) { | ||
+ | currentRA = number2string(iFrom + run * stepwidth); | ||
+ | } | ||
- | static void writeDec(SerialPort p) { | + | // step 2: finish motion |
- | String str = " | + | run++; |
- | //" | + | if (run > 10) { |
- | byte[] buffer = new byte[7]; | + | currentRA = targetRA; |
- | byte[] byte_str = str.getBytes(); | + | cancel(); |
- | for (int i = 0; i < 7; i++) | + | } |
- | buffer[i] = byte_str[i]; | + | } |
- | // | + | }; |
- | // buffer[7] = 0x0D; | + | Timer timer = new Timer(" |
- | // buffer[8] | + | timer.scheduleAtFixedRate(repeatedTask, 0, 250); |
- | + | ||
- | p.writeBytes(buffer, (long) buffer.length); | + | |
} | } | ||
} | } | ||
+ | |||
</ | </ | ||
+ | <WRAP center round info 90%> | ||
+ | Das dieses einfache Programm (die nur wenige Kommandos des Protokolls implementiert) funktioniert, | ||
+ | </ | ||
+ | |||
virtuelles_java-teleskop.1561376966.txt.gz · Zuletzt geändert: 2020/11/22 16:52 (Externe Bearbeitung)