Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added more timestamps.

  • Loading branch information...
commit 82a00292829156ddc6fd07a72f49635083d688bd 1 parent 006e0cb
@mitar authored
Showing with 38 additions and 6 deletions.
  1. +27 −3 lib/Capture.hs
  2. +11 −3 lib/CaptureTypes.hs
View
30 lib/Capture.hs
@@ -1,8 +1,9 @@
-module NXT.Capture (initCapture, terminateCapture, getLastRobotPosition) where
+module NXT.Capture (initCapture, terminateCapture, getLastRobotPosition, waitForRobotPosition, waitForRobotPosition') where
import Control.Exception
import Control.Monad.State
import qualified Data.ByteString.Lazy.Char8 as C
+import Data.Time.Clock.POSIX
import System.IO
import System.IO.Error
import System.Process
@@ -14,7 +15,7 @@ import NXT.NXTTypes
-- TODO Move to configuration file
enableCapture :: Bool
-enableCapture = False
+enableCapture = True
initCapture :: NXT ()
initCapture = do
@@ -57,7 +58,30 @@ getLastRobotPosition = do
modify (\s -> s { capture = Just (Capture captureOut capturePid currentBuffer) })
if null positions
then return Nothing
- else return $ Just $ head . head $ positions -- positions are stored as lists in the reverse order so (head . head) is the last position from capture process
+ else do
+ let Robot x y d ct pt _ = head . head $ positions -- positions are stored as lists in the reverse order so (head . head) is the last position from capture process
+ currentTime <- io $ getPOSIXTime
+ return $ Just $ Robot x y d ct pt currentTime
+
+waitForRobotPosition :: NXT ()
+waitForRobotPosition = do
+ _ <- waitForRobotPosition' (-1)
+ return ()
+
+waitForRobotPosition' :: Int -> NXT Bool
+waitForRobotPosition' timeout = do
+ c <- gets capture
+ case c of
+ Nothing -> fail "No capture process"
+ Just (Capture captureOut capturePid _) -> do
+ processRunning <- io $ getProcessExitCode capturePid
+ case processRunning of
+ Just _ -> captureProcessExited
+ _ -> do
+ ret <- io $ tryJust (guard . isEOFError) $ hWaitForInput captureOut timeout
+ case ret of
+ Left _ -> captureProcessExited -- EOF
+ Right inputAvailable -> return inputAvailable
captureProcessExited :: NXT a
captureProcessExited = do
View
14 lib/CaptureTypes.hs
@@ -10,15 +10,23 @@ import Numeric
import System.IO
import System.Process
-data Robot = Robot Double Double Double POSIXTime POSIXTime deriving (Eq, Read, Show) -- X and Y coordinates of a robot, direction of a robot in radians from positive X axis, capture and processed timestamps in microseconds
+-- TODO Change to record
+data Robot = Robot
+ Double -- X coordinate of a robot
+ Double -- Y coordinate of a robot
+ Double -- direction of a robot in radians from positive X axis
+ POSIXTime -- capture timestamp in microseconds (as determined by a clock of the remote process)
+ POSIXTime -- processed timestamp in microseconds (as determined by a clock of the remote process)
+ POSIXTime -- receive timestamp in microseconds (as determined by a clock of this process)
+ deriving (Eq, Read, Show)
-- Approximate but larger than possible length of Robot data type text description
-- We assume that total length of POSIXTime is at most three times its precision, and 42 characters for data type format structure itself
maxRobotDescLength :: Int
#if __GLASGOW_HASKELL__ <= 610
-maxRobotDescLength = length "Robot" + 3 * (floatDigits (undefined :: Double)) + 2 * 3 * (length . show $ resolution (undefined :: E12)) + 42
+maxRobotDescLength = length "Robot" + 3 * (floatDigits (undefined :: Double)) + 3 * 3 * (length . show $ resolution (undefined :: E12)) + 42
#else
-maxRobotDescLength = length "Robot" + 3 * (floatDigits (undefined :: Double)) + 2 * 3 * (length . show $ resolution (undefined :: Pico)) + 42
+maxRobotDescLength = length "Robot" + 3 * (floatDigits (undefined :: Double)) + 3 * 3 * (length . show $ resolution (undefined :: Pico)) + 42
#endif
instance Read NominalDiffTime where -- POSIXTime is NominalDiffTime
Please sign in to comment.
Something went wrong with that request. Please try again.