Skip to content

Commit

Permalink
Parse common CAN bus options
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterBowman committed Jan 23, 2024
1 parent 5233a06 commit 94c7332
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
27 changes: 15 additions & 12 deletions libraries/YarpPlugins/CanBusHico/DeviceDriverImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string>

#include <yarp/os/LogStream.h>
#include <yarp/os/Property.h>
#include <yarp/os/Time.h>

#include "LogComponent.hpp"
Expand All @@ -33,22 +34,24 @@ using namespace roboticslab;

// ------------------- DeviceDriver Related ------------------------------------

bool CanBusHico::open(yarp::os::Searchable& config)
bool CanBusHico::open(yarp::os::Searchable & config)
{
std::string devicePath = config.check("port", yarp::os::Value(DEFAULT_PORT), "CAN device path").asString();
int bitrate = config.check("bitrate", yarp::os::Value(DEFAULT_BITRATE), "CAN bitrate (bps)").asInt32();
yarp::os::Property options;
options.fromString(config.findGroup("common").toString());
options.fromString(config.toString(), false); // override common options

yarp::dev::DeviceDriver::setId(devicePath);
auto devicePath = options.check("port", yarp::os::Value(DEFAULT_PORT), "CAN device path").asString();
auto bitrate = options.check("bitrate", yarp::os::Value(DEFAULT_BITRATE), "CAN bitrate (bps)").asInt32();

blockingMode = config.check("blockingMode", yarp::os::Value(DEFAULT_BLOCKING_MODE), "CAN blocking mode enabled").asBool();
allowPermissive = config.check("allowPermissive", yarp::os::Value(DEFAULT_ALLOW_PERMISSIVE), "CAN read/write permissive mode").asBool();
blockingMode = options.check("blockingMode", yarp::os::Value(DEFAULT_BLOCKING_MODE), "CAN blocking mode enabled").asBool();
allowPermissive = options.check("allowPermissive", yarp::os::Value(DEFAULT_ALLOW_PERMISSIVE), "CAN read/write permissive mode").asBool();

if (blockingMode)
{
yCIInfo(HICO, id()) << "Blocking mode enabled";

rxTimeoutMs = config.check("rxTimeoutMs", yarp::os::Value(DEFAULT_RX_TIMEOUT_MS), "CAN RX timeout (milliseconds)").asInt32();
txTimeoutMs = config.check("txTimeoutMs", yarp::os::Value(DEFAULT_TX_TIMEOUT_MS), "CAN TX timeout (milliseconds)").asInt32();
rxTimeoutMs = options.check("rxTimeoutMs", yarp::os::Value(DEFAULT_RX_TIMEOUT_MS), "CAN RX timeout (milliseconds)").asInt32();
txTimeoutMs = options.check("txTimeoutMs", yarp::os::Value(DEFAULT_TX_TIMEOUT_MS), "CAN TX timeout (milliseconds)").asInt32();

if (rxTimeoutMs <= 0)
{
Expand All @@ -67,7 +70,7 @@ bool CanBusHico::open(yarp::os::Searchable& config)

yCIInfo(HICO, id()) << "Permissive mode flag for read/write operations:" << allowPermissive;

std::string filterConfigStr = config.check("filterConfiguration", yarp::os::Value(DEFAULT_FILTER_CONFIGURATION),
std::string filterConfigStr = options.check("filterConfiguration", yarp::os::Value(DEFAULT_FILTER_CONFIGURATION),
"CAN filter configuration (disabled|noRange|maskAndRange)").asString();

yCIInfo(HICO, id()) << "CAN filter configuration:" << filterConfigStr;
Expand Down Expand Up @@ -119,7 +122,7 @@ bool CanBusHico::open(yarp::os::Searchable& config)
{
filterManager = new FilterManager(*this, fileDescriptor, filterConfig == FilterManager::MASK_AND_RANGE);

if (!config.check("preserveFilters", "don't clear acceptance filters on init"))
if (!options.check("preserveFilters", "don't clear acceptance filters on init"))
{
if (!filterManager->clearFilters())
{
Expand All @@ -137,9 +140,9 @@ bool CanBusHico::open(yarp::os::Searchable& config)
}

//-- Load initial node IDs and set acceptance filters.
if (config.check("filteredIds", "filtered node IDs"))
if (options.check("filteredIds", "filtered node IDs"))
{
const yarp::os::Bottle * ids = config.findGroup("filteredIds").get(1).asList();
const yarp::os::Bottle * ids = options.findGroup("filteredIds").get(1).asList();

if (ids->size() != 0)
{
Expand Down
26 changes: 14 additions & 12 deletions libraries/YarpPlugins/CanBusPeak/DeviceDriverImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>

#include <yarp/os/LogStream.h>
#include <yarp/os/Property.h>

#include "LogComponent.hpp"

Expand All @@ -24,25 +25,26 @@ using namespace roboticslab;

// ------------------- DeviceDriver Related ------------------------------------

bool CanBusPeak::open(yarp::os::Searchable& config)
bool CanBusPeak::open(yarp::os::Searchable & config)
{
std::string devicePath = config.check("port", yarp::os::Value(DEFAULT_PORT), "CAN device path").asString();
yarp::os::Property options;
options.fromString(config.findGroup("common").toString());
options.fromString(config.toString(), false); // override common options

yarp::dev::DeviceDriver::setId(devicePath);
auto devicePath = options.check("port", yarp::os::Value(DEFAULT_PORT), "CAN device path").asString();
auto bitrate = options.check("bitrate", yarp::os::Value(DEFAULT_BITRATE), "CAN bitrate (bps)").asInt32();

int bitrate = config.check("bitrate", yarp::os::Value(DEFAULT_BITRATE), "CAN bitrate (bps)").asInt32();

blockingMode = config.check("blockingMode", yarp::os::Value(DEFAULT_BLOCKING_MODE), "blocking mode enabled").asBool();
allowPermissive = config.check("allowPermissive", yarp::os::Value(DEFAULT_ALLOW_PERMISSIVE), "read/write permissive mode").asBool();
blockingMode = options.check("blockingMode", yarp::os::Value(DEFAULT_BLOCKING_MODE), "blocking mode enabled").asBool();
allowPermissive = options.check("allowPermissive", yarp::os::Value(DEFAULT_ALLOW_PERMISSIVE), "read/write permissive mode").asBool();

int flags = OFD_BITRATE | PCANFD_INIT_STD_MSG_ONLY;

if (blockingMode)
{
yCIInfo(PEAK, id()) << "Blocking mode enabled";

rxTimeoutMs = config.check("rxTimeoutMs", yarp::os::Value(DEFAULT_RX_TIMEOUT_MS), "RX timeout (milliseconds)").asInt32();
txTimeoutMs = config.check("txTimeoutMs", yarp::os::Value(DEFAULT_TX_TIMEOUT_MS), "TX timeout (milliseconds)").asInt32();
rxTimeoutMs = options.check("rxTimeoutMs", yarp::os::Value(DEFAULT_RX_TIMEOUT_MS), "RX timeout (milliseconds)").asInt32();
txTimeoutMs = options.check("txTimeoutMs", yarp::os::Value(DEFAULT_TX_TIMEOUT_MS), "TX timeout (milliseconds)").asInt32();

if (rxTimeoutMs <= 0)
{
Expand Down Expand Up @@ -75,7 +77,7 @@ bool CanBusPeak::open(yarp::os::Searchable& config)
fileDescriptor = res;
}

auto preserveFilters = config.check("preserveFilters", yarp::os::Value(DEFAULT_PRESERVE_FILTERS), "don't clear acceptance filters on init").asBool();
auto preserveFilters = options.check("preserveFilters", yarp::os::Value(DEFAULT_PRESERVE_FILTERS), "don't clear acceptance filters on init").asBool();

if (!preserveFilters)
{
Expand All @@ -97,9 +99,9 @@ bool CanBusPeak::open(yarp::os::Searchable& config)
}

//-- Load initial node IDs and set acceptance filters.
if (config.check("filteredIds", "filtered node IDs"))
if (options.check("filteredIds", "filtered node IDs"))
{
const yarp::os::Bottle * ids = config.findGroup("filteredIds").get(1).asList();
const yarp::os::Bottle * ids = options.findGroup("filteredIds").get(1).asList();

if (ids->size() != 0)
{
Expand Down
25 changes: 14 additions & 11 deletions libraries/YarpPlugins/CanBusSocket/DeviceDriverImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <utility>

#include <yarp/os/LogStream.h>
#include <yarp/os/Property.h>

#include "LogComponent.hpp"

Expand All @@ -27,20 +28,22 @@ constexpr auto DEFAULT_TX_TIMEOUT_MS = 0; // '0' means no timeout

// ------------------- DeviceDriver Related ------------------------------------

bool CanBusSocket::open(yarp::os::Searchable& config)
bool CanBusSocket::open(yarp::os::Searchable & config)
{
iface = config.check("port", yarp::os::Value(DEFAULT_PORT), "CAN socket interface").asString();
blockingMode = config.check("blockingMode", yarp::os::Value(DEFAULT_BLOCKING_MODE), "blocking mode enabled").asBool();
allowPermissive = config.check("allowPermissive", yarp::os::Value(DEFAULT_ALLOW_PERMISSIVE), "read/write permissive mode").asBool();
filterFunctionCodes = config.check("filterFunctionCodes", yarp::os::Value(true), "filter mask ignores CANopen function codes").asBool();
bitrate = config.check("bitrate", yarp::os::Value(0), "CAN bitrate (bps)").asInt32();
yarp::os::Property options;
options.fromString(config.findGroup("common").toString());
options.fromString(config.toString(), false); // override common options

yarp::dev::DeviceDriver::setId(iface);
iface = options.check("port", yarp::os::Value(DEFAULT_PORT), "CAN socket interface").asString();
blockingMode = options.check("blockingMode", yarp::os::Value(DEFAULT_BLOCKING_MODE), "blocking mode enabled").asBool();
allowPermissive = options.check("allowPermissive", yarp::os::Value(DEFAULT_ALLOW_PERMISSIVE), "read/write permissive mode").asBool();
filterFunctionCodes = options.check("filterFunctionCodes", yarp::os::Value(true), "filter mask ignores CANopen function codes").asBool();
bitrate = options.check("bitrate", yarp::os::Value(0), "CAN bitrate (bps)").asInt32();

if (blockingMode)
{
rxTimeoutMs = config.check("rxTimeoutMs", yarp::os::Value(DEFAULT_RX_TIMEOUT_MS), "CAN RX timeout (milliseconds)").asInt32();
txTimeoutMs = config.check("txTimeoutMs", yarp::os::Value(DEFAULT_TX_TIMEOUT_MS), "CAN TX timeout (milliseconds)").asInt32();
rxTimeoutMs = options.check("rxTimeoutMs", yarp::os::Value(DEFAULT_RX_TIMEOUT_MS), "CAN RX timeout (milliseconds)").asInt32();
txTimeoutMs = options.check("txTimeoutMs", yarp::os::Value(DEFAULT_TX_TIMEOUT_MS), "CAN TX timeout (milliseconds)").asInt32();

if (rxTimeoutMs <= 0)
{
Expand Down Expand Up @@ -111,9 +114,9 @@ bool CanBusSocket::open(yarp::os::Searchable& config)
return false;
}

if (config.check("filteredIds", "filtered node IDs"))
if (options.check("filteredIds", "filtered node IDs"))
{
const auto * ids = config.findGroup("filteredIds").get(1).asList();
const auto * ids = options.findGroup("filteredIds").get(1).asList();

if (ids->size() != 0)
{
Expand Down

0 comments on commit 94c7332

Please sign in to comment.