Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add psinfo tool #182

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}