Search code examples
matlaboopclass-diagram

class diagram sensor interface


To organize my software better I'm starting to port my matlab functions to matlab classes and I'm struggling a bit with the oop concept.

Thats the situation:

  • there are physical sensors measuring a position in various ways. Each sensor has a different calculation algorithm, but all sensors do have smiliar properties: temperature, rawData, serialNumber etc.
  • I want to simulate the sensor's output (via previously saved data from a database or a csv file) or get data directly from an UART interface.
  • the data format differs, but the readout functions are similar (loading columns from a csv file or from a database)
  • I want to calibrate the sensors within a class "SensorToolbox". This class could be used by a GUI and shall be as generic as possible.

Here is my first class diagram attached: classDiagram

My main objective is where to put the "loadDataFromX" and "calibrateSensor" methods. If I consider a sensor object representating a "real" sensor, then the sensor can not load data or calibrate itself. It's only possible via a function in the "SensorToolbox". But the calibration realization is different for each sensor. So putting the function into the toolbox would require the detection of the class type and a implementation outside the sensor. The same applies to the sensor's data. Calling "loadDataFromFile" from the toolbox would require to know the needed dataformat outside of the sensor.

Hopefully you understand my problem and you can push me into the right direction, thank you!


Solution

  • I think you should put these methods into sensors. Don't worry that they don't 100% represent the behavior of the real sensor, it is just a model.

    Another option is to share these methods between SensorToolbox and Sensor. For example, SensorToolbox can read the data from different datasources and then pass in to the Sensor, for example (pseudocode):

    SensorToolbox::loadDataFromDatabase():
       database = Database()
       data = database->readData()
       sensor->loadData(data)
    
    SensorToolbox::loadDataFromFile():
       file = File('path/to/file')
       data = file->read()
       sensor->loadData(data)
    

    Same for the calibration, SensorToolbox can have this method and then just path it to the Sensor (or maybe do some preparation before it):

    SensorToolbox::calibrate(Sensor sensor, SensorData data):
       // check the data
       // ...
       sensor->calibrate(data)