Skip to content
This repository
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor
May 21, 2012
Sébastien Vaucher sebyx31 Correct implementation of the timeout + ping system
Signed-off-by: Sébastien Vaucher <seb.vaucher@gmail.com>
1c357d7
Sébastien Vaucher sebyx31 Parse Accelerometer & Compass updates
Signed-off-by: Sébastien Vaucher <seb.vaucher@gmail.com>
bead9bc
23 projetp1/src/com/github/projetp1/Pic.java
@@ -33,7 +33,7 @@
33 33 */
34 34 private double compass;
35 35
36   - private PicMode mode;
  36 + private PicMode mode = PicMode.SIMULATION;
37 37
38 38 public enum PicMode {
39 39 /**
@@ -50,8 +50,6 @@
50 50 SIMULATION
51 51 ; }
52 52
53   - private RS232Command commande;
54   -
55 53 protected MainView mainview;
56 54
57 55 private RS232 rs;
@@ -152,27 +150,32 @@ public PicMode getMode() {
152 150 public void setMode(PicMode mode) {
153 151 this.mode = mode;
154 152 }
  153 +
155 154 public void run()
156 155 {
  156 + RS232Command commande;
157 157 //Reception des données
158 158 while((commande = rs.getLastCommand()) != null)
159 159 {
160 160 //Switch pour trier les données
161 161 switch(commande.getCommandNumber())
162 162 {
163   - case EMPTY :
164   - break;
165   - case CHANGE_TO_POINT_MODE:
166   - break;
167   - case CHANGE_TO_ARROW_MODE:
168   - break;
169 163 case LOCATION_UPDATE:
170 164 break;
171 165 case ACCELEROMETER_UPDATE:
172   - break;
173 166 case MAGNETOMETER_UPDATE:
  167 + String[] components = commande.getDatas().split(",", 3);
  168 + double[] values = new double[3];
  169 + for (int l_i = 0; l_i < components.length; l_i++)
  170 + values[l_i] = Double.parseDouble(components[l_i]);
  171 +
  172 + if(commande.getCommandNumber() == RS232CommandType.ACCELEROMETER_UPDATE)
  173 + this.angle = Mathematics.calculateAngleInclinometer(values[0], values[1], values[2]);
  174 + else
  175 + this.compass = Mathematics.calculateAngleCompass(values[0], values[1], values[2]);
174 176 break;
175 177 case PIC_STATUS:
  178 + // TODO: Traitement du message de statut
176 179 break;
177 180 default:
178 181 break;
103 projetp1/src/com/github/projetp1/rs232/RS232.java
... ... @@ -1,10 +1,12 @@
1 1 package com.github.projetp1.rs232;
2 2
3   -import java.util.Timer;
4   -import java.util.TimerTask;
  3 +import java.awt.event.ActionEvent;
  4 +import java.awt.event.ActionListener;
5 5 import java.util.concurrent.ConcurrentLinkedQueue;
6 6 import java.util.logging.Logger;
7 7
  8 +import javax.swing.Timer;
  9 +
8 10 import com.github.projetp1.*;
9 11 import com.github.projetp1.Pic.PicMode;
10 12
@@ -28,8 +30,20 @@
28 30 private ConcurrentLinkedQueue<RS232Command> commandQueue = new ConcurrentLinkedQueue<RS232Command>();
29 31 /** The buffer in which the received datas are temporarily put. */
30 32 private StringBuffer buffer = new StringBuffer();
31   - private Timer pingTimer;
32   - private Timer timeoutTimer;
  33 + private Timer pingTimer = new Timer(PINGDELAY, new ActionListener()
  34 + {
  35 + public void actionPerformed(ActionEvent _arg0)
  36 + {
  37 + sendPing();
  38 + }
  39 + });
  40 + private Timer timeoutTimer = new Timer(PINGTIMEOUT, new ActionListener()
  41 + {
  42 + public void actionPerformed(ActionEvent _e)
  43 + {
  44 + disconnect();
  45 + }
  46 + });
33 47
34 48 Logger log = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
35 49
@@ -100,16 +114,22 @@ public RS232(Settings _settings, Pic _pic) throws Exception, SerialPortException
100 114 throw ex;
101 115 }
102 116
  117 +
  118 + timeoutTimer.setRepeats(false);
  119 + timeoutTimer.setInitialDelay(PINGTIMEOUT);
  120 + pingTimer.setRepeats(true);
  121 + pingTimer.setInitialDelay(PINGDELAY);
  122 +
103 123 if (!this.sendPing())
104 124 {
105 125 Exception ex = new Exception("The initial ping could not be sent, aborting...");
106 126 log.warning(ex.getMessage());
107 127 throw ex;
108 128 }
  129 +
109 130 log.info("Serial port opened");
110   -
111   - this.connect();
112   - log.info("RS232 object created");
  131 +
  132 + log.info("RS232 object created, waiting for the ping reply to finish");
113 133 }
114 134
115 135 /**
@@ -122,12 +142,14 @@ protected Boolean sendPing()
122 142 try
123 143 {
124 144 this.sendFrame(RS232CommandType.EMPTY, "");
  145 + timeoutTimer.restart();
125 146 log.info("Ping sent");
126 147 return true;
127 148 }
128 149 catch (SerialPortException ex)
129 150 {
130 151 log.warning("Unable to send ping");
  152 + disconnect();
131 153 return false;
132 154 }
133 155 }
@@ -137,26 +159,10 @@ protected Boolean sendPing()
137 159 */
138 160 private void connect()
139 161 {
140   - if(pingTimer != null)
141   - {
142   - pingTimer.cancel();
143   - pingTimer = null;
144   - }
145   -
146 162 if(pic.getMode() == PicMode.SIMULATION)
147 163 pic.setMode(PicMode.POINTING);
148   - if(pic.getMode() == PicMode.POINTING)
149   - {
150   - pingTimer = new Timer("pingDaemon", true);
151   - pingTimer.schedule(new TimerTask()
152   - {
153   - public void run()
154   - {
155   - sendPing();
156   - resetTimeout();
157   - }
158   - }, 0, PINGDELAY);
159   - }
  164 + sendPing();
  165 + pingTimer.restart();
160 166 }
161 167
162 168 /**
@@ -166,16 +172,8 @@ private void disconnect()
166 172 {
167 173 log.info("Disconnecting the PIC");
168 174
169   - if(pingTimer != null)
170   - {
171   - pingTimer.cancel();
172   - pingTimer = null;
173   - }
174   - if(timeoutTimer != null)
175   - {
176   - timeoutTimer.cancel();
177   - timeoutTimer = null;
178   - }
  175 + pingTimer.stop();
  176 + timeoutTimer.stop();
179 177
180 178 pic.setMode(PicMode.SIMULATION);
181 179 }
@@ -253,34 +251,7 @@ public synchronized void sendFrame(RS232CommandType cNum, String datas) throws S
253 251 String trame = "$" + cNum.toString() + "," + datas + "*";
254 252 trame += hexToAscii(RS232.computeCrc(trame)) + "\r\n";
255 253 sp.writeString(trame);
256   - log.info("Frame sent : " + trame);
257   - }
258   -
259   - /**
260   - * Reset the timeout of the ping
261   - */
262   - private void resetTimeout()
263   - {
264   - log.info("Timeout reset");
265   -
266   - if(timeoutTimer != null)
267   - {
268   - timeoutTimer.cancel();
269   - timeoutTimer = null;
270   - }
271   -
272   - if(pic.getMode() == PicMode.POINTING)
273   - {
274   - timeoutTimer = new Timer();
275   - timeoutTimer.schedule(new TimerTask()
276   - {
277   - @Override
278   - public void run()
279   - {
280   - disconnect();
281   - }
282   - }, PINGTIMEOUT);
283   - }
  254 + log.fine("Frame sent : " + trame);
284 255 }
285 256
286 257 /**
@@ -293,11 +264,13 @@ public void serialEvent(SerialPortEvent e)
293 264 if (!e.isRXCHAR()) // If no data is available
294 265 return;
295 266
  267 +
  268 + pingTimer.restart();
296 269 String received;
297 270 try
298 271 {
299 272 received = sp.readString();
300   - log.info("Data received : " + received);
  273 + log.fine("Data received : " + received);
301 274 }
302 275 catch (SerialPortException ex)
303 276 {
@@ -323,8 +296,8 @@ public void serialEvent(SerialPortEvent e)
323 296 commandQueue.add(com);
324 297 else
325 298 {
  299 + timeoutTimer.stop();
326 300 log.info("Ping received");
327   - resetTimeout();
328 301 }
329 302 newComs = true;
330 303 }

No commit comments for this range

Something went wrong with that request. Please try again.