Skip to content

Commit

Permalink
Merge branch 'multiple-buses' into apocanlypse
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBowman committed Nov 29, 2019
2 parents 928ade2 + 1b41cb9 commit 187a238
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 150 deletions.
25 changes: 10 additions & 15 deletions libraries/YarpPlugins/CanBusControlboard/CanBusControlboard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
#ifndef __CAN_BUS_CONTROLBOARD_HPP__
#define __CAN_BUS_CONTROLBOARD_HPP__

#include <string>
#include <vector>

#include <yarp/dev/CanBusInterface.h>
#include <yarp/dev/ControlBoardInterfaces.h>
#include <yarp/dev/PolyDriver.h>
#include <yarp/dev/PolyDriverList.h>

#include "ICanBusSharer.hpp"
#include "PositionDirectThread.hpp"
#include "DeviceMapper.hpp"
#include "CanRxTxThreads.hpp"
Expand All @@ -21,10 +19,6 @@
#define DEFAULT_LIN_INTERP_BUFFER_SIZE 1
#define DEFAULT_LIN_INTERP_MODE "pt"

#define DEFAULT_CAN_RX_BUFFER_SIZE 500
#define DEFAULT_CAN_TX_BUFFER_SIZE 500
#define DEFAULT_CAN_RX_PERIOD_MS -1
#define DEFAULT_CAN_TX_PERIOD_MS 1.0
#define DEFAULT_CAN_SDO_TIMEOUT_MS 25.0 // FIXME unused
#define DEFAULT_CAN_DRIVE_STATE_TIMEOUT 2.5 // FIXME unused

Expand Down Expand Up @@ -64,8 +58,7 @@ class CanBusControlboard : public yarp::dev::DeviceDriver,
public:

CanBusControlboard()
: iCanBus(nullptr), canReaderThread(nullptr), canWriterThread(nullptr),
posdThread(nullptr), linInterpPeriodMs(0), linInterpBufferSize(0)
: posdThread(nullptr), linInterpPeriodMs(0), linInterpBufferSize(0)
{ }

// -------- DeviceDriver declarations. Implementation in DeviceDriverImpl.cpp --------
Expand Down Expand Up @@ -314,16 +307,18 @@ class CanBusControlboard : public yarp::dev::DeviceDriver,

private:

yarp::dev::PolyDriver canBusDevice;
yarp::dev::ICanBus * iCanBus;
struct CanThreads
{
std::string busName;
CanReaderThread * reader = nullptr;
CanWriterThread * writer = nullptr;
};

yarp::dev::PolyDriverList busDevices;
yarp::dev::PolyDriverList nodeDevices;

DeviceMapper deviceMapper;
std::vector<ICanBusSharer *> iCanBusSharers;

CanReaderThread * canReaderThread;
CanWriterThread * canWriterThread;
std::vector<CanThreads> canThreads;

PositionDirectThread * posdThread;
int linInterpPeriodMs;
Expand Down
28 changes: 12 additions & 16 deletions libraries/YarpPlugins/CanBusControlboard/CanRxTxThreads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@

using namespace roboticslab;

CanReaderThread::CanReaderThread(const std::string & id, const std::vector<ICanBusSharer *> & iCanBusSharers)
CanReaderThread::CanReaderThread(const std::string & id)
: CanReaderWriterThread("read", id)
{}

void CanReaderThread::registerHandle(ICanBusSharer * p)
{
for (auto p : iCanBusSharers)
{
canIdToHandle[p->getId()] = p;
canIdToHandle[p->getId()] = p;

for (auto id : p->getAdditionalIds())
{
canIdToHandle[id] = p;
}
for (auto id : p->getAdditionalIds())
{
canIdToHandle[id] = p;
}
}

Expand All @@ -33,7 +33,7 @@ void CanReaderThread::run()
{
//-- Lend CPU time to write threads.
// https://github.com/roboticslab-uc3m/yarp-devices/issues/191
yarp::os::Time::delay(period);
yarp::os::Time::delay(delay);

//-- Return immediately if there is nothing to be read (non-blocking call), return false on errors.
ok = iCanBus->canRead(canBuffer, bufferSize, &read);
Expand Down Expand Up @@ -67,19 +67,15 @@ void CanReaderThread::run()

CanWriterThread::CanWriterThread(const std::string & id)
: CanReaderWriterThread("write", id),
sender(0),
sender(nullptr),
preparedMessages(0)
{}

// -----------------------------------------------------------------------------

CanWriterThread::~CanWriterThread()
{
if (sender)
{
delete sender;
sender = 0;
}
delete sender;
}

// -----------------------------------------------------------------------------
Expand All @@ -93,7 +89,7 @@ void CanWriterThread::run()
{
//-- Lend CPU time to read threads.
// https://github.com/roboticslab-uc3m/yarp-devices/issues/191
yarp::os::Time::delay(period);
yarp::os::Time::delay(delay);

std::lock_guard<std::mutex> lock(bufferMutex);

Expand Down
12 changes: 6 additions & 6 deletions libraries/YarpPlugins/CanBusControlboard/CanRxTxThreads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <map>
#include <mutex>
#include <string>
#include <vector>

#include <yarp/os/Thread.h>
#include <yarp/dev/CanBusInterface.h>
Expand All @@ -27,7 +26,7 @@ class CanReaderWriterThread : public yarp::os::Thread
{
public:
CanReaderWriterThread(const std::string & type, const std::string & id)
: iCanBus(0), iCanBufferFactory(0), type(type), id(id), bufferSize(0), period(0.0)
: iCanBus(nullptr), iCanBufferFactory(nullptr), type(type), id(id), bufferSize(0), delay(0.0)
{}

virtual bool threadInit() override
Expand All @@ -48,16 +47,16 @@ class CanReaderWriterThread : public yarp::os::Thread
virtual void setCanHandles(yarp::dev::ICanBus * iCanBus, yarp::dev::ICanBufferFactory * iCanBufferFactory, unsigned int bufferSize)
{ this->iCanBus = iCanBus; this->iCanBufferFactory = iCanBufferFactory; this->bufferSize = bufferSize; }

void setPeriod(double periodMs)
{ period = periodMs < 0 ? std::numeric_limits<double>::min() : periodMs * 0.001; }
void setDelay(double delay)
{ this->delay = delay <= 0 ? std::numeric_limits<double>::min() : delay; }

protected:
yarp::dev::ICanBus * iCanBus;
yarp::dev::ICanBufferFactory * iCanBufferFactory;
yarp::dev::CanBuffer canBuffer;

unsigned int bufferSize;
double period;
double delay;

private:
std::string type;
Expand All @@ -70,7 +69,8 @@ class CanReaderWriterThread : public yarp::os::Thread
class CanReaderThread : public CanReaderWriterThread
{
public:
CanReaderThread(const std::string & id, const std::vector<ICanBusSharer *> & iCanBusSharers);
CanReaderThread(const std::string & id);
void registerHandle(ICanBusSharer * p);
virtual void run() override;

private:
Expand Down
Loading

0 comments on commit 187a238

Please sign in to comment.