Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added period checking in RandomInput to reduce CPU load #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions linux/libio/RandomInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
* ============================================================ */

#include "RandomInput.h"
#include "TimeUtils.h"
#include <stdlib.h>
#include <stdexcept>
#include <unistd.h>

namespace tactile {

RandomInput::RandomInput(size_t noTaxels)
: InputInterface(noTaxels)
RandomInput::RandomInput(size_t noTaxels, const uint64_t period)
: InputInterface(noTaxels),
period(period)
{
// initialize smallest values
data[1] = 2048;
Expand All @@ -39,6 +42,7 @@ RandomInput::RandomInput(size_t noTaxels)
void RandomInput::connect(const std::string &dummy)
{
connected = true;
prev_time = getMicroseconds();
}

void RandomInput::disconnect()
Expand All @@ -49,7 +53,8 @@ void RandomInput::disconnect()
const InputInterface::data_vector& RandomInput::readFrame()
{
if (!connected) throw std::runtime_error("not connected");


// first prepare data (takes some time)
size_t idx = 0;
for (data_vector::iterator it=data.begin(), end=data.end(); it != end; ++it, ++idx) {
long int rndnumber = random();
Expand All @@ -60,6 +65,18 @@ const InputInterface::data_vector& RandomInput::readFrame()
if (idx == 14) *it = std::min<data_type>(*it + 3700, 0xFFF); // goniometer joint value
}
}
// then check time elapsed
uint64_t cur_time = getMicroseconds();
uint64_t time_diff_us = cur_time - prev_time;
//printf("time us %ld\n", time_diff_us);

// if required wait until period has passed
if (time_diff_us < period)
{
usleep(period - time_diff_us);
}

prev_time = getMicroseconds(); // store time when returning the data
return data;
}

Expand Down
10 changes: 9 additions & 1 deletion linux/libio/RandomInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@ namespace tactile {
class RandomInput : public InputInterface
{
public:
RandomInput(size_t noTaxels);
RandomInput(size_t noTaxels, const uint64_t period=1000);
void connect(const std::string &dummy);
void disconnect();
const data_vector& readFrame ();
void setPeriod(const uint64_t microseconds) // set period in microseconds
{
period = microseconds;
};

private:
uint64_t period; // period in microseconds
uint64_t prev_time;
};

}
48 changes: 48 additions & 0 deletions linux/libio/TimeUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* ============================================================
*
* Copyright (C) 2022 by Guillaume Walck <gwalck at techfak dot uni-bielefeld dot de>
*
* This file may be licensed under the terms of the
* GNU Lesser General Public License Version 3 (the "LGPL"),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the LGPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the LGPL along with this
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by:
* CITEC, "Cognitive Interaction Technology" Excellence Cluster
* Bielefeld University
*
* ============================================================ */
#include "TimeUtils.h"
#include <stdio.h> // `printf()`
#include <time.h> // `timespec_get()`

/// Convert seconds to microseconds
#define SEC_TO_US(sec) ((sec) * 1000000)

uint64_t getMicroseconds()
{
uint64_t microseconds;
struct timespec ts;
int return_code = timespec_get(&ts, TIME_UTC);
if (return_code == 0)
{
printf("Failed to obtain timestamp.\n");
microseconds = UINT64_MAX; // use this to indicate error
}
else
{
// `ts` now contains your timestamp in seconds and nanoseconds! To
// convert the whole struct to nanoseconds, do this:
microseconds = SEC_TO_US((uint64_t)ts.tv_sec) + ((uint64_t)ts.tv_nsec) / 1000;
}
return microseconds;
}
28 changes: 28 additions & 0 deletions linux/libio/TimeUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ============================================================
*
* Copyright (C) 2022 by Guillaume Walck <gwalck at techfak dot uni-bielefeld dot de>
*
* This file may be licensed under the terms of the
* GNU Lesser General Public License Version 3 (the "LGPL"),
* or (at your option) any later version.
*
* Software distributed under the License is distributed
* on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the LGPL for the specific language
* governing rights and limitations.
*
* You should have received a copy of the LGPL along with this
* program. If not, go to http://www.gnu.org/licenses/lgpl.html
* or write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The development of this software was supported by:
* CITEC, "Cognitive Interaction Technology" Excellence Cluster
* Bielefeld University
*
* ============================================================ */
#pragma once

#include <stdint.h> // `UINT64`

uint64_t getMicroseconds();