added a bunch of properties and consolodated some things in obd2source 45/2345/1
authorKevron Rees <kevron_m_rees@linux.intel.com>
Mon, 29 Oct 2012 21:23:06 +0000 (14:23 -0700)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Mon, 29 Oct 2012 21:23:06 +0000 (14:23 -0700)
lib/vehicleproperty.cpp
lib/vehicleproperty.h
plugins/dbus/properties.h
plugins/obd2plugin/CMakeLists.txt
plugins/obd2plugin/obd2source.cpp
plugins/obd2plugin/obd2source.h

index 4ffae17..3549c13 100644 (file)
@@ -81,6 +81,13 @@ const VehicleProperty::Property VehicleProperty::InteriorLightDriver = "Interior
 const VehicleProperty::Property VehicleProperty::InteriorLightCenter = "InteriorLightCenter";
 const VehicleProperty::Property VehicleProperty::InteriorLightPassenger = "InteriorLightPassenger";
 const VehicleProperty::Property VehicleProperty::EngineLoad = "EngineLoad";
+const VehicleProperty::Property VehicleProperty::Horn = "Horn";
+const VehicleProperty::Property VehicleProperty::FuelLevel = "FuelLevel";
+const VehicleProperty::Property VehicleProperty::FuelConsumption = "FuelConsumption";
+const VehicleProperty::Property VehicleProperty::FuelRange = "FuelRange";
+const VehicleProperty::Property VehicleProperty::FuelEconomy = "FuelEconomy";
+const VehicleProperty::Property VehicleProperty::FuelAverageEconomy = "FuelAverageEconomy";
+const VehicleProperty::Property VehicleProperty::EngineOilRemaining = "EngineOilRemaining";
 
 std::list<VehicleProperty::Property> VehicleProperty::mCapabilities;
 
@@ -97,6 +104,8 @@ VehicleProperty::VehicleProperty()
        registerProperty(TurnSignal, [](){ return new TurnSignalType(TurnSignals::Off); });
        registerProperty(ClutchStatus, [](){ return new ClutchStatusType(false); });
        registerProperty(EngineOilPressure, [](){ return new EngineOilPressureType(0); });
+       registerProperty(EngineOilTemperature, [](){ return new EngineOilTemperatureType(0); });
+       REGISTERPROPERTY(EngineOilRemaining,0);
        registerProperty(EngineCoolantTemperature, [](){ return new EngineCoolantTemperatureType(0); });
        registerProperty(MachineGunTurretStatus, [](){ return new MachineGunTurretStatusType(false); });
        registerProperty(AccelerationX, [](){ return new AccelerationType(0); });
@@ -106,7 +115,6 @@ VehicleProperty::VehicleProperty()
        registerProperty(ButtonEvent, [](){ return new ButtonEventType(ButtonEvents::NoButton); });
        registerProperty(BatteryVoltage, [](){ return new BatteryVoltageType(0); });
        registerProperty(InteriorTemperature, [](){ return new InteriorTemperatureType(0); });
-       registerProperty(EngineOilTemperature, [](){ return new EngineOilTemperatureType(0); });
        registerProperty(VIN, [](){ return new VINType(""); });
        registerProperty(WMI, [](){ return new WMIType(""); });
        REGISTERPROPERTYWITHTYPE(TirePressureLeftFront, TirePressureType, 0);
@@ -135,6 +143,14 @@ VehicleProperty::VehicleProperty()
        REGISTERPROPERTYWITHTYPE(InteriorLightPassenger, LightStatusType, false);
        REGISTERPROPERTYWITHTYPE(InteriorLightCenter, LightStatusType, false);
        REGISTERPROPERTY(EngineLoad,0);
+       REGISTERPROPERTY(Horn,false);
+       REGISTERPROPERTY(FuelLevel, 0);
+       REGISTERPROPERTY(FuelRange, 0);
+       REGISTERPROPERTY(FuelConsumption, 0);
+       REGISTERPROPERTY(FuelEconomy, 0);
+       REGISTERPROPERTY(FuelAverageEconomy, 0);
+
+
 }
 
 std::list<VehicleProperty::Property> VehicleProperty::capabilities()
index ed77f25..5b4348b 100644 (file)
@@ -218,6 +218,9 @@ public:
        static const Property EngineOilTemperature;
        typedef BasicPropertyType<int> EngineOilTemperatureType;
 
+       static const Property EngineOilRemaining;
+       typedef BasicPropertyType<uint16_t> EngineOilRemainingType;
+
        /**< Vehicle Identification Number (ISO 3779) 17 chars**/
        static const Property VIN;
        typedef StringPropertyType VINType;
@@ -273,6 +276,24 @@ public:
        static const Property EngineLoad;
        typedef BasicPropertyType<uint16_t> EngineLoadType;
 
+       static const Property Horn;
+       typedef BasicPropertyType<bool> HornType;
+
+       static const Property FuelLevel;
+       typedef BasicPropertyType<uint16_t> FuelLevelType;
+
+       static const Property FuelRange;
+       typedef BasicPropertyType<uint16_t> FuelRangeType;
+
+       static const Property FuelConsumption;
+       typedef BasicPropertyType<uint16_t> FuelConsumptionType;
+
+       static const Property FuelEconomy;
+       typedef BasicPropertyType<uint16_t> FuelEconomyType;
+
+       static const Property FuelAverageEconomy;
+       typedef BasicPropertyType<uint16_t> FuelAverageEconomyType;
+
        static std::list<VehicleProperty::Property> capabilities();
 
        /*! getPropertyTypeForPropertyNameValue returns an AbstractPropertyType* for the property name
index de13977..72b527e 100644 (file)
@@ -172,4 +172,43 @@ public:
        }
 };
 
+class HornProperty: public DBusSink
+{
+public:
+       HornProperty(AbstractRoutingEngine *re, GDBusConnection *connection)
+               :DBusSink("org.automotive.horn","/org/automotive/runningstatus/horn", re, connection, map<string, string>())
+       {
+               wantProperty<bool>(VehicleProperty::Horn,"On","b",AbstractProperty::Read);
+               supportedChanged(re->supported());
+       }
+};
+
+class FuelProperty: public DBusSink
+{
+public:
+       FuelProperty(AbstractRoutingEngine *re, GDBusConnection *connection)
+               :DBusSink("org.automotive.fuel", "/org/automotive/runningstatus/fuel", re, connection, map<string, string>())
+       {
+               wantProperty<uint16_t>(VehicleProperty::FuelLevel,"Level", "y", AbstractProperty::Read);
+               wantProperty<uint16_t>(VehicleProperty::FuelRange,"Range", "q", AbstractProperty::Read);
+               wantProperty<uint16_t>(VehicleProperty::FuelConsumption,"InstantConsumption", "q", AbstractProperty::Read);
+               wantProperty<uint16_t>(VehicleProperty::FuelEconomy,"InstantEconomy", "q", AbstractProperty::Read);
+               wantProperty<uint16_t>(VehicleProperty::FuelAverageEconomy,"AverageEconomy", "q", AbstractProperty::ReadWrite);
+               supportedChanged(re->supported());
+       }
+};
+
+class EngineOilProperty: public DBusSink
+{
+public:
+       EngineOilProperty(AbstractRoutingEngine *re, GDBusConnection *connection)
+                       :DBusSink("org.automotive.engineOil", "/org/automotive/runningstatus/engineOil", re, connection, map<string, string>())
+       {
+               wantProperty<uint16_t>(VehicleProperty::EngineOilRemaining, "Remaining", "y", AbstractProperty::Read);
+               wantProperty<uint16_t>(VehicleProperty::EngineOilTemperature, "Temperature", "y", AbstractProperty::Read);
+               wantProperty<uint16_t>(VehicleProperty::EngineOilPressure, "Pressure", "y", AbstractProperty::Read);
+               supportedChanged(re->supported());
+       }
+};
+
 #endif
index 7edec81..ae2d3c3 100644 (file)
@@ -1,3 +1,5 @@
+if(obd2_plugin)
+
 include(CheckIncludeFiles)
 
 
@@ -14,3 +16,5 @@ set_target_properties(obd2sourceplugin PROPERTIES PREFIX "")
 target_link_libraries(obd2sourceplugin amb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries} ${gio_LIBRARIES} ${gio-unix_LIBRARIES} )
 
 install(TARGETS obd2sourceplugin LIBRARY DESTINATION lib/automotive-message-broker)
+
+endif(obd2_plugin)
index 5807fbc..1bb227d 100644 (file)
 #define __SMALLFILE__ std::string(__FILE__).substr(std::string(__FILE__).rfind("/")+1)
 AbstractRoutingEngine *m_re;
 
+uint16_t Obd2Amb::velocity = 0;
+double Obd2Amb::fuelConsumptionOldTime = 0;
+
+
 int calledPersecond = 0;
 
 bool sendElmCommand(obdLib *obd,std::string command)
@@ -205,6 +209,7 @@ void threadLoop(gpointer data)
                                        double rpm = ((replyVector[2] << 8) + replyVector[3]) / 4.0;
                                        ObdReply *rep = new ObdReply();
                                        rep->req = "0C";
+                                       rep->property = VehicleProperty::EngineSpeed;
                                        rep->reply = boost::lexical_cast<string>(rpm);
                                        g_async_queue_push(privResponseQueue,rep);
                                        //printf("RPM: %f\n",rpm);
@@ -214,6 +219,7 @@ void threadLoop(gpointer data)
                                      int mph = replyVector[2];
                                      ObdReply *rep = new ObdReply();
                                      rep->req = "0D";
+                                         rep->property = VehicleProperty::VehicleSpeed;
                                      rep->reply = boost::lexical_cast<string>(mph);
                                      g_async_queue_push(privResponseQueue,rep);
                                }
@@ -222,6 +228,7 @@ void threadLoop(gpointer data)
                                        int temp = replyVector[2] - 40;
                                        ObdReply *rep = new ObdReply();
                                        rep->req = "05";
+                                       rep->property = VehicleProperty::EngineCoolantTemperature;
                                        rep->reply = boost::lexical_cast<string>(temp);
                                        g_async_queue_push(privResponseQueue,rep);
                                }
@@ -230,13 +237,14 @@ void threadLoop(gpointer data)
                                        double maf = ((replyVector[2] << 8) + replyVector[3]) / 100.0;
                                        ObdReply *rep = new ObdReply();
                                        rep->req = "10";
+                                       rep->property = VehicleProperty::MassAirFlow;
                                        rep->reply = boost::lexical_cast<string>(maf);
                                        g_async_queue_push(privResponseQueue,rep);
                                }
                                else
                                {
                                        //printf("Unknown response type: %i\n",replyVector[1]);
-                                       DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Unknown response type" << replyVector[1] << "\n";
+                                       DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Unknown response type" << replyVector[1] << endl;
                                }
                        }
                        else if (replyVector[0] == 0x49)
@@ -287,7 +295,24 @@ static int updateProperties(/*gpointer retval,*/ gpointer data)
        while(gpointer retval = g_async_queue_try_pop(src->responseQueue))
        {
                ObdReply *reply = (ObdReply*)retval;
-               if (reply->req == "05")
+
+               Obd2Amb obd2amb;
+
+               if(obd2amb.propertyPidMap.count(reply->property) != 0)
+               {
+                       std::string convValue = reply->reply;
+
+                       if(obd2amb.propertyConversionMap.count(reply->property))
+                       {
+                               convValue = obd2amb.propertyConversionMap[reply->property](reply->reply);
+                       }
+
+
+                       AbstractPropertyType* value = VehicleProperty::getPropertyTypeForPropertyNameValue(reply->property, convValue);
+                       src->updateProperty(reply->property, value);
+               }
+
+               /*if (reply->req == "05")
                {
                        VehicleProperty::EngineCoolantTemperatureType speed(reply->reply);
                        src->updateProperty(VehicleProperty::EngineCoolantTemperature,&speed);
@@ -332,7 +357,8 @@ static int updateProperties(/*gpointer retval,*/ gpointer data)
                        src->updateProperty(VehicleProperty::InteriorTemperature,&temp);
                }
                //5C -- engine oil temp
-               //46 interior temp
+               //46 interior temp*/
+
                delete reply;
        }
 
@@ -634,6 +660,7 @@ void OBD2Source::unsubscribeToPropertyChanges(VehicleProperty::Property property
 
        Obd2Amb obd2amb;
        ObdRequest *requ = new ObdRequest();
+       requ->property = property;
        requ->req = obd2amb.propertyPidMap[property];
        g_async_queue_push(subscriptionRemoveQueue,requ);
 }
@@ -711,6 +738,7 @@ void OBD2Source::getPropertyAsync(AsyncPropertyReply *reply)
 
        Obd2Amb obd2amb;
        ObdRequest *requ = new ObdRequest();
+       requ->property = property;
        requ->req = obd2amb.propertyPidMap[property];
        g_async_queue_push(singleShotQueue,requ);
 }
index 931bc0b..603133d 100644 (file)
@@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 #include <abstractsource.h>
 #include <string>
+#include <sstream>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -36,6 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 class ObdRequest
 {
 public:
+  VehicleProperty::Property property;
   std::string req;
   std::string arg;
 };
@@ -43,6 +45,7 @@ public:
 class ObdReply
 {
 public:
+  VehicleProperty::Property property;
   std::string req;
   std::string reply;
 };
@@ -51,6 +54,9 @@ public:
 class Obd2Amb
 {
 public:
+
+       typedef function<std::string (std::string)> ConversionFunction;
+
        Obd2Amb()
        {
                propertyPidMap[VehicleProperty::VehicleSpeed] = "010D1\r";
@@ -58,15 +64,57 @@ public:
                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::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";
+               propertyPidMap[VehicleProperty::FuelConsumption] = "01101\r";
+
+
+
+               propertyConversionMap[VehicleProperty::VehicleSpeed] = [](std::string input)
+               {
+                       ///velocity is used in other equations.  We'll save it off in a static variable:
+                       stringstream vssConvert(input);
+
+                       vssConvert>>velocity;
+
+                       return input;
+               };
+
+               propertyConversionMap[VehicleProperty::WMI] = [](std::string input)
+               {
+                       return input.substr(0,3);
+               };
+
+               propertyConversionMap[VehicleProperty::FuelConsumption] = [](std::string input)
+               {
+                       double maf;
+                       stringstream mafConvert(input);
+
+                       mafConvert>>maf;
+
+                       mafConvert<<1 / (14.75 * 6.26) * maf * 0/60;
+
+                       return mafConvert.str();
+               };
+
+
+
        }
 
+
+
        map<VehicleProperty::Property, std::string> propertyPidMap;
+       map<VehicleProperty::Property, ConversionFunction> propertyConversionMap;
+
+private:
+
+       static uint16_t velocity;
+       static double fuelConsumptionOldTime;
 };
 
 class OBD2Source : public AbstractSource