11#include <cuda_runtime_api.h>
17#include <mx/improc/eigenCube.hpp>
18#include <mx/improc/eigenImage.hpp>
20#include "../../libMagAOX/libMagAOX.hpp"
21#include "../../magaox_git_version.h"
23using namespace nvinfer1;
26class Logger :
public ILogger {
28 if (
severity <= Severity::kWARNING) {
29 std::cout << msg << std::endl;
49 size(_num_rows * _num_cols),
60 void add(
float* image) {
289 mx::app::appConfigurator &
_config );
335 std::ifstream file(
filename, std::ios::binary);
337 std::cout <<
"Error opening " <<
filename << std::endl;
340 file.seekg(0, std::ios::end);
343 file.seekg(0, std::ios::beg);
353 std::cout <<
"Failed to createInferRuntime\n";
358 std::cout <<
"Failed to deserialize CUDA engine.\n";
360 std::cout <<
"Deserialized CUDA engine.\n";
367 std::cout <<
"Number of IO Tensors: " <<
numIOTensors << std::endl;
369 auto inputName =
engine->getIOTensorName(0);
370 auto outputName =
engine->getIOTensorName(1);
371 std::cout <<
"Tensor IO names: " << inputName <<
" " << outputName << std::endl;
381 std::cout <<
"Tensor input dimensions: " <<
inputC <<
"x" <<
inputH <<
"x" <<
inputW << std::endl;
382 std::cout <<
"Tensor output dimensions: " <<
outputSize << std::endl;
396 std::cout <<
"Failed to deserialize CUDA engine.\n";
398 std::cout <<
"Deserialized CUDA engine.\n";
405 std::cout <<
"Start switching engine" << std::endl;
413 std::cout <<
"Done switching engine" << std::endl;
483 std::cout <<
"setupConfig()" << std::endl;
486 config.add(
"parameters.dataDirs",
488 "parameters.dataDirs",
494 "The path to the directory with the onnx model." );
496 config.add(
"parameters.engineDirs",
498 "parameters.engineDirs",
504 "The path to the directory with the TRT engine." );
507 config.add(
"parameters.engineName",
509 "parameters.engineName",
515 "Name of the TRT engine." );
517 config.add(
"parameters.channel",
519 "parameters.channel",
525 "The output channel." );
527 config.add(
"parameters.observation_channel",
529 "parameters.observation_channel",
532 "observation_channel",
535 "The output channel for the observations." );
537 config.add(
"parameters.action_channel",
539 "parameters.action_channel",
545 "The output channel for the actions." );
547 config.add(
"parameters.iterations_per_ep",
549 "parameters.iterations_per_ep",
555 "Number of iterations per episode." );
557 config.add(
"parameters.integrator_gain",
559 "parameters.integrator_gain",
565 "Gain of the integrator during warmup." );
567 config.add(
"parameters.warmup_episodes",
569 "parameters.warmup_episodes",
575 "Number of episodes for the warmup." );
577 config.add(
"parameters.replay_buffer_size",
579 "parameters.replay_buffer_size",
582 "replay_buffer_size",
585 "Number of samples in the real-time buffer." );
587 config.add(
"parameters.max_sigma",
589 "parameters.max_sigma",
595 "Standard deviation of the exploration noise." );
597 config.add(
"parameters.Nhist",
605 "Number of frames in the history vector." );
607 config.add(
"parameters.modal_filt_matrix",
609 "parameters.modal_filt_matrix",
615 "Name of the model filtering matrix file." );
617 config.add(
"parameters.reloadEngine",
619 "parameters.reloadEngine",
625 "Whether to reload the engine." );
631 std::cout <<
"loadConfigImpl()" << std::endl;
651 std::cout <<
"Debug configuration loading: " << std::endl;
652 std::cout <<
"dataDirs: " <<
dataDirs << std::endl;
653 std::cout <<
"engineDirs: " <<
engineDirs << std::endl;
654 std::cout <<
"engineName: " <<
engineName << std::endl;
739 std::cout <<
"allocate()" << std::endl;
740 static_cast<void>(
dummy );
754 std::cout <<
"Close shmims" << std::endl;
760 std::cout <<
"Open shmims" << std::endl;
772 std::cout <<
"Done!" << std::endl;
822 static_cast<void>(
dummy );
909 if(
ipRecv.getName() != m_indiP_reloadToggle.getName())
911 log<software_error>({__FILE__, __LINE__,
"invalid indi property received"});
916 if(
ipRecv[
"toggle"].getSwitchState() == pcf::IndiElement::On)
918 std::cout <<
"Reloading model" << std::endl;
921 engineReloaded =
true;
922 updateSwitchIfChanged(m_indiP_reloadToggle,
"toggle", pcf::IndiElement::Off,
INDI_BUSY);
928 if(
ipRecv[
"toggle"].getSwitchState() == pcf::IndiElement::Off)
930 std::cout <<
"No new model" << std::endl;
CircularBuffer(size_t _num_rows, size_t _num_cols, size_t _historySize)
void add_eigenimage(Eigen::Map< eigenImage< float > > image)
void log(Severity severity, const char *msg) noexcept override
The base-class for MagAO-X applications.
stateCodes::stateCodeT state()
Get the current state code.
int registerIndiPropertyNew(pcf::IndiProperty &prop, int(*)(void *, const pcf::IndiProperty &))
Register an INDI property which is exposed for others to request a New Property for.
int createStandardIndiToggleSw(pcf::IndiProperty &prop, const std::string &name, const std::string &label="", const std::string &group="")
Create a standard R/W INDI switch with a single toggle element.
void updateSwitchIfChanged(pcf::IndiProperty &p, const std::string &el, const pcf::IndiElement::SwitchStateType &newVal, pcf::IndiProperty::PropertyStateType ipState=pcf::IndiProperty::Ok)
Update an INDI switch element value if it has changed.
static int log(const typename logT::messageT &msg, logPrioT level=logPrio::LOG_DEFAULT)
Make a log entry.
std::mutex m_indiMutex
Mutex for locking INDI communications.
int appStartup()
Startup function.
int setupConfig(mx::app::appConfigurator &config)
Setup the configuration system.
int updateINDI()
Update the INDI properties for this device controller.
int appLogic()
Checks the shmimMonitor thread.
int appShutdown()
Shuts down the shmimMonitor thread.
int loadConfig(mx::app::appConfigurator &config)
load the configuration system results
float realT
Floating point type in which to do all calculations.engine.
uint32_t iterations_per_ep
virtual int appStartup()
Startup function.
virtual int appShutdown()
Shutdown the app.
virtual void setupConfig()
size_t po4ao_obs_TypeSize
The size of the type, in bytes.
std::string m_outputChannel
uint8_t m_outputDataType
The ImageStreamIO type code.
INDI_NEWCALLBACK_DECL(po4ao, m_indiP_reloadToggle)
int allocate(const dev::shmimT &dummy)
std::vector< char > engineData
float * integrator_commands
CircularBuffer * reconstructed_buffer
CircularBuffer * command_buffer
dev::shmimMonitor< po4ao > shmimMonitorT
virtual int appLogic()
Implementation of the FSM for po4ao.
void load_engine(const std::string filename)
void cleanup_engine_context()
uint32_t po4ao_obs_Height
The height of the shmim.
void cleanup_engine_memory()
std::string po4ao_act_Channel
unsigned long episode_counter
void prepare_engine_memory()
size_t m_outputTypeSize
The size of the type, in bytes.
unsigned long frame_counter
IExecutionContext * context
int processImage(void *curr_src, const dev::shmimT &dummy)
pcf::IndiProperty m_indiP_reloadToggle
uint32_t po4ao_obs_Width
The width of the shmim.
int loadConfigImpl(mx::app::appConfigurator &_config)
Implementation of loadConfig logic, separated for testing.
virtual void loadConfig()
std::string po4ao_obs_Channel
IExecutionContext * context2
uint8_t po4ao_obs_DataType
The ImageStreamIO type code.
uint32_t m_outputHeight
The height of the shmim.
~po4ao() noexcept
D'tor, declared and defined for noexcept.
void create_engine_context()
uint32_t m_outputWidth
The width of the shmim.
#define INDI_NEWCALLBACK_DEFN(class, prop)
Define the callback for a new property request.
#define INDI_NEWCALLBACK(prop)
Get the name of the static callback wrapper for a new property.
@ OPERATING
The device is operating, other than homing.
const pcf::IndiProperty & ipRecv
std::unique_lock< std::mutex > lock(m_indiMutex)
static constexpr logPrioT LOG_NOTICE
A normal but significant condition.