1- /* Copyright 2013 - 2015 Nikita Batov and CyberTech Labs Ltd.
1+ /* Copyright 2013 - 2015 Nikita Batov, Kseniya Gonta and CyberTech Labs Ltd.
22 *
33 * Licensed under the Apache License, Version 2.0 (the "License");
44 * you may not use this file except in compliance with the License.
@@ -26,26 +26,20 @@ AnalogSensor::AnalogSensor(const QString &port, const trikKernel::Configurer &co
2626 : mCommunicator(communicator)
2727{
2828 mI2cCommandNumber = ConfigurerHelper::configureInt (configurer, mState , port, " i2cCommandNumber" );
29-
30- // We use linear subjection to normalize sensor values:
29+ mIRType = configurer. attributeByPort (port, " type " ) == " SharpGP2 " ? Type::sharpGP2 : Type::analog;
30+ // We use linear subjection to normalize common analog sensor values:
3131 // normalizedValue = k * rawValue + b
3232 // To calculate k and b we need two raw values and two corresponding them normalized values.
3333
34- const int rawValue1 = ConfigurerHelper::configureInt (configurer, mState , port, " rawValue1" );
35- const int rawValue2 = ConfigurerHelper::configureInt (configurer, mState , port, " rawValue2" );
36- const int normalizedValue1 = ConfigurerHelper::configureInt (configurer, mState , port, " normalizedValue1" );
37- const int normalizedValue2 = ConfigurerHelper::configureInt (configurer, mState , port, " normalizedValue2" );
34+ // We use hyperbolical subjection to normalize SharpGP2 sensor values:
35+ // normalizedValue = s / (rawValue + l) + n
36+ // To calculate s, l and n we need sensor readings at three distances.
3837
39- if (rawValue1 == rawValue2) {
40- QLOG_ERROR () << " Sensor calibration error: rawValue1 = rawValue2!" ;
41- mState .fail ();
42- mK = 0 ;
43- mB = 0 ;
38+ if (mIRType == Type::sharpGP2){
39+ calculateLNS (port, configurer);
4440 } else {
45- mK = static_cast <qreal>(normalizedValue2 - normalizedValue1) / (rawValue2 - rawValue1);
46- mB = normalizedValue1 - mK * rawValue1;
41+ calculateKB (port, configurer);
4742 }
48-
4943 mState .ready ();
5044}
5145
@@ -56,14 +50,11 @@ AnalogSensor::Status AnalogSensor::status() const
5650
5751int AnalogSensor::read ()
5852{
59- if (!mState .isReady () || mCommunicator .status () != DeviceInterface::Status::ready) {
60- return 0 ;
53+ const auto raw = readRawData ();
54+ if (mIRType == Type::sharpGP2) {
55+ return mS /(raw + mL ) + mN ;
6156 }
62-
63- QByteArray command (1 , ' \0 ' );
64- command[0 ] = static_cast <char >(mI2cCommandNumber & 0xFF );
65-
66- return mK * mCommunicator .read (command) + mB ;
57+ return mK * raw + mB ;
6758}
6859
6960int AnalogSensor::readRawData ()
@@ -77,3 +68,41 @@ int AnalogSensor::readRawData()
7768
7869 return mCommunicator .read (command);
7970}
71+
72+ void AnalogSensor::calculateLNS (const QString &port, const trikKernel::Configurer &configurer)
73+ {
74+ QStringList result;
75+ for (const QString &str : configurer.attributeByPort (port, " values" ).split (" )" )) {
76+ result += str.mid (1 ).split (" ;" );
77+ }
78+
79+ const int x1 = result[0 ].toInt ();
80+ const int y1 = result[1 ].toInt ();
81+ const int x2 = result[2 ].toInt ();
82+ const int y2 = result[3 ].toInt ();
83+ const int x3 = result[4 ].toInt ();
84+ const int y3 = result[5 ].toInt ();
85+
86+ // Counted from equations x1 = mS/(y1 + mL) + mN, x2 = mS/(y2 + mL) + mN, x3 = mS/(y3 + mL) + mN
87+ mL = (-x1 * y1 * y3 - x3 * y2 * y3 + x3 * y1 * y3 + x1 * y1 * y2 + x2 * y2 * y3 - x2 * y1 * y2) /
88+ (x1 * y3 - x2 * y3 + x2 * y1 - x1 * y2 + x3 * y2 - x3 * y1);
89+ mS = (x1 - x2) * (y1 + mL ) * (y2 + mL ) / (y2 - y1);
90+ mN = x1 - mS / (y1 + mL );
91+ }
92+
93+ void AnalogSensor::calculateKB (const QString &port, const trikKernel::Configurer &configurer)
94+ {
95+ const int rawValue1 = ConfigurerHelper::configureInt (configurer, mState , port, " rawValue1" );
96+ const int rawValue2 = ConfigurerHelper::configureInt (configurer, mState , port, " rawValue2" );
97+ const int normalizedValue1 = ConfigurerHelper::configureInt (configurer, mState , port, " normalizedValue1" );
98+ const int normalizedValue2 = ConfigurerHelper::configureInt (configurer, mState , port, " normalizedValue2" );
99+ if (rawValue1 == rawValue2) {
100+ QLOG_ERROR () << " Sensor calibration error: rawValue1 = rawValue2!" ;
101+ mState .fail ();
102+ mK = 0 ;
103+ mB = 0 ;
104+ } else {
105+ mK = static_cast <qreal>(normalizedValue2 - normalizedValue1) / (rawValue2 - rawValue1);
106+ mB = normalizedValue1 - mK * rawValue1;
107+ }
108+ }
0 commit comments