Newer
Older
/*
* Copyright 2011,
* Olivier Stasse,
*
* CNRS
*
* This file is part of sot-core.
* sot-core is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
* sot-core is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. You should
* have received a copy of the GNU Lesser General Public License along
* with sot-core. If not, see <http://www.gnu.org/licenses/>.
*/
/* -------------------------------------------------------------------------- */
/* --- INCLUDES ------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
#include <dynamic_graph_bridge/sot_loader.hh>
#include "dynamic_graph_bridge/ros_init.hh"
// POSIX.1-2001
#include <dlfcn.h>
#include <boost/thread/condition.hpp>
boost::condition_variable cond;
using namespace std;
namespace po = boost::program_options;
std::vector<double> ittimes;
DataToLog(std::size_t N_)
: N(N_)
, idx(0)
, iter(0)
, iters(N, 0)
, times(N, 0)
, ittimes(N, 0)
{}
void record(const double t, const double itt) {
iters [idx] = iter;
times [idx] = t;
ittimes[idx] = itt;
std::ostringstream oss;
oss << prefix << "-times.log";
std::ofstream aof(oss.str().c_str());
if (aof.is_open()) {
for (std::size_t k = 0; k < N; ++k) {
aof
<< iters [(idx + k) % N] << ' '
<< times [(idx + k) % N] << ' '
<< ittimes[(idx + k) % N] << '\n';
if (ros::param::has("/sot_controller/dt")) {
ros::param::get("/sot_controller/dt", periodd);
rate.reset();
while (ros::ok() && aSotLoader->isDynamicGraphStopped()) {
rate.sleep();
ros::Time timeOrigin = ros::Time::now();
ros::Time time;
while (ros::ok() && !aSotLoader->isDynamicGraphStopped()) {
nh.param<bool>("paused", paused, false);
if (!paused) {
ros::Duration d = ros::Time::now() - time;
dataToLog.record((time - timeOrigin).toSec(), d.toSec());
cond.notify_all();
ros::waitForShutdown();
}
SotLoader::SotLoader()
: sensorsIn_(),
controlValues_(),
angleEncoder_(),
angleControl_(),
forces_(),
torques_(),
baseAtt_(),
accelerometer_(3),
gyrometer_(3),
thread_() {
readSotVectorStateParam();
initPublication();
dynamic_graph_stopped_ = true;
thread_.join();
}
void SotLoader::startControlLoop() {
thread_ = boost::thread(workThreadLoader, this);
SotLoaderBasic::initializeRosNode(argc, argv);
// Temporary fix. TODO: where should nbOfJoints_ be initialized from?
Rohan Budhiraja
committed
if (ros::param::has("/sot/state_vector_map")) {
angleEncoder_.resize(nbOfJoints_);
Rohan Budhiraja
committed
}
startControlLoop();
}
void SotLoader::fillSensors(map<string, dgs::SensorValues> &sensorsIn) {
// Update joint values.w
Rohan Budhiraja
committed
assert(angleControl_.size() == angleEncoder_.size());
sensorsIn["joints"].setName("angle");
angleEncoder_[i] = angleControl_[i];
void SotLoader::readControl(map<string, dgs::ControlValues> &controlValues) {
// Update joint values.
angleControl_ = controlValues["control"].getValues();
// Debug
std::map<std::string, dgs::ControlValues>::iterator it =
controlValues.begin();
sotDEBUG(30) << "ControlValues to be broadcasted:" << std::endl;
for (; it != controlValues.end(); it++) {
sotDEBUG(30) << it->first << ":";
std::vector<double> ctrlValues_ = it->second.getValues();
std::vector<double>::iterator it_d = ctrlValues_.begin();
for (; it_d != ctrlValues_.end(); it_d++) sotDEBUG(30) << *it_d << " ";
sotDEBUG(30) << std::endl;
// Check if the size if coherent with the robot description.
if (angleControl_.size() != (unsigned int)nbOfJoints_) {
std::cerr << " angleControl_" << angleControl_.size() << " and nbOfJoints"
<< (unsigned int)nbOfJoints_ << " are different !" << std::endl;
exit(-1);
}
joint_state_.header.stamp = ros::Time::now();
for (int i = 0; i < nbOfJoints_; i++) {
joint_state_.position[i] = angleControl_[i];
}
for (unsigned int i = 0; i < parallel_joints_to_state_vector_.size(); i++) {
joint_state_.position[i + nbOfJoints_] =
coefficient_parallel_joints_[i] *
angleControl_[parallel_joints_to_state_vector_[i]];
}
joint_pub_.publish(joint_state_);
std::vector<double> poseValue_ = controlValues["baseff"].getValues();
freeFlyerPose_.setOrigin(
tf::Vector3(poseValue_[0], poseValue_[1], poseValue_[2]));
tf::Quaternion poseQ_(poseValue_[4], poseValue_[5], poseValue_[6],
poseValue_[3]);
freeFlyerPose_.setRotation(poseQ_);
// Publish
freeFlyerPublisher_.sendTransform(tf::StampedTransform(
freeFlyerPose_, ros::Time::now(), "odom", "base_link"));
fillSensors(sensorsIn_);
sotController_->setupSetSensors(sensorsIn_);
sotController_->getControl(controlValues_);
readControl(controlValues_);
fillSensors(sensorsIn_);
try {
sotController_->nominalSetSensors(sensorsIn_);
sotController_->getControl(controlValues_);
} catch (std::exception &e) {
throw e;
}
readControl(controlValues_);
}