Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote branch 'origin/master' into skymap

  • Loading branch information...
commit eb4412f97a7860875292645a5cbec8433d366142 2 parents d8aefe6 + 8d65235
@thedarkmammouth thedarkmammouth authored
View
BIN  projetp1/res/hyg.db
Binary file not shown
View
32 projetp1/src/com/github/projetp1/CelestialObject.java
@@ -109,14 +109,14 @@ public CelestialObject(int _id,int _HIP,int _HD,int _HR,String _sProperName,doub
//this.dVXYZ[2] = _vz;
}
- /**
- * getHeader
- * Gives the header of columns from the database
- */
- public void getHeader()
- {
- System.out.println("ID\tStarID\tHIP\tHD\tHR\tGliese\tBayerFlamsteed\tProperName\tRA\tDec\tDistance\tPMRA\tPMDec\tRV\tMag\tAbsMag\tSpectrum\tColorIndex\tX\tY\tZ\tVX\tVY\tVZ\tXReal\tYReal");
- }
+ ///**
+ // * getHeader
+ // * Gives the header of columns from the database
+ // */
+ //public void getHeader()
+ //{
+ // System.out.println("ID\tStarID\tHIP\tHD\tHR\tGliese\tBayerFlamsteed\tProperName\tRA\tDec\tDistance\tPMRA\tPMDec\tRV\tMag\tAbsMag\tSpectrum\tColorIndex\tX\tY\tZ\tVX\tVY\tVZ\tXReal\tYReal");
+ //}
/*
* getInfos
@@ -340,5 +340,21 @@ public void setXReal(double x) {
public void setYReal(double y) {
this.dYReal = y;
}
+
+ /**
+ * setdRA
+ * setter of the private value
+ */
+ public void setdRA(double y) {
+ this.dRA = y;
+ }
+
+ /**
+ * setdDec
+ * setter of the private value
+ */
+ public void setdDec(double y) {
+ this.dDec = y;
+ }
}
View
21 projetp1/src/com/github/projetp1/DataBase.java
@@ -398,6 +398,16 @@ private ResultSet selectQuery(String _sFields[],String _sTable[],String _sWhere[
CelestialObject l_star = new CelestialObject(l_id,l_HIP,l_HD,l_HR,l_ProperName,l_dRA,l_Dec,l_dDistance,l_dMag,l_dColorIndex);
Mathematics l_calc = new Mathematics(_date,_dLat, _dLon,l_star.getDec(),l_star.getRA());
+ if(l_ProperName == "Sol")
+ {
+ l_calc = new Mathematics(_date,_dLat, _dLon);
+ l_star.setdDec(l_calc.getDeclination());
+ l_star.setdRA(l_calc.getAscension());
+ System.out.println("SOLEIL");
+ }
+ else
+ l_calc = new Mathematics(_date,_dLat, _dLon,l_star.getDec(),l_star.getRA());
+
if(l_calc.getHeight() >= 0)
{
l_star.setXReal(l_calc.getX());
@@ -507,7 +517,16 @@ private ResultSet selectQuery(String _sFields[],String _sTable[],String _sWhere[
//CelestialObject l_star = new CelestialObject(l_id,l_StarId,l_HIP,l_HD,l_HR,l_Gliese,l_BayerFlamsteed,l_ProperName,l_dRA,l_Dec,l_dDistance,l_dPMRA,l_dPMDec,l_dRV,l_dMag,l_dAbsMag,l_sSpectrum,l_dColorIndex,l_dXYZ[0],l_dXYZ[1],l_dXYZ[2],l_dVXYZ[0],l_dVXYZ[1],l_dVXYZ[2]);
CelestialObject l_star = new CelestialObject(l_id,l_HIP,l_HD,l_HR,l_ProperName,l_dRA,l_Dec,l_dDistance,l_dMag,l_dColorIndex);
- Mathematics l_calc = new Mathematics(_date,_dLat, _dLon,l_star.getDec(),l_star.getRA());
+ Mathematics l_calc = null;
+
+ if(l_ProperName == "Sol")
+ {
+ l_calc = new Mathematics(_date,_dLat, _dLon);
+ l_star.setdDec(l_calc.getDeclination());
+ l_star.setdRA(l_calc.getAscension());
+ }
+ else
+ l_calc = new Mathematics(_date,_dLat, _dLon,l_star.getDec(),l_star.getRA());
if(l_calc.getHeight() > 0)
{
View
2  projetp1/src/com/github/projetp1/MainView.java
@@ -150,7 +150,7 @@ public MainView() {
settings = new Settings();
Serializer.serialize("settings.lol",settings);
- pic = new Pic();
+ pic = new Pic(this);
addComponentListener(new java.awt.event.ComponentAdapter() {
View
92 projetp1/src/com/github/projetp1/Mathematics.java
@@ -22,7 +22,7 @@
public class Mathematics
{
static private final double pi = Math.PI;
- //static private final double deg2rad = pi / 180.0;
+ static private final double deg2rad = pi / 180.0;
//static private final double rad2deg = 180.0 / pi;
//Constants for Julian Date
@@ -73,14 +73,7 @@
*/
public Mathematics(Calendar _date, double _dLat, double _dLon,double _dDec,double _dAsc)
{
- this.dHour = _date.get(Calendar.HOUR_OF_DAY);
- this.dMinute = _date.get(Calendar.MINUTE);
- this.dSecond = _date.get(Calendar.SECOND);
-
- this.dGMT = calculate_Hour_GMT(_date);
- this.dDay = Mathematics.kadditionnal_dDay_of_Date_object + _date.get(Calendar.DATE);
- this.dMonth = Mathematics.kadditionnal_dMonth_of_Date_object + _date.get(Calendar.MONTH);
- this.dYear = Mathematics.kadditionnal_dYear_of_gregorian_calendar + _date.get(Calendar.YEAR);
+ calculate_date_time(_date);
this.dLatitude = _dLat;
this.dLongitude = _dLon;
@@ -91,6 +84,27 @@ public Mathematics(Calendar _date, double _dLat, double _dLon,double _dDec,doubl
}
/**
+ * Mathematics Constructor,use for the sun
+ *
+ * @param _date : Date of the computer. Will be use to calculate the Sideral Time
+ * @param _dLat : It's the Latitude of the star's pointer
+ * @param _dLon : It's the Longitude of the star's pointer
+ */
+ public Mathematics(Calendar _date, double _dLat, double _dLon)
+ {
+ calculate_date_time(_date);
+
+ this.dLatitude = _dLat;
+ this.dLongitude = _dLon;
+
+ this.dDate_JulianCalendar = calculate_JulianDate(this.dDay, this.dMonth, this.dYear, this.dHour,this.dMinute, this.dSecond);
+
+ calculate_position_sun(this.dDate_JulianCalendar);
+
+ calculate_all();
+ }
+
+ /**
* calculate_all
* Calculates all the informations that the program needs
*/
@@ -113,6 +127,23 @@ private void calculate_all()
}
/**
+ * calculate_date_time
+ * Calculates the informations from the date and the time
+ * @param _date : Use a calendar for calculate the GTM hour
+ */
+ private void calculate_date_time(Calendar _date)
+ {
+ this.dHour = _date.get(Calendar.HOUR_OF_DAY);
+ this.dMinute = _date.get(Calendar.MINUTE);
+ this.dSecond = _date.get(Calendar.SECOND);
+
+ this.dGMT = calculate_Hour_GMT(_date);
+ this.dDay = Mathematics.kadditionnal_dDay_of_Date_object + _date.get(Calendar.DATE);
+ this.dMonth = Mathematics.kadditionnal_dMonth_of_Date_object + _date.get(Calendar.MONTH);
+ this.dYear = Mathematics.kadditionnal_dYear_of_gregorian_calendar + _date.get(Calendar.YEAR);
+ }
+
+ /**
* get_all
* Give all the informations of the values calculated
*/
@@ -154,6 +185,27 @@ public void get_all()
}
/**
+ * calculate_position_sun
+ * Calculates the sun's declination and ascencion
+ * @param _dDate_JulianCalendar : Use the julian date of the day
+ */
+ private void calculate_position_sun(double _dDate_JulianCalendar)
+ {
+ //http://www.cppfrance.com/codes/CALCUL-POSITION-SOLEIL-DECLINAISON-ANGLE-HORAIRE-ALTITUDE-AZIMUT_31774.aspx
+ double g=357.529+0.98560028*_dDate_JulianCalendar;
+ double q=280.459+0.98564736*_dDate_JulianCalendar;
+ double l=q+1.915*sin(g*pi/180.0)+0.020*sin(2*g*pi/180.0);
+ double e=23.439-0.00000036*_dDate_JulianCalendar;
+
+ this.dAscension = arctan(cos(e*pi/180.0)*sin(l*pi/180.0)/cos(l*pi/180.0))*(180.0/pi)/15.0;
+ if(cos(l*pi/180.0)<0)
+ this.dAscension = 12.0+this.dAscension;
+ else if(cos(l*pi/180.0)>0 && sin(l*pi/180.0)<0)
+ this.dAscension = this.dAscension+24.0;
+ this.dDeclination = arcsin(sin(e*pi/180.0)*sin(l*pi/180.0))*180.0/pi;
+ }
+
+ /**
* calculate_Hour_GMT
* Gives the hour from HMT
* @param _cal : Use a calendar for calculate the GTM hour
@@ -375,6 +427,28 @@ static private double arcsin(double _dX)
return Math.asin(_dX);
}
+ /**
+ * tan
+ * Calculate the tan of a number
+ * @param _dX : The value to transform
+ * @return : Return the result of the operation
+ */
+ static private double tan(double _dX)
+ {
+ return Math.tan(_dX);
+ }
+
+ /**
+ * arctan
+ * Calculate the ArcTan of a number
+ * @param _dX : The value to transform
+ * @return : Return the result of the operation
+ */
+ static private double arctan(double _dX)
+ {
+ return Math.atan(_dX);
+ }
+
/**
* getHour
* getter of the private value
View
79 projetp1/src/com/github/projetp1/Pic.java
@@ -6,7 +6,15 @@
/**
* @author alexandr.perez
*/
-public class Pic {
+
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+import jssc.SerialPortException;
+
+import com.github.projetp1.*;
+import com.github.projetp1.rs232.*;
+
+public class Pic extends Thread{
/**
* @uml.property name="longitude"
@@ -26,13 +34,51 @@
private double compass;
private PicMode mode;
- public enum PicMode { POINTING, GUIDING; }
+
+ public enum PicMode {
+ /**
+ * The default mode : point, click and see
+ */
+ POINTING,
+ /**
+ * The searching star mode
+ */
+ GUIDING,
+ /**
+ * The simulation mode. No PIC is connected, user commands come from the keyboard
+ */
+ SIMULATION
+ ; }
+
+ private RS232Command commande;
+
+ protected MainView mainview;
+
+ private RS232 rs;
/**
*
*/
- public Pic() {
+ public Pic(MainView _mainview) {
// TODO Auto-generated constructor stub
+ this.mainview = _mainview;
+
+ try
+ {
+ rs = new RS232(mainview.getSettings(), this);
+ }
+ catch (SerialPortException ex)
+ {
+ // TODO Auto-generated catch block
+ ex.printStackTrace();
+ }
+ catch (Exception ex)
+ {
+ // TODO Auto-generated catch block
+ ex.printStackTrace();
+ }
+
+ this.start();
}
/**
@@ -106,4 +152,31 @@ public PicMode getMode() {
public void setMode(PicMode mode) {
this.mode = mode;
}
+ public void run()
+ {
+ //Reception des données
+ while((commande = rs.getLastCommand()) != null)
+ {
+ //Switch pour trier les données
+ switch(commande.getCommandNumber())
+ {
+ case EMPTY :
+ break;
+ case CHANGE_TO_POINT_MODE:
+ break;
+ case CHANGE_TO_ARROW_MODE:
+ break;
+ case LOCATION_UPDATE:
+ break;
+ case ACCELEROMETER_UPDATE:
+ break;
+ case MAGNETOMETER_UPDATE:
+ break;
+ case PIC_STATUS:
+ break;
+ default:
+ break;
+ }
+ }
+ }
}
View
396 projetp1/src/com/github/projetp1/rs232/RS232.java
@@ -1,11 +1,9 @@
-/**
- *
- */
package com.github.projetp1.rs232;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.logging.Logger;
import com.github.projetp1.*;
import com.github.projetp1.Pic.PicMode;
@@ -18,81 +16,100 @@
* @author alexandr.perez
* @author sebastie.vaucher
*/
-public class RS232 implements SerialPortEventListener {
+public class RS232 implements SerialPortEventListener
+{
/** The Settings */
- protected Settings _settings;
- /** The MainView */
- protected MainView _mainview;
+ protected Settings settings;
/** The object maintaining the PIC status */
- protected Pic _pic;
+ protected Pic pic;
/** The serial port object. */
- private SerialPort _sp;
+ private SerialPort sp;
/** The queue of the latest received commands. */
private ConcurrentLinkedQueue<RS232Command> commandQueue = new ConcurrentLinkedQueue<RS232Command>();
/** The buffer in which the received datas are temporarily put. */
private StringBuffer buffer = new StringBuffer();
- private Timer pingTimer = new Timer("timerDaemon", true);
- private Timer timeoutTimer = new Timer(false);
- /** True if the PIC is online. */
- private boolean connectionIsActive = false;
+ private Timer pingTimer;
+ private Timer timeoutTimer;
+ Logger log = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
+
/** The delay between 2 pings */
private static final int PINGDELAY = 3000;
/** The delay after which a ping timeout occurs */
private static final int PINGTIMEOUT = 2000;
/**
+ * Represents the direction of the arrow that is displayed on the PIC screen.
+ */
+ public enum PicArrowDirection
+ {
+ NORTH(0), NORTHWEST(1), WEST(2), SOUTHWEST(3), SOUTH(4), SOUTHEAST(5), EAST(6), NORTHEAST(7);
+ private int number;
+ private PicArrowDirection(int _number)
+ {
+ number = _number;
+ }
+ }
+
+ /**
* Instantiates a new RS232 object.
- *
- * @param mainview The global MainView
+ *
+ * @param _settings A Settings object
+ * @param _pic A Pic object
* @throws Exception A generic Exception. May occur if the PIC isn't found.
- * @throws SerialPortException A SerialPort Exception
+ * @throws SerialPortException A SerialPortException
*/
- public RS232(MainView mainview) throws Exception, SerialPortException {
- this._mainview = mainview;
- this._settings = mainview.getSettings();
- this._pic = mainview.getPic();
+ public RS232(Settings _settings, Pic _pic) throws Exception, SerialPortException
+ {
+ this.settings = _settings;
+ this.pic = _pic;
// Initialisation du sp
- String port = _settings.getPort();
- if (port == null || port.equals("")) {
- try {
+ String port = settings.getPort();
+ if (port == null || port.equals(""))
+ {
+ try
+ {
port = SerialPortList.getPortNames()[0];
if (port == null || port.equals(""))
throw new Exception();
- System.out.println("The default SerialPort has been selected : " + port);
- _settings.setPort(port);
- } catch (Exception e) {
- System.out.println("FATAL : No SerialPort has been found !");
- throw new SerialPortException("NoPort",
- "RS232.RS232(MainView)",
+ log.info("The default SerialPort has been selected : " + port);
+ settings.setPort(port);
+ }
+ catch (Exception e)
+ {
+ log.severe("No SerialPort has been found !");
+ throw new SerialPortException("NoPort", "RS232.RS232(MainView)",
"No RS-232 port found on the computer !");
}
}
- this._sp = new SerialPort(port);
-
- try {
- this._sp.openPort();// Open serial port
- this._sp.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
- } catch (SerialPortException ex) {
- System.out.println("SerialPortException at port opening : "
- + ex.getMessage());
+ this.sp = new SerialPort(port);
+
+ try
+ {
+ this.sp.openPort();// Open serial port
+ this.sp.setParams(SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8,
+ SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+ this.sp.setEventsMask(SerialPort.MASK_RXCHAR);//Set mask
+ this.sp.addEventListener(this);//Add SerialPortEventListener
+ }
+ catch (SerialPortException ex)
+ {
+ log.warning("SerialPortException at port opening : " + ex.getMessage());
throw ex;
}
if (!this.sendPing())
- throw new Exception(
- "The initial ping could not be sent, aborting...");
-
-
- // TODO: Envoi du ping seulement si liaison inactive
- pingTimer.schedule(new TimerTask() {
- @Override
- public void run() {
- sendPing();
- }
- }, PINGDELAY, PINGDELAY);
+ {
+ Exception ex = new Exception("The initial ping could not be sent, aborting...");
+ log.warning(ex.getMessage());
+ throw ex;
+ }
+ log.info("Serial port opened");
+
+ this.connect();
+ log.info("RS232 object created");
}
/**
@@ -100,82 +117,169 @@ public void run() {
*
* @return True if it succeeds, false otherwise
*/
- public Boolean sendPing() {
- try {
- String start = "$" + RS232CommandType.EMPTY.toString() + "*";
- _sp.writeString(start + computeCrc(start) + "\r\n");
+ protected Boolean sendPing()
+ {
+ try
+ {
+ this.sendFrame(RS232CommandType.EMPTY, "");
+ log.info("Ping sent");
return true;
- } catch (SerialPortException ex) {
+ }
+ catch (SerialPortException ex)
+ {
+ log.warning("Unable to send ping");
return false;
}
}
-
+
/**
- * Send an NCK message to the PIC informing that a packet has been
- * corrupted.
- *
- * @param command The command number
+ * Connect to the PIC (send ping, schedule ping)
*/
- public synchronized void sendNck(RS232CommandType command) {
- if (_pic.getMode() == PicMode.GUIDING || command.equals(RS232CommandType.PIC_STATUS))
- return;
+ private void connect()
+ {
+ if(pingTimer != null)
+ {
+ pingTimer.cancel();
+ pingTimer = null;
+ }
- try {
- sendFrame(command, "");
- } catch (SerialPortException ex) {
- System.out.println("RS232 : Unable to send a NCK on port " + ex.getPortName());
+ if(pic.getMode() == PicMode.SIMULATION)
+ pic.setMode(PicMode.POINTING);
+ if(pic.getMode() == PicMode.POINTING)
+ {
+ pingTimer = new Timer("pingDaemon", true);
+ pingTimer.schedule(new TimerTask()
+ {
+ public void run()
+ {
+ sendPing();
+ resetTimeout();
+ }
+ }, 0, PINGDELAY);
}
}
- public void sendFrame(RS232CommandType cNum, String datas) throws SerialPortException
+ /**
+ * Disconnect from the PIC (cancel ping scheduling)
+ */
+ private void disconnect()
{
- String trame = "$" + cNum.toString() + "*" + datas;
- _sp.writeString(trame + RS232.computeCrc(trame) + "\r\n");
- }
-
- /* TODO: Reopen port (may need a rethink)
- private boolean reopenPort() {
- if(connectionIsActive)
+ log.info("Disconnecting the PIC");
- try {
- _sp.closePort();
- _sp.addEventListener(this);
- _sp.openPort();
- } catch (SerialPortException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ if(pingTimer != null)
+ {
+ pingTimer.cancel();
+ pingTimer = null;
}
+ if(timeoutTimer != null)
+ {
+ timeoutTimer.cancel();
+ timeoutTimer = null;
+ }
+
+ pic.setMode(PicMode.SIMULATION);
}
- //*/
- private void resetTimeout() {
- timeoutTimer.cancel();
- timeoutTimer = null;
+ /**
+ * Indicate to this object that the operation mode has changed
+ * @param _mode The new mode of operation
+ * @throws SerialPortException If the PIC can not be reached
+ */
+ public void modeHasChanged(PicMode _mode) throws SerialPortException
+ {
+ switch (_mode)
+ {
+ case SIMULATION:
+ disconnect();
+ break;
+ case GUIDING:
+ connect();
+ sendArrowToPic(PicArrowDirection.NORTH);
+ break;
+ case POINTING:
+ connect();
+ sendFrame(RS232CommandType.CHANGE_TO_POINT_MODE, "");
+ break;
+ default:
+ log.warning("Mode not yet supported, the state may be inaccurate !");
+ disconnect();
+ break;
+ }
- timeoutTimer = new Timer();
- timeoutTimer.schedule(new TimerTask() {
- @Override
- public void run() {
- // TODO: Le PIC n'a pas répondu à temps : la liaison est perdue
- }
- }, PINGTIMEOUT);
+ log.info("PIC mode switched to : " + _mode);
}
+
/**
- * Check if the ping originating from the PIC was received
+ * Send the direction of the arrow to the PIC.
*
- * @return true, if successful
+ * @param _dir The direction of the arrow
+ * @throws SerialPortException
*/
- private boolean pingReceived() {
- try {
- boolean ping = false;
- while(commandQueue.remove(new RS232Command(RS232CommandType.EMPTY, "")))
- ping = true;
-
- return ping;
- } catch (Exception e) {
- System.out.println("Erreur impossible : " + e.getMessage());
- return false;
+ public void sendArrowToPic(PicArrowDirection _dir) throws SerialPortException
+ {
+ this.sendFrame(RS232CommandType.CHANGE_TO_ARROW_MODE, String.valueOf(_dir.number));
+ }
+
+ /**
+ * Send an NCK message to the PIC informing that a packet has been corrupted.
+ *
+ * @param command The command number
+ */
+ public void sendNck(RS232CommandType command)
+ {
+ if (command == null || pic.getMode() == PicMode.GUIDING || command.equals(RS232CommandType.PIC_STATUS))
+ return;
+
+ try
+ {
+ sendFrame(command, "");
+ }
+ catch (SerialPortException ex)
+ {
+ log.warning("Unable to send a NCK on port " + ex.getPortName());
+ }
+ }
+
+
+ /**
+ * Send a frame to the PIC
+ * @param cNum The command to send
+ * @param datas The datas (can be empty)
+ * @throws SerialPortException If the SerialPort cannot be written
+ */
+ public synchronized void sendFrame(RS232CommandType cNum, String datas) throws SerialPortException
+ {
+ String trame = "$" + cNum.toString() + "," + datas + "*";
+ trame += hexToAscii(RS232.computeCrc(trame)) + "\r\n";
+ sp.writeString(trame);
+ log.info("Frame sent : " + trame);
+ }
+
+ /**
+ * Reset the timeout of the ping
+ */
+ private void resetTimeout()
+ {
+ log.info("Timeout reset");
+
+ if(timeoutTimer != null)
+ {
+ timeoutTimer.cancel();
+ timeoutTimer = null;
+ }
+
+ if(pic.getMode() == PicMode.POINTING)
+ {
+ timeoutTimer = new Timer();
+ timeoutTimer.schedule(new TimerTask()
+ {
+ @Override
+ public void run()
+ {
+ disconnect();
+ }
+ }, PINGTIMEOUT);
}
}
@@ -183,19 +287,22 @@ private boolean pingReceived() {
* @see jssc.SerialPortEventListener#serialEvent(jssc.SerialPortEvent)
*/
@Override
- public void serialEvent(SerialPortEvent e) {
+ public void serialEvent(SerialPortEvent e)
+ {
// Callback du SerialPort
if (!e.isRXCHAR()) // If no data is available
return;
String received;
- try {
- received = _sp.readString();
- } catch (SerialPortException ex) {
- System.out
- .println("RS232 : port "
- + ex.getPortName()
- + " unreadable. Please check that we are the only one listening to this port.");
+ try
+ {
+ received = sp.readString();
+ log.info("Data received : " + received);
+ }
+ catch (SerialPortException ex)
+ {
+ log.warning("Port " + ex.getPortName()
+ + " unreadable. Please check that we are the only one listening to this port.");
return;
}
@@ -204,53 +311,84 @@ public void serialEvent(SerialPortEvent e) {
int pos;
boolean newComs = false;
- while ((pos = buffer.indexOf("\r\n")) != -1) {
+ while ((pos = buffer.indexOf("\r\n")) != -1)
+ {
RS232Command com;
String chain = buffer.substring(0, pos + 2);
- try {
+ buffer.delete(0, pos + 2);
+ try
+ {
com = new RS232Command(chain);
- commandQueue.add(com);
+ if(com.getCommandNumber() != RS232CommandType.EMPTY)
+ commandQueue.add(com);
+ else
+ {
+ log.info("Ping received");
+ resetTimeout();
+ }
newComs = true;
- } catch (CrcException e1) {
+ }
+ catch (CrcException e1)
+ {
this.sendNck(RS232Command.extractCommand(chain));
- } catch (Exception e1) {
- System.out.println(e1.getMessage());
+ log.info("Frame corrupted");
+ }
+ catch (Exception e1)
+ {
+ log.warning("Exception while trying to decode frame : " + e1.getMessage());
}
}
- if(newComs) {
- // TODO: Notify the Pic object that new data is available
+ if (newComs)
+ {
+ pic.run();
}
-
+
}
+ public RS232Command getLastCommand()
+ {
+ return commandQueue.poll();
+ }
+
/**
- * Returns whether the chains and the CRC matches
+ * Check the CRC against the raw string received via RS-232
*
- * @param datas The datas
+ * @param datas The data against which to check the CRC
* @param crc The received CRC
* @return True if it matches or false otherwise
*/
- public static boolean checkCrc(String datas, byte crc) {
+ public static boolean checkCrc(String datas, byte crc)
+ {
return crc == (computeCrc(datas));
}
/**
- * Returns the computed CRC of the chain following NMEA-0183 method
+ * Returns the computed CRC of the string following NMEA-0183 method
*
* @param datas The String of which to compute the CRC
* @return The computed CRC as a byte
*/
- public static byte computeCrc(String datas) {
+ public static byte computeCrc(String datas)
+ {
byte crc = 0;
- for (byte b : datas.getBytes()) {
+ for (byte b : datas.getBytes())
+ {
crc = (byte) (crc ^ b);
}
return crc;
}
-
- public boolean isConnectionActive() {
- return connectionIsActive;
+
+
+ /**
+ * Convert a byte to an ASCII hex string
+ *
+ * @param b The byte to be converted
+ * @return A String
+ */
+ public static String hexToAscii(byte b)
+ {
+ return Integer.toString( ( b & 0xff ) + 0x100, 16).substring( 1 );
}
}
View
30 projetp1/src/com/github/projetp1/rs232/RS232Command.java
@@ -1,9 +1,13 @@
package com.github.projetp1.rs232;
+import java.util.logging.Logger;
+
public class RS232Command
{
- private RS232CommandType _commandNumber;
- private String _datas;
+ private RS232CommandType commandNumber;
+ public RS232CommandType getCommandNumber() { return commandNumber; }
+ private String datas;
+ public String getDatas() { return datas; }
/**
* Create a new RS232Command object (with CRC check)
@@ -15,8 +19,8 @@
*/
public RS232Command(String commandNumber, String datas, byte crc) throws CrcException, IllegalArgumentException
{
- this._commandNumber = RS232CommandType.valueOfByNum(commandNumber);
- this._datas = datas;
+ this.commandNumber = RS232CommandType.valueOfByNum(commandNumber);
+ this.datas = datas;
if(!RS232.checkCrc(datas, crc))
throw new CrcException(crc);
@@ -30,8 +34,8 @@ public RS232Command(String commandNumber, String datas, byte crc) throws CrcExce
*/
public RS232Command(RS232CommandType commandNumber, String datas)
{
- this._commandNumber = commandNumber;
- this._datas = datas;
+ this.commandNumber = commandNumber;
+ this.datas = datas;
}
/**
@@ -49,10 +53,10 @@ public RS232Command(String chain) throws CrcException, IllegalArgumentException
// Extract the CRC
// Extract the datas
- this._datas = extractDatas(chain);
- this._commandNumber = extractCommand(chain);
+ this.datas = extractDatas(chain);
+ this.commandNumber = extractCommand(chain);
- if(!RS232.checkCrc(_datas, extractCrc(chain)))
+ if(!RS232.checkCrc(chain.substring(0, chain.indexOf("*") + 1), extractCrc(chain)))
throw new CrcException(extractCrc(chain));
}
@@ -63,7 +67,7 @@ public RS232Command(String chain) throws CrcException, IllegalArgumentException
*/
public void sendNck(RS232 rs)
{
- rs.sendNck(_commandNumber);
+ rs.sendNck(commandNumber);
}
/**
@@ -88,7 +92,7 @@ public static RS232CommandType extractCommand(String chain)
try {
return RS232CommandType.valueOfByNum(chain.substring(1, chain.indexOf(",")));
} catch (IllegalArgumentException e) {
- System.out.println("Not a valid command number !");
+ Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).warning("Not a valid command number !");
return null;
}
}
@@ -101,6 +105,8 @@ public static RS232CommandType extractCommand(String chain)
*/
public static byte extractCrc(String chain)
{
- return (byte) chain.charAt(chain.indexOf("*") + 1);
+ String crcS = chain.substring(chain.indexOf("*") + 1, chain.indexOf("\r\n"));
+ int crcI = Integer.parseInt(crcS, 16);
+ return (byte) crcI;
}
}
View
6 projetp1/src/com/github/projetp1/rs232/RS232CommandType.java
@@ -14,6 +14,12 @@
CHANGE_TO_POINT_MODE("01"),
/** Changes the PIC mode to search + Send the arrow */
CHANGE_TO_ARROW_MODE("02"),
+ /** Contains the GPS coordinates */
+ LOCATION_UPDATE("03"),
+ /** Contains the accelerometer vectors */
+ ACCELEROMETER_UPDATE("04"),
+ /** Contains the compass vectors */
+ MAGNETOMETER_UPDATE("05"),
/** Used by the PIC to communicates its status to the PC */
PIC_STATUS("99")
;
Please sign in to comment.
Something went wrong with that request. Please try again.