YARP
Yet Another Robot Platform
property/main.cpp

How to read a configuration file. This example is designed to read a file like the following (property/config.txt):

// Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
// Copyright (C) 2006-2010 RobotCub Consortium
// All rights reserved.
//
// This software may be modified and distributed under the terms of the
// BSD-3-Clause license. See the accompanying LICENSE file for details.
//
// Initialization file for robot head, 8 dof on can bus controller.
//
[GENERAL]
Joints 8
MaxDAC 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
AxisMap 1 7 0 6 2 3 5 4
Encoder 360.0 360.0 360.0 360.0 360.0 360.0 360.0 360.0
Zeros 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
Signs 0 0 0 0 0 0 0 0
Stiff 1 1 1 1 1 1 1 1
CurrentLimits 1220.0 1220.0 1220.0 1220.0 10000.0 10000.0 10000.0 10000.0
CanAddresses 14 13 12 11 128 128 128 128 128 128 128 128 128 128 128 128
// limits are: neck pan, neck tilt, eye tilt, version, vergence
[LIMITS]
Max 250000 250000 250000 250000 250000 180000 180000 180000
Min -250000 -250000 -250000 -250000 -250000 -180000 -180000 -180000
[HIGHPID]
Pid0 32 128 2 0 0 1333 0 1333 4 0
Pid1 32 128 2 0 0 1333 0 1333 4 0
Pid2 32 128 2 0 0 1333 0 1333 4 0
Pid3 32 128 2 0 0 1333 0 1333 4 0
Pid4 32 128 4 0 0 1333 0 1333 4 0
Pid5 32 128 1 0 0 1333 0 1333 4 0
Pid6 32 128 1 0 0 1333 0 1333 4 0
Pid7 32 128 1 0 0 1333 0 1333 4 0
[LOWPID]
Pid0 0 0 0 0 0 32767 0 32767 0 0
Pid1 0 0 0 0 0 32767 0 32767 0 0
Pid2 0 0 0 0 0 32767 0 32767 0 0
Pid3 0 0 0 0 0 32767 0 32767 0 0
Pid4 0 0 0 0 0 32767 0 32767 0 0
Pid5 0 0 0 0 0 32767 0 32767 0 0
Pid6 0 0 0 0 0 32767 0 32767 0 0
Pid7 0 0 0 0 0 32767 0 32767 0 0
/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* Copyright (C) 2006-2010 RobotCub Consortium
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/
#include <stdio.h>
#include <stdlib.h>
using namespace yarp::os;
int main(int argc, char *argv[]) {
Property cmdLine;
cmdLine.fromCommand(argc,argv);
if (!cmdLine.check("file")) {
printf("Please call with: --file config.txt\n");
exit(1);
}
std::string fname = cmdLine.find("file").asString();
printf("Working with config file %s\n", fname.c_str());
Property robot;
robot.fromConfigFile(fname.c_str());
if (!robot.check("GENERAL")) {
printf("Cannot understand config file\n");
exit(1);
}
int joints = robot.findGroup("GENERAL").find("Joints").asInt32();
printf("Robot has %d joints\n", joints);
Bottle& maxes = robot.findGroup("LIMITS").findGroup("Max");
printf("Robot has limits: ");
for (int i=1; i<maxes.size(); i++) {
printf("%d ", maxes.get(i).asInt32());
}
printf("\n");
//printf("If you wanted to transmit this configuration, here it is in Bottle format:\n");
//printf("%s\n", robot.toString().c_str());
return 0;
}
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:72
yarp::os::Bottle::size
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:254
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:259
yarp::os::Property::find
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Property.cpp:1034
yarp::os::Bottle::find
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Bottle.cpp:290
yarp::os::Bottle::findGroup
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition: Bottle.cpp:305
yarp::os::Property::fromCommand
void fromCommand(int argc, char *argv[], bool skipFirst=true, bool wipe=true)
Interprets a list of command arguments as a list of properties.
Definition: Property.cpp:1057
yarp::os::Bottle::get
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
Property.h
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
yarp::os::Property::check
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Property.cpp:1024
yarp::os::Value::asInt32
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:207
yarp::os::Property::fromConfigFile
bool fromConfigFile(const std::string &fname, bool wipe=true)
Interprets a file as a list of properties.
Definition: Property.cpp:1081
yarp::os
Definition: AbstractCarrier.h:17
yarp::os::Property::findGroup
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition: Property.cpp:1125
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:34