Skip to content

Commit

Permalink
Move mimic motor management to motormanager
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai von Szadkowski authored and Kai von Szadkowski committed Aug 28, 2015
1 parent 46ed0d4 commit 18a719b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 48 deletions.
45 changes: 1 addition & 44 deletions entity_generation/smurf/src/smurf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,7 @@ namespace mars {
if (!loadMotor(motorList[i]))
return 0;

for (std::map<unsigned long, std::string>::iterator it = mimicmotors.begin();
it != mimicmotors.end(); ++it)
loadMimic(it->first, it->second);
control->motors->connectMimics();

for (unsigned int i = 0; i < sensorList.size(); ++i)
if (!loadSensor(sensorList[i]))
Expand Down Expand Up @@ -1049,50 +1047,9 @@ namespace mars {

entity->addMotor(motor.index, motor.name);

sim::SimMotor* newMotor = control->motors->getSimMotor(newId);

// set motor mimics
if (config.find("mimic_motor") != config.end()) {
mimicmotors[newId] = (std::string)config["mimic_motor"];
newMotor->setMimic(
(sReal)config["mimic_multiplier"], (sReal)config["mimic_offset"]);
}

// set approximation functions
if (config.find("maxeffort_approximation") != config.end()) {
std::vector<sReal>* maxeffort_coefficients = new std::vector<sReal>;
ConfigVector::iterator vIt = config["maxeffort_coefficients"].begin();
for (; vIt != config["maxeffort_coefficients"].end(); ++vIt) {
maxeffort_coefficients->push_back((double)(*vIt));
newMotor->setMaxEffortApproximation(
utils::getApproximationFunctionFromString((std::string)config["maxeffort_approximation"]),
maxeffort_coefficients);
}
}
if (config.find("maxspeed_approximation") != config.end()) {
fprintf(stderr, "found maxspeed_approximation in %s\n", ((std::string)config["name"]).c_str());
std::vector<sReal>* maxspeed_coefficients = new std::vector<sReal>;
ConfigVector::iterator vIt = config["maxspeed_coefficients"].begin();
for (; vIt != config["maxspeed_coefficients"].end(); ++vIt) {
maxspeed_coefficients->push_back((double)(*vIt));
newMotor->setMaxSpeedApproximation(
utils::getApproximationFunctionFromString((std::string)config["maxspeed_approximation"]),
maxspeed_coefficients);
}
}
return true;
}

void SMURF::loadMimic(unsigned long mimicId, std::string parent_name) {
sim::SimMotor* parentmotor =
control->motors->getSimMotorByName(parent_name);
if (parentmotor != NULL)
fprintf(stderr, ", %s\n", parentmotor->getName().c_str());
else
fprintf(stderr, "no parentmotor found\n");
parentmotor->addMimic(control->motors->getSimMotor(mimicId));
}

BaseSensor* SMURF::loadSensor(ConfigMap config) {
config["mapIndex"].push_back(ConfigItem(mapIndex));
BaseSensor *sensor = control->sensors->createAndAddSensor(&config);
Expand Down
3 changes: 0 additions & 3 deletions entity_generation/smurf/src/smurf.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ namespace mars {
std::vector<configmaps::ConfigMap> controllerList;
std::vector<configmaps::ConfigMap> graphicList;
std::vector<configmaps::ConfigMap> lightList;
std::map<unsigned long, std::string> mimicmotors;

private:
int groupID;
Expand Down Expand Up @@ -112,8 +111,6 @@ namespace mars {
boost::shared_ptr<urdf::ModelInterface> model;
sim::SimEntity* entity;

void loadMimic(unsigned long mimicId, std::string parent_name);

void handleURI(configmaps::ConfigMap *map, std::string uri);
void handleURIs(configmaps::ConfigMap *map);
void getSensorIDList(configmaps::ConfigMap *map);
Expand Down
2 changes: 2 additions & 0 deletions interfaces/src/MotorData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ namespace mars {
GET_VALUE("maxPosition", maxValue, Double);
GET_VALUE("minPosition", minValue, Double);

this->config = *config;

return 1;
}

Expand Down
2 changes: 2 additions & 0 deletions interfaces/src/sim/MotorManagerInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ namespace mars {
virtual void getDataBrokerNames(unsigned long jointId,
std::string *groupName,
std::string *dataName) const = 0;

virtual void connectMimics() = 0;
}; // class MotorManagerInterface

} // end of namespace interfaces
Expand Down
47 changes: 46 additions & 1 deletion sim/src/core/MotorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <mars/interfaces/sim/SimulatorInterface.h>
#include <mars/interfaces/sim/JointManagerInterface.h>
#include <mars/utils/MutexLocker.h>
#include <mars/utils/mathUtils.h>

namespace mars {
namespace sim {
Expand Down Expand Up @@ -96,6 +97,39 @@ namespace mars {
simMotors[newMotor->getIndex()] = newMotor;
iMutex.unlock();
control->sim->sceneHasChanged(false);

configmaps::ConfigMap &config = motorS->config;

// set motor mimics
if (config.find("mimic_motor") != config.end()) {
mimicmotors[motorS->index] = (std::string)config["mimic_motor"];
newMotor->setMimic(
(sReal)config["mimic_multiplier"], (sReal)config["mimic_offset"]);
}

// set approximation functions
if (config.find("maxeffort_approximation") != config.end()) {
std::vector<sReal>* maxeffort_coefficients = new std::vector<sReal>;
ConfigVector::iterator vIt = config["maxeffort_coefficients"].begin();
for (; vIt != config["maxeffort_coefficients"].end(); ++vIt) {
maxeffort_coefficients->push_back((double)(*vIt));
newMotor->setMaxEffortApproximation(
utils::getApproximationFunctionFromString((std::string)config["maxeffort_approximation"]),
maxeffort_coefficients);
}
}
if (config.find("maxspeed_approximation") != config.end()) {
fprintf(stderr, "found maxspeed_approximation in %s\n", ((std::string)config["name"]).c_str());
std::vector<sReal>* maxspeed_coefficients = new std::vector<sReal>;
ConfigVector::iterator vIt = config["maxspeed_coefficients"].begin();
for (; vIt != config["maxspeed_coefficients"].end(); ++vIt) {
maxspeed_coefficients->push_back((double)(*vIt));
newMotor->setMaxSpeedApproximation(
utils::getApproximationFunctionFromString((std::string)config["maxspeed_approximation"]),
maxspeed_coefficients);
}
}

return motorS->index;
}

Expand Down Expand Up @@ -384,9 +418,10 @@ namespace mars {
void MotorManager::clearAllMotors(bool clear_all) {
MutexLocker locker(&iMutex);
map<unsigned long, SimMotor*>::iterator iter;
for(iter = simMotors.begin(); iter != simMotors.end(); iter++)
for(iter = simMotors.begin(); iter != simMotors.end(); iter++)
delete iter->second;
simMotors.clear();
mimicmotors.clear();
if(clear_all) simMotorsReload.clear();
next_motor_id = 1;
}
Expand All @@ -407,6 +442,7 @@ namespace mars {
iMutex.lock();
}
iMutex.unlock();
connectMimics();
}

/**
Expand Down Expand Up @@ -487,5 +523,14 @@ namespace mars {
iter->second->getDataBrokerNames(groupName, dataName);
}

void MotorManager::connectMimics() {
std::map<unsigned long, std::string>::iterator it;
for (it = mimicmotors.begin(); it!=mimicmotors.end(); ++it) {
SimMotor* parentmotor = getSimMotorByName(it->second);
if (parentmotor != NULL)
parentmotor->addMimic(simMotors[it->first]);
}
}

} // end of namespace sim
} // end of namespace mars
4 changes: 4 additions & 0 deletions sim/src/core/MotorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ namespace mars {
std::string *groupName,
std::string *dataName) const;

virtual void connectMimics();

private:
//! the id of the next motor that is added to the simulation
unsigned long next_motor_id;
Expand All @@ -329,6 +331,8 @@ namespace mars {
//! a mutex for the motor containters
mutable utils::Mutex iMutex;

// map of mimicmotors
std::map<unsigned long, std::string> mimicmotors;
}; // class MotorManager

} // end of namespace sim
Expand Down
1 change: 1 addition & 0 deletions sim/src/core/SimMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ namespace mars {
// delete any coefficient vectors we might have created
delete maxspeed_coefficients;
delete maxeffort_coefficients;
mimics.clear();
}

void SimMotor::addMimic(SimMotor* mimic) {
Expand Down

0 comments on commit 18a719b

Please sign in to comment.