From f9477956f2d6d774972412f04be7b5a6cb2c6254 Mon Sep 17 00:00:00 2001 From: Leon Oostrum Date: Fri, 15 Nov 2024 15:53:39 +0100 Subject: [PATCH] Add psinfo tool --- host/CMakeLists.txt | 4 +- host/src/psinfo.cc | 92 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 host/src/psinfo.cc diff --git a/host/CMakeLists.txt b/host/CMakeLists.txt index 8838905..c71fd99 100644 --- a/host/CMakeLists.txt +++ b/host/CMakeLists.txt @@ -5,6 +5,7 @@ add_library(PowerSensor src/PowerSensor.cc src/sensors.cc) add_executable(psconfig src/psconfig.cc) add_executable(pstest src/pstest.cc) add_executable(psrun src/psrun.cc) +add_executable(psinfo src/psinfo.cc) target_link_libraries(PowerSensor Threads::Threads) set_property(TARGET PowerSensor PROPERTY POSITION_INDEPENDENT_CODE ON) @@ -16,7 +17,8 @@ if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0) endif() target_link_libraries(pstest PowerSensor) target_link_libraries(psrun PowerSensor) +target_link_libraries(psinfo PowerSensor) -install(TARGETS psconfig pstest psrun DESTINATION bin) +install(TARGETS psconfig pstest psrun psinfo DESTINATION bin) install(FILES include/PowerSensor.hpp include/Semaphore.hpp DESTINATION include) install(TARGETS PowerSensor DESTINATION lib) diff --git a/host/src/psinfo.cc b/host/src/psinfo.cc new file mode 100644 index 0000000..9de03aa --- /dev/null +++ b/host/src/psinfo.cc @@ -0,0 +1,92 @@ +#include + +#include +#include + +#include "PowerSensor.hpp" + + +std::unique_ptr powerSensor; + + +void getPowerSensor(std::string device) { + if (device.empty()) + device = "/dev/ttyACM0"; + if (powerSensor.get() == nullptr) + powerSensor = std::unique_ptr(new PowerSensor3::PowerSensor(device)); +} + + +void printInfo() { + // firmware version + std::cout << powerSensor->getVersion() << std::endl; + + PowerSensor3::State state = powerSensor->read(); + + double totalPower = 0; + + for (unsigned pair = 0; pair < PowerSensor3::MAX_PAIRS; pair++) { + const std::string pairName = powerSensor->getPairName(pair); + const unsigned sensor_current = 2 * pair; + const unsigned sensor_voltage = 2 * pair + 1; + // only print detailed info for active sensor pairs + if (powerSensor->getInUse(sensor_current) && powerSensor->getInUse(sensor_voltage)) { + totalPower += state.current[pair] * state.voltage[pair]; + std::cout << "Sensor pair " << pair << ":" << std::endl; + std::cout << "\tName: " << pairName << std::endl; + std::cout << "\tLatest measured power: " << state.current[pair] * state.voltage[pair] << " W" << std::endl; + std::cout << "\tCurrent sensor details:" << std::endl; + std::cout << "\t\tVref: " << powerSensor->getVref(sensor_current) << " V" << std::endl; + std::cout << "\t\tSensitivity: " << 1000 * powerSensor->getSensitivity(sensor_current) << " mV/A" << std::endl; + std::cout << "\t\tPolarity: " << powerSensor->getPolarity(sensor_current) << std::endl; + std::cout << "\t\tLatest value: " << state.current[pair] << " A" << std::endl; + std::cout << "\tVoltage sensor details:" << std::endl; + std::cout << "\t\tVref: " << powerSensor->getVref(sensor_voltage) << " V" << std::endl; + std::cout << "\t\tGain: " << powerSensor->getSensitivity(sensor_voltage) << std::endl; + std::cout << "\t\tPolarity: " << powerSensor->getPolarity(sensor_voltage) << std::endl; + std::cout << "\t\tLatest value: " << state.voltage[pair] << " V" << std::endl; + } else { + std::cout << "Sensor pair " << pair << ": inactive" << std::endl; + } + } + std::cout << std::endl << "Total power: " << totalPower << " W" << std::endl; +} + + +void usage(char *argv[]) { + std::cerr << "usage: " << argv[0] << " [-h] [-d device]" << std::endl; + std::cerr << "-h prints this help" << std::endl; + std::cerr << "-d selects the device (default: /dev/ttyACM0)" << std::endl; + exit(1); +} + + +int main(int argc, char *argv[]) { + std::string device; + + std::cout << "psinfo version " << PowerSensor3::POWERSENSOR_VERSION << std::endl << std::endl; + for (int opt; (opt = getopt(argc, argv, "d:h")) >= 0;) { + switch (opt) { + // device select + case 'd': + device = optarg; + break; + // help + case 'h': + usage(argv); + break; + + default: + usage(argv); + break; + } + } + + if ((optind < argc)) + usage(argv); + + getPowerSensor(device); + printInfo(); + + return 0; +}