An update will be applied December 9th, between 12PM and 1:00PM EST (UTC -5:00). The site may be slow during that time.

Commit 9c58ec48 authored by Alexis Girault's avatar Alexis Girault
Browse files

ENH: Implement Device API in SimulationManager

parent 9a058f2a
......@@ -107,6 +107,105 @@ SimulationManager::removeScene(std::string sceneName)
LOG(INFO) << "Scene removed: " << sceneName;
}
bool
SimulationManager::isDeviceServerRegistered(std::string serverName) const
{
return m_deviceServerMap.find(serverName) != m_deviceServerMap.end();
}
std::shared_ptr<VRPNDeviceServer>
SimulationManager::getDeviceServer(std::string serverName) const
{
if (!this->isDeviceServerRegistered(serverName))
{
LOG(WARNING) << "No device server at '" << serverName
<< "' was registered in this simulation";
return nullptr;
}
return m_deviceServerMap.at(serverName);
}
void
SimulationManager::addDeviceServer(std::shared_ptr<VRPNDeviceServer> newServer)
{
std::string newServerName = newServer->getName();
if (this->isDeviceServerRegistered(newServerName))
{
LOG(WARNING) << "Can not add device server: '" << newServerName
<< "' is already registered in this simulation\n"
<< "Set this server address to a unique ip:port first";
return;
}
m_deviceServerMap[newServerName] = newServer;
LOG(INFO) << "Device server added: " << newServerName;
}
void
SimulationManager::removeDeviceServer(std::string serverName)
{
if (!this->isDeviceServerRegistered(serverName))
{
LOG(WARNING) << "No device server at '" << serverName
<< "' was registered in this simulation";
return;
}
m_deviceServerMap.erase(serverName);
LOG(INFO) << "Device server removed: " << serverName;
}
bool
SimulationManager::isDeviceClientRegistered(std::string deviceClientName) const
{
return m_deviceClientMap.find(deviceClientName) != m_deviceClientMap.end();
}
std::shared_ptr<DeviceClient>
SimulationManager::getDeviceClient(std::string deviceClientName) const
{
if (!this->isDeviceClientRegistered(deviceClientName))
{
LOG(WARNING) << "No device client named '" << deviceClientName
<< "' was registered in this simulation";
return nullptr;
}
return m_deviceClientMap.at(deviceClientName);
}
void
SimulationManager::addDeviceClient(std::shared_ptr<DeviceClient> newDeviceClient)
{
std::string newDeviceClientName = newDeviceClient->getName();
if (this->isDeviceClientRegistered(newDeviceClientName))
{
LOG(WARNING) << "Can not add device client: '" << newDeviceClientName
<< "' is already registered in this simulation\n"
<< "Set this device name to a unique name first";
return;
}
m_deviceClientMap[newDeviceClientName] = newDeviceClient;
LOG(INFO) << "Device client added: " << newDeviceClientName;
}
void
SimulationManager::removeDeviceClient(std::string deviceClientName)
{
if (!this->isDeviceClientRegistered(deviceClientName))
{
LOG(WARNING) << "No device client named '" << deviceClientName
<< "' was registered in this simulation";
return;
}
m_deviceClientMap.erase(deviceClientName);
LOG(INFO) << "Device client removed: " << deviceClientName;
}
std::shared_ptr<Viewer>
SimulationManager::getViewer() const
{
......@@ -200,15 +299,30 @@ SimulationManager::startSimulation(bool debug)
return;
}
// Start Simulation
// Simulation
if( !debug )
{
LOG(INFO) << "Starting simulation";
m_viewer->setRenderingMode(Renderer::Mode::SIMULATION);
// Start device servers
for(const auto& pair : m_deviceServerMap)
{
this->startModuleInNewThread(pair.second);
}
// Start device clients
for(const auto& pair : m_deviceClientMap)
{
this->startModuleInNewThread(pair.second);
}
// Start scene
this->startModuleInNewThread(startingScene);
m_status = SimulationStatus::RUNNING;
}
// Start Debug
// Debug
else
{
m_viewer->setRenderingMode(Renderer::Mode::DEBUG);
......@@ -243,6 +357,18 @@ SimulationManager::runSimulation()
// Run scene
m_sceneMap.at(m_currentSceneName)->run();
// Run device servers
for(const auto& pair : m_deviceServerMap)
{
(pair.second)->run();
}
// Run device clients
for(const auto& pair : m_deviceClientMap)
{
(pair.second)->run();
}
// Update simulation status
m_status = SimulationStatus::RUNNING;
}
......@@ -261,6 +387,18 @@ SimulationManager::pauseSimulation()
// Pause scene
m_sceneMap.at(m_currentSceneName)->pause();
// Pause device clients
for(const auto& pair : m_deviceClientMap)
{
(pair.second)->pause();
}
// Pause device servers
for(const auto& pair : m_deviceServerMap)
{
(pair.second)->pause();
}
// Update simulation status
m_status = SimulationStatus::PAUSED;
}
......@@ -280,6 +418,20 @@ SimulationManager::endSimulation()
// Update Renderer
m_viewer->setRenderingMode(Renderer::Mode::DEBUG);
// End device clients
for(const auto& pair : m_deviceClientMap)
{
(pair.second)->end();
m_threadMap.at(pair.first).join();
}
// Pause device servers
for(const auto& pair : m_deviceServerMap)
{
(pair.second)->end();
m_threadMap.at(pair.first).join();
}
// End all scenes
for (auto pair : m_sceneMap)
{
......
......@@ -27,6 +27,9 @@
#include <thread>
#include <memory>
#include "imstkScene.h"
#include "imstkVRPNDeviceServer.h"
#include "imstkDeviceClient.h"
#include "imstkScene.h"
#include "imstkViewer.h"
#include "imstkLogUtility.h"
......@@ -49,12 +52,24 @@ public:
const SimulationStatus& getStatus() const;
// Scene
bool isSceneRegistered(std::string sceneName) const;
std::shared_ptr<Scene> getScene(std::string sceneName) const;
std::shared_ptr<Scene> createNewScene(std::string newSceneName);
std::shared_ptr<Scene> createNewScene();
void addScene(std::shared_ptr<Scene>newScene);
void removeScene(std::string sceneName);
bool isSceneRegistered(std::string sceneName) const;
std::shared_ptr<Scene> getScene(std::string sceneName) const;
std::shared_ptr<Scene> createNewScene(std::string newSceneName);
std::shared_ptr<Scene> createNewScene();
void addScene(std::shared_ptr<Scene> newScene);
void removeScene(std::string sceneName);
// Device Server
bool isDeviceServerRegistered(std::string serverName) const;
std::shared_ptr<VRPNDeviceServer> getDeviceServer(std::string serverName) const;
void addDeviceServer(std::shared_ptr<VRPNDeviceServer> newServer);
void removeDeviceServer(std::string serverName);
// Device Client
bool isDeviceClientRegistered(std::string deviceClientName) const;
std::shared_ptr<DeviceClient> getDeviceClient(std::string deviceClientName) const;
void addDeviceClient(std::shared_ptr<DeviceClient> newDeviceClient);
void removeDeviceClient(std::string deviceClientName);
// Viewer
std::shared_ptr<Viewer> getViewer() const;
......@@ -73,7 +88,11 @@ private:
SimulationStatus m_status = SimulationStatus::INACTIVE;
std::string m_currentSceneName = "";
std::unordered_map<std::string, std::shared_ptr<Scene> > m_sceneMap;
std::unordered_map<std::string, std::shared_ptr<Scene>> m_sceneMap;
std::unordered_map<std::string, std::shared_ptr<VRPNDeviceServer>> m_deviceServerMap;
std::unordered_map<std::string, std::shared_ptr<DeviceClient>> m_deviceClientMap;
std::unordered_map<std::string, std::thread> m_threadMap;
std::shared_ptr<Viewer> m_viewer = std::make_shared<Viewer>(this);
......
......@@ -62,24 +62,32 @@ int main()
void testDevices()
{
// SDK and Scene
auto sdk = std::make_shared<imstk::SimulationManager>();
auto scene = sdk->createNewScene("SceneTestDevice");
scene->setLoopDelay(1000);
// Device server
auto server = std::make_shared<imstk::VRPNDeviceServer>("127.0.0.1");
server->addDevice("device0", imstk::DeviceType::NOVINT_FALCON);
server->setLoopDelay(100);
sdk->addDeviceServer(server);
// Device Client
auto client = std::make_shared<imstk::VRPNDeviceClient>("device0", "localhost"); // localhost = 127.0.0.1
client->setLoopDelay(100);
sdk->addDeviceClient(client);
// Start server in other thread
auto t = std::thread([server] { server->start(); });
// Start client here
client->start();
// Sphere
auto sphereGeom = std::make_shared<imstk::Sphere>();
sphereGeom->scale(0.1);
auto sphereObj = std::make_shared<imstk::VisualObject>("VisualSphere");
sphereObj->setVisualGeometry(sphereGeom);
scene->addSceneObject(sphereObj);
// When client quits, end server
server->end();
t.join();
// Run
sdk->setCurrentScene("SceneTestDevice");
sdk->startSimulation(true);
}
void testReadMesh()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment