some enhancements to obd2source 21/2321/1
authorKevron Rees <kevron_m_rees@linux.intel.com>
Thu, 25 Oct 2012 22:56:33 +0000 (15:56 -0700)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Thu, 25 Oct 2012 22:56:33 +0000 (15:56 -0700)
TODO
lib/vehicleproperty.cpp
lib/vehicleproperty.h
plugins/obd2plugin/README [new file with mode: 0644]
plugins/obd2plugin/obd2source.cpp
plugins/obd2plugin/obd2source.h

diff --git a/TODO b/TODO
index c1d8aee..15e767d 100644 (file)
--- a/TODO
+++ b/TODO
@@ -5,3 +5,5 @@
 - sqlite database storage plugin
 - connect/disconnect on first/last subscription in obd2source
 - improve obd2source speed via Physical Addressing and Fast polling (see https://www.scantool.net/forum/index.php?topic=6661.msg25024#msg25024)
+- fix issue regarding obd2 thread loop not exiting sometimes
+- optimize CPU performance in obd2source
index d94347c..8619ef2 100644 (file)
@@ -76,7 +76,7 @@ const VehicleProperty::Property VehicleProperty::LightBrake= "LightBrake";
 const VehicleProperty::Property VehicleProperty::InteriorLightDriver = "InteriorLightDriver";
 const VehicleProperty::Property VehicleProperty::InteriorLightCenter = "InteriorLightCenter";
 const VehicleProperty::Property VehicleProperty::InteriorLightPassenger = "InteriorLightPassenger";
-
+const VehicleProperty::Property VehicleProperty::EngineLoad = "EngineLoad";
 
 std::list<VehicleProperty::Property> VehicleProperty::mCapabilities;
 
@@ -126,6 +126,7 @@ VehicleProperty::VehicleProperty()
        REGISTERPROPERTYWITHTYPE(InteriorLightDriver, LightStatusType, false);
        REGISTERPROPERTYWITHTYPE(InteriorLightPassenger, LightStatusType, false);
        REGISTERPROPERTYWITHTYPE(InteriorLightCenter, LightStatusType, false);
+       REGISTERPROPERTY(EngineLoad,0);
 }
 
 std::list<VehicleProperty::Property> VehicleProperty::capabilities()
index 23813f9..c42cc42 100644 (file)
@@ -264,7 +264,8 @@ public:
        static const Property InteriorLightCenter;
        static const Property InteriorLightPassenger;
 
-
+       static const Property EngineLoad;
+       typedef BasicPropertyType<uint16_t> EngineLoadType;
 
        static std::list<VehicleProperty::Property> capabilities();
 
diff --git a/plugins/obd2plugin/README b/plugins/obd2plugin/README
new file mode 100644 (file)
index 0000000..57c11eb
--- /dev/null
@@ -0,0 +1,51 @@
+OBD-II Source Plugin
+
+The OBD-II Source plugin uses ELM compatible scantools to get vehicle information.
+It is known to work with a number of commercial scantools including:
+
+OBDLink MX
+OBDLink S
+OBDPro USB
+
+To use the OBD-II Source plugin, add the following config to your /etc/ambd/config:
+
+
+"sources" : [
+                               {
+                                               "name" : "OBD2Source",
+                                               "path" : "/usr/lib/automotive-message-broker/obd2sourceplugin.so",
+                                               "device" : "/dev/ttyUSB0",
+                                               "baud" : "9600",
+                                               "bluetoothAdapter" : ""
+                               }
+               ]
+
+Configuration Key Definitions:
+"name"
+name of plugin.  This key is not used by the plugin at this moment.
+
+"path"
+path to plugin on the filesystem.
+
+"device"
+OBD-II Scantool device.  This is usually a serial device.  For bluetooth devices, use the bluetooth address for example: "00:00:11:aa:bb:cc".
+
+"baud"
+baud rate for OBD-II scantool.  OBDLink *-style scantool usually default to 115200.  Refer to your scantool docutmentation for the correct baud rate.  Note also that some bluetooth scantools, like the OBDLink MX have automatic baud selection and will ignore this value.
+
+"bluetoothAdapter"
+Hardware address of the bluetooth adapter to use.  If value is blank (ie "bluetoothAdapter" : ""), the system default adapter will be used.  The bluetoothAdapter key is only used if the "device" key is also set to a bluetooth device address.
+
+
+Performance
+
+Performance will vary with the scantool.  Here are the current performance numbers per tested scantool:
+
+scantool - properties/second = (pids/s) - baud
+
+OBDLink MX - 80 pids/s - N/A
+OBDLink S - 100 pids/s - 115200
+OBDPro USB - 43 pids/s - 9600
+
+
+
index d40315f..5291d21 100644 (file)
@@ -436,16 +436,14 @@ OBD2Source::OBD2Source(AbstractRoutingEngine *re, map<string, string> config) :
 {
        clientConnected = false;
        m_re = re;  
-       
-       m_supportedProperties.push_back(VehicleProperty::EngineSpeed); //0D
-       m_supportedProperties.push_back(VehicleProperty::VehicleSpeed); //0C
-       m_supportedProperties.push_back(VehicleProperty::EngineCoolantTemperature); //05
-       m_supportedProperties.push_back(VehicleProperty::MassAirFlow); //10
-       m_supportedProperties.push_back(VehicleProperty::VIN);
-       m_supportedProperties.push_back(VehicleProperty::WMI);
-       m_supportedProperties.push_back(VehicleProperty::EngineOilTemperature);
-       m_supportedProperties.push_back(VehicleProperty::InteriorTemperature);
-       m_supportedProperties.push_back(VehicleProperty::BatteryVoltage);
+
+       Obd2Amb obd2amb;
+
+       for(auto itr = obd2amb.propertyPidMap.begin(); itr != obd2amb.propertyPidMap.end(); itr++)
+       {
+               m_supportedProperties.push_back((*itr).first);
+       }
+
        re->setSupported(supported(), this);
        /*if (openPort(std::string("/dev/pts/7"),115200))
        {
@@ -483,29 +481,29 @@ string OBD2Source::uuid()
 }
 void OBD2Source::subscribeToPropertyChanges(VehicleProperty::Property property)
 {
-       //printf("Subscribed to property: %s\n",property.c_str());
+       /*//printf("Subscribed to property: %s\n",property.c_str());
        if (property == VehicleProperty::EngineSpeed)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "010C\r";
+               requ->req = "010C1\r";
                g_async_queue_push(subscriptionAddQueue,requ);
        }
        else if (property == VehicleProperty::MassAirFlow)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "0110\r";
+               requ->req = "01101\r";
                g_async_queue_push(subscriptionAddQueue,requ);
        }
        else if (property == VehicleProperty::VehicleSpeed)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "010D\r";
+               requ->req = "010D1\r";
                g_async_queue_push(subscriptionAddQueue,requ);
        }
        else if (property == VehicleProperty::EngineCoolantTemperature)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "0105\r";
+               requ->req = "01051\r";
                g_async_queue_push(subscriptionAddQueue,requ);
        }
        else if (property == VehicleProperty::VIN)
@@ -519,13 +517,13 @@ void OBD2Source::subscribeToPropertyChanges(VehicleProperty::Property property)
        else if (property == VehicleProperty::EngineOilTemperature)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "015C\r";
+               requ->req = "015C1\r";
                g_async_queue_push(subscriptionAddQueue,requ);
        }
        else if (property == VehicleProperty::InteriorTemperature)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "0146\r";
+               requ->req = "01461\r";
                g_async_queue_push(subscriptionAddQueue,requ);
        }
        else if (property == VehicleProperty::BatteryVoltage)
@@ -539,10 +537,32 @@ void OBD2Source::subscribeToPropertyChanges(VehicleProperty::Property property)
        m_supportedProperties.push_back(VehicleProperty::EngineOilTemperature);
        m_supportedProperties.push_back(VehicleProperty::InteriorTemperature);
        m_supportedProperties.push_back(VehicleProperty::BatteryVoltage);*/
-       else
+       /*else
        {
                //printf("Unsupported property: %s\n",property.c_str());
                DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Unsupported property requested:" << property << "\n";
+       }*/
+
+       if (property == VehicleProperty::VIN)
+       {
+               DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "VIN subscription requested... but there's no point!\n";
+       }
+       else if (property == VehicleProperty::WMI)
+       {
+               DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "WMI subscription requested... but there's no point!\n";
+       }
+       else
+       {
+               if(!ListPlusPlus<VehicleProperty::Property>(&m_supportedProperties).contains(property))
+               {
+                       DebugOut(0)<<"obd plugin does not support: "<<property<<endl;
+                       return;
+               }
+
+               Obd2Amb obd2amb;
+               ObdRequest *requ = new ObdRequest();
+               requ->req = obd2amb.propertyPidMap[property];
+               g_async_queue_push(subscriptionAddQueue,requ);
        }
 }
 
@@ -550,28 +570,28 @@ void OBD2Source::subscribeToPropertyChanges(VehicleProperty::Property property)
 void OBD2Source::unsubscribeToPropertyChanges(VehicleProperty::Property property)
 {
        //
-       if (property == VehicleProperty::EngineSpeed)
+       /*if (property == VehicleProperty::EngineSpeed)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "010C\r";
+               requ->req = "010C1\r";
                g_async_queue_push(subscriptionRemoveQueue,requ);
        }
        else if (property == VehicleProperty::MassAirFlow)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "0110\r";
+               requ->req = "01101\r";
                g_async_queue_push(subscriptionRemoveQueue,requ);
        }
        else if (property == VehicleProperty::VehicleSpeed)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "010D\r";
+               requ->req = "010D1\r";
                g_async_queue_push(subscriptionRemoveQueue,requ);
        }
        else if (property == VehicleProperty::EngineCoolantTemperature)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "0105\r";
+               requ->req = "01051\r";
                g_async_queue_push(subscriptionRemoveQueue,requ);
        }
        else if (property == VehicleProperty::VIN)
@@ -589,13 +609,13 @@ void OBD2Source::unsubscribeToPropertyChanges(VehicleProperty::Property property
        else if (property == VehicleProperty::EngineOilTemperature)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "015C\r";
+               requ->req = "015C1\r";
                g_async_queue_push(subscriptionRemoveQueue,requ);
        }
        else if (property == VehicleProperty::InteriorTemperature)
        {
                ObdRequest *requ = new ObdRequest();
-               requ->req = "0146\r";
+               requ->req = "01461\r";
                g_async_queue_push(subscriptionRemoveQueue,requ);
        }
        else if (property == VehicleProperty::BatteryVoltage)
@@ -604,6 +624,18 @@ void OBD2Source::unsubscribeToPropertyChanges(VehicleProperty::Property property
                requ->req = "ATRV\r";
                g_async_queue_push(subscriptionRemoveQueue,requ);
        }
+       */
+
+       if(!ListPlusPlus<VehicleProperty::Property>(&m_supportedProperties).contains(property))
+       {
+               DebugOut(0)<<"obd plugin does not support: "<<property<<endl;
+               return;
+       }
+
+       Obd2Amb obd2amb;
+       ObdRequest *requ = new ObdRequest();
+       requ->req = obd2amb.propertyPidMap[property];
+       g_async_queue_push(subscriptionRemoveQueue,requ);
 }
 
 
@@ -611,8 +643,9 @@ void OBD2Source::getPropertyAsync(AsyncPropertyReply *reply)
 {
        propertyReplyMap[reply->property] = reply;
        VehicleProperty::Property property = reply->property;
+
        //TODO: There is a much better way to do this, but for now it's hardcoded.
-       if (property == VehicleProperty::EngineSpeed)
+       /*if (property == VehicleProperty::EngineSpeed)
        {
                ObdRequest *requ = new ObdRequest();
                requ->req = "010C\r";
@@ -666,6 +699,20 @@ void OBD2Source::getPropertyAsync(AsyncPropertyReply *reply)
                requ->req = "ATRV\r";
                g_async_queue_push(singleShotQueue,requ);
        }
+       */
+
+       ///Here's a better way:
+
+       if(!ListPlusPlus<VehicleProperty::Property>(&m_supportedProperties).contains(property))
+       {
+               DebugOut(0)<<"obd plugin does not support: "<<property<<endl;
+               return;
+       }
+
+       Obd2Amb obd2amb;
+       ObdRequest *requ = new ObdRequest();
+       requ->req = obd2amb.propertyPidMap[property];
+       g_async_queue_push(singleShotQueue,requ);
 }
 
 void OBD2Source::setProperty(VehicleProperty::Property , AbstractPropertyType * )
index ae9b1bb..2a66c1b 100644 (file)
@@ -48,6 +48,26 @@ public:
 };
 
 
+class Obd2Amb
+{
+public:
+       Obd2Amb()
+       {
+               propertyPidMap[VehicleProperty::VehicleSpeed] = "010D1\r";
+               propertyPidMap[VehicleProperty::EngineSpeed] = "010C1\r";
+               propertyPidMap[VehicleProperty::MassAirFlow] = "01101\r";
+               propertyPidMap[VehicleProperty::AirIntakeTemperature] = "010F1\r";
+               propertyPidMap[VehicleProperty::ThrottlePosition] = "01111\r";
+               propertyPidMap[VehicleProperty::BatteryVoltage] = "ATRV\r";     propertyPidMap[VehicleProperty::EngineCoolantTemperature]  = "0105a\r";
+               propertyPidMap[VehicleProperty::EngineLoad] = "01041/r";
+               propertyPidMap[VehicleProperty::VIN] = "0902/r";
+               propertyPidMap[VehicleProperty::WMI] = "0902/r";
+               propertyPidMap[VehicleProperty::EngineOilTemperature] = "015C1\r";
+               propertyPidMap[VehicleProperty::InteriorTemperature] = "01461\r";
+       }
+
+       map<VehicleProperty::Property, std::string> propertyPidMap;
+};
 
 class OBD2Source : public AbstractSource
 {