2525
2626using namespace trikControl ;
2727
28- CameraLineDetectorSensorWorker::CameraLineDetectorSensorWorker (
29- QString const &roverCvBinary
28+ CameraLineDetectorSensorWorker::CameraLineDetectorSensorWorker (QString const &roverCvBinary
3029 , QString const &inputFile
31- , QString const &outputFile)
30+ , QString const &outputFile
31+ , double toleranceFactor)
3232 : mReading(0 )
3333 , mRoverCvBinary(roverCvBinary)
34- , mOutputFileDescriptor(0 )
34+ , mOutputFileDescriptor(-1 )
35+ , mRoverCvProcess(this )
3536 , mInputFile(inputFile)
3637 , mOutputFile(outputFile)
3738 , mReady(false )
39+ , mToleranceFactor(toleranceFactor)
3840{
3941 qRegisterMetaType<QProcess::ProcessError>(" QProcess::ProcessError" );
4042
@@ -53,13 +55,9 @@ CameraLineDetectorSensorWorker::~CameraLineDetectorSensorWorker()
5355 deinitialize ();
5456}
5557
56- void CameraLineDetectorSensorWorker::moveChildrenToCorrectThread ()
57- {
58- mRoverCvProcess .moveToThread (this ->thread ());
59- }
60-
6158void CameraLineDetectorSensorWorker::init ()
6259{
60+ // Since rover-cv can die silently, we need to check not only mReady flag, but also input and output fifos.
6361 if (!mReady || !mInputFile .exists () || !mOutputFile .exists ()) {
6462 initDetector ();
6563 }
@@ -162,15 +160,16 @@ void CameraLineDetectorSensorWorker::readFile()
162160 int const value = parsedLine[5 ].toInt ();
163161 int const valueTolerance = parsedLine[6 ].toInt ();
164162
165- // These values are not needed in current implementation, but are left here for reference too.
166- Q_UNUSED (hue)
167- Q_UNUSED (hueTolerance)
168- Q_UNUSED (saturation)
169- Q_UNUSED (saturationTolerance)
170- Q_UNUSED (value)
171- Q_UNUSED (valueTolerance)
163+ mInputStream
164+ << QString (" hsv %0 %1 %2 %3 %4 %5 %6" )
165+ .arg (hue)
166+ .arg (hueTolerance * mToleranceFactor )
167+ .arg (saturation)
168+ .arg (saturationTolerance * mToleranceFactor )
169+ .arg (value)
170+ .arg (valueTolerance * mToleranceFactor )
171+ << " \n " ;
172172
173- mInputStream << QString (line).remove (" :" ) << " \n " ;
174173 mInputStream .flush ();
175174 }
176175 }
@@ -214,7 +213,7 @@ void CameraLineDetectorSensorWorker::openFifos()
214213{
215214 qDebug () << " opening" << mOutputFile .fileName ();
216215
217- if (! mOutputFileDescriptor ) {
216+ if (mOutputFileDescriptor == - 1 ) {
218217 mOutputFileDescriptor = open (mOutputFile .fileName ().toStdString ().c_str (), O_SYNC | O_NONBLOCK, O_RDONLY);
219218 }
220219
@@ -223,12 +222,10 @@ void CameraLineDetectorSensorWorker::openFifos()
223222 return ;
224223 }
225224
226- qDebug () << mOutputFileDescriptor ;
227-
228225 mSocketNotifier .reset (new QSocketNotifier (mOutputFileDescriptor , QSocketNotifier::Read));
229226
230227 connect (mSocketNotifier .data (), SIGNAL (activated (int )), this , SLOT (readFile ()));
231- mSocketNotifier ->setEnabled (false );
228+ mSocketNotifier ->setEnabled (true );
232229
233230 qDebug () << " opening" << mInputFile .fileName ();
234231
@@ -260,8 +257,10 @@ void CameraLineDetectorSensorWorker::tryToExecute()
260257
261258void CameraLineDetectorSensorWorker::deinitialize ()
262259{
263- disconnect (mSocketNotifier .data (), SIGNAL (activated (int )), this , SLOT (readFile ()));
264- mSocketNotifier ->setEnabled (false );
260+ if (mSocketNotifier ) {
261+ disconnect (mSocketNotifier .data (), SIGNAL (activated (int )), this , SLOT (readFile ()));
262+ mSocketNotifier ->setEnabled (false );
263+ }
265264 if (::close (mOutputFileDescriptor ) != 0 ) {
266265 qDebug () << mOutputFile .fileName () << " : fifo close failed: " << errno;
267266 }
0 commit comments