Skip to content

Commit

Permalink
Merge pull request #182 from nlesc-recruit/178-psinfo
Browse files Browse the repository at this point in the history
Add psinfo tool
  • Loading branch information
loostrum authored Nov 18, 2024
2 parents d17d3ca + f947795 commit 6cddd5d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
4 changes: 3 additions & 1 deletion host/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
92 changes: 92 additions & 0 deletions host/src/psinfo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <unistd.h>

#include <iostream>
#include <chrono>

#include "PowerSensor.hpp"


std::unique_ptr<PowerSensor3::PowerSensor> powerSensor;


void getPowerSensor(std::string device) {
if (device.empty())
device = "/dev/ttyACM0";
if (powerSensor.get() == nullptr)
powerSensor = std::unique_ptr<PowerSensor3::PowerSensor>(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;
}

0 comments on commit 6cddd5d

Please sign in to comment.