Skip to content

Commit

Permalink
Merge pull request #138 from nlesc-recruit/136-sensor-pair-names
Browse files Browse the repository at this point in the history
Add sensor pair name to eeprom
  • Loading branch information
loostrum authored Jul 20, 2023
2 parents 2ecef7b + 6d68bc2 commit af5b9d6
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
1 change: 1 addition & 0 deletions device/PowerSensor/PowerSensor.ino
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ bool sendMarkerNext = false;

struct Sensor {
char type[16];
char pairName[16];
float vref;
float sensitivity;
bool inUse;
Expand Down
7 changes: 7 additions & 0 deletions host/include/PowerSensor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,20 @@ class PowerSensor {

void writeSensorsToEEPROM();
void setType(unsigned int sensorID, const std::string type);
void setPairName(unsigned int sensorID, const std::string pairName);
void setVref(unsigned int sensorID, const float vref);
void setSensitivity(unsigned int sensorID, const float slope);
void setInUse(unsigned int sensorID, const bool inUse);

std::string getType(unsigned int sensorID) const;
std::string getPairName(unsigned int sensorID) const;
float getVref(unsigned int sensorID) const;
float getSensitivity(unsigned int sensorID) const;
bool getInUse(unsigned int sensorID) const;

private:
static const unsigned MAX_TYPE_LENGTH = 16;
static const unsigned MAX_PAIRNAME_LENGTH = 16;

int fd;
int pipe_fd;
Expand Down Expand Up @@ -104,6 +107,8 @@ class PowerSensor {
struct EEPROM {
/** @brief Sensor type */
char type[MAX_TYPE_LENGTH];
/** @brief Sensor pair name */
char pairName[MAX_PAIRNAME_LENGTH];
/** @brief Sensor reference voltage */
float vref;
/** @brief Sensor sensitivity (V/A for current sensors, unitless gain for voltage sensors) */
Expand All @@ -113,12 +118,14 @@ class PowerSensor {
} __attribute__((packed));

std::string type;
std::string pairName;
float vref;
float sensitivity;
bool inUse;
uint16_t level;
double valueAtLastMeasurement;
void setType(const std::string type);
void setPairName(const std::string pairName);
void setVref(const float vref);
void setSensitivity(const float slope);
void setInUse(const bool inUse);
Expand Down
20 changes: 20 additions & 0 deletions host/src/PowerSensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,16 @@ namespace PowerSensor3 {
return sensors[sensorID].type;
}

/**
* @brief Get sensor pair name of given sensor
*
* @param sensorID
* @return std::string
*/
std::string PowerSensor::getPairName(unsigned int sensorID) const {
return sensors[sensorID].pairName;
}

/**
* @brief Get reference voltage (V) of given sensor
*
Expand Down Expand Up @@ -601,6 +611,16 @@ namespace PowerSensor3 {
sensors[sensorID].setType(type);
}

/**
* @brief Set sensor pair name of given sensor
*
* @param sensorID
* @param pairName
*/
void PowerSensor::setPairName(unsigned int sensorID, const std::string pairName) {
sensors[sensorID].setPairName(pairName);
}

/**
* @brief Set reference voltage (V) of given sensor
*
Expand Down
15 changes: 14 additions & 1 deletion host/src/psconfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ void print() {

std::cout << "sensor " << sensor << " (" << sensorType << "): "
"type: " << powerSensor->getType(sensor) << ", "
"name: " << powerSensor->getPairName(sensor) << ", "
"Vref: " << powerSensor->getVref(sensor) << " V, " <<
sensitivityName << ": " << factor * powerSensor->getSensitivity(sensor) << unit << ", "
"Status: " << (powerSensor->getInUse(sensor) ? "on" : "off") << std::endl;
Expand All @@ -171,6 +172,7 @@ void usage(char *argv[]) {
std::cerr << "-s selects the sensor (0-" << PowerSensor3::MAX_SENSORS << ")" << std::endl;
std::cerr << "-t sets the sensor type. This also sets the sensitivity to the default value if "
"the sensor is of a type known to this programme (see list at the bottom of this help)." << std::endl;
std::cerr << "-m sets the sensor pair name." << std::endl;
std::cerr << "-v sets the reference voltage level" << std::endl;
std::cerr << "-a automatically calibrate vref of the current sensor. "
"The input to the sensor must be zero volt or ampere" << std::endl;
Expand All @@ -191,7 +193,7 @@ int main(int argc, char *argv[]) {
bool doPrint = false;

std::cout << "psconfig version " << PowerSensor3::POWERSENSOR_VERSION << std::endl << std::endl;
for (int opt; (opt = getopt(argc, argv, "d:s:i:t:av:n:o:ph")) >= 0;) {
for (int opt; (opt = getopt(argc, argv, "d:s:i:t:m:av:n:o:ph")) >= 0;) {
switch (opt) {
// device select
case 'd':
Expand All @@ -214,6 +216,17 @@ int main(int argc, char *argv[]) {
break;
}

// sensor pair name
case 'm':
getPowerSensor(device)->setPairName(sensor, optarg);
// ensure to set same pair name for both the current and voltage sensor
if ((sensor % 2) == 0) {
getPowerSensor(device)->setPairName(sensor + 1, optarg);
} else {
getPowerSensor(device)->setPairName(sensor - 1, optarg);
}
break;

// sensor auto calibration of reference voltage
case 'a':
getPowerSensor(device);
Expand Down
27 changes: 26 additions & 1 deletion host/src/sensors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ void PowerSensor::Sensor::readFromEEPROM(int fd) {
// If EEPROM is corrupted, type name may be too long. Avoid unrecoverable situation by changing type if that happens
std::string type = eeprom.type;
if (type.length() >= MAX_TYPE_LENGTH) {
std::cerr << "Read invalid type from device, EEPROM data may be corrupt" << std::endl;
std::cerr << "Read invalid sensor type from device, EEPROM data may be corrupt" << std::endl;
type = "INVALID";
}

// Same check as for type name
std::string pairName = eeprom.pairName;
if (pairName.length() >= MAX_PAIRNAME_LENGTH) {
std::cerr << "Read invalid sensor pair name from device, EEPROM data may be corrupt" << std::endl;
pairName = "INVALID";
}

setType(type);
setPairName(pairName);
setVref(eeprom.vref);
setSensitivity(eeprom.sensitivity);
setInUse(eeprom.inUse);
Expand All @@ -48,6 +56,7 @@ void PowerSensor::Sensor::writeToEEPROM(int fd) const {
EEPROM eeprom;

strncpy(eeprom.type, type.c_str(), type.length() + 1); // plus one for null termination character
strncpy(eeprom.pairName, pairName.c_str(), pairName.length() + 1); // plus one for null termination character
eeprom.vref = vref;
eeprom.sensitivity = sensitivity;
eeprom.inUse = inUse;
Expand Down Expand Up @@ -103,6 +112,22 @@ void PowerSensor::Sensor::setType(const std::string type) {
}
}

/**
* @brief Set name of sensor pair
*
* @param pairName
*/
void PowerSensor::Sensor::setPairName(const std::string pairName) {
if (pairName.length() >= MAX_PAIRNAME_LENGTH) {
// MAX_PAIRNAME_LENGTH includes null termination character
std::cerr << "Sensor pair name can be at most " << MAX_PAIRNAME_LENGTH - 1 << " characters" << std::endl;
exit(1);
} else {
this->pairName = pairName;
}
}


/**
* @brief Set reference voltage of sensor
*
Expand Down

0 comments on commit af5b9d6

Please sign in to comment.