Skip to content
Snippets Groups Projects

robot state memory update

Merged Fabian Reister requested to merge feature/armem-robot-state-memory-update into armem/dev
1 file
+ 21
18
Compare changes
  • Side-by-side
  • Inline
#include "Segment.h"
#include <mutex>
#include <sstream>
#include <ArmarXCore/core/time/TimeUtil.h>
#include "ArmarXCore/core/logging/Logging.h"
#include "RobotAPI/libraries/armem_robot_state/types.h"
#include "RobotAPI/libraries/aron/common/aron_conversions.h"
#include <RobotAPI/libraries/armem/core/aron_conversions.h>
#include <RobotAPI/libraries/armem/core/workingmemory/Visitor.h>
#include "RobotAPI/libraries/armem/core/MemoryID.h"
#include <RobotAPI/libraries/armem/client/Writer.h>
#include <RobotAPI/libraries/armem/client/query/Builder.h>
#include <RobotAPI/libraries/armem/client/query/query_fns.h>
#include <RobotAPI/libraries/armem/server/MemoryToIceAdapter.h>
#include "RobotAPI/libraries/armem_robot/robot_conversions.h"
#include <RobotAPI/libraries/armem_robot/aron/Robot.aron.generated.h>
#include <RobotAPI/libraries/armem_robot_state/aron/JointState.aron.generated.h>
#include <RobotAPI/libraries/armem_objects/aron_conversions.h>
#include <RobotAPI/libraries/armem/util/util.h>
namespace armarx::armem::server::robot_state::proprioception
{
Segment::Segment(armem::server::MemoryToIceAdapter& memoryToIceAdapter, std::mutex& memoryMutex) :
iceMemory(memoryToIceAdapter),
memoryMutex(memoryMutex)
{
Logging::setTag("ProprioceptionSegment");
}
Segment::~Segment() = default;
void Segment::defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string& prefix)
{
defs->optional(p.coreSegment, prefix + "seg.proprioception.CoreSegment", "Name of the object instance core segment.");
defs->optional(p.maxHistorySize, prefix + "seg.proprioception.MaxHistorySize", "Maximal size of object poses history (-1 for infinite).");
}
void Segment::init()
{
ARMARX_CHECK_NOT_NULL(iceMemory.workingMemory);
ARMARX_INFO << "Adding core segment '" << p.coreSegment << "'";
coreSegment = &iceMemory.workingMemory->addCoreSegment(p.coreSegment);
coreSegment->setMaxHistorySize(p.maxHistorySize);
}
void Segment::connect(viz::Client arviz, RobotUnitInterfacePrx robotUnitPrx)
{
robotUnit = robotUnitPrx;
auto kinematicUnit = robotUnit->getKinematicUnit();
const auto providerSegmentName = kinematicUnit->getRobotName();
// TODO what is the purpose?
auto encoderEntryType = std::make_shared<aron::typenavigator::ObjectNavigator>("RobotUnitEncoderEntry");
auto encoderNameType = std::make_shared<aron::typenavigator::StringNavigator>();
auto encoderIterationIDType = std::make_shared<aron::typenavigator::LongNavigator>();
encoderEntryType->addMemberType("EncoderGroupName", encoderNameType);
encoderEntryType->addMemberType("IterationId", encoderIterationIDType);
//auto encoderValueType = std::make_shared<aron::typenavigator::AnyType>();
//encoderEntryType->addMemberType("value", encoderValueType);
ARMARX_INFO << "Adding provider segment " << p.coreSegment << "/" << providerSegmentName;
armem::data::AddSegmentInput input;
input.coreSegmentName = p.coreSegment;
input.providerSegmentName = providerSegmentName;
{
std::lock_guard g{memoryMutex};
auto result = iceMemory.addSegments({input})[0];
if (!result.success)
{
ARMARX_ERROR << "Could not add segment " << p.coreSegment << "/" << providerSegmentName << ". The error message is: " << result.errorMessage;
}
}
robotUnitProviderID.memoryName = iceMemory.workingMemory->id().memoryName;
robotUnitProviderID.coreSegmentName = p.coreSegment;
robotUnitProviderID.providerSegmentName = providerSegmentName;
}
std::unordered_map<std::string, std::map<std::string, float>> Segment::getRobotJointPositions(const armem::Time& timestamp) const
{
std::unordered_map<std::string, std::map<std::string, float>> jointMap;
for (const auto& [robotName, provSeg] : iceMemory.workingMemory->getCoreSegment(p.coreSegment))
{
for (const auto& [name, entity] : provSeg.entities())
{
const auto& entityInstance = entity.getLatestSnapshot().getInstance(0);
const auto jointState = tryCast<armarx::armem::arondto::JointState>(entityInstance);
if (not jointState)
{
// ARMARX_WARNING << "Could not convert entity instance to 'JointState'";
continue;
}
jointMap[robotName].emplace(jointState->name, jointState->position);
}
}
ARMARX_INFO << deactivateSpam(10) << "Number of known robot joint maps: " << jointMap.size();
return jointMap;
}
const armem::MemoryID& Segment::getRobotUnitProviderID() const
{
return robotUnitProviderID;
}
// void Segment::RemoteGui::setup(const Segment& data)
// {
// using namespace armarx::RemoteGui::Client;
// maxHistorySize.setValue(std::max(1, int(data.p.maxHistorySize)));
// maxHistorySize.setRange(1, 1e6);
// infiniteHistory.setValue(data.p.maxHistorySize == -1);
// discardSnapshotsWhileAttached.setValue(data.p.discardSnapshotsWhileAttached);
// GridLayout grid;
// int row = 0;
// grid.add(Label("Max History Size"), {row, 0}).add(maxHistorySize, {row, 1});
// row++;
// grid.add(Label("Infinite History Size"), {row, 0}).add(infiniteHistory, {row, 1});
// row++;
// grid.add(Label("Discard Snapshots while Attached"), {row, 0}).add(discardSnapshotsWhileAttached, {row, 1});
// row++;
// group.setLabel("Data");
// group.addChild(grid);
// }
// void Segment::RemoteGui::update(Segment& data)
// {
// if (infiniteHistory.hasValueChanged() || maxHistorySize.hasValueChanged()
// || discardSnapshotsWhileAttached.hasValueChanged())
// {
// std::scoped_lock lock(data.memoryMutex);
// if (infiniteHistory.hasValueChanged() || maxHistorySize.hasValueChanged())
// {
// data.p.maxHistorySize = infiniteHistory.getValue() ? -1 : maxHistorySize.getValue();
// if (data.coreSegment)
// {
// data.coreSegment->setMaxHistorySize(long(data.p.maxHistorySize));
// }
// }
// data.p.discardSnapshotsWhileAttached = discardSnapshotsWhileAttached.getValue();
// }
// }
} // namespace armarx::armem::server::robot_state::proprioception
Loading