Skip to content

Commit

Permalink
Added period checking in RandomInput to reduce CPU load
Browse files Browse the repository at this point in the history
  • Loading branch information
guihomework committed Aug 30, 2022
1 parent 8a8d642 commit 1fde6f1
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
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;
};

}
46 changes: 46 additions & 0 deletions linux/libio/TimeUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* ============================================================
*
* 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"

/// 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;
}
31 changes: 31 additions & 0 deletions linux/libio/TimeUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* ============================================================
*
* 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_MAX`
#include <unistd.h>
#include <stdio.h> // `printf()`
#include <time.h> // `timespec_get()`

uint64_t getMicroseconds();

0 comments on commit 1fde6f1

Please sign in to comment.